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