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 * ComparableObjectItem.java
029 * -------------------------
030 * (C) Copyright 2006-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.Objects;
041import org.jfree.chart.util.Args;
042
043/**
044 * Represents one (Comparable, Object) data item for use in a
045 * {@link ComparableObjectSeries}.
046 */
047public class ComparableObjectItem implements Cloneable, Comparable,
048        Serializable {
049
050    /** For serialization. */
051    private static final long serialVersionUID = 2751513470325494890L;
052
053    /** The x-value. */
054    private Comparable x;
055
056    /** The y-value. */
057    private Object obj;
058
059    /**
060     * Constructs a new data item.
061     *
062     * @param x  the x-value ({@code null} NOT permitted).
063     * @param y  the y-value ({@code null} permitted).
064     */
065    public ComparableObjectItem(Comparable x, Object y) {
066        Args.nullNotPermitted(x, "x");
067        this.x = x;
068        this.obj = y;
069    }
070
071    /**
072     * Returns the x-value.
073     *
074     * @return The x-value (never {@code null}).
075     */
076    protected Comparable getComparable() {
077        return this.x;
078    }
079
080    /**
081     * Returns the y-value.
082     *
083     * @return The y-value (possibly {@code null}).
084     */
085    protected Object getObject() {
086        return this.obj;
087    }
088
089    /**
090     * Sets the y-value for this data item.  Note that there is no
091     * corresponding method to change the x-value.
092     *
093     * @param y  the new y-value ({@code null} permitted).
094     */
095    protected void setObject(Object y) {
096        this.obj = y;
097    }
098
099    /**
100     * Returns an integer indicating the order of this object relative to
101     * another object.
102     * <P>
103     * For the order we consider only the x-value:
104     * negative == "less-than", zero == "equal", positive == "greater-than".
105     *
106     * @param o1  the object being compared to.
107     *
108     * @return An integer indicating the order of this data pair object
109     *      relative to another object.
110     */
111    @Override
112    public int compareTo(Object o1) {
113
114        int result;
115
116        // CASE 1 : Comparing to another ComparableObjectItem object
117        // ---------------------------------------------------------
118        if (o1 instanceof ComparableObjectItem) {
119            ComparableObjectItem that = (ComparableObjectItem) o1;
120            return this.x.compareTo(that.x);
121        }
122
123        // CASE 2 : Comparing to a general object
124        // ---------------------------------------------
125        else {
126            // consider these to be ordered after general objects
127            result = 1;
128        }
129
130        return result;
131
132    }
133
134    /**
135     * Returns a clone of this object.
136     *
137     * @return A clone.
138     *
139     * @throws CloneNotSupportedException not thrown by this class, but
140     *         subclasses may differ.
141     */
142    @Override
143    public Object clone() throws CloneNotSupportedException {
144        return super.clone();
145    }
146
147    /**
148     * Tests if this object is equal to another.
149     *
150     * @param obj  the object to test against for equality ({@code null}
151     *             permitted).
152     *
153     * @return A boolean.
154     */
155    @Override
156    public boolean equals(Object obj) {
157        if (obj == this) {
158            return true;
159        }
160        if (!(obj instanceof ComparableObjectItem)) {
161            return false;
162        }
163        ComparableObjectItem that = (ComparableObjectItem) obj;
164        if (!this.x.equals(that.x)) {
165            return false;
166        }
167        if (!Objects.equals(this.obj, that.obj)) {
168            return false;
169        }
170        return true;
171    }
172
173    /**
174     * Returns a hash code.
175     *
176     * @return A hash code.
177     */
178    @Override
179    public int hashCode() {
180        int result;
181        result = this.x.hashCode();
182        result = 29 * result + (this.obj != null ? this.obj.hashCode() : 0);
183        return result;
184    }
185
186}