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
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
FieldsModifier and TypeFieldDescriptionprivate static final int
private static final int
private int
private int
private final Map<String,
SoftReference<T>> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Removes all entries stored in this cache.boolean
containsKey
(String input) Tests whether the cache contains the given keyReturns the value associated with the given string.int
Returns the maximum length aString
key can have to be used as a key in this cache.int
Returns the size limit of this string cache.protected abstract T
Converts a given string to a valuevoid
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 Details
-
DEFAULT_SIZE_LIMIT
private static final int DEFAULT_SIZE_LIMIT- See Also:
-
DEFAULT_MAX_STRING_LENGTH
private static final int DEFAULT_MAX_STRING_LENGTH- See Also:
-
stringCache
-
sizeLimit
private int sizeLimit -
maxStringLength
private int maxStringLength
-
-
Constructor Details
-
StringCache
public StringCache()
-
-
Method Details
-
process
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
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
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
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
-