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.Object
A 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 int
DEFAULT_MAX_STRING_LENGTH
private static int
DEFAULT_SIZE_LIMIT
private int
maxStringLength
private int
sizeLimit
private 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 void
clear()
Removes all entries stored in this cache.boolean
containsKey(java.lang.String input)
Tests whether the cache contains the given keyT
get(java.lang.String input)
Returns the value associated with the given string.int
getMaxStringLength()
Returns the maximum length aString
key can have to be used as a key in this cache.int
getSizeLimit()
Returns the size limit of this string cache.protected abstract T
process(java.lang.String input)
Converts a given string to a valuevoid
put(java.lang.String input, T value)
Associates a value to a stringvoid
setMaxStringLength(int maxStringLength)
Returns the maximum length aString
key can have to be used as a key in this cache.void
setSizeLimit(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:
true
if 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 aString
key can have to be used as a key in this cache. If theString
length exceeds this limit, the value associated with it won't be cached. Defaults to 1024- Returns:
- the maximum length a
String
key can have
-
setMaxStringLength
public void setMaxStringLength(int maxStringLength)
Returns the maximum length aString
key can have to be used as a key in this cache. If theString
length exceeds this limit, the value associated with it won't be cached. Defaults to 1024- Parameters:
maxStringLength
- the maximum length aString
key can have
-
-