Package com.univocity.parsers.common
Class StringCache<T>
- java.lang.Object
-
- com.univocity.parsers.common.StringCache<T>
-
- Type Parameters:
T- the type of entry to be stored in the cache
public abstract class StringCache<T> extends java.lang.ObjectA simple cache of values associated with strings. It is built to simply prevent generating the same value over and over again over a short period of time. Once its size limit is reached, the cache will be fully cleared. Do not use this as a general purpose caching solution. This meant for storing values that can be cheaply produced and re-generating them every now and then won't incur in any major performance impact.
-
-
Field Summary
Fields Modifier and Type Field Description private static intDEFAULT_MAX_STRING_LENGTHprivate static intDEFAULT_SIZE_LIMITprivate intmaxStringLengthprivate intsizeLimitprivate java.util.Map<java.lang.String,java.lang.ref.SoftReference<T>>stringCache
-
Constructor Summary
Constructors Constructor Description StringCache()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidclear()Removes all entries stored in this cache.booleancontainsKey(java.lang.String input)Tests whether the cache contains the given keyTget(java.lang.String input)Returns the value associated with the given string.intgetMaxStringLength()Returns the maximum length aStringkey can have to be used as a key in this cache.intgetSizeLimit()Returns the size limit of this string cache.protected abstract Tprocess(java.lang.String input)Converts a given string to a valuevoidput(java.lang.String input, T value)Associates a value to a stringvoidsetMaxStringLength(int maxStringLength)Returns the maximum length aStringkey can have to be used as a key in this cache.voidsetSizeLimit(int sizeLimit)Defines the size limit of this string cache (16,384 by default).
-
-
-
Field Detail
-
DEFAULT_SIZE_LIMIT
private static final int DEFAULT_SIZE_LIMIT
- See Also:
- Constant Field Values
-
DEFAULT_MAX_STRING_LENGTH
private static final int DEFAULT_MAX_STRING_LENGTH
- See Also:
- Constant Field Values
-
stringCache
private final java.util.Map<java.lang.String,java.lang.ref.SoftReference<T>> stringCache
-
sizeLimit
private int sizeLimit
-
maxStringLength
private int maxStringLength
-
-
Method Detail
-
process
protected abstract T process(java.lang.String input)
Converts a given string to a value- Parameters:
input- the input to be converted and stored in the cache- Returns:
- the value generated from the given string/
-
containsKey
public boolean containsKey(java.lang.String input)
Tests whether the cache contains the given key- Parameters:
input- a string that might have a value associated to it.- Returns:
trueif the cache contains (or contained) a value associated with the given key.
-
getSizeLimit
public int getSizeLimit()
Returns the size limit of this string cache. Defaults to 16,384. For simplicity, when this limit is reached, the entire cache is cleared.- Returns:
- the maximum number of entries that can be stored in this string cache.
-
setSizeLimit
public void setSizeLimit(int sizeLimit)
Defines the size limit of this string cache (16,384 by default). For simplicity, when this limit is reached, the entire cache is cleared.- Parameters:
sizeLimit- the maximum number of entries that can be stored in this string cache.
-
put
public void put(java.lang.String input, T value)Associates a value to a string- Parameters:
input- the string to be associated with a given valuevalue- the value associated with the given string
-
get
public T get(java.lang.String input)
Returns the value associated with the given string. If it doesn't exist, or if it has been evicted, a value will be populated usingprocess(String)- Parameters:
input- the string whose associated value will be returned- Returns:
- the value associated with the given string.
-
clear
public void clear()
Removes all entries stored in this cache.
-
getMaxStringLength
public int getMaxStringLength()
Returns the maximum length aStringkey can have to be used as a key in this cache. If theStringlength exceeds this limit, the value associated with it won't be cached. Defaults to 1024- Returns:
- the maximum length a
Stringkey can have
-
setMaxStringLength
public void setMaxStringLength(int maxStringLength)
Returns the maximum length aStringkey can have to be used as a key in this cache. If theStringlength exceeds this limit, the value associated with it won't be cached. Defaults to 1024- Parameters:
maxStringLength- the maximum length aStringkey can have
-
-