|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover 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 |
/**
|
|
21 |
* Used to "uniquify" names within a given context. A base name is passed in, and the return value
|
|
22 |
* is the base name, or the base name extended with a suffix to make it unique.
|
|
23 |
*
|
|
24 |
* @author Howard Lewis Ship
|
|
25 |
* @since 3.0
|
|
26 |
*/
|
|
27 |
|
|
28 |
public class IdAllocator |
|
29 |
{ |
|
30 |
private Map _generatorMap = new HashMap(); |
|
31 |
|
|
32 |
private static class NameGenerator |
|
33 |
{ |
|
34 |
private String _baseId;
|
|
35 |
|
|
36 |
private int _index; |
|
37 |
|
|
38 | 1039 |
NameGenerator(String baseId) |
39 |
{ |
|
40 | 1039 |
_baseId = baseId + "$";
|
41 |
} |
|
42 |
|
|
43 | 112 |
public String nextId()
|
44 |
{ |
|
45 | 112 |
return _baseId + _index++;
|
46 |
} |
|
47 |
} |
|
48 |
|
|
49 |
/**
|
|
50 |
* Allocates the id. Repeated calls for the same name will return "name", "name$0", "name$1",
|
|
51 |
* etc.
|
|
52 |
*/
|
|
53 |
|
|
54 | 1150 |
public String allocateId(String name)
|
55 |
{ |
|
56 | 1150 |
NameGenerator g = (NameGenerator) _generatorMap.get(name); |
57 | 1150 |
String result = null;
|
58 |
|
|
59 | 1150 |
if (g == null) |
60 |
{ |
|
61 | 1039 |
g = new NameGenerator(name);
|
62 | 1039 |
result = name; |
63 |
} |
|
64 |
else
|
|
65 | 111 |
result = g.nextId(); |
66 |
|
|
67 |
// Handle the degenerate case, where a base name of the form "foo$0" has been
|
|
68 |
// requested. Skip over any duplicates thus formed.
|
|
69 |
|
|
70 | 1150 |
while (_generatorMap.containsKey(result))
|
71 | 1 |
result = g.nextId(); |
72 |
|
|
73 | 1150 |
_generatorMap.put(result, g); |
74 |
|
|
75 | 1150 |
return result;
|
76 |
} |
|
77 |
|
|
78 |
/**
|
|
79 |
* Clears the allocator, resetting it to freshly allocated state.
|
|
80 |
*/
|
|
81 |
|
|
82 | 337 |
public void clear() |
83 |
{ |
|
84 | 337 |
_generatorMap.clear(); |
85 |
} |
|
86 |
} |
|