KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > xy > XYDataItem


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ---------------
28  * XYDataItem.java
29  * ---------------
30  * (C) Copyright 2003-2005, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: XYDataItem.java,v 1.6.2.1 2005/10/25 21:36:51 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 05-Aug-2003 : Renamed XYDataPair --> XYDataItem (DG);
40  * 03-Feb-2004 : Fixed bug in equals() method (DG);
41  * 21-Feb-2005 : Added setY(double) method (DG);
42  *
43  */

44
45 package org.jfree.data.xy;
46
47 import java.io.Serializable JavaDoc;
48
49 import org.jfree.util.ObjectUtilities;
50
51 /**
52  * Represents one (x, y) data item for an {@link XYSeries}.
53  */

54 public class XYDataItem implements Cloneable JavaDoc, Comparable JavaDoc, Serializable JavaDoc {
55
56     private static final long serialVersionUID = 2751513470325494890L;
57     
58     /** The x-value. */
59     private Number JavaDoc x;
60
61     /** The y-value. */
62     private Number JavaDoc y;
63
64     /**
65      * Constructs a new data item.
66      *
67      * @param x the x-value (<code>null</code> NOT permitted).
68      * @param y the y-value (<code>null</code> permitted).
69      */

70     public XYDataItem(Number JavaDoc x, Number JavaDoc y) {
71         if (x == null) {
72             throw new IllegalArgumentException JavaDoc("Null 'x' argument.");
73         }
74         this.x = x;
75         this.y = y;
76     }
77
78     /**
79      * Constructs a new data item.
80      *
81      * @param x the x-value.
82      * @param y the y-value.
83      */

84     public XYDataItem(double x, double y) {
85         this(new Double JavaDoc(x), new Double JavaDoc(y));
86     }
87
88     /**
89      * Returns the x-value.
90      *
91      * @return The x-value (never <code>null</code>).
92      */

93     public Number JavaDoc getX() {
94         return this.x;
95     }
96
97     /**
98      * Returns the y-value.
99      *
100      * @return The y-value (possibly <code>null</code>).
101      */

102     public Number JavaDoc 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 JavaDoc(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 JavaDoc 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 JavaDoc 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             }
151             else {
152                 if (compare < 0.0) {
153                     result = -1;
154                 }
155                 else {
156                     result = 0;
157                 }
158             }
159         }
160
161         // CASE 2 : Comparing to a general object
162
// ---------------------------------------------
163
else {
164             // consider time periods to be ordered after general objects
165
result = 1;
166         }
167
168         return result;
169
170     }
171
172     /**
173      * Returns a clone of this object.
174      *
175      * @return A clone.
176      *
177      * @throws CloneNotSupportedException not thrown by this class, but
178      * subclasses may differ.
179      */

180     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
181         return super.clone();
182     }
183     
184     /**
185      * Tests if this object is equal to another.
186      *
187      * @param obj the object to test against for equality (<code>null</code>
188      * permitted).
189      *
190      * @return A boolean.
191      */

192     public boolean equals(Object JavaDoc obj) {
193         if (obj == this) {
194             return true;
195         }
196         if (!(obj instanceof XYDataItem)) {
197             return false;
198         }
199         XYDataItem that = (XYDataItem) obj;
200         if (!this.x.equals(that.x)) {
201             return false;
202         }
203         if (!ObjectUtilities.equal(this.y, that.y)) {
204             return false;
205         }
206         return true;
207     }
208
209     /**
210      * Returns a hash code.
211      *
212      * @return A hash code.
213      */

214     public int hashCode() {
215         int result;
216         result = this.x.hashCode();
217         result = 29 * result + (this.y != null ? this.y.hashCode() : 0);
218         return result;
219     }
220     
221 }
222
Popular Tags