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: * TimePeriodValuesCollection.java
029: * -------------------------------
030: * (C) Copyright 2003-2007, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: TimePeriodValuesCollection.java,v 1.10.2.5 2007/06/11 10:19:03 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 22-Apr-2003 : Version 1 (DG);
040: * 05-May-2004 : Now extends AbstractIntervalXYDataset (DG);
041: * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
042: * getYValue() (DG);
043: * 06-Oct-2004 : Updated for changes in DomainInfo interface (DG);
044: * 11-Jan-2005 : Removed deprecated code in preparation for 1.0.0 release (DG);
045: * ------------- JFREECHART 1.0.x ---------------------------------------------
046: * 03-Oct-2006 : Deprecated get/setDomainIsPointsInTime() (DG);
047: * 11-Jun-2007 : Fixed bug in getDomainBounds() method, and changed default
048: * value for domainIsPointsInTime to false (DG);
049: *
050: */
051:
052: package org.jfree.data.time;
053:
054: import java.io.Serializable;
055: import java.util.Iterator;
056: import java.util.List;
057:
058: import org.jfree.data.DomainInfo;
059: import org.jfree.data.Range;
060: import org.jfree.data.xy.AbstractIntervalXYDataset;
061: import org.jfree.data.xy.IntervalXYDataset;
062: import org.jfree.util.ObjectUtilities;
063:
064: /**
065: * A collection of {@link TimePeriodValues} objects.
066: * <P>
067: * This class implements the {@link org.jfree.data.xy.XYDataset} interface, as
068: * well as the extended {@link IntervalXYDataset} interface. This makes it a
069: * convenient dataset for use with the {@link org.jfree.chart.plot.XYPlot}
070: * class.
071: */
072: public class TimePeriodValuesCollection extends
073: AbstractIntervalXYDataset implements IntervalXYDataset,
074: DomainInfo, Serializable {
075:
076: /** For serialization. */
077: private static final long serialVersionUID = -3077934065236454199L;
078:
079: /** Storage for the time series. */
080: private List data;
081:
082: /**
083: * The position within a time period to return as the x-value (START,
084: * MIDDLE or END).
085: */
086: private TimePeriodAnchor xPosition;
087:
088: /**
089: * A flag that indicates that the domain is 'points in time'. If this
090: * flag is true, only the x-value is used to determine the range of values
091: * in the domain, the start and end x-values are ignored.
092: */
093: private boolean domainIsPointsInTime;
094:
095: /**
096: * Constructs an empty dataset.
097: */
098: public TimePeriodValuesCollection() {
099: this ((TimePeriodValues) null);
100: }
101:
102: /**
103: * Constructs a dataset containing a single series. Additional series can
104: * be added.
105: *
106: * @param series the series (<code>null</code> ignored).
107: */
108: public TimePeriodValuesCollection(TimePeriodValues series) {
109: this .data = new java.util.ArrayList();
110: this .xPosition = TimePeriodAnchor.MIDDLE;
111: this .domainIsPointsInTime = false;
112: if (series != null) {
113: this .data.add(series);
114: series.addChangeListener(this );
115: }
116: }
117:
118: /**
119: * Returns the position of the X value within each time period.
120: *
121: * @return The position (never <code>null</code>).
122: *
123: * @see #setXPosition(TimePeriodAnchor)
124: */
125: public TimePeriodAnchor getXPosition() {
126: return this .xPosition;
127: }
128:
129: /**
130: * Sets the position of the x axis within each time period.
131: *
132: * @param position the position (<code>null</code> not permitted).
133: *
134: * @see #getXPosition()
135: */
136: public void setXPosition(TimePeriodAnchor position) {
137: if (position == null) {
138: throw new IllegalArgumentException(
139: "Null 'position' argument.");
140: }
141: this .xPosition = position;
142: }
143:
144: /**
145: * Returns the number of series in the collection.
146: *
147: * @return The series count.
148: */
149: public int getSeriesCount() {
150: return this .data.size();
151: }
152:
153: /**
154: * Returns a series.
155: *
156: * @param series the index of the series (zero-based).
157: *
158: * @return The series.
159: */
160: public TimePeriodValues getSeries(int series) {
161: if ((series < 0) || (series >= getSeriesCount())) {
162: throw new IllegalArgumentException(
163: "Index 'series' out of range.");
164: }
165: return (TimePeriodValues) this .data.get(series);
166: }
167:
168: /**
169: * Returns the key for a series.
170: *
171: * @param series the index of the series (zero-based).
172: *
173: * @return The key for a series.
174: */
175: public Comparable getSeriesKey(int series) {
176: // defer argument checking
177: return getSeries(series).getKey();
178: }
179:
180: /**
181: * Adds a series to the collection. A
182: * {@link org.jfree.data.general.DatasetChangeEvent} is sent to all
183: * registered listeners.
184: *
185: * @param series the time series.
186: */
187: public void addSeries(TimePeriodValues series) {
188:
189: if (series == null) {
190: throw new IllegalArgumentException(
191: "Null 'series' argument.");
192: }
193:
194: this .data.add(series);
195: series.addChangeListener(this );
196: fireDatasetChanged();
197:
198: }
199:
200: /**
201: * Removes the specified series from the collection.
202: *
203: * @param series the series to remove (<code>null</code> not permitted).
204: */
205: public void removeSeries(TimePeriodValues series) {
206:
207: if (series == null) {
208: throw new IllegalArgumentException(
209: "Null 'series' argument.");
210: }
211: this .data.remove(series);
212: series.removeChangeListener(this );
213: fireDatasetChanged();
214:
215: }
216:
217: /**
218: * Removes a series from the collection.
219: *
220: * @param index the series index (zero-based).
221: */
222: public void removeSeries(int index) {
223: TimePeriodValues series = getSeries(index);
224: if (series != null) {
225: removeSeries(series);
226: }
227: }
228:
229: /**
230: * Returns the number of items in the specified series.
231: * <P>
232: * This method is provided for convenience.
233: *
234: * @param series the index of the series of interest (zero-based).
235: *
236: * @return The number of items in the specified series.
237: */
238: public int getItemCount(int series) {
239: return getSeries(series).getItemCount();
240: }
241:
242: /**
243: * Returns the x-value for the specified series and item.
244: *
245: * @param series the series (zero-based index).
246: * @param item the item (zero-based index).
247: *
248: * @return The x-value for the specified series and item.
249: */
250: public Number getX(int series, int item) {
251: TimePeriodValues ts = (TimePeriodValues) this .data.get(series);
252: TimePeriodValue dp = ts.getDataItem(item);
253: TimePeriod period = dp.getPeriod();
254: return new Long(getX(period));
255: }
256:
257: /**
258: * Returns the x-value for a time period.
259: *
260: * @param period the time period.
261: *
262: * @return The x-value.
263: */
264: private long getX(TimePeriod period) {
265:
266: if (this .xPosition == TimePeriodAnchor.START) {
267: return period.getStart().getTime();
268: } else if (this .xPosition == TimePeriodAnchor.MIDDLE) {
269: return period.getStart().getTime() / 2
270: + period.getEnd().getTime() / 2;
271: } else if (this .xPosition == TimePeriodAnchor.END) {
272: return period.getEnd().getTime();
273: } else {
274: throw new IllegalStateException("TimePeriodAnchor unknown.");
275: }
276:
277: }
278:
279: /**
280: * Returns the starting X value for the specified series and item.
281: *
282: * @param series the series (zero-based index).
283: * @param item the item (zero-based index).
284: *
285: * @return The starting X value for the specified series and item.
286: */
287: public Number getStartX(int series, int item) {
288: TimePeriodValues ts = (TimePeriodValues) this .data.get(series);
289: TimePeriodValue dp = ts.getDataItem(item);
290: return new Long(dp.getPeriod().getStart().getTime());
291: }
292:
293: /**
294: * Returns the ending X value for the specified series and item.
295: *
296: * @param series the series (zero-based index).
297: * @param item the item (zero-based index).
298: *
299: * @return The ending X value for the specified series and item.
300: */
301: public Number getEndX(int series, int item) {
302: TimePeriodValues ts = (TimePeriodValues) this .data.get(series);
303: TimePeriodValue dp = ts.getDataItem(item);
304: return new Long(dp.getPeriod().getEnd().getTime());
305: }
306:
307: /**
308: * Returns the y-value for the specified series and item.
309: *
310: * @param series the series (zero-based index).
311: * @param item the item (zero-based index).
312: *
313: * @return The y-value for the specified series and item.
314: */
315: public Number getY(int series, int item) {
316: TimePeriodValues ts = (TimePeriodValues) this .data.get(series);
317: TimePeriodValue dp = ts.getDataItem(item);
318: return dp.getValue();
319: }
320:
321: /**
322: * Returns the starting Y value for the specified series and item.
323: *
324: * @param series the series (zero-based index).
325: * @param item the item (zero-based index).
326: *
327: * @return The starting Y value for the specified series and item.
328: */
329: public Number getStartY(int series, int item) {
330: return getY(series, item);
331: }
332:
333: /**
334: * Returns the ending Y value for the specified series and item.
335: *
336: * @param series the series (zero-based index).
337: * @param item the item (zero-based index).
338: *
339: * @return The ending Y value for the specified series and item.
340: */
341: public Number getEndY(int series, int item) {
342: return getY(series, item);
343: }
344:
345: /**
346: * Returns the minimum x-value in the dataset.
347: *
348: * @param includeInterval a flag that determines whether or not the
349: * x-interval is taken into account.
350: *
351: * @return The minimum value.
352: */
353: public double getDomainLowerBound(boolean includeInterval) {
354: double result = Double.NaN;
355: Range r = getDomainBounds(includeInterval);
356: if (r != null) {
357: result = r.getLowerBound();
358: }
359: return result;
360: }
361:
362: /**
363: * Returns the maximum 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 maximum value.
369: */
370: public double getDomainUpperBound(boolean includeInterval) {
371: double result = Double.NaN;
372: Range r = getDomainBounds(includeInterval);
373: if (r != null) {
374: result = r.getUpperBound();
375: }
376: return result;
377: }
378:
379: /**
380: * Returns the range of the values in this dataset's domain.
381: *
382: * @param includeInterval a flag that determines whether or not the
383: * x-interval is taken into account.
384: *
385: * @return The range.
386: */
387: public Range getDomainBounds(boolean includeInterval) {
388: boolean interval = includeInterval || this .domainIsPointsInTime;
389: Range result = null;
390: Range temp = null;
391: Iterator iterator = this .data.iterator();
392: while (iterator.hasNext()) {
393: TimePeriodValues series = (TimePeriodValues) iterator
394: .next();
395: int count = series.getItemCount();
396: if (count > 0) {
397: TimePeriod start = series.getTimePeriod(series
398: .getMinStartIndex());
399: TimePeriod end = series.getTimePeriod(series
400: .getMaxEndIndex());
401: if (!interval) {
402: if (this .xPosition == TimePeriodAnchor.START) {
403: TimePeriod maxStart = series
404: .getTimePeriod(series
405: .getMaxStartIndex());
406: temp = new Range(start.getStart().getTime(),
407: maxStart.getStart().getTime());
408: } else if (this .xPosition == TimePeriodAnchor.MIDDLE) {
409: TimePeriod minMiddle = series
410: .getTimePeriod(series
411: .getMinMiddleIndex());
412: long s1 = minMiddle.getStart().getTime();
413: long e1 = minMiddle.getEnd().getTime();
414: TimePeriod maxMiddle = series
415: .getTimePeriod(series
416: .getMaxMiddleIndex());
417: long s2 = maxMiddle.getStart().getTime();
418: long e2 = maxMiddle.getEnd().getTime();
419: temp = new Range(s1 + (e1 - s1) / 2, s2
420: + (e2 - s2) / 2);
421: } else if (this .xPosition == TimePeriodAnchor.END) {
422: TimePeriod minEnd = series.getTimePeriod(series
423: .getMinEndIndex());
424: temp = new Range(minEnd.getEnd().getTime(), end
425: .getEnd().getTime());
426: }
427: } else {
428: temp = new Range(start.getStart().getTime(), end
429: .getEnd().getTime());
430: }
431: result = Range.combine(result, temp);
432: }
433: }
434: return result;
435: }
436:
437: /**
438: * Tests this instance for equality with an arbitrary object.
439: *
440: * @param obj the object (<code>null</code> permitted).
441: *
442: * @return A boolean.
443: */
444: public boolean equals(Object obj) {
445: if (obj == this ) {
446: return true;
447: }
448: if (!(obj instanceof TimePeriodValuesCollection)) {
449: return false;
450: }
451: TimePeriodValuesCollection that = (TimePeriodValuesCollection) obj;
452: if (this .domainIsPointsInTime != that.domainIsPointsInTime) {
453: return false;
454: }
455: if (this .xPosition != that.xPosition) {
456: return false;
457: }
458: if (!ObjectUtilities.equal(this .data, that.data)) {
459: return false;
460: }
461: return true;
462: }
463:
464: // --- DEPRECATED METHODS -------------------------------------------------
465:
466: /**
467: * Returns a flag that controls whether the domain is treated as 'points
468: * in time'. This flag is used when determining the max and min values for
469: * the domain. If true, then only the x-values are considered for the max
470: * and min values. If false, then the start and end x-values will also be
471: * taken into consideration
472: *
473: * @return The flag.
474: *
475: * @deprecated This flag is no longer used by JFreeChart (as of version
476: * 1.0.3).
477: */
478: public boolean getDomainIsPointsInTime() {
479: return this .domainIsPointsInTime;
480: }
481:
482: /**
483: * Sets a flag that controls whether the domain is treated as 'points in
484: * time', or time periods.
485: *
486: * @param flag the new value of the flag.
487: *
488: * @deprecated This flag is no longer used by JFreeChart (as of version
489: * 1.0.3).
490: */
491: public void setDomainIsPointsInTime(boolean flag) {
492: this.domainIsPointsInTime = flag;
493: }
494:
495: }
|