Clover coverage report - Code Coverage for tapestry release 4.0-beta-9
Coverage timestamp: Sat Oct 1 2005 08:36:20 EDT
file stats: LOC: 106   Methods: 6
NCLOC: 52   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
IdAllocator.java 100% 100% 100% 100%
coverage
 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 static final String SEPARATOR = "_";
 33   
 34    private final Map _generatorMap = new HashMap();
 35   
 36    private final String _namespace;
 37   
 38    private static class NameGenerator
 39    {
 40    private final String _baseId;
 41   
 42    private int _index;
 43   
 44  2896 NameGenerator(String baseId)
 45    {
 46  2896 _baseId = baseId + SEPARATOR;
 47    }
 48   
 49  201 public String nextId()
 50    {
 51  201 return _baseId + _index++;
 52    }
 53    }
 54   
 55  1002 public IdAllocator()
 56    {
 57  1002 this("");
 58    }
 59   
 60  1088 public IdAllocator(String namespace)
 61    {
 62  1088 Defense.notNull(namespace, "namespace");
 63   
 64  1088 _namespace = namespace;
 65    }
 66   
 67    /**
 68    * Allocates the id. Repeated calls for the same name will return "name", "name_0", "name_1",
 69    * etc.
 70    */
 71   
 72  3096 public String allocateId(String name)
 73    {
 74  3096 String key = name + _namespace;
 75   
 76  3096 NameGenerator g = (NameGenerator) _generatorMap.get(key);
 77  3096 String result = null;
 78   
 79  3096 if (g == null)
 80    {
 81  2896 g = new NameGenerator(key);
 82  2896 result = key;
 83    }
 84    else
 85  200 result = g.nextId();
 86   
 87    // Handle the degenerate case, where a base name of the form "foo$0" has been
 88    // requested. Skip over any duplicates thus formed.
 89   
 90  3096 while (_generatorMap.containsKey(result))
 91  1 result = g.nextId();
 92   
 93  3096 _generatorMap.put(result, g);
 94   
 95  3096 return result;
 96    }
 97   
 98    /**
 99    * Clears the allocator, resetting it to freshly allocated state.
 100    */
 101   
 102  361 public void clear()
 103    {
 104  361 _generatorMap.clear();
 105    }
 106    }