KickJava   Java API By Example, From Geeks To Geeks.

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


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  * TimePeriodValuesCollection.java
28  * -------------------------------
29  * (C) Copyright 2003-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: TimePeriodValuesCollection.java,v 1.10 2005/05/20 08:20:03 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 22-Apr-2003 : Version 1 (DG);
39  * 05-May-2004 : Now extends AbstractIntervalXYDataset (DG);
40  * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
41  * getYValue() (DG);
42  * 06-Oct-2004 : Updated for changes in DomainInfo interface (DG);
43  * 11-Jan-2005 : Removed deprecated code in preparation for 1.0.0 release (DG);
44  *
45  */

46
47 package org.jfree.data.time;
48
49 import java.io.Serializable JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.List JavaDoc;
52
53 import org.jfree.data.DomainInfo;
54 import org.jfree.data.Range;
55 import org.jfree.data.xy.AbstractIntervalXYDataset;
56 import org.jfree.data.xy.IntervalXYDataset;
57 import org.jfree.util.ObjectUtilities;
58
59 /**
60  * A collection of {@link TimePeriodValues} objects.
61  * <P>
62  * This class implements the {@link org.jfree.data.xy.XYDataset} interface, as
63  * well as the extended {@link IntervalXYDataset} interface. This makes it a
64  * convenient dataset for use with the {@link org.jfree.chart.plot.XYPlot}
65  * class.
66  */

