|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
IdAllocator.java | 100% | 100% | 100% | 100% |
|
1 | // Copyright 2004, 2005 The Apache Software Foundation | |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | // you may not use this file except in compliance with the License. | |
5 | // You may obtain a copy of the License at | |
6 | // | |
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
9 | // Unless required by applicable law or agreed to in writing, software | |
10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | // See the License for the specific language governing permissions and | |
13 | // limitations under the License. | |
14 | ||
15 | package org.apache.tapestry.util; | |
16 | ||
17 | import java.util.HashMap; | |
18 | import java.util.Map; | |
19 | ||
20 | import org.apache.hivemind.util.Defense; | |
21 | ||
22 | /** | |
23 | * Used to "uniquify" names within a given context. A base name is passed in, and the return value | |
24 | * is the base name, or the base name extended with a suffix to make it unique. | |
25 | * | |
26 | * @author Howard Lewis Ship | |
27 | * @since 3.0 | |
28 | */ | |
29 | ||
30 | public class IdAllocator | |
31 | { | |
32 | private final Map _generatorMap = new HashMap(); | |
33 | ||
34 | private final String _namespace; | |
35 | ||
36 | private static class NameGenerator | |
37 | { | |
38 | private final String _baseId; | |
39 | ||
40 | private int _index; | |
41 | ||
42 | 2856 | NameGenerator(String baseId) |
43 | { | |
44 | 2856 | _baseId = baseId + "$"; |
45 | } | |
46 | ||
47 | 191 | public String nextId() |
48 | { | |
49 | 191 | return _baseId + _index++; |
50 | } | |
51 | } | |
52 | ||
53 | 957 | public IdAllocator() |
54 | { | |
55 | 957 | this(""); |
56 | } | |
57 | ||
58 | 1043 | public IdAllocator(String namespace) |
59 | { | |
60 | 1043 | Defense.notNull(namespace, "namespace"); |
61 | ||
62 | 1043 | _namespace = namespace; |
63 | } | |
64 | ||
65 | /** | |
66 | * Allocates the id. Repeated calls for the same name will return "name", "name$0", "name$1", | |
67 | * etc. | |
68 | */ | |
69 | ||
70 | 3046 | public String allocateId(String name) |
71 | { | |
72 | 3046 | String key = name + _namespace; |
73 | ||
74 | 3046 | NameGenerator g = (NameGenerator) _generatorMap.get(key); |
75 | 3046 | String result = null; |
76 | ||
77 | 3046 | if (g == null) |
78 | { | |
79 | 2856 | g = new NameGenerator(key); |
80 | 2856 | result = key; |
81 | } | |
82 | else | |
83 | 190 | result = g.nextId(); |
84 | ||
85 | // Handle the degenerate case, where a base name of the form "foo$0" has been | |
86 | // requested. Skip over any duplicates thus formed. | |
87 | ||
88 | 3046 | while (_generatorMap.containsKey(result)) |
89 | 1 | result = g.nextId(); |
90 | ||
91 | 3046 | _generatorMap.put(result, g); |
92 | ||
93 | 3046 | return result; |
94 | } | |
95 | ||
96 | /** | |
97 | * Clears the allocator, resetting it to freshly allocated state. | |
98 | */ | |
99 | ||
100 | 356 | public void clear() |
101 | { | |
102 | 356 | _generatorMap.clear(); |
103 | } | |
104 | } |
|