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