67 public class TimePeriodValuesCollection extends AbstractIntervalXYDataset
68                                         implements IntervalXYDataset,
69                                                    DomainInfo,
70                                                    Serializable JavaDoc {
71
72     /** For serialization. */
73     private static final long serialVersionUID = -3077934065236454199L;
74     
75     /** Storage for the time series. */
76     private List JavaDoc data;
77
78     /**
79      * The position within a time period to return as the x-value (START,
80      * MIDDLE or END).
81      */

82     private TimePeriodAnchor xPosition;
83     
84     /**
85      * A flag that indicates that the domain is 'points in time'. If this
86      * flag is true, only the x-value is used to determine the range of values
87      * in the domain, the start and end x-values are ignored.
88      */

89     private boolean domainIsPointsInTime;
90
91     /**
92      * Constructs an empty dataset.
93      */

94     public TimePeriodValuesCollection() {
95         this((TimePeriodValues) null);
96     }
97
98     /**
99      * Constructs a dataset containing a single series. Additional series can
100      * be added.
101      *
102      * @param series the series.
103      */

104     public TimePeriodValuesCollection(TimePeriodValues series) {
105         this.data = new java.util.ArrayList JavaDoc();
106         this.xPosition = TimePeriodAnchor.MIDDLE;
107         this.domainIsPointsInTime = true;
108         if (series != null) {
109             this.data.add(series);
110             series.addChangeListener(this);
111         }
112     }
113
114     /**
115      * Returns the position of the X value within each time period.
116      *
117      * @return The position (never <code>null</code>).
118      */

119     public TimePeriodAnchor getXPosition() {
120         return this.xPosition;
121     }
122
123     /**
124      * Sets the position of the x axis within each time period.
125      *
126      * @param position the position (<code>null</code> not permitted).
127      */

128     public void setXPosition(TimePeriodAnchor position) {
129         if (position == null) {
130             throw new IllegalArgumentException JavaDoc("Null 'position' argument.");
131         }
132         this.xPosition = position;
133     }
134     
135     /**
136      * Returns a flag that controls whether the domain is treated as 'points
137      * in time'. This flag is used when determining the max and min values for
138      * the domain. If true, then only the x-values are considered for the max
139      * and min values. If false, then the start and end x-values will also be
140      * taken into consideration
141      *
142      * @return The flag.
143      */

144     public boolean getDomainIsPointsInTime() {
145         return this.domainIsPointsInTime;
146     }
147
148     /**
149      * Sets a flag that controls whether the domain is treated as 'points in
150      * time', or time periods.
151      *
152      * @param flag the new value of the flag.
153      */

154     public void setDomainIsPointsInTime(boolean flag) {
155         this.domainIsPointsInTime = flag;
156     }
157
158     /**
159      * Returns the number of series in the collection.
160      *
161      * @return The series count.
162      */

163     public int getSeriesCount() {
164         return this.data.size();
165     }
166
167     /**
168      * Returns a series.
169      *
170      * @param series the index of the series (zero-based).
171      *
172      * @return The series.
173      */

174     public TimePeriodValues getSeries(int series) {
175
176         if ((series < 0) || (series > getSeriesCount())) {
177             throw new IllegalArgumentException JavaDoc("Index 'series' out of range.");
178         }
179
180         return (TimePeriodValues) this.data.get(series);
181
182     }
183
184     /**
185      * Returns the key for a series.
186      *
187      * @param series the index of the series (zero-based).
188      *
189      * @return The key for a series.
190      */

191     public Comparable JavaDoc getSeriesKey(int series) {
192         // defer argument checking
193
return getSeries(series).getKey();
194     }
195
196     /**
197      * Adds a series to the collection. A
198      * {@link org.jfree.data.general.DatasetChangeEvent} is sent to all
199      * registered listeners.
200      *
201      * @param series the time series.
202      */

203     public void addSeries(TimePeriodValues series) {
204
205         if (series == null) {
206             throw new IllegalArgumentException JavaDoc("Null 'series' argument.");
207         }
208
209         this.data.add(series);
210         series.addChangeListener(this);
211         fireDatasetChanged();
212
213     }
214
215     /**
216      * Removes the specified series from the collection.
217      *
218      * @param series the series to remove (<code>null</code> not permitted).
219      */

220     public void removeSeries(TimePeriodValues series) {
221
222         if (series == null) {
223             throw new IllegalArgumentException JavaDoc("Null 'series' argument.");
224         }
225         this.data.remove(series);
226         series.removeChangeListener(this);
227         fireDatasetChanged();
228
229     }
230
231     /**
232      * Removes a series from the collection.
233      *
234      * @param index the series index (zero-based).
235      */

236     public void removeSeries(int index) {
237         TimePeriodValues series = getSeries(index);
238         if (series != null) {
239             removeSeries(series);
240         }
241     }
242
243     /**
244      * Returns the number of items in the specified series.
245      * <P>
246      * This method is provided for convenience.
247      *
248      * @param series the index of the series of interest (zero-based).
249      *
250      * @return The number of items in the specified series.
251      */

252     public int getItemCount(int series) {
253         return getSeries(series).getItemCount();
254     }
255
256     /**
257      * Returns the x-value for the specified series and item.
258      *
259      * @param series the series (zero-based index).
260      * @param item the item (zero-based index).
261      *
262      * @return The x-value for the specified series and item.
263      */

264     public Number JavaDoc getX(int series, int item) {
265         TimePeriodValues ts = (TimePeriodValues) this.data.get(series);
266         TimePeriodValue dp = ts.getDataItem(item);
267         TimePeriod period = dp.getPeriod();
268         return new Long JavaDoc(getX(period));
269     }
270
271     /**
272      * Returns the x-value for a time period.
273      *
274      * @param period the time period.
275      *
276      * @return The x-value.
277      */

278     private long getX(TimePeriod period) {
279
280         if (this.xPosition == TimePeriodAnchor.START) {
281             return period.getStart().getTime();
282         }
283         else if (this.xPosition == TimePeriodAnchor.MIDDLE) {
284             return period.getStart().getTime()
285                 / 2 + period.getEnd().getTime() / 2;
286         }
287         else if (this.xPosition == TimePeriodAnchor.END) {
288             return period.getEnd().getTime();
289         }
290         else {
291             throw new IllegalStateException JavaDoc("TimePeriodAnchor unknown.");
292         }
293
294     }
295
296     /**
297      * Returns the starting X value for the specified series and item.
298      *
299      * @param series the series (zero-based index).
300      * @param item the item (zero-based index).
301      *
302      * @return The starting X value for the specified series and item.
303      */

304     public Number JavaDoc getStartX(int series, int item) {
305         TimePeriodValues ts = (TimePeriodValues) this.data.get(series);
306         TimePeriodValue dp = ts.getDataItem(item);
307         return new Long JavaDoc(dp.getPeriod().getStart().getTime());
308     }
309
310     /**
311      * Returns the ending X value for the specified series and item.
312      *
313      * @param series the series (zero-based index).
314      * @param item the item (zero-based index).
315      *
316      * @return The ending X value for the specified series and item.
317      */

318     public Number JavaDoc getEndX(int series, int item) {
319         TimePeriodValues ts = (TimePeriodValues) this.data.get(series);
320         TimePeriodValue dp = ts.getDataItem(item);
321         return new Long JavaDoc(dp.getPeriod().getEnd().getTime());
322     }
323
324     /**
325      * Returns the y-value for the specified series and item.
326      *
327      * @param series the series (zero-based index).
328      * @param item the item (zero-based index).
329      *
330      * @return The y-value for the specified series and item.
331      */

332     public Number JavaDoc getY(int series, int item) {
333         TimePeriodValues ts = (TimePeriodValues) this.data.get(series);
334         TimePeriodValue dp = ts.getDataItem(item);
335         return dp.getValue();
336     }
337
338     /**
339      * Returns the starting Y value for the specified series and item.
340      *
341      * @param series the series (zero-based index).
342      * @param item the item (zero-based index).
343      *
344      * @return The starting Y value for the specified series and item.
345      */

346     public Number JavaDoc getStartY(int series, int item) {
347         return getY(series, item);
348     }
349
350     /**
351      * Returns the ending Y value for the specified series and item.
352      *
353      * @param series the series (zero-based index).
354      * @param item the item (zero-based index).
355      *
356      * @return The ending Y value for the specified series and item.
357      */

358     public Number JavaDoc getEndY(int series, int item) {
359         return getY(series, item);
360     }
361
362     /**
363      * Returns the minimum x-value in the dataset.
364      *
365      * @param includeInterval a flag that determines whether or not the
366      * x-interval is taken into account.
367      *
368      * @return The minimum value.
369      */

370     public double getDomainLowerBound(boolean includeInterval) {
371         double result = Double.NaN;
372         Range r = getDomainBounds(includeInterval);
373         if (r != null) {
374             result = r.getLowerBound();
375         }
376         return result;
377     }
378
379     /**
380      * Returns the maximum x-value in the dataset.
381      *
382      * @param includeInterval a flag that determines whether or not the
383      * x-interval is taken into account.
384      *
385      * @return The maximum value.
386      */

387     public double getDomainUpperBound(boolean includeInterval) {
388         double result = Double.NaN;
389         Range r = getDomainBounds(includeInterval);
390         if (r != null) {
391             result = r.getUpperBound();
392         }
393         return result;
394     }
395
396     /**
397      * Returns the range of the values in this dataset's domain.
398      *
399      * @param includeInterval a flag that determines whether or not the
400      * x-interval is taken into account.
401      *
402      * @return The range.
403      */

404     public Range getDomainBounds(boolean includeInterval) {
405         Range result = null;
406         Range temp = null;
407         Iterator JavaDoc iterator = this.data.iterator();
408         while (iterator.hasNext()) {
409             TimePeriodValues series = (TimePeriodValues) iterator.next();
410             int count = series.getItemCount();
411             if (count > 0) {
412                 TimePeriod start = series.getTimePeriod(
413                     series.getMinStartIndex()
414                 );
415                 TimePeriod end = series.getTimePeriod(series.getMaxEndIndex());
416                 if (this.domainIsPointsInTime) {
417                     if (this.xPosition == TimePeriodAnchor.START) {
418                         TimePeriod maxStart = series.getTimePeriod(
419                             series.getMaxStartIndex()
420                         );
421                         temp = new Range(
422                             start.getStart().getTime(),
423                             maxStart.getStart().getTime()
424                         );
425                     }
426                     else if (this.xPosition == TimePeriodAnchor.MIDDLE) {
427                         TimePeriod minMiddle = series.getTimePeriod(
428                             series.getMinMiddleIndex()
429                         );
430                         long s1 = minMiddle.getStart().getTime();
431                         long e1 = minMiddle.getEnd().getTime();
432                         TimePeriod maxMiddle = series.getTimePeriod(
433                             series.getMaxMiddleIndex()
434                         );
435                         long s2 = maxMiddle.getStart().getTime();
436                         long e2 = maxMiddle.getEnd().getTime();
437                         temp = new Range(
438                             s1 + (e1 - s1) / 2, s2 + (e2 - s2) / 2
439                         );
440                     }
441                     else if (this.xPosition == TimePeriodAnchor.END) {
442                         TimePeriod minEnd = series.getTimePeriod(
443                             series.getMinEndIndex()
444                         );
445                         temp = new Range(
446                             minEnd.getEnd().getTime(), end.getEnd().getTime()
447                         );
448                     }
449                 }
450                 else {
451                     temp = new Range(
452                         start.getStart().getTime(), end.getEnd().getTime()
453                     );
454                 }
455                 result = Range.combine(result, temp);
456             }
457         }
458         return result;
459     }
460
461     /**
462      * Tests this instance for equality with an arbitrary object.
463      *
464      * @param obj the object (<code>null</code> permitted).
465      *
466      * @return A boolean.
467      */

468     public boolean equals(Object JavaDoc obj) {
469         if (obj == this) {
470             return true;
471         }
472         if (!(obj instanceof TimePeriodValuesCollection)) {
473             return false;
474         }
475         TimePeriodValuesCollection that = (TimePeriodValuesCollection) obj;
476         if (this.domainIsPointsInTime != that.domainIsPointsInTime) {
477             return false;
478         }
479         if (this.xPosition != that.xPosition) {
480             return false;
481         }
482         if (!ObjectUtilities.equal(this.data, that.data)) {
483             return false;
484         }
485         return true;
486     }
487 }
488
Popular Tags