1 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 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 NameGenerator(String baseId) 43 { 44 _baseId = baseId + "$"; 45 } 46 47 public String nextId() 48 { 49 return _baseId + _index++; 50 } 51 } 52 53 public IdAllocator() 54 { 55 this(""); 56 } 57 58 public IdAllocator(String namespace) 59 { 60 Defense.notNull(namespace, "namespace"); 61 62 _namespace = namespace; 63 } 64 65 69 70 public String allocateId(String name) 71 { 72 String key = name + _namespace; 73 74 NameGenerator g = (NameGenerator) _generatorMap.get(key); 75 String 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 88 while (_generatorMap.containsKey(result)) 89 result = g.nextId(); 90 91 _generatorMap.put(result, g); 92 93 return result; 94 } 95 96 99 100 public void clear() 101 { 102 _generatorMap.clear(); 103 } 104 } | Popular Tags |