KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > util > IdAllocator


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 JavaDoc;
18 import java.util.Map JavaDoc;
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 JavaDoc _generatorMap = new HashMap JavaDoc();
33
34     private final String JavaDoc _namespace;
35
36     private static class NameGenerator
37     {
38         private final String JavaDoc _baseId;
39
40         private int _index;
41
42         NameGenerator(String JavaDoc baseId)
43         {
44             _baseId = baseId + "$";
45         }
46
47         public String JavaDoc nextId()
48         {
49             return _baseId + _index++;
50         }
51     }
52
53     public IdAllocator()
54     {
55         this("");
56     }
57
58     public IdAllocator(String JavaDoc namespace)
59     {
60         Defense.notNull(namespace, "namespace");
61
62         _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     public String JavaDoc allocateId(String JavaDoc name)
71     {
72         String JavaDoc key = name + _namespace;
73
74         NameGenerator g = (NameGenerator) _generatorMap.get(key);
75         String JavaDoc result = null;
76
77         if (g == null)
78         {
79             g = new NameGenerator(key);
80             result = key;
81         }
82         else
83             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         while (_generatorMap.containsKey(result))
89             result = g.nextId();
90
91         _generatorMap.put(result, g);
92
93         return result;
94     }
95
96     /**
97      * Clears the allocator, resetting it to freshly allocated state.
98      */

99
100     public void clear()
101     {
102         _generatorMap.clear();
103     }
104 }
Popular Tags