Mercurial > hg > jslab
comparison src/samer/tools/CompoundTask.java @ 0:bf79fb79ee13
Initial Mercurial check in.
author | samer |
---|---|
date | Tue, 17 Jan 2012 17:50:20 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:bf79fb79ee13 |
---|---|
1 /* | |
2 * Copyright (c) 2000, Samer Abdallah, King's College London. | |
3 * All rights reserved. | |
4 * | |
5 * This software is provided AS iS and WITHOUT ANY WARRANTY; | |
6 * without even the implied warranty of MERCHANTABILITY or | |
7 * FITNESS FOR A PARTICULAR PURPOSE. | |
8 */ | |
9 | |
10 package samer.tools; | |
11 | |
12 /** | |
13 * This is a task that consists of a list of | |
14 * sub tasks. The task is no more than the sum | |
15 * of its subtasks -- ie there can be no extra | |
16 * processing other than that done by the tasks | |
17 * in the list. This is why the Task methods | |
18 * are decalared <code>final</code>. | |
19 */ | |
20 | |
21 public class CompoundTask implements Task | |
22 { | |
23 private TaskLink head=null, tail=null; | |
24 | |
25 public CompoundTask() {} | |
26 public CompoundTask(Task t) { addTask(t); } | |
27 | |
28 public String toString() { return "CompoundTask"; } | |
29 | |
30 // task list implementation | |
31 | |
32 public void addTask(Task t) | |
33 { | |
34 if (head==null) { | |
35 head=new TaskLink(t,null); | |
36 tail=head; | |
37 } else { | |
38 tail.next=new TaskLink(t,null); | |
39 tail=tail.next; | |
40 } | |
41 } | |
42 | |
43 public void addTaskAfter(Task t, Task b) | |
44 { | |
45 if (head==null) addTask(t); | |
46 else if (b==null) head=new TaskLink(t,head); | |
47 else { | |
48 TaskLink q=head; | |
49 while (q!=null && q.task!=b) q=q.next; | |
50 if (q!=null) q.next=new TaskLink(t,q.next); | |
51 else addTask(t); | |
52 } | |
53 } | |
54 | |
55 public void removeTask(Task t) | |
56 { | |
57 TaskLink prev=null; | |
58 | |
59 for (TaskLink l=head; l!=null; prev=l, l=l.next) { | |
60 if (l.task==t) { | |
61 if (prev!=null) prev.next=l.next; | |
62 else head=l.next; | |
63 if (tail==l) tail=prev; | |
64 } | |
65 } | |
66 } | |
67 | |
68 public Iterator iterator() { return new Iterator(); } | |
69 | |
70 // Task implementation | |
71 | |
72 public final void starting() { | |
73 for (TaskLink l=head; l!=null; l=l.next) l.task.starting(); | |
74 } | |
75 public final void stopping() { | |
76 for (TaskLink l=head; l!=null; l=l.next) l.task.stopping(); | |
77 } | |
78 public final void run() throws Exception { | |
79 for (TaskLink l=head; l!=null; l=l.next) l.task.run(); | |
80 } | |
81 public void dispose() { | |
82 for (TaskLink l=head; l!=null; l=l.next) l.task.dispose(); | |
83 } | |
84 | |
85 // using a linked list for speed | |
86 private static class TaskLink { | |
87 Task task; | |
88 TaskLink next; | |
89 | |
90 TaskLink(Task t, TaskLink n) { task=t; next=n; } | |
91 } | |
92 | |
93 // list iterator | |
94 public class Iterator implements java.util.Iterator { | |
95 TaskLink l=head; | |
96 public final boolean more() { return l!=null; } | |
97 public final boolean hasNext() { return l!=null; } | |
98 public final Task next() { Task t=l.task; l=l.next; return t; } | |
99 public final void remove() { l.task=l.next.task; l.next=l.next.next; } | |
100 } | |
101 } |