Coverage Report - net.sf.beanform.util.StringCache
 
Classes in this File Line Coverage Branch Coverage Complexity
StringCache
100% 
100% 
0
 
 1  
 // Copyright 2006 Daniel Gredler
 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 net.sf.beanform.util;
 16  
 
 17  
 import java.util.HashMap;
 18  
 import java.util.Map;
 19  
 
 20  
 /**
 21  
  * A thread-safe cache of strings that allows aliasing of long strings to smaller
 22  
  * strings that can then be used to reverse the process.
 23  
  *
 24  
  * @author Daniel Gredler
 25  
  */
 26  
 public class StringCache {
 27  
 
 28  1
     protected final static char[] CHARS = {
 29  
         'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
 30  
         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
 31  
         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
 32  
 
 33  
     protected int maxSize;
 34  
     protected Map<String, String> cacheByShort;
 35  
     protected Map<String, String> cacheByReal;
 36  
 
 37  5
     public StringCache( int maxSize ) {
 38  5
         this.maxSize = maxSize;
 39  5
         this.cacheByShort = new HashMap<String, String>( maxSize );
 40  5
         this.cacheByReal = new HashMap<String, String>( maxSize );
 41  5
     }
 42  
 
 43  
     public synchronized String getShortVersion( String real ) {
 44  23
         if( this.cacheByReal.size() == this.maxSize ) return real;
 45  
         String s;
 46  21
         if( this.cacheByReal.containsKey( real ) ) {
 47  5
             s = this.cacheByReal.get( real );
 48  5
         }
 49  
         else {
 50  16
             s = Character.toString( CHARS[ 0 ] );
 51  38
             for( int i = 1; this.cacheByShort.containsKey( s ); i++ ) {
 52  22
                 s = getChars( CHARS, i );
 53  
             }
 54  16
             this.cacheByShort.put( s, real );
 55  16
             this.cacheByReal.put( real, s );
 56  
         }
 57  21
         return s;
 58  
     }
 59  
 
 60  
     public synchronized String getRealVersion( String shortVersion ) {
 61  
         String real;
 62  26
         if( this.cacheByShort.containsKey( shortVersion ) ) {
 63  21
             real = this.cacheByShort.get( shortVersion );
 64  21
         }
 65  
         else {
 66  5
             real = shortVersion;
 67  
         }
 68  26
         return real;
 69  
     }
 70  
 
 71  
     protected static String getChars( char[] chars, int index ) {
 72  90
         String s = new String();
 73  90
         int i = index / chars.length;
 74  90
         if( i > 0 ) {
 75  38
             s = getChars( chars, i - 1 ) + s;
 76  
         }
 77  90
         s = s + chars[ index % chars.length ];
 78  90
         return s;
 79  
     }
 80  
 
 81  
 }