001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2022, 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 * Series.java
029 * -----------
030 * (C) Copyright 2001-2022, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 * 
035 */
036
037package org.jfree.data.general;
038
039import org.jfree.chart.internal.Args;
040
041import javax.swing.event.EventListenerList;
042import java.io.Serializable;
043
044/**
045 * Base class representing a data series.  Subclasses are left to implement the
046 * actual data structures.
047 * <P>
048 * The series has two properties ("Key" and "Description") for which you can
049 * register a {@code PropertyChangeListener}.
050 * <P>
051 * You can also register a {@link SeriesChangeListener} to receive notification
052 * of changes to the series data.
053 */
054public abstract class Series<K extends Comparable<K>> 
055        implements Cloneable, Serializable {
056
057    /** For serialization. */
058    private static final long serialVersionUID = -6906561437538683581L;
059
060    /** The key for the series. */
061    private final K key;
062
063    /** Storage for registered change listeners. */
064    private EventListenerList listeners;
065
066    /** A flag that controls whether changes are notified. */
067    private boolean notify;
068
069    /**
070     * Creates a new series with the specified key and description.
071     *
072     * @param key  the series key ({@code null} not permitted).
073     */
074    protected Series(K key) {
075        Args.nullNotPermitted(key, "key");
076        this.key = key;
077        this.listeners = new EventListenerList();
078        this.notify = true;
079    }
080
081    /**
082     * Returns the key for the series.
083     *
084     * @return The series key (never {@code null}).
085     */
086    public K getKey() {
087        return this.key;
088    }
089
090    /**
091     * Returns the flag that controls whether change events are sent to
092     * registered listeners.
093     *
094     * @return A boolean.
095     *
096     * @see #setNotify(boolean)
097     */
098    public boolean getNotify() {
099        return this.notify;
100    }
101
102    /**
103     * Sets the flag that controls whether change events are sent to
104     * registered listeners.
105     *
106     * @param notify  the new value of the flag.
107     *
108     * @see #getNotify()
109     */
110    public void setNotify(boolean notify) {
111        if (this.notify != notify) {
112            this.notify = notify;
113            fireSeriesChanged();
114        }
115    }
116
117    /**
118     * Returns {@code true} if the series contains no data items, and
119     * {@code false} otherwise.
120     *
121     * @return A boolean.
122     *
123     * @since 1.0.7
124     */
125    public boolean isEmpty() {
126        return (getItemCount() == 0);
127    }
128
129    /**
130     * Returns the number of data items in the series.
131     *
132     * @return The number of data items in the series.
133     */
134    public abstract int getItemCount();
135
136    /**
137     * Returns a clone of the series.
138     * <P>
139     * Notes:
140     * <ul>
141     * <li>No need to clone the name or description, since String object is
142     * immutable.</li>
143     * <li>We set the listener list to empty, since the listeners did not
144     * register with the clone.</li>
145     * <li>Same applies to the PropertyChangeSupport instance.</li>
146     * </ul>
147     *
148     * @return A clone of the series.
149     *
150     * @throws CloneNotSupportedException  not thrown by this class, but
151     *         subclasses may differ.
152     */
153    @Override
154    public Object clone() throws CloneNotSupportedException {
155        @SuppressWarnings("unchecked")
156        Series<K> clone = (Series) super.clone();
157        clone.listeners = new EventListenerList();
158        return clone;
159    }
160
161    /**
162     * Tests the series for equality with another object.
163     *
164     * @param obj  the object ({@code null} permitted).
165     *
166     * @return {@code true} or {@code false}.
167     */
168    @Override
169    public boolean equals(Object obj) {
170        if (obj == this) {
171            return true;
172        }
173        if (!(obj instanceof Series)) {
174            return false;
175        }
176        @SuppressWarnings("unchecked")
177        Series<K> that = (Series) obj;
178        if (!getKey().equals(that.getKey())) {
179            return false;
180        }
181        return true;
182    }
183
184    /**
185     * Returns a hash code.
186     *
187     * @return A hash code.
188     */
189    @Override
190    public int hashCode() {
191        return this.key.hashCode();
192    }
193
194    /**
195     * Registers an object with this series, to receive notification whenever
196     * the series changes.
197     * <P>
198     * Objects being registered must implement the {@link SeriesChangeListener}
199     * interface.
200     *
201     * @param listener  the listener to register.
202     */
203    public void addChangeListener(SeriesChangeListener listener) {
204        this.listeners.add(SeriesChangeListener.class, listener);
205    }
206
207    /**
208     * Deregisters an object, so that it no longer receives notification
209     * whenever the series changes.
210     *
211     * @param listener  the listener to deregister.
212     */
213    public void removeChangeListener(SeriesChangeListener listener) {
214        this.listeners.remove(SeriesChangeListener.class, listener);
215    }
216
217    /**
218     * General method for signalling to registered listeners that the series
219     * has been changed.
220     */
221    public void fireSeriesChanged() {
222        if (this.notify) {
223            notifyListeners(new SeriesChangeEvent(this));
224        }
225    }
226
227    /**
228     * Sends a change event to all registered listeners.
229     *
230     * @param event  contains information about the event that triggered the
231     *               notification.
232     */
233    protected void notifyListeners(SeriesChangeEvent event) {
234        Object[] listenerList = this.listeners.getListenerList();
235        for (int i = listenerList.length - 2; i >= 0; i -= 2) {
236            if (listenerList[i] == SeriesChangeListener.class) {
237                ((SeriesChangeListener) listenerList[i + 1]).seriesChanged(
238                        event);
239            }
240        }
241    }
242
243}