001    /*
002     *  jDTAUS - DTAUS fileformat.
003     *  Copyright (c) 2005 Christian Schulte <cs@schulte.it>
004     *
005     *  This library is free software; you can redistribute it and/or
006     *  modify it under the terms of the GNU Lesser General Public
007     *  License as published by the Free Software Foundation; either
008     *  version 2.1 of the License, or any later version.
009     *
010     *  This library is distributed in the hope that it will be useful,
011     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
012     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013     *  Lesser General Public License for more details.
014     *
015     *  You should have received a copy of the GNU Lesser General Public
016     *  License along with this library; if not, write to the Free Software
017     *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
018     *
019     */
020    package org.jdtaus.core.monitor;
021    
022    import java.io.Serializable;
023    import java.rmi.server.UID;
024    import org.jdtaus.core.text.Message;
025    
026    /**
027     * A task of execution.
028     * <p>A task is a sequence of operations taking time. If property
029     * {@code indeterminate} is {@code false}, properties {@code minimum},
030     * {@code maximum} and {@code progress} hold information regarding the progress
031     * of the task.</p>
032     *
033     * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
034     * @version $Id: Task.java 2916 2007-04-20 19:43:03Z schulte2005 $
035     *
036     * @see TaskEventSource
037     */
038    public abstract class Task implements Cloneable, Serializable
039    {
040    
041        //--Constructors------------------------------------------------------------
042    
043        /** Creates a new {@code Task} instance. */
044        public Task()
045        {
046            super();
047            this.uid = new UID();
048            this.indeterminate = true;
049        }
050    
051        //------------------------------------------------------------Constructors--
052        //--Task--------------------------------------------------------------------
053    
054        /**
055         * Unique task identifier.
056         * @serial
057         */
058        private UID uid;
059    
060        /**
061         * The lower bound of the range.
062         * @serial
063         */
064        private int minimum;
065    
066        /**
067         * The upper bound of the range.
068         * @serial
069         */
070        private int maximum;
071    
072        /**
073         * Indicates the progress of the task.
074         * @serial
075         */
076        private int progress;
077    
078        /**
079         * Flag indicating if the operations performed by the task are of unknown
080         * length.
081         * @serial
082         */
083        private boolean indeterminate;
084    
085        /**
086         * Gets the lower end of the progress value.
087         *
088         * @return an int representing the minimum value.
089         */
090        public final int getMinimum()
091        {
092            return this.minimum;
093        }
094    
095        /**
096         * Sets the lower end of the progress value.
097         *
098         * @param minimum an int specifying the minimum value.
099         */
100        public final void setMinimum(int minimum)
101        {
102            this.minimum = minimum;
103        }
104    
105        /**
106         * Gets the higher end of the progress value.
107         *
108         * @return an int representing the maximum value.
109         */
110        public final int getMaximum()
111        {
112            return this.maximum;
113        }
114    
115        /**
116         * Sets the higher end of the progress value.
117         *
118         * @param maximum an int specifying the maximum value.
119         */
120        public final void setMaximum(int maximum)
121        {
122            this.maximum = maximum;
123        }
124    
125        /**
126         * Gets the progress of the task.
127         *
128         * @return the progress of the task.
129         */
130        public final int getProgress()
131        {
132            return this.progress;
133        }
134    
135        /**
136         * Sets the progress of the task.
137         *
138         * @param progress an int specifying the current value, between the
139         * maximum and minimum specified for this task.
140         *
141         * @throws IllegalArgumentException if {@code progress} is lower than
142         * the minimum of the range or greater than the maximum of the range.
143         */
144        public final void setProgress(final int progress)
145        {
146            if(progress < this.minimum || progress > this.maximum)
147            {
148                throw new IllegalArgumentException(Integer.toString(progress));
149            }
150    
151            this.progress = progress;
152        }
153    
154        /**
155         * Flag indicating if the operations performed by the task are of unknown
156         * length.
157         *
158         * @return {@code true} if the operations performed by the task are of
159         * unknown length; {@code false} if properties {@code minimum},
160         * {@code maximum} and {@code progress} hold progress information.
161         */
162        public final boolean isIndeterminate()
163        {
164            return this.indeterminate;
165        }
166    
167        /**
168         * Setter for property {@code indeterminate}.
169         *
170         * @param indeterminate {@code true} if the operations performed by the task
171         * are of unknown length; {@code false} if properties {@code minimum},
172         * {@code maximum} and {@code progress} hold progress information.
173         */
174        public final void setIndeterminate(final boolean indeterminate)
175        {
176            this.indeterminate = indeterminate;
177        }
178    
179        /**
180         * Getter for property {@code description}.
181         *
182         * @return description of the task.
183         */
184        public abstract Message getDescription();
185    
186        /**
187         * Creates a string representing the properties of the instance.
188         *
189         * @return a string representing the properties of the instance.
190         */
191        private String internalString()
192        {
193            return new StringBuffer(500).
194                append("\n\tindeterminate=").append(this.indeterminate).
195                append("\n\tmaximum=").append(this.maximum).
196                append("\n\tminimum=").append(this.minimum).
197                append("\n\tprogress=").append(this.progress).
198                append("\n\tuid=").append(this.uid).
199                append("\n\tdescription=").append(this.getDescription()).
200                toString();
201    
202        }
203    
204        //--------------------------------------------------------------------Task--
205        //--Object------------------------------------------------------------------
206    
207        /**
208         * Returns a string representation of the object.
209         *
210         * @return a string representation of the object.
211         */
212        public String toString()
213        {
214            return super.toString() + this.internalString();
215        }
216    
217        /**
218         * Returns a hash code value for this object.
219         *
220         * @return a hash code value for this object.
221         */
222        public final int hashCode()
223        {
224            return this.uid.hashCode();
225        }
226    
227        /**
228         * Indicates whether some other object is equal to this one.
229         * <p>Tasks internally cary a UID which is created during instantiation.
230         * This UID is used for comparing {@code o} with the instance.</p>
231         *
232         * @param o the reference object with which to compare.
233         *
234         * @return {@code true} if this object is the same as {@code o};
235         * {@code false} otherwise.
236         *
237         * @see UID
238         */
239        public final boolean equals(final Object o)
240        {
241            return o == this || (o instanceof Task &&
242                ((Task) o).uid.equals(this.uid));
243    
244        }
245    
246        /**
247         * Creates and returns a copy of this object.
248         *
249         * @return a clone of this instance.
250         */
251        public Object clone()
252        {
253            try
254            {
255                return super.clone();
256            }
257            catch(CloneNotSupportedException e)
258            {
259                throw new AssertionError(e);
260            }
261        }
262    
263        //------------------------------------------------------------------Object--
264    
265    }