23 * have any questions. |
23 * have any questions. |
24 */ |
24 */ |
25 |
25 |
26 package com.sun.tools.javac.comp; |
26 package com.sun.tools.javac.comp; |
27 |
27 |
28 import com.sun.tools.javac.util.*; |
28 import java.util.AbstractQueue; |
|
29 import com.sun.tools.javac.util.Context; |
|
30 import java.util.HashMap; |
|
31 import java.util.Iterator; |
|
32 import java.util.LinkedList; |
|
33 import java.util.Map; |
|
34 import java.util.Queue; |
|
35 import javax.tools.JavaFileObject; |
29 |
36 |
30 /** A queue of all as yet unattributed classes. |
37 /** A queue of all as yet unattributed classes. |
31 * |
38 * |
32 * <p><b>This is NOT part of any API supported by Sun Microsystems. If |
39 * <p><b>This is NOT part of any API supported by Sun Microsystems. If |
33 * you write code that depends on this, you do so at your own risk. |
40 * you write code that depends on this, you do so at your own risk. |
34 * This code and its internal interfaces are subject to change or |
41 * This code and its internal interfaces are subject to change or |
35 * deletion without notice.</b> |
42 * deletion without notice.</b> |
36 */ |
43 */ |
37 public class Todo extends ListBuffer<Env<AttrContext>> { |
44 public class Todo extends AbstractQueue<Env<AttrContext>> { |
38 /** The context key for the todo list. */ |
45 /** The context key for the todo list. */ |
39 protected static final Context.Key<Todo> todoKey = |
46 protected static final Context.Key<Todo> todoKey = |
40 new Context.Key<Todo>(); |
47 new Context.Key<Todo>(); |
41 |
48 |
42 /** Get the Todo instance for this context. */ |
49 /** Get the Todo instance for this context. */ |
49 |
56 |
50 /** Create a new todo list. */ |
57 /** Create a new todo list. */ |
51 protected Todo(Context context) { |
58 protected Todo(Context context) { |
52 context.put(todoKey, this); |
59 context.put(todoKey, this); |
53 } |
60 } |
|
61 |
|
62 public void append(Env<AttrContext> env) { |
|
63 add(env); |
|
64 } |
|
65 |
|
66 @Override |
|
67 public Iterator<Env<AttrContext>> iterator() { |
|
68 return contents.iterator(); |
|
69 } |
|
70 |
|
71 @Override |
|
72 public int size() { |
|
73 return contents.size(); |
|
74 } |
|
75 |
|
76 public boolean offer(Env<AttrContext> e) { |
|
77 if (contents.add(e)) { |
|
78 if (contentsByFile != null) |
|
79 addByFile(e); |
|
80 return true; |
|
81 } else { |
|
82 return false; |
|
83 } |
|
84 } |
|
85 |
|
86 public Env<AttrContext> poll() { |
|
87 if (size() == 0) |
|
88 return null; |
|
89 Env<AttrContext> env = contents.remove(0); |
|
90 if (contentsByFile != null) |
|
91 removeByFile(env); |
|
92 return env; |
|
93 } |
|
94 |
|
95 public Env<AttrContext> peek() { |
|
96 return (size() == 0 ? null : contents.get(0)); |
|
97 } |
|
98 |
|
99 public Queue<Queue<Env<AttrContext>>> groupByFile() { |
|
100 if (contentsByFile == null) { |
|
101 contentsByFile = new LinkedList<Queue<Env<AttrContext>>>(); |
|
102 for (Env<AttrContext> env: contents) { |
|
103 addByFile(env); |
|
104 } |
|
105 } |
|
106 return contentsByFile; |
|
107 } |
|
108 |
|
109 private void addByFile(Env<AttrContext> env) { |
|
110 JavaFileObject file = env.toplevel.sourcefile; |
|
111 if (fileMap == null) |
|
112 fileMap = new HashMap<JavaFileObject, FileQueue>(); |
|
113 FileQueue fq = fileMap.get(file); |
|
114 if (fq == null) { |
|
115 fq = new FileQueue(); |
|
116 fileMap.put(file, fq); |
|
117 contentsByFile.add(fq); |
|
118 } |
|
119 fq.fileContents.add(env); |
|
120 } |
|
121 |
|
122 private void removeByFile(Env<AttrContext> env) { |
|
123 JavaFileObject file = env.toplevel.sourcefile; |
|
124 FileQueue fq = fileMap.get(file); |
|
125 if (fq == null) |
|
126 return; |
|
127 if (fq.fileContents.remove(env)) { |
|
128 if (fq.isEmpty()) { |
|
129 fileMap.remove(file); |
|
130 contentsByFile.remove(fq); |
|
131 } |
|
132 } |
|
133 } |
|
134 |
|
135 LinkedList<Env<AttrContext>> contents = new LinkedList<Env<AttrContext>>(); |
|
136 LinkedList<Queue<Env<AttrContext>>> contentsByFile; |
|
137 Map<JavaFileObject, FileQueue> fileMap; |
|
138 |
|
139 class FileQueue extends AbstractQueue<Env<AttrContext>> { |
|
140 @Override |
|
141 public Iterator<Env<AttrContext>> iterator() { |
|
142 return fileContents.iterator(); |
|
143 } |
|
144 |
|
145 @Override |
|
146 public int size() { |
|
147 return fileContents.size(); |
|
148 } |
|
149 |
|
150 public boolean offer(Env<AttrContext> e) { |
|
151 if (fileContents.offer(e)) { |
|
152 contents.add(e); |
|
153 return true; |
|
154 } |
|
155 return false; |
|
156 } |
|
157 |
|
158 public Env<AttrContext> poll() { |
|
159 if (fileContents.size() == 0) |
|
160 return null; |
|
161 Env<AttrContext> env = fileContents.remove(0); |
|
162 contents.remove(env); |
|
163 return env; |
|
164 } |
|
165 |
|
166 public Env<AttrContext> peek() { |
|
167 return (fileContents.size() == 0 ? null : fileContents.get(0)); |
|
168 } |
|
169 |
|
170 LinkedList<Env<AttrContext>> fileContents = new LinkedList<Env<AttrContext>>(); |
|
171 } |
54 } |
172 } |