Frames | No Frames |
1: /* =========================================================== 2: * JFreeChart : a free chart library for the Java(tm) platform 3: * =========================================================== 4: * 5: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors. 6: * 7: * Project Info: http://www.jfree.org/jfreechart/index.html 8: * 9: * This library is free software; you can redistribute it and/or modify it 10: * under the terms of the GNU Lesser General Public License as published by 11: * the Free Software Foundation; either version 2.1 of the License, or 12: * (at your option) any later version. 13: * 14: * This library is distributed in the hope that it will be useful, but 15: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17: * License for more details. 18: * 19: * You should have received a copy of the GNU Lesser General Public 20: * License along with this library; if not, write to the Free Software 21: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22: * USA. 23: * 24: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 25: * in the United States and other countries.] 26: * 27: * ------------------------- 28: * KeyedValueComparator.java 29: * ------------------------- 30: * (C) Copyright 2003-2007, by Object Refinery Limited. 31: * 32: * Original Author: David Gilbert (for Object Refinery Limited); 33: * Contributor(s): -; 34: * 35: * Changes: 36: * -------- 37: * 05-Mar-2003 : Version 1 (DG); 38: * 27-Aug-2003 : Moved SortOrder from org.jfree.data --> org.jfree.util (DG); 39: * 12-Jan-2005 : Added accessor methods (DG); 40: * 41: */ 42: 43: package org.jfree.data; 44: 45: import java.util.Comparator; 46: 47: import org.jfree.util.SortOrder; 48: 49: /** 50: * A utility class that can compare and order two {@link KeyedValue} instances 51: * and sort them into ascending or descending order by key or by value. 52: */ 53: public class KeyedValueComparator implements Comparator { 54: 55: /** The comparator type. */ 56: private KeyedValueComparatorType type; 57: 58: /** The sort order. */ 59: private SortOrder order; 60: 61: /** 62: * Creates a new comparator. 63: * 64: * @param type the type (<code>BY_KEY</code> or <code>BY_VALUE</code>, 65: * <code>null</code> not permitted). 66: * @param order the order (<code>null</code> not permitted). 67: */ 68: public KeyedValueComparator(KeyedValueComparatorType type, 69: SortOrder order) { 70: if (order == null) { 71: throw new IllegalArgumentException("Null 'order' argument."); 72: } 73: this.type = type; 74: this.order = order; 75: } 76: 77: /** 78: * Returns the type. 79: * 80: * @return The type (never <code>null</code>). 81: */ 82: public KeyedValueComparatorType getType() { 83: return this.type; 84: } 85: 86: /** 87: * Returns the sort order. 88: * 89: * @return The sort order (never <code>null</code>). 90: */ 91: public SortOrder getOrder() { 92: return this.order; 93: } 94: 95: /** 96: * Compares two {@link KeyedValue} instances and returns an 97: * <code>int</code> that indicates the relative order of the two objects. 98: * 99: * @param o1 object 1. 100: * @param o2 object 2. 101: * 102: * @return An int indicating the relative order of the objects. 103: */ 104: public int compare(Object o1, Object o2) { 105: 106: if (o2 == null) { 107: return -1; 108: } 109: if (o1 == null) { 110: return 1; 111: } 112: 113: int result; 114: 115: KeyedValue kv1 = (KeyedValue) o1; 116: KeyedValue kv2 = (KeyedValue) o2; 117: 118: if (this.type == KeyedValueComparatorType.BY_KEY) { 119: if (this.order.equals(SortOrder.ASCENDING)) { 120: result = kv1.getKey().compareTo(kv2.getKey()); 121: } 122: else if (this.order.equals(SortOrder.DESCENDING)) { 123: result = kv2.getKey().compareTo(kv1.getKey()); 124: } 125: else { 126: throw new IllegalArgumentException("Unrecognised sort order."); 127: } 128: } 129: else if (this.type == KeyedValueComparatorType.BY_VALUE) { 130: Number n1 = kv1.getValue(); 131: Number n2 = kv2.getValue(); 132: if (n2 == null) { 133: return -1; 134: } 135: if (n1 == null) { 136: return 1; 137: } 138: double d1 = n1.doubleValue(); 139: double d2 = n2.doubleValue(); 140: if (this.order.equals(SortOrder.ASCENDING)) { 141: if (d1 > d2) { 142: result = 1; 143: } 144: else if (d1 < d2) { 145: result = -1; 146: } 147: else { 148: result = 0; 149: } 150: } 151: else if (this.order.equals(SortOrder.DESCENDING)) { 152: if (d1 > d2) { 153: result = -1; 154: } 155: else if (d1 < d2) { 156: result = 1; 157: } 158: else { 159: result = 0; 160: } 161: } 162: else { 163: throw new IllegalArgumentException("Unrecognised sort order."); 164: } 165: } 166: else { 167: throw new IllegalArgumentException("Unrecognised type."); 168: } 169: 170: return result; 171: } 172: 173: }