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