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