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 * TaskSeries.java
029 * ---------------
030 * (C) Copyright 2002-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   Tracy Hiltbrand (equals/hashCode comply with EqualsVerifier);
034 *
035 */
036
037package org.jfree.data.gantt;
038
039import java.util.Collections;
040import java.util.List;
041import java.util.Objects;
042import org.jfree.chart.util.ObjectUtils;
043import org.jfree.chart.util.Args;
044
045import org.jfree.data.general.Series;
046
047/**
048 * A series that contains zero, one or many {@link Task} objects.
049 * <P>
050 * This class is used as a building block for the {@link TaskSeriesCollection}
051 * class that can be used to construct basic Gantt charts.
052 */
053public class TaskSeries extends Series {
054
055    /** Storage for the tasks in the series. */
056    private List tasks;
057
058    /**
059     * Constructs a new series with the specified name.
060     *
061     * @param name  the series name ({@code null} not permitted).
062     */
063    public TaskSeries(String name) {
064        super(name);
065        this.tasks = new java.util.ArrayList();
066    }
067
068    /**
069     * Adds a task to the series and sends a
070     * {@link org.jfree.data.general.SeriesChangeEvent} to all registered
071     * listeners.
072     *
073     * @param task  the task ({@code null} not permitted).
074     */
075    public void add(Task task) {
076        Args.nullNotPermitted(task, "task");
077        this.tasks.add(task);
078        fireSeriesChanged();
079    }
080
081    /**
082     * Removes a task from the series and sends
083     * a {@link org.jfree.data.general.SeriesChangeEvent}
084     * to all registered listeners.
085     *
086     * @param task  the task.
087     */
088    public void remove(Task task) {
089        this.tasks.remove(task);
090        fireSeriesChanged();
091    }
092
093    /**
094     * Removes all tasks from the series and sends
095     * a {@link org.jfree.data.general.SeriesChangeEvent}
096     * to all registered listeners.
097     */
098    public void removeAll() {
099        this.tasks.clear();
100        fireSeriesChanged();
101    }
102
103    /**
104     * Returns the number of items in the series.
105     *
106     * @return The item count.
107     */
108    @Override
109    public int getItemCount() {
110        return this.tasks.size();
111    }
112
113    /**
114     * Returns a task from the series.
115     *
116     * @param index  the task index (zero-based).
117     *
118     * @return The task.
119     */
120    public Task get(int index) {
121        return (Task) this.tasks.get(index);
122    }
123
124    /**
125     * Returns the task in the series that has the specified description.
126     *
127     * @param description  the name ({@code null} not permitted).
128     *
129     * @return The task (possibly {@code null}).
130     */
131    public Task get(String description) {
132        Task result = null;
133        int count = this.tasks.size();
134        for (int i = 0; i < count; i++) {
135            Task t = (Task) this.tasks.get(i);
136            if (t.getDescription().equals(description)) {
137                result = t;
138                break;
139            }
140        }
141        return result;
142    }
143
144    /**
145     * Returns an unmodifialble list of the tasks in the series.
146     *
147     * @return The tasks.
148     */
149    public List getTasks() {
150        return Collections.unmodifiableList(this.tasks);
151    }
152
153    /**
154     * Tests this object for equality with an arbitrary object.
155     *
156     * @param obj  the object to test against ({@code null} permitted).
157     *
158     * @return A boolean.
159     */
160    @Override
161    public boolean equals(Object obj) {
162        if (obj == this) {
163            return true;
164        }
165        if (!(obj instanceof TaskSeries)) {
166            return false;
167        }
168        TaskSeries that = (TaskSeries) obj;
169        if (!Objects.equals(this.tasks, that.tasks)) {
170            return false;
171        }
172        return super.equals(obj);
173    }
174
175    /**
176     * Ensures symmetry between super/subclass implementations of equals. For
177     * more detail, see http://jqno.nl/equalsverifier/manual/inheritance.
178     *
179     * @param other Object
180     * 
181     * @return true ONLY if the parameter is THIS class type
182     */
183    @Override
184    public boolean canEqual(Object other) {
185        // fix the "equals not symmetric" problem
186        return (other instanceof TaskSeries);
187    }
188    
189    @Override
190    public int hashCode() {
191        int hash = super.hashCode(); // equals calls superclass, hashCode must also
192        hash = 29 * hash + java.util.Objects.hashCode(this.tasks);
193        return hash;
194    }
195
196    /**
197     * Returns an independent copy of this series.
198     *
199     * @return A clone of the series.
200     *
201     * @throws CloneNotSupportedException if there is some problem cloning
202     *     the dataset.
203     */
204    @Override
205    public Object clone() throws CloneNotSupportedException {
206        TaskSeries clone = (TaskSeries) super.clone();
207        clone.tasks = (List) ObjectUtils.deepClone(this.tasks);
208        return clone;
209    }
210
211}