KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * ---------------
23  * XYDataItem.java
24  * ---------------
25  * (C) Copyright 2003, by Object Refinery Limited.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: XYDataItem.java,v 1.2 2003/11/28 11:37:28 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 05-Aug-2003 : Renamed XYDataPair --> XYDataItem (DG);
35  *
36  */

37
38 package org.jfree.data;
39
40 import java.io.Serializable JavaDoc;
41
42 /**
43  * Represents one (x, y) data item for an xy-series.
44  *
45  * @author David Gilbert
46  */

47 public class XYDataItem implements Cloneable JavaDoc, Comparable JavaDoc, Serializable JavaDoc {
48
49     /** The x-value. */
50     private Number JavaDoc x;
51
52     /** The y-value. */
53     private Number JavaDoc y;
54
55     /**
56      * Constructs a new data item.
57      *
58      * @param x the x-value (<code>null</code> NOT permitted).
59      * @param y the y-value (<code>null</code> permitted).
60      */

61     public XYDataItem(Number JavaDoc x, Number JavaDoc y) {
62         if (x == null) {
63             throw new IllegalArgumentException JavaDoc("XYDataItem constructor : null x not allowed.");
64         }
65         this.x = x;
66         this.y = y;
67     }
68
69     /**
70      * Constructs a new data pair.
71      *
72      * @param x the x-value.
73      * @param y the y-value.
74      */

75     public XYDataItem(double x, double y) {
76         this(new Double JavaDoc(x), new Double JavaDoc(y));
77     }
78
79     /**
80      * Returns the x-value.
81      *
82      * @return the x-value (never <code>null</code>).
83      */

84     public Number JavaDoc getX() {
85         return this.x;
86     }
87
88     /**
89      * Returns the y-value.
90      *
91      * @return the y-value (possibly <code>null</code>).
92      */

93     public Number JavaDoc getY() {
94         return this.y;
95     }
96
97     /**
98      * Sets the y-value for this data pair.
99      * <P>
100      * Note that there is no corresponding method to change the x-value.
101      *
102      * @param y the new y-value (<code>null</code> permitted).
103      */

104     public void setY(Number JavaDoc y) {
105         this.y = y;
106     }
107
108     /**
109      * Returns an integer indicating the order of this data pair object relative to another object.
110      * <P>
111      * For the order we consider only the x-value:
112      * negative == "less-than", zero == "equal", positive == "greater-than".
113      *
114      * @param o1 the object being compared to.
115      *
116      * @return an integer indicating the order of this data pair object
117      * relative to another object.
118      */

119     public int compareTo(Object JavaDoc o1) {
120
121         int result;
122
123         // CASE 1 : Comparing to another TimeSeriesDataPair object
124
// -------------------------------------------------------
125
if (o1 instanceof XYDataItem) {
126             XYDataItem dataItem = (XYDataItem) o1;
127             double compare = this.x.doubleValue() - dataItem.getX().doubleValue();
128             if (compare > 0) {
129                 result = 1;
130             }
131             else {
132                 if (compare < 0) {
133                     result = -1;
134                 }
135                 else {
136                     result = 0;
137                 }
138             }
139         }
140
141         // CASE 2 : Comparing to a general object
142
// ---------------------------------------------
143
else {
144             // consider time periods to be ordered after general objects
145
result = 1;
146         }
147
148         return result;
149
150     }
151
152     /**
153      * Returns a clone of this object.
154      *
155      * @return a clone.
156      */

157     public Object JavaDoc clone() {
158
159         Object JavaDoc clone = null;
160
161         try {
162             clone = super.clone();
163         }
164         catch (CloneNotSupportedException JavaDoc e) { // won't get here...
165
System.err.println("XYDataItem.clone(): operation not supported.");
166         }
167
168         return clone;
169
170     }
171
172 }
173
Popular Tags