001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2022, by David Gilbert 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-2022, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.data;
038
039import java.io.Serializable;
040import java.util.Comparator;
041import org.jfree.chart.internal.Args;
042import org.jfree.chart.api.SortOrder;
043
044/**
045 * A utility class that can compare and order two {@link KeyedValue} instances
046 * and sort them into ascending or descending order by key or by value.
047 */
048public class KeyedValueComparator implements Comparator<KeyedValue>, Serializable {
049
050    /** The comparator type. */
051    private KeyedValueComparatorType type;
052
053    /** The sort order. */
054    private SortOrder order;
055
056    /**
057     * Creates a new comparator.
058     *
059     * @param type  the type ({@code BY_KEY} or {@code BY_VALUE},
060     *              {@code null} not permitted).
061     * @param order  the order ({@code null} not permitted).
062     */
063    public KeyedValueComparator(KeyedValueComparatorType type,
064                                SortOrder order) {
065        Args.nullNotPermitted(type, "type");
066        Args.nullNotPermitted(order, "order");
067        this.type = type;
068        this.order = order;
069    }
070
071    /**
072     * Returns the type.
073     *
074     * @return The type (never {@code null}).
075     */
076    public KeyedValueComparatorType getType() {
077        return this.type;
078    }
079
080    /**
081     * Returns the sort order.
082     *
083     * @return The sort order (never {@code null}).
084     */
085    public SortOrder getOrder() {
086        return this.order;
087    }
088
089    /**
090     * Compares two {@link KeyedValue} instances and returns an
091     * {@code int} that indicates the relative order of the two objects.
092     *
093     * @param kv1  object 1.
094     * @param kv2  object 2.
095     *
096     * @return An int indicating the relative order of the objects.
097     */
098    @Override
099    public int compare(KeyedValue kv1, KeyedValue kv2) {
100
101        if (kv2 == null) {
102            return -1;
103        }
104        if (kv1 == null) {
105            return 1;
106        }
107
108        int result;
109
110        if (this.type == KeyedValueComparatorType.BY_KEY) {
111            if (this.order.equals(SortOrder.ASCENDING)) {
112                result = kv1.getKey().compareTo(kv2.getKey());
113            }
114            else if (this.order.equals(SortOrder.DESCENDING)) {
115                result = kv2.getKey().compareTo(kv1.getKey());
116            }
117            else {
118                throw new IllegalArgumentException("Unrecognised sort order.");
119            }
120        }
121        else if (this.type == KeyedValueComparatorType.BY_VALUE) {
122            Number n1 = kv1.getValue();
123            Number n2 = kv2.getValue();
124            if (n2 == null) {
125                return -1;
126            }
127            if (n1 == null) {
128                return 1;
129            }
130            double d1 = n1.doubleValue();
131            double d2 = n2.doubleValue();
132            if (this.order.equals(SortOrder.ASCENDING)) {
133                if (d1 > d2) {
134                    result = 1;
135                }
136                else if (d1 < d2) {
137                    result = -1;
138                }
139                else {
140                    result = 0;
141                }
142            }
143            else if (this.order.equals(SortOrder.DESCENDING)) {
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 {
155                throw new IllegalArgumentException("Unrecognised sort order.");
156            }
157        }
158        else {
159            throw new IllegalArgumentException("Unrecognised type.");
160        }
161
162        return result;
163    }
164
165}