001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, 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 * [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 * ComparableObjectSeries.java
029 * ---------------------------
030 * (C) Copyright 2006-2013, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes
036 * -------
037 * 19-Oct-2006 : New class (DG);
038 * 31-Oct-2007 : Implemented faster hashCode() (DG);
039 * 27-Nov-2007 : Changed clear() from protected to public (DG);
040 *
041 */
042
043package org.jfree.data;
044
045import java.io.Serializable;
046import java.util.Collections;
047import java.util.List;
048import org.jfree.chart.util.ParamChecks;
049
050import org.jfree.data.general.Series;
051import org.jfree.data.general.SeriesChangeEvent;
052import org.jfree.data.general.SeriesException;
053import org.jfree.util.ObjectUtilities;
054
055/**
056 * A (possibly ordered) list of (Comparable, Object) data items.
057 *
058 * @since 1.0.3
059 */
060public class ComparableObjectSeries extends Series
061        implements Cloneable, Serializable {
062
063    /** Storage for the data items in the series. */
064    protected List data;
065
066    /** The maximum number of items for the series. */
067    private int maximumItemCount = Integer.MAX_VALUE;
068
069    /** A flag that controls whether the items are automatically sorted. */
070    private boolean autoSort;
071
072    /** A flag that controls whether or not duplicate x-values are allowed. */
073    private boolean allowDuplicateXValues;
074
075    /**
076     * Creates a new empty series.  By default, items added to the series will
077     * be sorted into ascending order by x-value, and duplicate x-values will
078     * be allowed (these defaults can be modified with another constructor.
079     *
080     * @param key  the series key (<code>null</code> not permitted).
081     */
082    public ComparableObjectSeries(Comparable key) {
083        this(key, true, true);
084    }
085
086    /**
087     * Constructs a new series that contains no data.  You can specify
088     * whether or not duplicate x-values are allowed for the series.
089     *
090     * @param key  the series key (<code>null</code> not permitted).
091     * @param autoSort  a flag that controls whether or not the items in the
092     *                  series are sorted.
093     * @param allowDuplicateXValues  a flag that controls whether duplicate
094     *                               x-values are allowed.
095     */
096    public ComparableObjectSeries(Comparable key, boolean autoSort,
097            boolean allowDuplicateXValues) {
098        super(key);
099        this.data = new java.util.ArrayList();
100        this.autoSort = autoSort;
101        this.allowDuplicateXValues = allowDuplicateXValues;
102    }
103
104    /**
105     * Returns the flag that controls whether the items in the series are
106     * automatically sorted.  There is no setter for this flag, it must be
107     * defined in the series constructor.
108     *
109     * @return A boolean.
110     */
111    public boolean getAutoSort() {
112        return this.autoSort;
113    }
114
115    /**
116     * Returns a flag that controls whether duplicate x-values are allowed.
117     * This flag can only be set in the constructor.
118     *
119     * @return A boolean.
120     */
121    public boolean getAllowDuplicateXValues() {
122        return this.allowDuplicateXValues;
123    }
124
125    /**
126     * Returns the number of items in the series.
127     *
128     * @return The item count.
129     */
130    @Override
131    public int getItemCount() {
132        return this.data.size();
133    }
134
135    /**
136     * Returns the maximum number of items that will be retained in the series.
137     * The default value is <code>Integer.MAX_VALUE</code>.
138     *
139     * @return The maximum item count.
140     * @see #setMaximumItemCount(int)
141     */
142    public int getMaximumItemCount() {
143        return this.maximumItemCount;
144    }
145
146    /**
147     * Sets the maximum number of items that will be retained in the series.
148     * If you add a new item to the series such that the number of items will
149     * exceed the maximum item count, then the first element in the series is
150     * automatically removed, ensuring that the maximum item count is not
151     * exceeded.
152     * <p>
153     * Typically this value is set before the series is populated with data,
154     * but if it is applied later, it may cause some items to be removed from
155     * the series (in which case a {@link SeriesChangeEvent} will be sent to
156     * all registered listeners.
157     *
158     * @param maximum  the maximum number of items for the series.
159     */
160    public void setMaximumItemCount(int maximum) {
161        this.maximumItemCount = maximum;
162        boolean dataRemoved = false;
163        while (this.data.size() > maximum) {
164            this.data.remove(0);
165            dataRemoved = true;
166        }
167        if (dataRemoved) {
168            fireSeriesChanged();
169        }
170    }
171
172    /**
173     * Adds new data to the series and sends a {@link SeriesChangeEvent} to
174     * all registered listeners.
175     * <P>
176     * Throws an exception if the x-value is a duplicate AND the
177     * allowDuplicateXValues flag is false.
178     *
179     * @param x  the x-value (<code>null</code> not permitted).
180     * @param y  the y-value (<code>null</code> permitted).
181     */
182    protected void add(Comparable x, Object y) {
183        // argument checking delegated...
184        add(x, y, true);
185    }
186
187    /**
188     * Adds new data to the series and, if requested, sends a
189     * {@link SeriesChangeEvent} to all registered listeners.
190     * <P>
191     * Throws an exception if the x-value is a duplicate AND the
192     * allowDuplicateXValues flag is false.
193     *
194     * @param x  the x-value (<code>null</code> not permitted).
195     * @param y  the y-value (<code>null</code> permitted).
196     * @param notify  a flag the controls whether or not a
197     *                {@link SeriesChangeEvent} is sent to all registered
198     *                listeners.
199     */
200    protected void add(Comparable x, Object y, boolean notify) {
201        // delegate argument checking to XYDataItem...
202        ComparableObjectItem item = new ComparableObjectItem(x, y);
203        add(item, notify);
204    }
205
206    /**
207     * Adds a data item to the series and, if requested, sends a
208     * {@link SeriesChangeEvent} to all registered listeners.
209     *
210     * @param item  the (x, y) item (<code>null</code> not permitted).
211     * @param notify  a flag that controls whether or not a
212     *                {@link SeriesChangeEvent} is sent to all registered
213     *                listeners.
214     */
215    protected void add(ComparableObjectItem item, boolean notify) {
216
217        ParamChecks.nullNotPermitted(item, "item");
218        if (this.autoSort) {
219            int index = Collections.binarySearch(this.data, item);
220            if (index < 0) {
221                this.data.add(-index - 1, item);
222            }
223            else {
224                if (this.allowDuplicateXValues) {
225                    // need to make sure we are adding *after* any duplicates
226                    int size = this.data.size();
227                    while (index < size
228                           && item.compareTo(this.data.get(index)) == 0) {
229                        index++;
230                    }
231                    if (index < this.data.size()) {
232                        this.data.add(index, item);
233                    }
234                    else {
235                        this.data.add(item);
236                    }
237                }
238                else {
239                    throw new SeriesException("X-value already exists.");
240                }
241            }
242        }
243        else {
244            if (!this.allowDuplicateXValues) {
245                // can't allow duplicate values, so we need to check whether
246                // there is an item with the given x-value already
247                int index = indexOf(item.getComparable());
248                if (index >= 0) {
249                    throw new SeriesException("X-value already exists.");
250                }
251            }
252            this.data.add(item);
253        }
254        if (getItemCount() > this.maximumItemCount) {
255            this.data.remove(0);
256        }
257        if (notify) {
258            fireSeriesChanged();
259        }
260    }
261
262    /**
263     * Returns the index of the item with the specified x-value, or a negative
264     * index if the series does not contain an item with that x-value.  Be
265     * aware that for an unsorted series, the index is found by iterating
266     * through all items in the series.
267     *
268     * @param x  the x-value (<code>null</code> not permitted).
269     *
270     * @return The index.
271     */
272    public int indexOf(Comparable x) {
273        if (this.autoSort) {
274            return Collections.binarySearch(this.data, new ComparableObjectItem(
275                    x, null));
276        }
277        else {
278            for (int i = 0; i < this.data.size(); i++) {
279                ComparableObjectItem item = (ComparableObjectItem)
280                        this.data.get(i);
281                if (item.getComparable().equals(x)) {
282                    return i;
283                }
284            }
285            return -1;
286        }
287    }
288
289    /**
290     * Updates an item in the series.
291     *
292     * @param x  the x-value (<code>null</code> not permitted).
293     * @param y  the y-value (<code>null</code> permitted).
294     *
295     * @throws SeriesException if there is no existing item with the specified
296     *         x-value.
297     */
298    protected void update(Comparable x, Object y) {
299        int index = indexOf(x);
300        if (index < 0) {
301            throw new SeriesException("No observation for x = " + x);
302        }
303        else {
304            ComparableObjectItem item = getDataItem(index);
305            item.setObject(y);
306            fireSeriesChanged();
307        }
308    }
309
310    /**
311     * Updates the value of an item in the series and sends a
312     * {@link SeriesChangeEvent} to all registered listeners.
313     *
314     * @param index  the item (zero based index).
315     * @param y  the new value (<code>null</code> permitted).
316     */
317    protected void updateByIndex(int index, Object y) {
318        ComparableObjectItem item = getDataItem(index);
319        item.setObject(y);
320        fireSeriesChanged();
321    }
322
323    /**
324     * Return the data item with the specified index.
325     *
326     * @param index  the index.
327     *
328     * @return The data item with the specified index.
329     */
330    protected ComparableObjectItem getDataItem(int index) {
331        return (ComparableObjectItem) this.data.get(index);
332    }
333
334    /**
335     * Deletes a range of items from the series and sends a
336     * {@link SeriesChangeEvent} to all registered listeners.
337     *
338     * @param start  the start index (zero-based).
339     * @param end  the end index (zero-based).
340     */
341    protected void delete(int start, int end) {
342        for (int i = start; i <= end; i++) {
343            this.data.remove(start);
344        }
345        fireSeriesChanged();
346    }
347
348    /**
349     * Removes all data items from the series and, unless the series is
350     * already empty, sends a {@link SeriesChangeEvent} to all registered
351     * listeners.
352     */
353    public void clear() {
354        if (this.data.size() > 0) {
355            this.data.clear();
356            fireSeriesChanged();
357        }
358    }
359
360    /**
361     * Removes the item at the specified index and sends a
362     * {@link SeriesChangeEvent} to all registered listeners.
363     *
364     * @param index  the index.
365     *
366     * @return The item removed.
367     */
368    protected ComparableObjectItem remove(int index) {
369        ComparableObjectItem result = (ComparableObjectItem) this.data.remove(
370                index);
371        fireSeriesChanged();
372        return result;
373    }
374
375    /**
376     * Removes the item with the specified x-value and sends a
377     * {@link SeriesChangeEvent} to all registered listeners.
378     *
379     * @param x  the x-value.
380
381     * @return The item removed.
382     */
383    public ComparableObjectItem remove(Comparable x) {
384        return remove(indexOf(x));
385    }
386
387    /**
388     * Tests this series for equality with an arbitrary object.
389     *
390     * @param obj  the object to test against for equality
391     *             (<code>null</code> permitted).
392     *
393     * @return A boolean.
394     */
395    @Override
396    public boolean equals(Object obj) {
397        if (obj == this) {
398            return true;
399        }
400        if (!(obj instanceof ComparableObjectSeries)) {
401            return false;
402        }
403        if (!super.equals(obj)) {
404            return false;
405        }
406        ComparableObjectSeries that = (ComparableObjectSeries) obj;
407        if (this.maximumItemCount != that.maximumItemCount) {
408            return false;
409        }
410        if (this.autoSort != that.autoSort) {
411            return false;
412        }
413        if (this.allowDuplicateXValues != that.allowDuplicateXValues) {
414            return false;
415        }
416        if (!ObjectUtilities.equal(this.data, that.data)) {
417            return false;
418        }
419        return true;
420    }
421
422    /**
423     * Returns a hash code.
424     *
425     * @return A hash code.
426     */
427    @Override
428    public int hashCode() {
429        int result = super.hashCode();
430        // it is too slow to look at every data item, so let's just look at
431        // the first, middle and last items...
432        int count = getItemCount();
433        if (count > 0) {
434            ComparableObjectItem item = getDataItem(0);
435            result = 29 * result + item.hashCode();
436        }
437        if (count > 1) {
438            ComparableObjectItem item = getDataItem(count - 1);
439            result = 29 * result + item.hashCode();
440        }
441        if (count > 2) {
442            ComparableObjectItem item = getDataItem(count / 2);
443            result = 29 * result + item.hashCode();
444        }
445        result = 29 * result + this.maximumItemCount;
446        result = 29 * result + (this.autoSort ? 1 : 0);
447        result = 29 * result + (this.allowDuplicateXValues ? 1 : 0);
448        return result;
449    }
450
451}