001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2011, 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 * XYDataItem.java
029 * ---------------
030 * (C) Copyright 2003-2009, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes
036 * -------
037 * 05-Aug-2003 : Renamed XYDataPair --> XYDataItem (DG);
038 * 03-Feb-2004 : Fixed bug in equals() method (DG);
039 * 21-Feb-2005 : Added setY(double) method (DG);
040 * ------------- JFREECHART 1.0.x ---------------------------------------------
041 * 30-Nov-2007 : Implemented getXValue() and getYValue(), plus toString() for
042 *               debugging use (DG);
043 * 10-Jun-2009 : Reimplemented cloning (DG);
044 *
045 */
046
047package org.jfree.data.xy;
048
049import java.io.Serializable;
050
051import org.jfree.util.ObjectUtilities;
052
053/**
054 * Represents one (x, y) data item for an {@link XYSeries}.  Note that
055 * subclasses are REQUIRED to support cloning.
056 */
057public class XYDataItem implements Cloneable, Comparable, Serializable {
058
059    /** For serialization. */
060    private static final long serialVersionUID = 2751513470325494890L;
061
062    /** The x-value (<code>null</code> not permitted). */
063    private Number x;
064
065    /** The y-value. */
066    private Number y;
067
068    /**
069     * Constructs a new data item.
070     *
071     * @param x  the x-value (<code>null</code> NOT permitted).
072     * @param y  the y-value (<code>null</code> permitted).
073     */
074    public XYDataItem(Number x, Number y) {
075        if (x == null) {
076            throw new IllegalArgumentException("Null 'x' argument.");
077        }
078        this.x = x;
079        this.y = y;
080    }
081
082    /**
083     * Constructs a new data item.
084     *
085     * @param x  the x-value.
086     * @param y  the y-value.
087     */
088    public XYDataItem(double x, double y) {
089        this(new Double(x), new Double(y));
090    }
091
092    /**
093     * Returns the x-value.
094     *
095     * @return The x-value (never <code>null</code>).
096     */
097    public Number getX() {
098        return this.x;
099    }
100
101    /**
102     * Returns the x-value as a double primitive.
103     *
104     * @return The x-value.
105     *
106     * @see #getX()
107     * @see #getYValue()
108     *
109     * @since 1.0.9
110     */
111    public double getXValue() {
112        // this.x is not allowed to be null...
113        return this.x.doubleValue();
114    }
115
116    /**
117     * Returns the y-value.
118     *
119     * @return The y-value (possibly <code>null</code>).
120     */
121    public Number getY() {
122        return this.y;
123    }
124
125    /**
126     * Returns the y-value as a double primitive.
127     *
128     * @return The y-value.
129     *
130     * @see #getY()
131     * @see #getXValue()
132     *
133     * @since 1.0.9
134     */
135    public double getYValue() {
136        double result = Double.NaN;
137        if (this.y != null) {
138            result = this.y.doubleValue();
139        }
140        return result;
141    }
142
143    /**
144     * Sets the y-value for this data item.  Note that there is no
145     * corresponding method to change the x-value.
146     *
147     * @param y  the new y-value.
148     */
149    public void setY(double y) {
150        setY(new Double(y));
151    }
152
153    /**
154     * Sets the y-value for this data item.  Note that there is no
155     * corresponding method to change the x-value.
156     *
157     * @param y  the new y-value (<code>null</code> permitted).
158     */
159    public void setY(Number y) {
160        this.y = y;
161    }
162
163    /**
164     * Returns an integer indicating the order of this object relative to
165     * another object.
166     * <P>
167     * For the order we consider only the x-value:
168     * negative == "less-than", zero == "equal", positive == "greater-than".
169     *
170     * @param o1  the object being compared to.
171     *
172     * @return An integer indicating the order of this data pair object
173     *      relative to another object.
174     */
175    public int compareTo(Object o1) {
176
177        int result;
178
179        // CASE 1 : Comparing to another TimeSeriesDataPair object
180        // -------------------------------------------------------
181        if (o1 instanceof XYDataItem) {
182            XYDataItem dataItem = (XYDataItem) o1;
183            double compare = this.x.doubleValue()
184                             - dataItem.getX().doubleValue();
185            if (compare > 0.0) {
186                result = 1;
187            }
188            else {
189                if (compare < 0.0) {
190                    result = -1;
191                }
192                else {
193                    result = 0;
194                }
195            }
196        }
197
198        // CASE 2 : Comparing to a general object
199        // ---------------------------------------------
200        else {
201            // consider time periods to be ordered after general objects
202            result = 1;
203        }
204
205        return result;
206
207    }
208
209    /**
210     * Returns a clone of this object.
211     *
212     * @return A clone.
213     */
214    public Object clone() {
215        Object clone = null;
216        try {
217            clone = super.clone();
218        }
219        catch (CloneNotSupportedException e) { // won't get here...
220            e.printStackTrace();
221        }
222        return clone;
223    }
224
225    /**
226     * Tests if this object is equal to another.
227     *
228     * @param obj  the object to test against for equality (<code>null</code>
229     *             permitted).
230     *
231     * @return A boolean.
232     */
233    public boolean equals(Object obj) {
234        if (obj == this) {
235            return true;
236        }
237        if (!(obj instanceof XYDataItem)) {
238            return false;
239        }
240        XYDataItem that = (XYDataItem) obj;
241        if (!this.x.equals(that.x)) {
242            return false;
243        }
244        if (!ObjectUtilities.equal(this.y, that.y)) {
245            return false;
246        }
247        return true;
248    }
249
250    /**
251     * Returns a hash code.
252     *
253     * @return A hash code.
254     */
255    public int hashCode() {
256        int result;
257        result = this.x.hashCode();
258        result = 29 * result + (this.y != null ? this.y.hashCode() : 0);
259        return result;
260    }
261
262    /**
263     * Returns a string representing this instance, primarily for debugging
264     * use.
265     *
266     * @return A string.
267     */
268    public String toString() {
269        return "[" + getXValue() + ", " + getYValue() + "]";
270    }
271
272}