1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.util; |
16 |
|
|
17 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
18 |
|
import org.apache.hivemind.util.Defense; |
19 |
|
import org.apache.tapestry.TapestryUtils; |
20 |
|
|
21 |
|
import java.util.ArrayList; |
22 |
|
import java.util.HashMap; |
23 |
|
import java.util.List; |
24 |
|
import java.util.Map; |
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
public class IdAllocator |
36 |
|
{ |
37 |
|
|
38 |
|
private static final String SEPARATOR = "_"; |
39 |
|
|
40 |
|
private static final char NAME_SEPARATOR = '$'; |
41 |
|
|
42 |
231 |
final Map _generatorMap = new HashMap(); |
43 |
231 |
final List _uniqueGenerators = new ArrayList(); |
44 |
|
|
45 |
|
final String _namespace; |
46 |
|
|
47 |
|
|
48 |
63 |
private class NameGenerator implements Cloneable |
49 |
|
{ |
50 |
|
private final String _baseId; |
51 |
|
|
52 |
|
private int _index; |
53 |
|
|
54 |
|
NameGenerator(String baseId) |
55 |
189 |
{ |
56 |
189 |
_baseId = baseId + SEPARATOR; |
57 |
189 |
} |
58 |
|
|
59 |
|
NameGenerator(String baseId, int index) |
60 |
33 |
{ |
61 |
33 |
_baseId = baseId + SEPARATOR; |
62 |
33 |
_index = index; |
63 |
33 |
} |
64 |
|
|
65 |
|
public String nextId() |
66 |
|
{ |
67 |
79 |
return _baseId + _index++; |
68 |
|
} |
69 |
|
|
70 |
|
public String peekId() |
71 |
|
{ |
72 |
6 |
return _baseId + _index; |
73 |
|
} |
74 |
|
|
75 |
|
public String getBaseId() |
76 |
|
{ |
77 |
8 |
return _baseId.substring(0, _baseId.length() - 1); |
78 |
|
} |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
protected Object clone() |
84 |
|
throws CloneNotSupportedException |
85 |
|
{ |
86 |
0 |
return super.clone(); |
87 |
|
} |
88 |
|
|
89 |
|
public boolean equals(Object o) |
90 |
|
{ |
91 |
0 |
if (this == o) |
92 |
0 |
return true; |
93 |
0 |
if (!(o instanceof NameGenerator)) |
94 |
0 |
return false; |
95 |
|
|
96 |
0 |
NameGenerator that = (NameGenerator) o; |
97 |
|
|
98 |
0 |
if (_baseId != null ? !_baseId.equals(that._baseId) : that._baseId != null) |
99 |
0 |
return false; |
100 |
|
|
101 |
0 |
return true; |
102 |
|
} |
103 |
|
|
104 |
|
public int hashCode() |
105 |
|
{ |
106 |
|
int result; |
107 |
0 |
result = (_baseId != null ? _baseId.hashCode() : 0); |
108 |
0 |
result = 31 * result + _index; |
109 |
0 |
return result; |
110 |
|
} |
111 |
|
|
112 |
|
public String toString() |
113 |
|
{ |
114 |
0 |
return "NameGenerator[" + |
115 |
|
"_baseId='" + _baseId + '\'' + |
116 |
|
'\n' + |
117 |
|
", _index=" + _index + |
118 |
|
'\n' + |
119 |
|
']'; |
120 |
|
} |
121 |
|
} |
122 |
|
|
123 |
|
public IdAllocator() |
124 |
|
{ |
125 |
210 |
this(""); |
126 |
210 |
} |
127 |
|
|
128 |
|
public IdAllocator(String namespace) |
129 |
231 |
{ |
130 |
231 |
Defense.notNull(namespace, "namespace"); |
131 |
|
|
132 |
231 |
_namespace = namespace; |
133 |
231 |
} |
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
public String allocateId(String name) |
146 |
|
{ |
147 |
244 |
String key = name + _namespace; |
148 |
|
|
149 |
244 |
NameGenerator g = (NameGenerator) _generatorMap.get(key.toLowerCase()); |
150 |
244 |
String result = null; |
151 |
|
|
152 |
244 |
if (g == null) |
153 |
|
{ |
154 |
188 |
g = new NameGenerator(key); |
155 |
188 |
_uniqueGenerators.add(g); |
156 |
188 |
result = key; |
157 |
|
} |
158 |
|
else |
159 |
56 |
result = g.nextId(); |
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
245 |
while(_generatorMap.containsKey(result.toLowerCase())) |
166 |
1 |
result = g.nextId(); |
167 |
|
|
168 |
244 |
_generatorMap.put(result.toLowerCase(), g); |
169 |
|
|
170 |
244 |
return result; |
171 |
|
} |
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
public String peekNextId(String name) |
182 |
|
{ |
183 |
7 |
String key = name + _namespace; |
184 |
|
|
185 |
7 |
NameGenerator g = (NameGenerator) _generatorMap.get(key.toLowerCase()); |
186 |
7 |
String result = null; |
187 |
|
|
188 |
7 |
if (g == null) |
189 |
|
{ |
190 |
1 |
g = new NameGenerator(key); |
191 |
1 |
result = key; |
192 |
|
} else |
193 |
6 |
result = g.peekId(); |
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
7 |
if (_generatorMap.containsKey(result.toLowerCase())) |
203 |
|
{ |
204 |
|
try { |
205 |
0 |
NameGenerator cg = (NameGenerator)g.clone(); |
206 |
|
|
207 |
0 |
while (_generatorMap.containsKey(result.toLowerCase())) |
208 |
0 |
result = cg.nextId(); |
209 |
|
|
210 |
0 |
} catch (CloneNotSupportedException e) { |
211 |
0 |
throw new ApplicationRuntimeException(e); |
212 |
0 |
} |
213 |
|
} |
214 |
|
|
215 |
7 |
return result; |
216 |
|
} |
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
public String toExternalString() |
225 |
|
{ |
226 |
7 |
StringBuffer str = new StringBuffer(_namespace); |
227 |
|
|
228 |
15 |
for (int i=0; i < _uniqueGenerators.size(); i++) |
229 |
|
{ |
230 |
|
|
231 |
|
|
232 |
8 |
str.append(","); |
233 |
|
|
234 |
8 |
NameGenerator g = (NameGenerator) _uniqueGenerators.get(i); |
235 |
|
|
236 |
8 |
str.append(g.getBaseId()).append(NAME_SEPARATOR); |
237 |
8 |
str.append(g._index); |
238 |
|
} |
239 |
|
|
240 |
7 |
return str.toString(); |
241 |
|
} |
242 |
|
|
243 |
|
|
244 |
|
|
245 |
|
|
246 |
|
|
247 |
|
|
248 |
|
|
249 |
|
|
250 |
|
void addSeed(String baseId, int index) |
251 |
|
{ |
252 |
33 |
NameGenerator g = new NameGenerator(baseId, 0); |
253 |
33 |
_uniqueGenerators.add(g); |
254 |
33 |
_generatorMap.put(baseId.toLowerCase(), g); |
255 |
|
|
256 |
|
|
257 |
55 |
while(g._index != index) |
258 |
|
{ |
259 |
22 |
String nextId = g.nextId().toLowerCase(); |
260 |
|
|
261 |
|
|
262 |
|
|
263 |
22 |
if (!_generatorMap.containsKey(nextId)) |
264 |
|
{ |
265 |
21 |
_generatorMap.put(nextId, g); |
266 |
|
} |
267 |
22 |
} |
268 |
33 |
} |
269 |
|
|
270 |
|
|
271 |
|
|
272 |
|
|
273 |
|
|
274 |
|
public void clear() |
275 |
|
{ |
276 |
52 |
_generatorMap.clear(); |
277 |
52 |
_uniqueGenerators.clear(); |
278 |
52 |
} |
279 |
|
|
280 |
|
public static IdAllocator fromExternalString(String seed) |
281 |
|
{ |
282 |
7 |
Defense.notNull(seed, "seed"); |
283 |
|
|
284 |
7 |
String[] values = TapestryUtils.split(seed); |
285 |
7 |
if (values.length == 0) |
286 |
|
{ |
287 |
1 |
return new IdAllocator(); |
288 |
|
} |
289 |
|
|
290 |
6 |
String namespace = values[0]; |
291 |
|
|
292 |
6 |
IdAllocator idAllocator = new IdAllocator(namespace); |
293 |
|
|
294 |
39 |
for (int i=1; i < values.length; i++) |
295 |
|
{ |
296 |
33 |
int index = values[i].lastIndexOf(NAME_SEPARATOR); |
297 |
|
|
298 |
33 |
if (index < 0) |
299 |
0 |
continue; |
300 |
|
|
301 |
33 |
String baseId = values[i].substring(0, index); |
302 |
33 |
int valueIndex = Integer.parseInt(values[i].substring(index + 1, values[i].length())); |
303 |
|
|
304 |
33 |
idAllocator.addSeed(baseId, valueIndex); |
305 |
|
} |
306 |
|
|
307 |
6 |
return idAllocator; |
308 |
|
} |
309 |
|
} |