001 /* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2007, 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 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025 * in the United States and other countries.]
026 *
027 * ---------------
028 * XYDataItem.java
029 * ---------------
030 * (C) Copyright 2003-2007, 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 *
041 */
042
043 package org.jfree.data.xy;
044
045 import java.io.Serializable;
046
047 import org.jfree.util.ObjectUtilities;
048
049 /**
050 * Represents one (x, y) data item for an {@link XYSeries}.
051 */
052 public class XYDataItem implements Cloneable, Comparable, Serializable {
053
054 /** For serialization. */
055 private static final long serialVersionUID = 2751513470325494890L;
056
057 /** The x-value. */
058 private Number x;
059
060 /** The y-value. */
061 private Number y;
062
063 /**
064 * Constructs a new data item.
065 *
066 * @param x the x-value (<code>null</code> NOT permitted).
067 * @param y the y-value (<code>null</code> permitted).
068 */
069 public XYDataItem(Number x, Number y) {
070 if (x == null) {
071 throw new IllegalArgumentException("Null 'x' argument.");
072 }
073 this.x = x;
074 this.y = y;
075 }
076
077 /**
078 * Constructs a new data item.
079 *
080 * @param x the x-value.
081 * @param y the y-value.
082 */
083 public XYDataItem(double x, double y) {
084 this(new Double(x), new Double(y));
085 }
086
087 /**
088 * Returns the x-value.
089 *
090 * @return The x-value (never <code>null</code>).
091 */
092 public Number getX() {
093 return this.x;
094 }
095
096 /**
097 * Returns the y-value.
098 *
099 * @return The y-value (possibly <code>null</code>).
100 */
101 public Number getY() {
102 return this.y;
103 }
104
105 /**
106 * Sets the y-value for this data item. Note that there is no
107 * corresponding method to change the x-value.
108 *
109 * @param y the new y-value.
110 */
111 public void setY(double y) {
112 setY(new Double(y));
113 }
114
115 /**
116 * Sets the y-value for this data item. Note that there is no
117 * corresponding method to change the x-value.
118 *
119 * @param y the new y-value (<code>null</code> permitted).
120 */
121 public void setY(Number y) {
122 this.y = y;
123 }
124
125 /**
126 * Returns an integer indicating the order of this object relative to
127 * another object.
128 * <P>
129 * For the order we consider only the x-value:
130 * negative == "less-than", zero == "equal", positive == "greater-than".
131 *
132 * @param o1 the object being compared to.
133 *
134 * @return An integer indicating the order of this data pair object
135 * relative to another object.
136 */
137 public int compareTo(Object o1) {
138
139 int result;
140
141 // CASE 1 : Comparing to another TimeSeriesDataPair object
142 // -------------------------------------------------------
143 if (o1 instanceof XYDataItem) {
144 XYDataItem dataItem = (XYDataItem) o1;
145 double compare = this.x.doubleValue()
146 - dataItem.getX().doubleValue();
147 if (compare > 0.0) {
148 result = 1;
149 }
150 else {
151 if (compare < 0.0) {
152 result = -1;
153 }
154 else {
155 result = 0;
156 }
157 }
158 }
159
160 // CASE 2 : Comparing to a general object
161 // ---------------------------------------------
162 else {
163 // consider time periods to be ordered after general objects
164 result = 1;
165 }
166
167 return result;
168
169 }
170
171 /**
172 * Returns a clone of this object.
173 *
174 * @return A clone.
175 *
176 * @throws CloneNotSupportedException not thrown by this class, but
177 * subclasses may differ.
178 */
179 public Object clone() throws CloneNotSupportedException {
180 return super.clone();
181 }
182
183 /**
184 * Tests if this object is equal to another.
185 *
186 * @param obj the object to test against for equality (<code>null</code>
187 * permitted).
188 *
189 * @return A boolean.
190 */
191 public boolean equals(Object obj) {
192 if (obj == this) {
193 return true;
194 }
195 if (!(obj instanceof XYDataItem)) {
196 return false;
197 }
198 XYDataItem that = (XYDataItem) obj;
199 if (!this.x.equals(that.x)) {
200 return false;
201 }
202 if (!ObjectUtilities.equal(this.y, that.y)) {
203 return false;
204 }
205 return true;
206 }
207
208 /**
209 * Returns a hash code.
210 *
211 * @return A hash code.
212 */
213 public int hashCode() {
214 int result;
215 result = this.x.hashCode();
216 result = 29 * result + (this.y != null ? this.y.hashCode() : 0);
217 return result;
218 }
219
220 }