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 * Task.java
029 * ---------
030 * (C) Copyright 2003-2022, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.data.gantt;
038
039import java.io.Serializable;
040import java.util.Date;
041import java.util.List;
042import java.util.Objects;
043
044import org.jfree.chart.internal.Args;
045import org.jfree.chart.api.PublicCloneable;
046
047import org.jfree.data.time.SimpleTimePeriod;
048import org.jfree.data.time.TimePeriod;
049
050/**
051 * A simple representation of a task.  The task has a description and a
052 * duration.  You can add sub-tasks to the task.
053 */
054public class Task implements Cloneable, PublicCloneable, Serializable {
055
056    /** For serialization. */
057    private static final long serialVersionUID = 1094303785346988894L;
058
059    /** The task description. */
060    private String description;
061
062    /** The time period for the task (estimated or actual). */
063    private TimePeriod duration;
064
065    /** The percent complete ({@code null} is permitted). */
066    private Double percentComplete;
067
068    /** Storage for the sub-tasks (if any). */
069    private List subtasks;
070
071    /**
072     * Creates a new task.
073     *
074     * @param description  the task description ({@code null} not
075     *                     permitted).
076     * @param duration  the task duration ({@code null} permitted).
077     */
078    public Task(String description, TimePeriod duration) {
079        Args.nullNotPermitted(description, "description");
080        this.description = description;
081        this.duration = duration;
082        this.percentComplete = null;
083        this.subtasks = new java.util.ArrayList();
084    }
085
086    /**
087     * Creates a new task.
088     *
089     * @param description  the task description ({@code null} not
090     *                     permitted).
091     * @param start  the start date ({@code null} not permitted).
092     * @param end  the end date ({@code null} not permitted).
093     */
094    public Task(String description, Date start, Date end) {
095        this(description, new SimpleTimePeriod(start, end));
096    }
097
098    /**
099     * Returns the task description.
100     *
101     * @return The task description (never {@code null}).
102     */
103    public String getDescription() {
104        return this.description;
105    }
106
107    /**
108     * Sets the task description.
109     *
110     * @param description  the description ({@code null} not permitted).
111     */
112    public void setDescription(String description) {
113        Args.nullNotPermitted(description, "description");
114        this.description = description;
115    }
116
117    /**
118     * Returns the duration (actual or estimated) of the task.
119     *
120     * @return The task duration (possibly {@code null}).
121     */
122    public TimePeriod getDuration() {
123        return this.duration;
124    }
125
126    /**
127     * Sets the task duration (actual or estimated).
128     *
129     * @param duration  the duration ({@code null} permitted).
130     */
131    public void setDuration(TimePeriod duration) {
132        this.duration = duration;
133    }
134
135    /**
136     * Returns the percentage complete for this task.
137     *
138     * @return The percentage complete (possibly {@code null}).
139     */
140    public Double getPercentComplete() {
141        return this.percentComplete;
142    }
143
144    /**
145     * Sets the percentage complete for the task.
146     *
147     * @param percent  the percentage ({@code null} permitted).
148     */
149    public void setPercentComplete(Double percent) {
150        this.percentComplete = percent;
151    }
152
153    /**
154     * Sets the percentage complete for the task.
155     *
156     * @param percent  the percentage.
157     */
158    public void setPercentComplete(double percent) {
159        setPercentComplete(Double.valueOf(percent));
160    }
161
162    /**
163     * Adds a sub-task to the task.
164     *
165     * @param subtask  the subtask ({@code null} not permitted).
166     */
167    public void addSubtask(Task subtask) {
168        Args.nullNotPermitted(subtask, "subtask");
169        this.subtasks.add(subtask);
170    }
171
172    /**
173     * Removes a sub-task from the task.
174     *
175     * @param subtask  the subtask.
176     */
177    public void removeSubtask(Task subtask) {
178        this.subtasks.remove(subtask);
179    }
180
181    /**
182     * Returns the sub-task count.
183     *
184     * @return The sub-task count.
185     */
186    public int getSubtaskCount() {
187        return this.subtasks.size();
188    }
189
190    /**
191     * Returns a sub-task.
192     *
193     * @param index  the index.
194     *
195     * @return The sub-task.
196     */
197    public Task getSubtask(int index) {
198        return (Task) this.subtasks.get(index);
199    }
200
201    /**
202     * Tests this object for equality with an arbitrary object.
203     *
204     * @param object  the other object ({@code null} permitted).
205     *
206     * @return A boolean.
207     */
208    @Override
209    public boolean equals(Object object) {
210        if (object == this) {
211            return true;
212        }
213        if (!(object instanceof Task)) {
214            return false;
215        }
216        Task that = (Task) object;
217        if (!Objects.equals(this.description, that.description)) {
218            return false;
219        }
220        if (!Objects.equals(this.duration, that.duration)) {
221            return false;
222        }
223        if (!Objects.equals(this.percentComplete, that.percentComplete)) {
224            return false;
225        }
226        if (!Objects.equals(this.subtasks, that.subtasks)) {
227            return false;
228        }
229        return true;
230    }
231
232    @Override
233    public int hashCode(){
234        int hash = 7;
235        hash = 71 * hash + Objects.hashCode(this.description);
236        hash = 71 * hash + Objects.hashCode(this.duration);
237        hash = 71 * hash + Objects.hashCode(this.percentComplete);
238        hash = 71 * hash + Objects.hashCode(this.subtasks);
239        return hash;
240    }
241
242    /**
243     * Returns a clone of the task.
244     *
245     * @return A clone.
246     *
247     * @throws CloneNotSupportedException  never thrown by this class, but
248     *         subclasses may not support cloning.
249     */
250    @Override
251    public Object clone() throws CloneNotSupportedException {
252        Task clone = (Task) super.clone();
253        return clone;
254    }
255
256}