3535
3636/**
3737 * Keeps track of progress associated with an execution of progressable code.
38- * {@Link Task}s should <b>not</b> be updated by the code itself; it should be
38+ * {@link Task}s should <b>not</b> be updated by the code itself; it should be
3939 * update via {@link Progress#update()}.
4040 *
4141 * @author Gabriel Selzer
@@ -48,7 +48,7 @@ public class Task {
4848 private final Task parent ;
4949
5050 /** Subtasks created by this task */
51- private Set <Task > subTasks = new HashSet <>();
51+ private final Set <Task > subTasks = new HashSet <>();
5252
5353 /** Designates task completion */
5454 private boolean completed = false ;
@@ -60,34 +60,39 @@ public class Task {
6060 private long numSubTasks = 0 ;
6161
6262 /** Progress in current stage */
63- private AtomicLong current = new AtomicLong (0 );
63+ private final AtomicLong current = new AtomicLong (0 );
6464
6565 /** Maximum of current stage */
66- private AtomicLong max = new AtomicLong (1 );
66+ private final AtomicLong max = new AtomicLong (1 );
6767
6868 /** Number of within-task stages completed */
69- private AtomicLong stagesCompleted = new AtomicLong (0 );
69+ private final AtomicLong stagesCompleted = new AtomicLong (0 );
7070
7171 /** Number of subtasks completed */
72- private AtomicLong subTasksCompleted = new AtomicLong (0 );
72+ private final AtomicLong subTasksCompleted = new AtomicLong (0 );
7373
7474 /**
75- * True iff a call to {@link Task#defineTotalProgress(int, int )} has been made
75+ * True iff a call to {@link Task#defineTotalProgress(long, long )} has been made
7676 */
7777 private boolean tasksDefined = false ;
7878
7979 /** True iff {@link Task#max} has been defined for the current stage */
8080 private boolean updateDefined = false ;
8181
82+ /** Computation status as defined by the task */
83+ private final String id ;
84+
8285 /** Computation status as defined by the task */
8386 private String status = "Executing..." ;
8487
85- public Task () {
88+ public Task (final String id ) {
8689 this .parent = null ;
90+ this .id = id ;
8791 }
8892
89- public Task (final Task parent ) {
93+ public Task (final Task parent , final String id ) {
9094 this .parent = parent ;
95+ this .id = id ;
9196 }
9297
9398 /**
@@ -112,7 +117,7 @@ public void complete() {
112117 * @return the subtask.
113118 */
114119 public synchronized Task createSubtask () {
115- final Task sub = new Task (this );
120+ final Task sub = new Task (this , "Subtask " + subTasks . size () + 1 + " of Task \" " + id + " \" " );
116121 subTasks .add (sub );
117122 return sub ;
118123 }
@@ -124,11 +129,11 @@ public synchronized Task createSubtask() {
124129 * {@link Task#progress()} are called, as otherwise total progress cannot be
125130 * defined.
126131 * <p>
127- * Under the hood, this method calls {@link #defineTotalProgress(int, int )}
132+ * Under the hood, this method calls {@link #defineTotalProgress(long, long )}
128133 *
129134 * @param totalStages the number of computation stages within the task
130135 */
131- public void defineTotalProgress (final int totalStages ) {
136+ public void defineTotalProgress (final long totalStages ) {
132137 defineTotalProgress (totalStages , 0 );
133138 }
134139
@@ -144,8 +149,8 @@ public void defineTotalProgress(final int totalStages) {
144149 * within the task. This <b>is not</b> the same as the number of
145150 * subtasks used (as one subtask may run multiple times).
146151 */
147- public void defineTotalProgress (final int totalStages ,
148- final int totalSubTasks )
152+ public void defineTotalProgress (final long totalStages ,
153+ final long totalSubTasks )
149154 {
150155 this .numStages = totalStages ;
151156 this .numSubTasks = totalSubTasks ;
@@ -164,7 +169,7 @@ public boolean isComplete() {
164169 /**
165170 * Calculates and returns the progress of the associated progressible
166171 * {@link Object}. If the total progress is defined using
167- * {@link Task#defineTotalProgress(int, int )}, then this method will return a
172+ * {@link Task#defineTotalProgress(long, long )}, then this method will return a
168173 * {@code double} within the range [0, 1]. If the progress is <b>not</b>
169174 * defined, then this task will return {@code 0} until {@link #complete()} is
170175 * called; after that call this method will return {@code 1.}.
@@ -180,15 +185,6 @@ public double progress() {
180185 return totalCompletion / (numStages + numSubTasks );
181186 }
182187
183- /**
184- * Returns {@code true} iff progress has been defined
185- *
186- * @return {@code true} iff progress has been defined
187- */
188- public boolean progressDefined () {
189- return tasksDefined ;
190- }
191-
192188 /**
193189 * Iff {@code task} was a task generated by this {@link Task}, then we update
194190 * progress based on its completion.
@@ -206,8 +202,6 @@ private void resetStage() {
206202 current .set (0 );
207203 stagesCompleted .incrementAndGet ();
208204 this .updateDefined = false ;
209- if (stagesCompleted .longValue () == numStages && subTasksCompleted
210- .longValue () == numSubTasks ) completed = true ;
211205 }
212206
213207 /**
@@ -241,6 +235,15 @@ public String status() {
241235 return status ;
242236 }
243237
238+ /**
239+ * The id of the progressible {@link Object}
240+ *
241+ * @return the id
242+ */
243+ public String id () {
244+ return id ;
245+ }
246+
244247 /**
245248 * Used by a progressible {@link Object}, through {@link Progress} to
246249 * increment progress
0 commit comments