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