001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2020, 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 * Series.java
029 * -----------
030 * (C) Copyright 2001-2020, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 * 
035 */
036
037package org.jfree.data.general;
038
039import java.beans.PropertyChangeListener;
040import java.beans.PropertyChangeSupport;
041import java.beans.PropertyVetoException;
042import java.beans.VetoableChangeListener;
043import java.beans.VetoableChangeSupport;
044import java.io.Serializable;
045
046import javax.swing.event.EventListenerList;
047import org.jfree.chart.util.ObjectUtils;
048
049import org.jfree.chart.util.Args;
050
051/**
052 * Base class representing a data series.  Subclasses are left to implement the
053 * actual data structures.
054 * <P>
055 * The series has two properties ("Key" and "Description") for which you can
056 * register a {@code PropertyChangeListener}.
057 * <P>
058 * You can also register a {@link SeriesChangeListener} to receive notification
059 * of changes to the series data.
060 */
061public abstract class Series implements Cloneable, Serializable {
062
063    /** For serialization. */
064    private static final long serialVersionUID = -6906561437538683581L;
065
066    /** The key for the series. */
067    private Comparable key;
068
069    /** A description of the series. */
070    private String description;
071
072    /** Storage for registered change listeners. */
073    private EventListenerList listeners;
074
075    /** Object to support property change notification. */
076    private PropertyChangeSupport propertyChangeSupport;
077
078    /** Object to support property change notification. */
079    private VetoableChangeSupport vetoableChangeSupport;
080
081    /** A flag that controls whether or not changes are notified. */
082    private boolean notify;
083
084    /**
085     * Creates a new series with the specified key.
086     *
087     * @param key  the series key ({@code null} not permitted).
088     */
089    protected Series(Comparable key) {
090        this(key, null);
091    }
092
093    /**
094     * Creates a new series with the specified key and description.
095     *
096     * @param key  the series key ({@code null} NOT permitted).
097     * @param description  the series description ({@code null} permitted).
098     */
099    protected Series(Comparable key, String description) {
100        Args.nullNotPermitted(key, "key");
101        this.key = key;
102        this.description = description;
103        this.listeners = new EventListenerList();
104        this.propertyChangeSupport = new PropertyChangeSupport(this);
105        this.vetoableChangeSupport = new VetoableChangeSupport(this);
106        this.notify = true;
107    }
108
109    /**
110     * Returns the key for the series.
111     *
112     * @return The series key (never {@code null}).
113     *
114     * @see #setKey(Comparable)
115     */
116    public Comparable getKey() {
117        return this.key;
118    }
119
120    /**
121     * Sets the key for the series and sends a {@code VetoableChangeEvent}
122     * (with the property name "Key") to all registered listeners.  For 
123     * backwards compatibility, this method also fires a regular 
124     * {@code PropertyChangeEvent}.  If the key change is vetoed this 
125     * method will throw an IllegalArgumentException.
126     *
127     * @param key  the key ({@code null} not permitted).
128     *
129     * @see #getKey()
130     */
131    public void setKey(Comparable key) {
132        Args.nullNotPermitted(key, "key");
133        Comparable old = this.key;
134        try {
135            // if this series belongs to a dataset, the dataset might veto the
136            // change if it results in two series within the dataset having the
137            // same key
138            this.vetoableChangeSupport.fireVetoableChange("Key", old, key);
139            this.key = key;
140            // prior to 1.0.14, we just fired a PropertyChange - so we need to
141            // keep doing this
142            this.propertyChangeSupport.firePropertyChange("Key", old, key);
143        } catch (PropertyVetoException e) {
144            throw new IllegalArgumentException(e.getMessage());
145        }
146    }
147
148    /**
149     * Returns a description of the series.
150     *
151     * @return The series description (possibly {@code null}).
152     *
153     * @see #setDescription(String)
154     */
155    public String getDescription() {
156        return this.description;
157    }
158
159    /**
160     * Sets the description of the series and sends a
161     * {@code PropertyChangeEvent} to all registered listeners.
162     *
163     * @param description  the description ({@code null} permitted).
164     *
165     * @see #getDescription()
166     */
167    public void setDescription(String description) {
168        String old = this.description;
169        this.description = description;
170        this.propertyChangeSupport.firePropertyChange("Description", old,
171                description);
172    }
173
174    /**
175     * Returns the flag that controls whether or not change events are sent to
176     * registered listeners.
177     *
178     * @return A boolean.
179     *
180     * @see #setNotify(boolean)
181     */
182    public boolean getNotify() {
183        return this.notify;
184    }
185
186    /**
187     * Sets the flag that controls whether or not change events are sent to
188     * registered listeners.
189     *
190     * @param notify  the new value of the flag.
191     *
192     * @see #getNotify()
193     */
194    public void setNotify(boolean notify) {
195        if (this.notify != notify) {
196            this.notify = notify;
197            fireSeriesChanged();
198        }
199    }
200
201    /**
202     * Returns {@code true} if the series contains no data items, and
203     * {@code false} otherwise.
204     *
205     * @return A boolean.
206     *
207     * @since 1.0.7
208     */
209    public boolean isEmpty() {
210        return (getItemCount() == 0);
211    }
212
213    /**
214     * Returns the number of data items in the series.
215     *
216     * @return The number of data items in the series.
217     */
218    public abstract int getItemCount();
219
220    /**
221     * Returns a clone of the series.
222     * <P>
223     * Notes:
224     * <ul>
225     * <li>No need to clone the name or description, since String object is
226     * immutable.</li>
227     * <li>We set the listener list to empty, since the listeners did not
228     * register with the clone.</li>
229     * <li>Same applies to the PropertyChangeSupport instance.</li>
230     * </ul>
231     *
232     * @return A clone of the series.
233     *
234     * @throws CloneNotSupportedException  not thrown by this class, but
235     *         subclasses may differ.
236     */
237    @Override
238    public Object clone() throws CloneNotSupportedException {
239        Series clone = (Series) super.clone();
240        clone.listeners = new EventListenerList();
241        clone.propertyChangeSupport = new PropertyChangeSupport(clone);
242        clone.vetoableChangeSupport = new VetoableChangeSupport(clone);
243        return clone;
244    }
245
246    /**
247     * Tests the series for equality with another object.
248     *
249     * @param obj  the object ({@code null} permitted).
250     *
251     * @return {@code true} or {@code false}.
252     */
253    @Override
254    public boolean equals(Object obj) {
255        if (obj == this) {
256            return true;
257        }
258        if (!(obj instanceof Series)) {
259            return false;
260        }
261        Series that = (Series) obj;
262        if (!getKey().equals(that.getKey())) {
263            return false;
264        }
265        if (!ObjectUtils.equal(getDescription(), that.getDescription())) {
266            return false;
267        }
268        return true;
269    }
270
271    /**
272     * Returns a hash code.
273     *
274     * @return A hash code.
275     */
276    @Override
277    public int hashCode() {
278        int result;
279        result = this.key.hashCode();
280        result = 29 * result + (this.description != null
281                ? this.description.hashCode() : 0);
282        return result;
283    }
284
285    /**
286     * Registers an object with this series, to receive notification whenever
287     * the series changes.
288     * <P>
289     * Objects being registered must implement the {@link SeriesChangeListener}
290     * interface.
291     *
292     * @param listener  the listener to register.
293     */
294    public void addChangeListener(SeriesChangeListener listener) {
295        this.listeners.add(SeriesChangeListener.class, listener);
296    }
297
298    /**
299     * Deregisters an object, so that it not longer receives notification
300     * whenever the series changes.
301     *
302     * @param listener  the listener to deregister.
303     */
304    public void removeChangeListener(SeriesChangeListener listener) {
305        this.listeners.remove(SeriesChangeListener.class, listener);
306    }
307
308    /**
309     * General method for signalling to registered listeners that the series
310     * has been changed.
311     */
312    public void fireSeriesChanged() {
313        if (this.notify) {
314            notifyListeners(new SeriesChangeEvent(this));
315        }
316    }
317
318    /**
319     * Sends a change event to all registered listeners.
320     *
321     * @param event  contains information about the event that triggered the
322     *               notification.
323     */
324    protected void notifyListeners(SeriesChangeEvent event) {
325
326        Object[] listenerList = this.listeners.getListenerList();
327        for (int i = listenerList.length - 2; i >= 0; i -= 2) {
328            if (listenerList[i] == SeriesChangeListener.class) {
329                ((SeriesChangeListener) listenerList[i + 1]).seriesChanged(
330                        event);
331            }
332        }
333
334    }
335
336    /**
337     * Adds a property change listener to the series.
338     *
339     * @param listener  the listener.
340     */
341    public void addPropertyChangeListener(PropertyChangeListener listener) {
342        this.propertyChangeSupport.addPropertyChangeListener(listener);
343    }
344
345    /**
346     * Removes a property change listener from the series.
347     *
348     * @param listener  the listener.
349     */
350    public void removePropertyChangeListener(PropertyChangeListener listener) {
351        this.propertyChangeSupport.removePropertyChangeListener(listener);
352    }
353
354    /**
355     * Fires a property change event.
356     *
357     * @param property  the property key.
358     * @param oldValue  the old value.
359     * @param newValue  the new value.
360     */
361    protected void firePropertyChange(String property, Object oldValue,
362            Object newValue) {
363        this.propertyChangeSupport.firePropertyChange(property, oldValue,
364                newValue);
365    }
366    
367    /**
368     * Adds a vetoable property change listener to the series.
369     *
370     * @param listener  the listener.
371     * 
372     * @since 1.0.14
373     */
374    public void addVetoableChangeListener(VetoableChangeListener listener) {
375        this.vetoableChangeSupport.addVetoableChangeListener(listener);
376    }
377
378    /**
379     * Removes a vetoable property change listener from the series.
380     *
381     * @param listener  the listener.
382     * 
383     * @since 1.0.14 
384     */
385    public void removeVetoableChangeListener(VetoableChangeListener listener) {
386        this.vetoableChangeSupport.removeVetoableChangeListener(listener);
387    }    
388
389    /**
390     * Fires a vetoable property change event.
391     *
392     * @param property  the property key.
393     * @param oldValue  the old value.
394     * @param newValue  the new value.
395     * 
396     * @throws PropertyVetoException if the change was vetoed.
397     */
398    protected void fireVetoableChange(String property, Object oldValue,
399            Object newValue) throws PropertyVetoException {
400        this.vetoableChangeSupport.fireVetoableChange(property, oldValue,
401                newValue);
402    }
403
404}