Clover coverage report - Code Coverage for tapestry release 4.0-alpha-3
Coverage timestamp: Mon May 16 2005 09:05:49 EDT
file stats: LOC: 104   Methods: 6
NCLOC: 51   Classes: 2
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%
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 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  3236
         NameGenerator(String baseId)
 43   
         {
 44  3236
             _baseId = baseId + "$";
 45   
         }
 46   
 
 47  182
         public String nextId()
 48   
         {
 49  182
             return _baseId + _index++;
 50   
         }
 51   
     }
 52   
 
 53  980
     public IdAllocator()
 54   
     {
 55  980
         this("");
 56   
     }
 57   
 
 58  1083
     public IdAllocator(String namespace)
 59   
     {
 60  1083
         Defense.notNull(namespace, "namespace");
 61   
 
 62  1083
         _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  3417
     public String allocateId(String name)
 71   
     {
 72  3417
         String key = name + _namespace;
 73   
 
 74  3417
         NameGenerator g = (NameGenerator) _generatorMap.get(key);
 75  3417
         String result = null;
 76   
 
 77  3417
         if (g == null)
 78   
         {
 79  3236
             g = new NameGenerator(key);
 80  3236
             result = key;
 81   
         }
 82   
         else
 83  181
             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  3417
         while (_generatorMap.containsKey(result))
 89  1
             result = g.nextId();
 90   
 
 91  3417
         _generatorMap.put(result, g);
 92   
 
 93  3417
         return result;
 94   
     }
 95   
 
 96   
     /**
 97   
      * Clears the allocator, resetting it to freshly allocated state.
 98   
      */
 99   
 
 100  397
     public void clear()
 101   
     {
 102  397
         _generatorMap.clear();
 103   
     }
 104   
 }