001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
025 * Other names may be trademarks of their respective owners.]
026 *
027 * -------------------------
028 * KeyedValueComparator.java
029 * -------------------------
030 * (C) Copyright 2003-2012, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes:
036 * --------
037 * 05-Mar-2003 : Version 1 (DG);
038 * 27-Aug-2003 : Moved SortOrder from org.jfree.data --> org.jfree.util (DG);
039 * 12-Jan-2005 : Added accessor methods (DG);
040 * 23-Sep-2012 : Make this class serializable (DG);
041 *
042 */
043
044package org.jfree.data;
045
046import java.io.Serializable;
047import java.util.Comparator;
048import org.jfree.chart.util.ParamChecks;
049
050import org.jfree.util.SortOrder;
051
052/**
053 * A utility class that can compare and order two {@link KeyedValue} instances
054 * and sort them into ascending or descending order by key or by value.
055 */
056public class KeyedValueComparator implements Comparator, Serializable {
057
058    /** The comparator type. */
059    private KeyedValueComparatorType type;
060
061    /** The sort order. */
062    private SortOrder order;
063
064    /**
065     * Creates a new comparator.
066     *
067     * @param type  the type (<code>BY_KEY</code> or <code>BY_VALUE</code>,
068     *              <code>null</code> not permitted).
069     * @param order  the order (<code>null</code> not permitted).
070     */
071    public KeyedValueComparator(KeyedValueComparatorType type,
072                                SortOrder order) {
073        ParamChecks.nullNotPermitted(type, "type");
074        ParamChecks.nullNotPermitted(order, "order");
075        this.type = type;
076        this.order = order;
077    }
078
079    /**
080     * Returns the type.
081     *
082     * @return The type (never <code>null</code>).
083     */
084    public KeyedValueComparatorType getType() {
085        return this.type;
086    }
087
088    /**
089     * Returns the sort order.
090     *
091     * @return The sort order (never <code>null</code>).
092     */
093    public SortOrder getOrder() {
094        return this.order;
095    }
096
097    /**
098     * Compares two {@link KeyedValue} instances and returns an
099     * <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    @Override
107    public int compare(Object o1, Object o2) {
108
109        if (o2 == null) {
110            return -1;
111        }
112        if (o1 == null) {
113            return 1;
114        }
115
116        int result;
117
118        KeyedValue kv1 = (KeyedValue) o1;
119        KeyedValue kv2 = (KeyedValue) o2;
120
121        if (this.type == KeyedValueComparatorType.BY_KEY) {
122            if (this.order.equals(SortOrder.ASCENDING)) {
123                result = kv1.getKey().compareTo(kv2.getKey());
124            }
125            else if (this.order.equals(SortOrder.DESCENDING)) {
126                result = kv2.getKey().compareTo(kv1.getKey());
127            }
128            else {
129                throw new IllegalArgumentException("Unrecognised sort order.");
130            }
131        }
132        else if (this.type == KeyedValueComparatorType.BY_VALUE) {
133            Number n1 = kv1.getValue();
134            Number n2 = kv2.getValue();
135            if (n2 == null) {
136                return -1;
137            }
138            if (n1 == null) {
139                return 1;
140            }
141            double d1 = n1.doubleValue();
142            double d2 = n2.doubleValue();
143            if (this.order.equals(SortOrder.ASCENDING)) {
144                if (d1 > d2) {
145                    result = 1;
146                }
147                else if (d1 < d2) {
148                    result = -1;
149                }
150                else {
151                    result = 0;
152                }
153            }
154            else if (this.order.equals(SortOrder.DESCENDING)) {
155                if (d1 > d2) {
156                    result = -1;
157                }
158                else if (d1 < d2) {
159                    result = 1;
160                }
161                else {
162                    result = 0;
163                }
164            }
165            else {
166                throw new IllegalArgumentException("Unrecognised sort order.");
167            }
168        }
169        else {
170            throw new IllegalArgumentException("Unrecognised type.");
171        }
172
173        return result;
174    }
175
176}