com.devexperts.util
Class StringCache

java.lang.Object
  extended by com.devexperts.util.StringCache

public class StringCache
extends Object

Caches strings and provides lookup methods by raw character data to avoid string construction. It is intended to be used in various parsers to reduce memory footprint and garbage.

The StringCache is a N-way set associative cache which distributes all strings into buckets based on hash function and then uses LRU algorithm within each bucket. The StringCache is a thread-safe, asynchronous, wait-free data structure.


Constructor Summary
StringCache()
          Creates a StringCache with default parameters.
StringCache(int size)
          Creates a StringCache with the specified number of elements and default bucket size.
StringCache(int bucketNumber, int bucketSize)
          Creates a StringCache with the specified number of buckets and their size.
 
Method Summary
 String get(char[] c)
          Returns string from the cache that matches specified character sequence.
 String get(char[] c, int offset, int length)
          Returns string from the cache that matches specified character sequence.
 String get(CharSequence cs)
          Returns string from the cache that matches specified character sequence.
 String get(String s)
          Returns string from the cache that matches specified string.
 String get(String s, boolean copy)
          Returns string from the cache that matches specified string.
 String getASCII(byte[] b)
          Returns string from the cache that matches specified character sequence.
 String getASCII(byte[] b, int offset, int length)
          Returns string from the cache that matches specified character sequence.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

StringCache

public StringCache()
Creates a StringCache with default parameters.


StringCache

public StringCache(int size)
Creates a StringCache with the specified number of elements and default bucket size.

Parameters:
size - the number of elements
Throws:
IllegalArgumentException - if size is not positive or result in too large cache

StringCache

public StringCache(int bucketNumber,
                   int bucketSize)
Creates a StringCache with the specified number of buckets and their size.

NOTE: cache uses linear search within each bucket, so do not use very large bucket sizes.

Parameters:
bucketNumber - the number of buckets
bucketSize - the size of each bucket
Throws:
IllegalArgumentException - if parameters are not positive or result in too large cache
Method Detail

get

public String get(String s)
Returns string from the cache that matches specified string.


get

public String get(String s,
                  boolean copy)
Returns string from the cache that matches specified string. If copy parameter is true then specified string will be copied before being put to cache.


get

public String get(CharSequence cs)
Returns string from the cache that matches specified character sequence.


get

public String get(char[] c)
Returns string from the cache that matches specified character sequence.


get

public String get(char[] c,
                  int offset,
                  int length)
Returns string from the cache that matches specified character sequence.


getASCII

public String getASCII(byte[] b)
Returns string from the cache that matches specified character sequence. This method uses only 7 lowest bits (ASCII range) of each character ignoring higher bits.


getASCII

public String getASCII(byte[] b,
                       int offset,
                       int length)
Returns string from the cache that matches specified character sequence. This method uses only 7 lowest bits (ASCII range) of each character ignoring higher bits.


toString

public String toString()
Overrides:
toString in class Object


Copyright © 2013 Devexperts. All Rights Reserved.