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 | 0 | final Map _generatorMap = new HashMap(); |
43 | 0 | final List _uniqueGenerators = new ArrayList(); |
44 | |
|
45 | |
final String _namespace; |
46 | |
|
47 | |
|
48 | 0 | private class NameGenerator implements Cloneable |
49 | |
{ |
50 | |
private final String _baseId; |
51 | |
|
52 | |
private int _index; |
53 | |
|
54 | |
NameGenerator(String baseId) |
55 | 0 | { |
56 | 0 | _baseId = baseId + SEPARATOR; |
57 | 0 | } |
58 | |
|
59 | |
NameGenerator(String baseId, int index) |
60 | 0 | { |
61 | 0 | _baseId = baseId + SEPARATOR; |
62 | 0 | _index = index; |
63 | 0 | } |
64 | |
|
65 | |
public String nextId() |
66 | |
{ |
67 | 0 | return _baseId + _index++; |
68 | |
} |
69 | |
|
70 | |
public String peekId() |
71 | |
{ |
72 | 0 | return _baseId + _index; |
73 | |
} |
74 | |
|
75 | |
public String getBaseId() |
76 | |
{ |
77 | 0 | 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 | 0 | this(""); |
126 | 0 | } |
127 | |
|
128 | |
public IdAllocator(String namespace) |
129 | 0 | { |
130 | 0 | Defense.notNull(namespace, "namespace"); |
131 | |
|
132 | 0 | _namespace = namespace; |
133 | 0 | } |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
public String allocateId(String name) |
146 | |
{ |
147 | 0 | String key = name + _namespace; |
148 | |
|
149 | 0 | NameGenerator g = (NameGenerator) _generatorMap.get(key.toLowerCase()); |
150 | 0 | String result = null; |
151 | |
|
152 | 0 | if (g == null) |
153 | |
{ |
154 | 0 | g = new NameGenerator(key); |
155 | 0 | _uniqueGenerators.add(g); |
156 | 0 | result = key; |
157 | |
} |
158 | |
else |
159 | 0 | result = g.nextId(); |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | 0 | while(_generatorMap.containsKey(result.toLowerCase())) |
166 | 0 | result = g.nextId(); |
167 | |
|
168 | 0 | _generatorMap.put(result.toLowerCase(), g); |
169 | |
|
170 | 0 | return result; |
171 | |
} |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
public String peekNextId(String name) |
182 | |
{ |
183 | 0 | String key = name + _namespace; |
184 | |
|
185 | 0 | NameGenerator g = (NameGenerator) _generatorMap.get(key.toLowerCase()); |
186 | 0 | String result = null; |
187 | |
|
188 | 0 | if (g == null) |
189 | |
{ |
190 | 0 | g = new NameGenerator(key); |
191 | 0 | result = key; |
192 | |
} else |
193 | 0 | result = g.peekId(); |
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | 0 | 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 | 0 | return result; |
216 | |
} |
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
public String toExternalString() |
225 | |
{ |
226 | 0 | StringBuffer str = new StringBuffer(_namespace); |
227 | |
|
228 | 0 | for (int i=0; i < _uniqueGenerators.size(); i++) |
229 | |
{ |
230 | |
|
231 | |
|
232 | 0 | str.append(","); |
233 | |
|
234 | 0 | NameGenerator g = (NameGenerator) _uniqueGenerators.get(i); |
235 | |
|
236 | 0 | str.append(g.getBaseId()).append(NAME_SEPARATOR); |
237 | 0 | str.append(g._index); |
238 | |
} |
239 | |
|
240 | 0 | return str.toString(); |
241 | |
} |
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
void addSeed(String baseId, int index) |
251 | |
{ |
252 | 0 | NameGenerator g = new NameGenerator(baseId, 0); |
253 | 0 | _uniqueGenerators.add(g); |
254 | 0 | _generatorMap.put(baseId.toLowerCase(), g); |
255 | |
|
256 | |
|
257 | 0 | while(g._index != index) |
258 | |
{ |
259 | 0 | String nextId = g.nextId().toLowerCase(); |
260 | |
|
261 | |
|
262 | |
|
263 | 0 | if (!_generatorMap.containsKey(nextId)) |
264 | |
{ |
265 | 0 | _generatorMap.put(nextId, g); |
266 | |
} |
267 | 0 | } |
268 | 0 | } |
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
public void clear() |
275 | |
{ |
276 | 0 | _generatorMap.clear(); |
277 | 0 | _uniqueGenerators.clear(); |
278 | 0 | } |
279 | |
|
280 | |
public static IdAllocator fromExternalString(String seed) |
281 | |
{ |
282 | 0 | Defense.notNull(seed, "seed"); |
283 | |
|
284 | 0 | String[] values = TapestryUtils.split(seed); |
285 | 0 | if (values.length == 0) |
286 | |
{ |
287 | 0 | return new IdAllocator(); |
288 | |
} |
289 | |
|
290 | 0 | String namespace = values[0]; |
291 | |
|
292 | 0 | IdAllocator idAllocator = new IdAllocator(namespace); |
293 | |
|
294 | 0 | for (int i=1; i < values.length; i++) |
295 | |
{ |
296 | 0 | int index = values[i].lastIndexOf(NAME_SEPARATOR); |
297 | |
|
298 | 0 | if (index < 0) |
299 | 0 | continue; |
300 | |
|
301 | 0 | String baseId = values[i].substring(0, index); |
302 | 0 | int valueIndex = Integer.parseInt(values[i].substring(index + 1, values[i].length())); |
303 | |
|
304 | 0 | idAllocator.addSeed(baseId, valueIndex); |
305 | |
} |
306 | |
|
307 | 0 | return idAllocator; |
308 | |
} |
309 | |
} |