Source for org.jfree.data.KeyedValueComparator

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2005, 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-2005, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: KeyedValueComparator.java,v 1.4.2.1 2005/10/25 21:29:13 mungady Exp $
  36:  *
  37:  * Changes:
  38:  * --------
  39:  * 05-Mar-2003 : Version 1 (DG);
  40:  * 27-Aug-2003 : Moved SortOrder from org.jfree.data --> org.jfree.util (DG);
  41:  * 12-Jan-2005 : Added accessor methods (DG);
  42:  *
  43:  */
  44: 
  45: package org.jfree.data;
  46: 
  47: import java.util.Comparator;
  48: 
  49: import org.jfree.util.SortOrder;
  50: 
  51: /**
  52:  * A utility class that can compare and order two {@link KeyedValue} instances 
  53:  * and sort them into ascending or descending order by key or by value.
  54:  */
  55: public class KeyedValueComparator implements Comparator {
  56: 
  57:     /** The comparator type. */
  58:     private KeyedValueComparatorType type;
  59: 
  60:     /** The sort order. */
  61:     private SortOrder order;
  62: 
  63:     /**
  64:      * Creates a new comparator.
  65:      *
  66:      * @param type  the type (<code>BY_KEY</code> or <code>BY_VALUE</code>, 
  67:      *              <code>null</code> not permitted).
  68:      * @param order  the order (<code>null</code> not permitted).
  69:      */
  70:     public KeyedValueComparator(KeyedValueComparatorType type, 
  71:                                 SortOrder order) {
  72:         if (order == null) {
  73:             throw new IllegalArgumentException("Null 'order' argument.");
  74:         }
  75:         this.type = type;
  76:         this.order = order;
  77:     }
  78: 
  79:     /**
  80:      * Returns the type.
  81:      * 
  82:      * @return The type (never <code>null</code>).
  83:      */
  84:     public KeyedValueComparatorType getType() {
  85:         return this.type;
  86:     }
  87:     
  88:     /**
  89:      * Returns the sort order.
  90:      * 
  91:      * @return The sort order (never <code>null</code>).
  92:      */
  93:     public SortOrder getOrder() {
  94:         return this.order;
  95:     }
  96:     
  97:     /**
  98:      * Compares two {@link KeyedValue} instances and returns an 
  99:      * <code>int</code> that indicates the relative order of the two objects.
 100:      *
 101:      * @param o1  object 1.
 102:      * @param o2  object 2.
 103:      *
 104:      * @return An int indicating the relative order of the objects.
 105:      */
 106:     public int compare(Object o1, Object o2) {
 107: 
 108:         if (o2 == null) {
 109:             return -1;
 110:         }
 111:         if (o1 == null) {
 112:             return 1;
 113:         }
 114: 
 115:         int result;
 116: 
 117:         KeyedValue kv1 = (KeyedValue) o1;
 118:         KeyedValue kv2 = (KeyedValue) o2;
 119: 
 120:         if (this.type == KeyedValueComparatorType.BY_KEY) {
 121:             if (this.order.equals(SortOrder.ASCENDING)) {
 122:                 result = kv1.getKey().compareTo(kv2.getKey());
 123:             }
 124:             else if (this.order.equals(SortOrder.DESCENDING)) {
 125:                 result = kv2.getKey().compareTo(kv1.getKey());
 126:             }
 127:             else {
 128:                 throw new IllegalArgumentException("Unrecognised sort order.");
 129:             }
 130:         }
 131:         else if (this.type == KeyedValueComparatorType.BY_VALUE) {
 132:             Number n1 = kv1.getValue();
 133:             Number n2 = kv2.getValue();
 134:             if (n2 == null) {
 135:                 return -1;
 136:             }
 137:             if (n1 == null) {
 138:                 return 1;
 139:             }
 140:             double d1 = n1.doubleValue();
 141:             double d2 = n2.doubleValue();
 142:             if (this.order.equals(SortOrder.ASCENDING)) {
 143:                 if (d1 > d2) {
 144:                     result = 1;
 145:                 }
 146:                 else if (d1 < d2) {
 147:                     result = -1;
 148:                 }
 149:                 else {
 150:                     result = 0;
 151:                 }
 152:             }
 153:             else if (this.order.equals(SortOrder.DESCENDING)) {
 154:                 if (d1 > d2) {
 155:                     result = -1;
 156:                 }
 157:                 else if (d1 < d2) {
 158:                     result = 1;
 159:                 }
 160:                 else {
 161:                     result = 0;
 162:                 }
 163:             }
 164:             else {
 165:                 throw new IllegalArgumentException("Unrecognised sort order.");
 166:             }
 167:         }
 168:         else {
 169:             throw new IllegalArgumentException("Unrecognised type.");
 170:         }
 171: 
 172:         return result;
 173:     }
 174: 
 175: }