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