KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > 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 License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * -------------------------
27  * KeyedValueComparator.java
28  * -------------------------
29  * (C) Copyright 2003-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: KeyedValueComparator.java,v 1.4 2005/03/28 19:40:26 mungady Exp $
35  *
36  * Changes:
37  * --------
38  * 05-Mar-2003 : Version 1 (DG);
39  * 27-Aug-2003 : Moved SortOrder from org.jfree.data --> org.jfree.util (DG);
40  * 12-Jan-2005 : Added accessor methods (DG);
41  *
42  */

43
44 package org.jfree.data;
45
46 import java.util.Comparator JavaDoc;
47
48 import org.jfree.util.SortOrder;
49
50 /**
51  * A utility class that can compare and order two {@link KeyedValue} instances
52  * and sort them into ascending or descending order by key or by value.
53  */

54 public class KeyedValueComparator implements Comparator JavaDoc {
55
56     /** The comparator type. */
57     private KeyedValueComparatorType type;
58
59     /** The sort order. */
60     private SortOrder order;
61
62     /**
63      * Creates a new comparator.
64      *
65      * @param type the type (<code>BY_KEY</code> or <code>BY_VALUE</code>,
66      * <code>null</code> not permitted).
67      * @param order the order (<code>null</code> not permitted).
68      */

69     public KeyedValueComparator(KeyedValueComparatorType type,
70                                 SortOrder order) {
71         if (order == null) {
72             throw new IllegalArgumentException JavaDoc("Null 'order' argument.");
73         }
74         this.type = type;
75         this.order = order;
76     }
77
78     /**
79      * Returns the type.
80      *
81      * @return The type (never <code>null</code>).
82      */

83     public KeyedValueComparatorType getType() {
84         return this.type;
85     }
86     
87     /**
88      * Returns the sort order.
89      *
90      * @return The sort order (never <code>null</code>).
91      */

92     public SortOrder getOrder() {
93         return this.order;
94     }
95     
96     /**
97      * Compares two {@link KeyedValue} instances and returns an
98      * <code>int</code> that indicates the relative order of the two objects.
99      *
100      * @param o1 object 1.
101      * @param o2 object 2.
102      *
103      * @return An int indicating the relative order of the objects.
104      */

105     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
106
107         if (o2 == null) {
108             return -1;
109         }
110         if (o1 == null) {
111             return 1;
112         }
113
114         int result;
115
116         KeyedValue kv1 = (KeyedValue) o1;
117         KeyedValue kv2 = (KeyedValue) o2;
118
119         if (this.type == KeyedValueComparatorType.BY_KEY) {
120             if (this.order.equals(SortOrder.ASCENDING)) {
121                 result = kv1.getKey().compareTo(kv2.getKey());
122             }
123             else if (this.order.equals(SortOrder.DESCENDING)) {
124                 result = kv2.getKey().compareTo(kv1.getKey());
125             }
126             else {
127                 throw new IllegalArgumentException JavaDoc("Unrecognised sort order.");
128             }
129         }
130         else if (this.type == KeyedValueComparatorType.BY_VALUE) {
131             Number JavaDoc n1 = kv1.getValue();
132             Number JavaDoc n2 = kv2.getValue();
133             if (n2 == null) {
134                 return -1;
135             }
136             if (n1 == null) {
137                 return 1;
138             }
139             double d1 = n1.doubleValue();
140             double d2 = n2.doubleValue();
141             if (this.order.equals(SortOrder.ASCENDING)) {
142                 if (d1 > d2) {
143                     result = 1;
144                 }
145                 else if (d1 < d2) {
146                     result = -1;
147                 }
148                 else {
149                     result = 0;
150                 }
151             }
152             else if (this.order.equals(SortOrder.DESCENDING)) {
153                 if (d1 > d2) {
154                     result = -1;
155                 }
156                 else if (d1 < d2) {
157                     result = 1;
158                 }
159                 else {
160                     result = 0;
161                 }
162             }
163             else {
164                 throw new IllegalArgumentException JavaDoc("Unrecognised sort order.");
165             }
166         }
167         else {
168             throw new IllegalArgumentException JavaDoc("Unrecognised type.");
169         }
170
171         return result;
172     }
173
174 }
175
Popular Tags