001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, 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-present, 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.util.Args;
042import org.jfree.chart.util.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, 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 o1  object 1.
094     * @param o2  object 2.
095     *
096     * @return An int indicating the relative order of the objects.
097     */
098    @Override
099    public int compare(Object o1, Object o2) {
100
101        if (o2 == null) {
102            return -1;
103        }
104        if (o1 == null) {
105            return 1;
106        }
107
108        int result;
109
110        KeyedValue kv1 = (KeyedValue) o1;
111        KeyedValue kv2 = (KeyedValue) o2;
112
113        if (this.type == KeyedValueComparatorType.BY_KEY) {
114            if (this.order.equals(SortOrder.ASCENDING)) {
115                result = kv1.getKey().compareTo(kv2.getKey());
116            }
117            else if (this.order.equals(SortOrder.DESCENDING)) {
118                result = kv2.getKey().compareTo(kv1.getKey());
119            }
120            else {
121                throw new IllegalArgumentException("Unrecognised sort order.");
122            }
123        }
124        else if (this.type == KeyedValueComparatorType.BY_VALUE) {
125            Number n1 = kv1.getValue();
126            Number n2 = kv2.getValue();
127            if (n2 == null) {
128                return -1;
129            }
130            if (n1 == null) {
131                return 1;
132            }
133            double d1 = n1.doubleValue();
134            double d2 = n2.doubleValue();
135            if (this.order.equals(SortOrder.ASCENDING)) {
136                if (d1 > d2) {
137                    result = 1;
138                }
139                else if (d1 < d2) {
140                    result = -1;
141                }
142                else {
143                    result = 0;
144                }
145            }
146            else if (this.order.equals(SortOrder.DESCENDING)) {
147                if (d1 > d2) {
148                    result = -1;
149                }
150                else if (d1 < d2) {
151                    result = 1;
152                }
153                else {
154                    result = 0;
155                }
156            }
157            else {
158                throw new IllegalArgumentException("Unrecognised sort order.");
159            }
160        }
161        else {
162            throw new IllegalArgumentException("Unrecognised type.");
163        }
164
165        return result;
166    }
167
168}