001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2020, 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.Args;
049import org.jfree.chart.util.SortOrder;
050
051/**
052 * A utility class that can compare and order two {@link KeyedValue} instances
053 * and sort them into ascending or descending order by key or by value.
054 */
055public class KeyedValueComparator implements Comparator, Serializable {
056
057    /** The comparator type. */
058    private KeyedValueComparatorType type;
059
060    /** The sort order. */
061    private SortOrder order;
062
063    /**
064     * Creates a new comparator.
065     *
066     * @param type  the type ({@code BY_KEY} or {@code BY_VALUE},
067     *              {@code null} not permitted).
068     * @param order  the order ({@code null} not permitted).
069     */
070    public KeyedValueComparator(KeyedValueComparatorType type,
071                                SortOrder order) {
072        Args.nullNotPermitted(type, "type");
073        Args.nullNotPermitted(order, "order");
074        this.type = type;
075        this.order = order;
076    }
077
078    /**
079     * Returns the type.
080     *
081     * @return The type (never {@code null}).
082     */
083    public KeyedValueComparatorType getType() {
084        return this.type;
085    }
086
087    /**
088     * Returns the sort order.
089     *
090     * @return The sort order (never {@code null}).
091     */
092    public SortOrder getOrder() {
093        return this.order;
094    }
095
096    /**
097     * Compares two {@link KeyedValue} instances and returns an
098     * {@code int} that indicates the relative order of the two objects.
099     *
100     * @param o1  object 1.
101     * @param o2  object 2.
102     *
103     * @return An int indicating the relative order of the objects.
104     */
105    @Override
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}