KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > util > WorkList


1 package polyglot.util;
2
3 import java.util.Collection JavaDoc;
4 import java.util.Collections JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.LinkedList JavaDoc;
8 import java.util.ListIterator JavaDoc;
9 import java.util.Map JavaDoc;
10
11 /**
12  * This class represents a set of calculations to be performed, some
13  * of which have already been completed. It optionally stores the results of
14  * those calculations.
15  *
16  * Requires: every 'calculation' object has a working hashcode and
17  * toString method.
18  **/

19 public class WorkList {
20
21   /**
22    * Creates a new, empty worklist.
23    **/

24   public WorkList() {
25     pending = new LinkedList JavaDoc();
26     results = new HashMap JavaDoc();
27     size = 0;
28   }
29
30   /**
31    * Adds the calculation represented by <o> to the worklist, if it has not
32    * already been calculated.
33    **/

34   public void addWork(Object JavaDoc o) {
35     if (! results.containsKey(o)) {
36       results.put(o, NOT_CALCULATED);
37       pending.addLast(o);
38       size++;
39     }
40   }
41
42   /**
43    * Adds every member of the collection <c> to the worklist, if it
44    * has not already been calculted.
45    **/

46   public void addWork(Collection JavaDoc c) {
47     for (Iterator JavaDoc i = c.iterator(); i.hasNext(); )
48       addWork(i.next());
49   }
50
51   /**
52    * Returns true iff there is no more work to do.
53    **/

54   public boolean finished() {
55     return size == 0;
56   }
57
58   /**
59    * Returns the first element with no known result. Throws
60    * NoSuchElementException if no such element exists.
61    **/

62   public Object JavaDoc getWork() {
63     if (size>0)
64       return pending.getFirst();
65     else
66       throw new java.util.NoSuchElementException JavaDoc("WorkList.getWork");
67   }
68
69   /**
70    * Announces that we have finished the calculation represented by <work>,
71    * getting the result <result>. Removes <work> from the pending list,
72    * and sets its result to <result>
73    **/

74   public void finishWork(Object JavaDoc work, Object JavaDoc result) {
75     if (results.get(work) == NOT_CALCULATED) {
76       for (ListIterator JavaDoc i = pending.listIterator(); i.hasNext(); ) {
77     if (i.next().equals(work))
78       i.remove();
79       }
80     }
81     results.put(work, result);
82   }
83
84   /**
85    * Announces that we have finished the calculation represented by <work>.
86    **/

87   public void finishWork(Object JavaDoc work) {
88     finishWork(work, null);
89   }
90
91   /**
92    * Returns true iff <work> has been completed.
93    **/

94   public boolean isFinished(Object JavaDoc work) {
95     return results.containsKey(work) && results.get(work) != NOT_CALCULATED;
96   }
97
98   /**
99    * Returns an immutable view of a map from calculation objects to
100    * their results. Non-computed values map to NOT_CALCULATED
101    **/

102   public Map JavaDoc getMap() {
103     return Collections.unmodifiableMap(results);
104   }
105
106   // The list of objects to be calculated on. The oldest element is first.
107
// RI: Every member of <pending> is a key in <results>.
108
LinkedList JavaDoc pending;
109   // A map from all objects to their results. Any object with no result
110
// maps to NOT_CALCULATED.
111
HashMap JavaDoc results;
112   // The number of elements in pending.
113
int size;
114
115   public static final Object JavaDoc NOT_CALCULATED = new Object JavaDoc();
116 }
117
Popular Tags