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: * TimeTableXYDataset.java
029: * -----------------------
030: * (C) Copyright 2004, 2005, 2007, by Andreas Schroeder and Contributors.
031: *
032: * Original Author: Andreas Schroeder;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: TimeTableXYDataset.java,v 1.10.2.3 2007/03/09 15:43:09 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 01-Apr-2004 : Version 1 (AS);
040: * 05-May-2004 : Now implements AbstractIntervalXYDataset (DG);
041: * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
042: * getYValue() (DG);
043: * 15-Sep-2004 : Added getXPosition(), setXPosition(), equals() and
044: * clone() (DG);
045: * 17-Nov-2004 : Updated methods for changes in DomainInfo interface (DG);
046: * 25-Nov-2004 : Added getTimePeriod(int) method (DG);
047: * 11-Jan-2005 : Removed deprecated code in preparation for the 1.0.0
048: * release (DG);
049: * 27-Jan-2005 : Modified to use TimePeriod rather than RegularTimePeriod (DG);
050: * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
051: *
052: */
053:
054: package org.jfree.data.time;
055:
056: import java.util.Calendar;
057: import java.util.List;
058: import java.util.Locale;
059: import java.util.TimeZone;
060:
061: import org.jfree.data.DefaultKeyedValues2D;
062: import org.jfree.data.DomainInfo;
063: import org.jfree.data.Range;
064: import org.jfree.data.general.DatasetChangeEvent;
065: import org.jfree.data.xy.AbstractIntervalXYDataset;
066: import org.jfree.data.xy.IntervalXYDataset;
067: import org.jfree.data.xy.TableXYDataset;
068: import org.jfree.util.PublicCloneable;
069:
070: /**
071: * A dataset for regular time periods that implements the
072: * {@link TableXYDataset} interface.
073: *
074: * @see org.jfree.data.xy.TableXYDataset
075: */
076: public class TimeTableXYDataset extends AbstractIntervalXYDataset
077: implements Cloneable, PublicCloneable, IntervalXYDataset,
078: DomainInfo, TableXYDataset {
079:
080: /**
081: * The data structure to store the values. Each column represents
082: * a series (elsewhere in JFreeChart rows are typically used for series,
083: * but it doesn't matter that much since this data structure is private
084: * and symmetrical anyway), each row contains values for the same
085: * {@link RegularTimePeriod} (the rows are sorted into ascending order).
086: */
087: private DefaultKeyedValues2D values;
088:
089: /**
090: * A flag that indicates that the domain is 'points in time'. If this flag
091: * is true, only the x-value (and not the x-interval) is used to determine
092: * the range of values in the domain.
093: */
094: private boolean domainIsPointsInTime;
095:
096: /**
097: * The point within each time period that is used for the X value when this
098: * collection is used as an {@link org.jfree.data.xy.XYDataset}. This can
099: * be the start, middle or end of the time period.
100: */
101: private TimePeriodAnchor xPosition;
102:
103: /** A working calendar (to recycle) */
104: private Calendar workingCalendar;
105:
106: /**
107: * Creates a new dataset.
108: */
109: public TimeTableXYDataset() {
110: // defer argument checking
111: this (TimeZone.getDefault(), Locale.getDefault());
112: }
113:
114: /**
115: * Creates a new dataset with the given time zone.
116: *
117: * @param zone the time zone to use (<code>null</code> not permitted).
118: */
119: public TimeTableXYDataset(TimeZone zone) {
120: // defer argument checking
121: this (zone, Locale.getDefault());
122: }
123:
124: /**
125: * Creates a new dataset with the given time zone and locale.
126: *
127: * @param zone the time zone to use (<code>null</code> not permitted).
128: * @param locale the locale to use (<code>null</code> not permitted).
129: */
130: public TimeTableXYDataset(TimeZone zone, Locale locale) {
131: if (zone == null) {
132: throw new IllegalArgumentException("Null 'zone' argument.");
133: }
134: if (locale == null) {
135: throw new IllegalArgumentException(
136: "Null 'locale' argument.");
137: }
138: this .values = new DefaultKeyedValues2D(true);
139: this .workingCalendar = Calendar.getInstance(zone, locale);
140: this .xPosition = TimePeriodAnchor.START;
141: }
142:
143: /**
144: * Returns a flag that controls whether the domain is treated as 'points in
145: * time'.
146: * <P>
147: * This flag is used when determining the max and min values for the domain.
148: * If true, then only the x-values are considered for the max and min
149: * values. If false, then the start and end x-values will also be taken
150: * into consideration.
151: *
152: * @return The flag.
153: */
154: public boolean getDomainIsPointsInTime() {
155: return this .domainIsPointsInTime;
156: }
157:
158: /**
159: * Sets a flag that controls whether the domain is treated as 'points in
160: * time', or time periods. A {@link DatasetChangeEvent} is sent to all
161: * registered listeners.
162: *
163: * @param flag the new value of the flag.
164: */
165: public void setDomainIsPointsInTime(boolean flag) {
166: this .domainIsPointsInTime = flag;
167: notifyListeners(new DatasetChangeEvent(this , this ));
168: }
169:
170: /**
171: * Returns the position within each time period that is used for the X
172: * value.
173: *
174: * @return The anchor position (never <code>null</code>).
175: */
176: public TimePeriodAnchor getXPosition() {
177: return this .xPosition;
178: }
179:
180: /**
181: * Sets the position within each time period that is used for the X values,
182: * then sends a {@link DatasetChangeEvent} to all registered listeners.
183: *
184: * @param anchor the anchor position (<code>null</code> not permitted).
185: */
186: public void setXPosition(TimePeriodAnchor anchor) {
187: if (anchor == null) {
188: throw new IllegalArgumentException(
189: "Null 'anchor' argument.");
190: }
191: this .xPosition = anchor;
192: notifyListeners(new DatasetChangeEvent(this , this ));
193: }
194:
195: /**
196: * Adds a new data item to the dataset and sends a
197: * {@link org.jfree.data.general.DatasetChangeEvent} to all registered
198: * listeners.
199: *
200: * @param period the time period.
201: * @param y the value for this period.
202: * @param seriesName the name of the series to add the value.
203: */
204: public void add(TimePeriod period, double y, String seriesName) {
205: add(period, new Double(y), seriesName, true);
206: }
207:
208: /**
209: * Adds a new data item to the dataset.
210: *
211: * @param period the time period (<code>null</code> not permitted).
212: * @param y the value for this period (<code>null</code> permitted).
213: * @param seriesName the name of the series to add the value
214: * (<code>null</code> not permitted).
215: * @param notify whether dataset listener are notified or not.
216: */
217: public void add(TimePeriod period, Number y, String seriesName,
218: boolean notify) {
219: this .values.addValue(y, period, seriesName);
220: if (notify) {
221: fireDatasetChanged();
222: }
223: }
224:
225: /**
226: * Removes an existing data item from the dataset.
227: *
228: * @param period the (existing!) time period of the value to remove
229: * (<code>null</code> not permitted).
230: * @param seriesName the (existing!) series name to remove the value
231: * (<code>null</code> not permitted).
232: */
233: public void remove(TimePeriod period, String seriesName) {
234: remove(period, seriesName, true);
235: }
236:
237: /**
238: * Removes an existing data item from the dataset.
239: *
240: * @param period the (existing!) time period of the value to remove
241: * (<code>null</code> not permitted).
242: * @param seriesName the (existing!) series name to remove the value
243: * (<code>null</code> not permitted).
244: * @param notify whether dataset listener are notified or not.
245: */
246: public void remove(TimePeriod period, String seriesName,
247: boolean notify) {
248: this .values.removeValue(period, seriesName);
249: if (notify) {
250: fireDatasetChanged();
251: }
252: }
253:
254: /**
255: * Returns the time period for the specified item. Bear in mind that all
256: * series share the same set of time periods.
257: *
258: * @param item the item index (0 <= i <= {@link #getItemCount()}).
259: *
260: * @return The time period.
261: */
262: public TimePeriod getTimePeriod(int item) {
263: return (TimePeriod) this .values.getRowKey(item);
264: }
265:
266: /**
267: * Returns the number of items in ALL series.
268: *
269: * @return The item count.
270: */
271: public int getItemCount() {
272: return this .values.getRowCount();
273: }
274:
275: /**
276: * Returns the number of items in a series. This is the same value
277: * that is returned by {@link #getItemCount()} since all series
278: * share the same x-values (time periods).
279: *
280: * @param series the series (zero-based index, ignored).
281: *
282: * @return The number of items within the series.
283: */
284: public int getItemCount(int series) {
285: return getItemCount();
286: }
287:
288: /**
289: * Returns the number of series in the dataset.
290: *
291: * @return The series count.
292: */
293: public int getSeriesCount() {
294: return this .values.getColumnCount();
295: }
296:
297: /**
298: * Returns the key for a series.
299: *
300: * @param series the series (zero-based index).
301: *
302: * @return The key for the series.
303: */
304: public Comparable getSeriesKey(int series) {
305: return this .values.getColumnKey(series);
306: }
307:
308: /**
309: * Returns the x-value for an item within a series. The x-values may or
310: * may not be returned in ascending order, that is up to the class
311: * implementing the interface.
312: *
313: * @param series the series (zero-based index).
314: * @param item the item (zero-based index).
315: *
316: * @return The x-value.
317: */
318: public Number getX(int series, int item) {
319: return new Double(getXValue(series, item));
320: }
321:
322: /**
323: * Returns the x-value (as a double primitive) for an item within a series.
324: *
325: * @param series the series index (zero-based).
326: * @param item the item index (zero-based).
327: *
328: * @return The value.
329: */
330: public double getXValue(int series, int item) {
331: TimePeriod period = (TimePeriod) this .values.getRowKey(item);
332: return getXValue(period);
333: }
334:
335: /**
336: * Returns the starting X value for the specified series and item.
337: *
338: * @param series the series (zero-based index).
339: * @param item the item within a series (zero-based index).
340: *
341: * @return The starting X value for the specified series and item.
342: */
343: public Number getStartX(int series, int item) {
344: return new Double(getStartXValue(series, item));
345: }
346:
347: /**
348: * Returns the start x-value (as a double primitive) for an item within
349: * a series.
350: *
351: * @param series the series index (zero-based).
352: * @param item the item index (zero-based).
353: *
354: * @return The value.
355: */
356: public double getStartXValue(int series, int item) {
357: TimePeriod period = (TimePeriod) this .values.getRowKey(item);
358: return period.getStart().getTime();
359: }
360:
361: /**
362: * Returns the ending X value for the specified series and item.
363: *
364: * @param series the series (zero-based index).
365: * @param item the item within a series (zero-based index).
366: *
367: * @return The ending X value for the specified series and item.
368: */
369: public Number getEndX(int series, int item) {
370: return new Double(getEndXValue(series, item));
371: }
372:
373: /**
374: * Returns the end x-value (as a double primitive) for an item within
375: * a series.
376: *
377: * @param series the series index (zero-based).
378: * @param item the item index (zero-based).
379: *
380: * @return The value.
381: */
382: public double getEndXValue(int series, int item) {
383: TimePeriod period = (TimePeriod) this .values.getRowKey(item);
384: return period.getEnd().getTime();
385: }
386:
387: /**
388: * Returns the y-value for an item within a series.
389: *
390: * @param series the series (zero-based index).
391: * @param item the item (zero-based index).
392: *
393: * @return The y-value (possibly <code>null</code>).
394: */
395: public Number getY(int series, int item) {
396: return this .values.getValue(item, series);
397: }
398:
399: /**
400: * Returns the starting Y value for the specified series and item.
401: *
402: * @param series the series (zero-based index).
403: * @param item the item within a series (zero-based index).
404: *
405: * @return The starting Y value for the specified series and item.
406: */
407: public Number getStartY(int series, int item) {
408: return getY(series, item);
409: }
410:
411: /**
412: * Returns the ending Y value for the specified series and item.
413: *
414: * @param series the series (zero-based index).
415: * @param item the item within a series (zero-based index).
416: *
417: * @return The ending Y value for the specified series and item.
418: */
419: public Number getEndY(int series, int item) {
420: return getY(series, item);
421: }
422:
423: /**
424: * Returns the x-value for a time period.
425: *
426: * @param period the time period.
427: *
428: * @return The x-value.
429: */
430: private long getXValue(TimePeriod period) {
431: long result = 0L;
432: if (this .xPosition == TimePeriodAnchor.START) {
433: result = period.getStart().getTime();
434: } else if (this .xPosition == TimePeriodAnchor.MIDDLE) {
435: long t0 = period.getStart().getTime();
436: long t1 = period.getEnd().getTime();
437: result = t0 + (t1 - t0) / 2L;
438: } else if (this .xPosition == TimePeriodAnchor.END) {
439: result = period.getEnd().getTime();
440: }
441: return result;
442: }
443:
444: /**
445: * Returns the minimum x-value in the dataset.
446: *
447: * @param includeInterval a flag that determines whether or not the
448: * x-interval is taken into account.
449: *
450: * @return The minimum value.
451: */
452: public double getDomainLowerBound(boolean includeInterval) {
453: double result = Double.NaN;
454: Range r = getDomainBounds(includeInterval);
455: if (r != null) {
456: result = r.getLowerBound();
457: }
458: return result;
459: }
460:
461: /**
462: * Returns the maximum x-value in the dataset.
463: *
464: * @param includeInterval a flag that determines whether or not the
465: * x-interval is taken into account.
466: *
467: * @return The maximum value.
468: */
469: public double getDomainUpperBound(boolean includeInterval) {
470: double result = Double.NaN;
471: Range r = getDomainBounds(includeInterval);
472: if (r != null) {
473: result = r.getUpperBound();
474: }
475: return result;
476: }
477:
478: /**
479: * Returns the range of the values in this dataset's domain.
480: *
481: * @param includeInterval a flag that controls whether or not the
482: * x-intervals are taken into account.
483: *
484: * @return The range.
485: */
486: public Range getDomainBounds(boolean includeInterval) {
487: List keys = this .values.getRowKeys();
488: if (keys.isEmpty()) {
489: return null;
490: }
491:
492: TimePeriod first = (TimePeriod) keys.get(0);
493: TimePeriod last = (TimePeriod) keys.get(keys.size() - 1);
494:
495: if (!includeInterval || this .domainIsPointsInTime) {
496: return new Range(getXValue(first), getXValue(last));
497: } else {
498: return new Range(first.getStart().getTime(), last.getEnd()
499: .getTime());
500: }
501: }
502:
503: /**
504: * Tests this dataset for equality with an arbitrary object.
505: *
506: * @param obj the object (<code>null</code> permitted).
507: *
508: * @return A boolean.
509: */
510: public boolean equals(Object obj) {
511: if (obj == this ) {
512: return true;
513: }
514: if (!(obj instanceof TimeTableXYDataset)) {
515: return false;
516: }
517: TimeTableXYDataset that = (TimeTableXYDataset) obj;
518: if (this .domainIsPointsInTime != that.domainIsPointsInTime) {
519: return false;
520: }
521: if (this .xPosition != that.xPosition) {
522: return false;
523: }
524: if (!this .workingCalendar.getTimeZone().equals(
525: that.workingCalendar.getTimeZone())) {
526: return false;
527: }
528: if (!this .values.equals(that.values)) {
529: return false;
530: }
531: return true;
532: }
533:
534: /**
535: * Returns a clone of this dataset.
536: *
537: * @return A clone.
538: *
539: * @throws CloneNotSupportedException if the dataset cannot be cloned.
540: */
541: public Object clone() throws CloneNotSupportedException {
542: TimeTableXYDataset clone = (TimeTableXYDataset) super .clone();
543: clone.values = (DefaultKeyedValues2D) this .values.clone();
544: clone.workingCalendar = (Calendar) this.workingCalendar.clone();
545: return clone;
546: }
547:
548: }
|