001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, 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: * TimeSeriesCollection.java
029: * -------------------------
030: * (C) Copyright 2001-2007, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: TimeSeriesCollection.java,v 1.10.2.5 2007/05/08 10:58:50 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 11-Oct-2001 : Version 1 (DG);
040: * 18-Oct-2001 : Added implementation of IntervalXYDataSource so that bar plots
041: * (using numerical axes) can be plotted from time series
042: * data (DG);
043: * 22-Oct-2001 : Renamed DataSource.java --> Dataset.java etc. (DG);
044: * 15-Nov-2001 : Added getSeries() method. Changed name from TimeSeriesDataset
045: * to TimeSeriesCollection (DG);
046: * 07-Dec-2001 : TimeSeries --> BasicTimeSeries (DG);
047: * 01-Mar-2002 : Added a time zone offset attribute, to enable fast calculation
048: * of the time period start and end values (DG);
049: * 29-Mar-2002 : The collection now registers itself with all the time series
050: * objects as a SeriesChangeListener. Removed redundant
051: * calculateZoneOffset method (DG);
052: * 06-Jun-2002 : Added a setting to control whether the x-value supplied in the
053: * getXValue() method comes from the START, MIDDLE, or END of the
054: * time period. This is a workaround for JFreeChart, where the
055: * current date axis always labels the start of a time
056: * period (DG);
057: * 24-Jun-2002 : Removed unnecessary import (DG);
058: * 24-Aug-2002 : Implemented DomainInfo interface, and added the
059: * DomainIsPointsInTime flag (DG);
060: * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
061: * 16-Oct-2002 : Added remove methods (DG);
062: * 10-Jan-2003 : Changed method names in RegularTimePeriod class (DG);
063: * 13-Mar-2003 : Moved to com.jrefinery.data.time package and implemented
064: * Serializable (DG);
065: * 04-Sep-2003 : Added getSeries(String) method (DG);
066: * 15-Sep-2003 : Added a removeAllSeries() method to match
067: * XYSeriesCollection (DG);
068: * 05-May-2004 : Now extends AbstractIntervalXYDataset (DG);
069: * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
070: * getYValue() (DG);
071: * 06-Oct-2004 : Updated for changed in DomainInfo interface (DG);
072: * 11-Jan-2005 : Removed deprecated code in preparation for the 1.0.0
073: * release (DG);
074: * 28-Mar-2005 : Fixed bug in getSeries(int) method (1170825) (DG);
075: * ------------- JFREECHART 1.0.x ---------------------------------------------
076: * 13-Dec-2005 : Deprecated the 'domainIsPointsInTime' flag as it is
077: * redundant. Fixes bug 1243050 (DG);
078: * 04-May-2007 : Override getDomainOrder() to indicate that items are sorted
079: * by x-value (ascending) (DG);
080: * 08-May-2007 : Added indexOf(TimeSeries) method (DG);
081: *
082: */
083:
084: package org.jfree.data.time;
085:
086: import java.io.Serializable;
087: import java.util.ArrayList;
088: import java.util.Calendar;
089: import java.util.Collections;
090: import java.util.Iterator;
091: import java.util.List;
092: import java.util.TimeZone;
093:
094: import org.jfree.data.DomainInfo;
095: import org.jfree.data.DomainOrder;
096: import org.jfree.data.Range;
097: import org.jfree.data.general.DatasetChangeEvent;
098: import org.jfree.data.xy.AbstractIntervalXYDataset;
099: import org.jfree.data.xy.IntervalXYDataset;
100: import org.jfree.data.xy.XYDataset;
101: import org.jfree.util.ObjectUtilities;
102:
103: /**
104: * A collection of time series objects. This class implements the
105: * {@link org.jfree.data.xy.XYDataset} interface, as well as the extended
106: * {@link IntervalXYDataset} interface. This makes it a convenient dataset for
107: * use with the {@link org.jfree.chart.plot.XYPlot} class.
108: */
109: public class TimeSeriesCollection extends AbstractIntervalXYDataset
110: implements XYDataset, IntervalXYDataset, DomainInfo,
111: Serializable {
112:
113: /** For serialization. */
114: private static final long serialVersionUID = 834149929022371137L;
115:
116: /** Storage for the time series. */
117: private List data;
118:
119: /** A working calendar (to recycle) */
120: private Calendar workingCalendar;
121:
122: /**
123: * The point within each time period that is used for the X value when this
124: * collection is used as an {@link org.jfree.data.xy.XYDataset}. This can
125: * be the start, middle or end of the time period.
126: */
127: private TimePeriodAnchor xPosition;
128:
129: /**
130: * A flag that indicates that the domain is 'points in time'. If this
131: * flag is true, only the x-value is used to determine the range of values
132: * in the domain, the start and end x-values are ignored.
133: *
134: * @deprecated No longer used (as of 1.0.1).
135: */
136: private boolean domainIsPointsInTime;
137:
138: /**
139: * Constructs an empty dataset, tied to the default timezone.
140: */
141: public TimeSeriesCollection() {
142: this (null, TimeZone.getDefault());
143: }
144:
145: /**
146: * Constructs an empty dataset, tied to a specific timezone.
147: *
148: * @param zone the timezone (<code>null</code> permitted, will use
149: * <code>TimeZone.getDefault()</code> in that case).
150: */
151: public TimeSeriesCollection(TimeZone zone) {
152: this (null, zone);
153: }
154:
155: /**
156: * Constructs a dataset containing a single series (more can be added),
157: * tied to the default timezone.
158: *
159: * @param series the series (<code>null</code> permitted).
160: */
161: public TimeSeriesCollection(TimeSeries series) {
162: this (series, TimeZone.getDefault());
163: }
164:
165: /**
166: * Constructs a dataset containing a single series (more can be added),
167: * tied to a specific timezone.
168: *
169: * @param series a series to add to the collection (<code>null</code>
170: * permitted).
171: * @param zone the timezone (<code>null</code> permitted, will use
172: * <code>TimeZone.getDefault()</code> in that case).
173: */
174: public TimeSeriesCollection(TimeSeries series, TimeZone zone) {
175:
176: if (zone == null) {
177: zone = TimeZone.getDefault();
178: }
179: this .workingCalendar = Calendar.getInstance(zone);
180: this .data = new ArrayList();
181: if (series != null) {
182: this .data.add(series);
183: series.addChangeListener(this );
184: }
185: this .xPosition = TimePeriodAnchor.START;
186: this .domainIsPointsInTime = true;
187:
188: }
189:
190: /**
191: * Returns a flag that controls whether the domain is treated as 'points in
192: * time'. This flag is used when determining the max and min values for
193: * the domain. If <code>true</code>, then only the x-values are considered
194: * for the max and min values. If <code>false</code>, then the start and
195: * end x-values will also be taken into consideration.
196: *
197: * @return The flag.
198: *
199: * @deprecated This flag is no longer used (as of 1.0.1).
200: */
201: public boolean getDomainIsPointsInTime() {
202: return this .domainIsPointsInTime;
203: }
204:
205: /**
206: * Sets a flag that controls whether the domain is treated as 'points in
207: * time', or time periods.
208: *
209: * @param flag the flag.
210: *
211: * @deprecated This flag is no longer used, as of 1.0.1. The
212: * <code>includeInterval</code> flag in methods such as
213: * {@link #getDomainBounds(boolean)} makes this unnecessary.
214: */
215: public void setDomainIsPointsInTime(boolean flag) {
216: this .domainIsPointsInTime = flag;
217: notifyListeners(new DatasetChangeEvent(this , this ));
218: }
219:
220: /**
221: * Returns the order of the domain values in this dataset.
222: *
223: * @return {@link DomainOrder#ASCENDING}
224: */
225: public DomainOrder getDomainOrder() {
226: return DomainOrder.ASCENDING;
227: }
228:
229: /**
230: * Returns the position within each time period that is used for the X
231: * value when the collection is used as an
232: * {@link org.jfree.data.xy.XYDataset}.
233: *
234: * @return The anchor position (never <code>null</code>).
235: */
236: public TimePeriodAnchor getXPosition() {
237: return this .xPosition;
238: }
239:
240: /**
241: * Sets the position within each time period that is used for the X values
242: * when the collection is used as an {@link XYDataset}, then sends a
243: * {@link DatasetChangeEvent} is sent to all registered listeners.
244: *
245: * @param anchor the anchor position (<code>null</code> not permitted).
246: */
247: public void setXPosition(TimePeriodAnchor anchor) {
248: if (anchor == null) {
249: throw new IllegalArgumentException(
250: "Null 'anchor' argument.");
251: }
252: this .xPosition = anchor;
253: notifyListeners(new DatasetChangeEvent(this , this ));
254: }
255:
256: /**
257: * Returns a list of all the series in the collection.
258: *
259: * @return The list (which is unmodifiable).
260: */
261: public List getSeries() {
262: return Collections.unmodifiableList(this .data);
263: }
264:
265: /**
266: * Returns the number of series in the collection.
267: *
268: * @return The series count.
269: */
270: public int getSeriesCount() {
271: return this .data.size();
272: }
273:
274: /**
275: * Returns the index of the specified series, or -1 if that series is not
276: * present in the dataset.
277: *
278: * @param series the series (<code>null</code> not permitted).
279: *
280: * @return The series index.
281: *
282: * @since 1.0.6
283: */
284: public int indexOf(TimeSeries series) {
285: if (series == null) {
286: throw new IllegalArgumentException(
287: "Null 'series' argument.");
288: }
289: return this .data.indexOf(series);
290: }
291:
292: /**
293: * Returns a series.
294: *
295: * @param series the index of the series (zero-based).
296: *
297: * @return The series.
298: */
299: public TimeSeries getSeries(int series) {
300: if ((series < 0) || (series >= getSeriesCount())) {
301: throw new IllegalArgumentException(
302: "The 'series' argument is out of bounds (" + series
303: + ").");
304: }
305: return (TimeSeries) this .data.get(series);
306: }
307:
308: /**
309: * Returns the series with the specified key, or <code>null</code> if
310: * there is no such series.
311: *
312: * @param key the series key (<code>null</code> permitted).
313: *
314: * @return The series with the given key.
315: */
316: public TimeSeries getSeries(String key) {
317: TimeSeries result = null;
318: Iterator iterator = this .data.iterator();
319: while (iterator.hasNext()) {
320: TimeSeries series = (TimeSeries) iterator.next();
321: Comparable k = series.getKey();
322: if (k != null && k.equals(key)) {
323: result = series;
324: }
325: }
326: return result;
327: }
328:
329: /**
330: * Returns the key for a series.
331: *
332: * @param series the index of the series (zero-based).
333: *
334: * @return The key for a series.
335: */
336: public Comparable getSeriesKey(int series) {
337: // check arguments...delegated
338: // fetch the series name...
339: return getSeries(series).getKey();
340: }
341:
342: /**
343: * Adds a series to the collection and sends a {@link DatasetChangeEvent} to
344: * all registered listeners.
345: *
346: * @param series the series (<code>null</code> not permitted).
347: */
348: public void addSeries(TimeSeries series) {
349: if (series == null) {
350: throw new IllegalArgumentException(
351: "Null 'series' argument.");
352: }
353: this .data.add(series);
354: series.addChangeListener(this );
355: fireDatasetChanged();
356: }
357:
358: /**
359: * Removes the specified series from the collection and sends a
360: * {@link DatasetChangeEvent} to all registered listeners.
361: *
362: * @param series the series (<code>null</code> not permitted).
363: */
364: public void removeSeries(TimeSeries series) {
365: if (series == null) {
366: throw new IllegalArgumentException(
367: "Null 'series' argument.");
368: }
369: this .data.remove(series);
370: series.removeChangeListener(this );
371: fireDatasetChanged();
372: }
373:
374: /**
375: * Removes a series from the collection.
376: *
377: * @param index the series index (zero-based).
378: */
379: public void removeSeries(int index) {
380: TimeSeries series = getSeries(index);
381: if (series != null) {
382: removeSeries(series);
383: }
384: }
385:
386: /**
387: * Removes all the series from the collection and sends a
388: * {@link DatasetChangeEvent} to all registered listeners.
389: */
390: public void removeAllSeries() {
391:
392: // deregister the collection as a change listener to each series in the
393: // collection
394: for (int i = 0; i < this .data.size(); i++) {
395: TimeSeries series = (TimeSeries) this .data.get(i);
396: series.removeChangeListener(this );
397: }
398:
399: // remove all the series from the collection and notify listeners.
400: this .data.clear();
401: fireDatasetChanged();
402:
403: }
404:
405: /**
406: * Returns the number of items in the specified series. This method is
407: * provided for convenience.
408: *
409: * @param series the series index (zero-based).
410: *
411: * @return The item count.
412: */
413: public int getItemCount(int series) {
414: return getSeries(series).getItemCount();
415: }
416:
417: /**
418: * Returns the x-value (as a double primitive) for an item within a series.
419: *
420: * @param series the series (zero-based index).
421: * @param item the item (zero-based index).
422: *
423: * @return The x-value.
424: */
425: public double getXValue(int series, int item) {
426: TimeSeries s = (TimeSeries) this .data.get(series);
427: TimeSeriesDataItem i = s.getDataItem(item);
428: RegularTimePeriod period = i.getPeriod();
429: return getX(period);
430: }
431:
432: /**
433: * Returns the x-value for the specified series and item.
434: *
435: * @param series the series (zero-based index).
436: * @param item the item (zero-based index).
437: *
438: * @return The value.
439: */
440: public Number getX(int series, int item) {
441: TimeSeries ts = (TimeSeries) this .data.get(series);
442: TimeSeriesDataItem dp = ts.getDataItem(item);
443: RegularTimePeriod period = dp.getPeriod();
444: return new Long(getX(period));
445: }
446:
447: /**
448: * Returns the x-value for a time period.
449: *
450: * @param period the time period (<code>null</code> not permitted).
451: *
452: * @return The x-value.
453: */
454: protected synchronized long getX(RegularTimePeriod period) {
455: long result = 0L;
456: if (this .xPosition == TimePeriodAnchor.START) {
457: result = period.getFirstMillisecond(this .workingCalendar);
458: } else if (this .xPosition == TimePeriodAnchor.MIDDLE) {
459: result = period.getMiddleMillisecond(this .workingCalendar);
460: } else if (this .xPosition == TimePeriodAnchor.END) {
461: result = period.getLastMillisecond(this .workingCalendar);
462: }
463: return result;
464: }
465:
466: /**
467: * Returns the starting X value for the specified series and item.
468: *
469: * @param series the series (zero-based index).
470: * @param item the item (zero-based index).
471: *
472: * @return The value.
473: */
474: public synchronized Number getStartX(int series, int item) {
475: TimeSeries ts = (TimeSeries) this .data.get(series);
476: TimeSeriesDataItem dp = ts.getDataItem(item);
477: return new Long(dp.getPeriod().getFirstMillisecond(
478: this .workingCalendar));
479: }
480:
481: /**
482: * Returns the ending X value for the specified series and item.
483: *
484: * @param series The series (zero-based index).
485: * @param item The item (zero-based index).
486: *
487: * @return The value.
488: */
489: public synchronized Number getEndX(int series, int item) {
490: TimeSeries ts = (TimeSeries) this .data.get(series);
491: TimeSeriesDataItem dp = ts.getDataItem(item);
492: return new Long(dp.getPeriod().getLastMillisecond(
493: this .workingCalendar));
494: }
495:
496: /**
497: * Returns the y-value for the specified series and item.
498: *
499: * @param series the series (zero-based index).
500: * @param item the item (zero-based index).
501: *
502: * @return The value (possibly <code>null</code>).
503: */
504: public Number getY(int series, int item) {
505: TimeSeries ts = (TimeSeries) this .data.get(series);
506: TimeSeriesDataItem dp = ts.getDataItem(item);
507: return dp.getValue();
508: }
509:
510: /**
511: * Returns the starting Y value for the specified series and item.
512: *
513: * @param series the series (zero-based index).
514: * @param item the item (zero-based index).
515: *
516: * @return The value (possibly <code>null</code>).
517: */
518: public Number getStartY(int series, int item) {
519: return getY(series, item);
520: }
521:
522: /**
523: * Returns the ending Y value for the specified series and item.
524: *
525: * @param series te series (zero-based index).
526: * @param item the item (zero-based index).
527: *
528: * @return The value (possibly <code>null</code>).
529: */
530: public Number getEndY(int series, int item) {
531: return getY(series, item);
532: }
533:
534: /**
535: * Returns the indices of the two data items surrounding a particular
536: * millisecond value.
537: *
538: * @param series the series index.
539: * @param milliseconds the time.
540: *
541: * @return An array containing the (two) indices of the items surrounding
542: * the time.
543: */
544: public int[] getSurroundingItems(int series, long milliseconds) {
545: int[] result = new int[] { -1, -1 };
546: TimeSeries timeSeries = getSeries(series);
547: for (int i = 0; i < timeSeries.getItemCount(); i++) {
548: Number x = getX(series, i);
549: long m = x.longValue();
550: if (m <= milliseconds) {
551: result[0] = i;
552: }
553: if (m >= milliseconds) {
554: result[1] = i;
555: break;
556: }
557: }
558: return result;
559: }
560:
561: /**
562: * Returns the minimum x-value in the dataset.
563: *
564: * @param includeInterval a flag that determines whether or not the
565: * x-interval is taken into account.
566: *
567: * @return The minimum value.
568: */
569: public double getDomainLowerBound(boolean includeInterval) {
570: double result = Double.NaN;
571: Range r = getDomainBounds(includeInterval);
572: if (r != null) {
573: result = r.getLowerBound();
574: }
575: return result;
576: }
577:
578: /**
579: * Returns the maximum x-value in the dataset.
580: *
581: * @param includeInterval a flag that determines whether or not the
582: * x-interval is taken into account.
583: *
584: * @return The maximum value.
585: */
586: public double getDomainUpperBound(boolean includeInterval) {
587: double result = Double.NaN;
588: Range r = getDomainBounds(includeInterval);
589: if (r != null) {
590: result = r.getUpperBound();
591: }
592: return result;
593: }
594:
595: /**
596: * Returns the range of the values in this dataset's domain.
597: *
598: * @param includeInterval a flag that determines whether or not the
599: * x-interval is taken into account.
600: *
601: * @return The range.
602: */
603: public Range getDomainBounds(boolean includeInterval) {
604: Range result = null;
605: Iterator iterator = this .data.iterator();
606: while (iterator.hasNext()) {
607: TimeSeries series = (TimeSeries) iterator.next();
608: int count = series.getItemCount();
609: if (count > 0) {
610: RegularTimePeriod start = series.getTimePeriod(0);
611: RegularTimePeriod end = series.getTimePeriod(count - 1);
612: Range temp;
613: if (!includeInterval) {
614: temp = new Range(getX(start), getX(end));
615: } else {
616: temp = new Range(
617: start
618: .getFirstMillisecond(this .workingCalendar),
619: end
620: .getLastMillisecond(this .workingCalendar));
621: }
622: result = Range.combine(result, temp);
623: }
624: }
625: return result;
626: }
627:
628: /**
629: * Tests this time series collection for equality with another object.
630: *
631: * @param obj the other object.
632: *
633: * @return A boolean.
634: */
635: public boolean equals(Object obj) {
636: if (obj == this ) {
637: return true;
638: }
639: if (!(obj instanceof TimeSeriesCollection)) {
640: return false;
641: }
642: TimeSeriesCollection that = (TimeSeriesCollection) obj;
643: if (this .xPosition != that.xPosition) {
644: return false;
645: }
646: if (this .domainIsPointsInTime != that.domainIsPointsInTime) {
647: return false;
648: }
649: if (!ObjectUtilities.equal(this .data, that.data)) {
650: return false;
651: }
652: return true;
653: }
654:
655: /**
656: * Returns a hash code value for the object.
657: *
658: * @return The hashcode
659: */
660: public int hashCode() {
661: int result;
662: result = this .data.hashCode();
663: result = 29
664: * result
665: + (this .workingCalendar != null ? this .workingCalendar
666: .hashCode() : 0);
667: result = 29
668: * result
669: + (this .xPosition != null ? this .xPosition.hashCode()
670: : 0);
671: result = 29 * result + (this .domainIsPointsInTime ? 1 : 0);
672: return result;
673: }
674:
675: }
|