KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > time > TimeSeriesDataItem


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 License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * -----------------------
27  * TimeSeriesDataItem.java
28  * -----------------------
29  * (C) Copyright 2001-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: TimeSeriesDataItem.java,v 1.5 2005/05/19 10:35:27 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 11-Oct-2001 : Version 1 (DG);
39  * 15-Nov-2001 : Updated Javadoc comments (DG);
40  * 29-Nov-2001 : Added cloning (DG);
41  * 24-Jun-2002 : Removed unnecessary import (DG);
42  * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
43  * 13-Mar-2003 : Renamed TimeSeriesDataPair --> TimeSeriesDataItem, moved to
44  * com.jrefinery.data.time package, implemented Serializable (DG)
45  */

46
47 package org.jfree.data.time;
48
49 import java.io.Serializable JavaDoc;
50
51 /**
52  * Represents one data item in a time series.
53  * <P>
54  * The time period can be any of the following:
55  * <ul>
56  * <li>{@link Year}</li>
57  * <li>{@link Quarter}</li>
58  * <li>{@link Month}</li>
59  * <li>{@link Week}</li>
60  * <li>{@link Day}</li>
61  * <li>{@link Hour}</li>
62  * <li>{@link Minute}</li>
63  * <li>{@link Second}</li>
64  * <li>{@link Millisecond}</li>
65  * <li>{@link FixedMillisecond}</li>
66  * </ul>
67  *
68  * The time period is an immutable property of the data item. Data items will
69  * often be sorted within a list, and allowing the time period to be changed
70  * could destroy the sort order.
71  * <P>
72  * Implements the <code>Comparable</code> interface so that standard Java
73  * sorting can be used to keep the data items in order.
74  *
75  */

76 public class TimeSeriesDataItem implements Cloneable JavaDoc, Comparable JavaDoc, Serializable JavaDoc {
77
78     /** For serialization. */
79     private static final long serialVersionUID = -2235346966016401302L;
80     
81     /** The time period. */
82     private RegularTimePeriod period;
83
84     /** The value associated with the time period. */
85     private Number JavaDoc value;
86
87     /**
88      * Constructs a new data item that associates a value with a time period.
89      *
90      * @param period the time period (<code>null</code> not permitted).
91      * @param value the value (<code>null</code> permitted).
92      */

93     public TimeSeriesDataItem(RegularTimePeriod period, Number JavaDoc value) {
94         if (period == null) {
95             throw new IllegalArgumentException JavaDoc("Null 'period' argument.");
96         }
97         this.period = period;
98         this.value = value;
99     }
100
101     /**
102      * Constructs a new data item that associates a value with a time period.
103      *
104      * @param period the time period (<code>null</code> not permitted).
105      * @param value the value associated with the time period.
106      */

107     public TimeSeriesDataItem(RegularTimePeriod period, double value) {
108         this(period, new Double JavaDoc(value));
109     }
110
111     /**
112      * Returns the time period.
113      *
114      * @return The time period (never <code>null</code>).
115      */

116     public RegularTimePeriod getPeriod() {
117         return this.period;
118     }
119
120     /**
121      * Returns the value.
122      *
123      * @return The value (<code>null</code> possible).
124      */

125     public Number JavaDoc getValue() {
126         return this.value;
127     }
128
129     /**
130      * Sets the value for this data item.
131      *
132      * @param value the value (<code>null</code> permitted).
133      */

134     public void setValue(Number JavaDoc value) {
135         this.value = value;
136     }
137
138     /**
139      * Tests this object for equality with an arbitrary object.
140      *
141      * @param o the other object.
142      *
143      * @return A boolean.
144      */

145     public boolean equals(Object JavaDoc o) {
146         if (this == o) {
147             return true;
148         }
149         if (!(o instanceof TimeSeriesDataItem)) {
150             return false;
151         }
152         TimeSeriesDataItem timeSeriesDataItem = (TimeSeriesDataItem) o;
153         if (this.period != null) {
154             if (!this.period.equals(timeSeriesDataItem.period)) {
155                 return false;
156             }
157         }
158         else if (timeSeriesDataItem.period != null) {
159            return false;
160         }
161         
162         if (this.value != null) {
163             if (!this.value.equals(timeSeriesDataItem.value)) {
164                 return false;
165             }
166         }
167         else if (timeSeriesDataItem.value != null) {
168             return false;
169         }
170
171         return true;
172     }
173
174     /**
175      * Returns a hash code.
176      *
177      * @return A hash code.
178      */

179     public int hashCode() {
180         int result;
181         result = (this.period != null ? this.period.hashCode() : 0);
182         result = 29 * result + (this.value != null ? this.value.hashCode() : 0);
183         return result;
184     }
185
186     /**
187      * Returns an integer indicating the order of this data pair object
188      * relative to another object.
189      * <P>
190      * For the order we consider only the timing:
191      * negative == before, zero == same, positive == after.
192      *
193      * @param o1 The object being compared to.
194      *
195      * @return An integer indicating the order of the data item object
196      * relative to another object.
197      */

198     public int compareTo(Object JavaDoc o1) {
199
200         int result;
201
202         // CASE 1 : Comparing to another TimeSeriesDataItem object
203
// -------------------------------------------------------
204
if (o1 instanceof TimeSeriesDataItem) {
205             TimeSeriesDataItem datapair = (TimeSeriesDataItem) o1;
206             result = getPeriod().compareTo(datapair.getPeriod());
207         }
208
209         // CASE 2 : Comparing to a general object
210
// ---------------------------------------------
211
else {
212             // consider time periods to be ordered after general objects
213
result = 1;
214         }
215
216         return result;
217
218     }
219
220     /**
221      * Clones the data item. Note: there is no need to clone the period or
222      * value since they are immutable classes.
223      *
224      * @return A clone of the data item.
225      */

226     public Object JavaDoc clone() {
227         Object JavaDoc clone = null;
228         try {
229             clone = super.clone();
230         }
231         catch (CloneNotSupportedException JavaDoc e) { // won't get here...
232
e.printStackTrace();
233         }
234         return clone;
235     }
236
237 }
238
Popular Tags