Top Description Fields Constructors Methods
com.univocity.parsers.common

public abstract Class StringCache<T>

extends Object
Class Inheritance
Type Parameters
<T>
the type of entry to be stored in the cache
Imports
java.lang.ref.*, java.util.*, java.util.concurrent.*

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

Modifier and TypeField and Description
private static final int
private static final int
private int
private int
private final Map<String, SoftReference<T>>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
clear()

Removes all entries stored in this cache.

public boolean

Returns:

true if the cache contains (or contained) a value associated with the given key.
containsKey
(String
a string that might have a value associated to it.
input
)

Tests whether the cache contains the given key

public T

Returns:

the value associated with the given string.
get
(String
the string whose associated value will be returned
input
)

Returns the value associated with the given string.

public int

Returns:

the maximum length a String key can have
getMaxStringLength
()

Returns the maximum length a String key can have to be used as a key in this cache.

public int

Returns:

the maximum number of entries that can be stored in this string cache.
getSizeLimit
()

Returns the size limit of this string cache.

protected abstract T

Returns:

the value generated from the given string/
process
(String
the input to be converted and stored in the cache
input
)

Converts a given string to a value

public void
put(String
the string to be associated with a given value
input
,
T
the value associated with the given string
value
)

Associates a value to a string

public void
setMaxStringLength(int
the maximum length a String key can have
maxStringLength
)

Returns the maximum length a String key can have to be used as a key in this cache.

public void
setSizeLimit(int
the maximum number of entries that can be stored in this string cache.
sizeLimit
)

Defines the size limit of this string cache (16,384 by default).

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULT_MAX_STRING_LENGTHback to summary
private static final int DEFAULT_MAX_STRING_LENGTH
DEFAULT_SIZE_LIMITback to summary
private static final int DEFAULT_SIZE_LIMIT
maxStringLengthback to summary
private int maxStringLength
sizeLimitback to summary
private int sizeLimit
stringCacheback to summary
private final Map<String, SoftReference<T>> stringCache

Constructor Detail

StringCacheback to summary
public StringCache()

Method Detail

clearback to summary
public void clear()

Removes all entries stored in this cache.

containsKeyback to summary
public boolean containsKey(String input)

Tests whether the cache contains the given key

Parameters
input:String

a string that might have a value associated to it.

Returns:boolean

true if the cache contains (or contained) a value associated with the given key.

getback to summary
public T get(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 using process(String)

Parameters
input:String

the string whose associated value will be returned

Returns:T

the value associated with the given string.

getMaxStringLengthback to summary
public int getMaxStringLength()

Returns the maximum length a String key can have to be used as a key in this cache. If the String length exceeds this limit, the value associated with it won't be cached. Defaults to 1024

Returns:int

the maximum length a String key can have

getSizeLimitback to summary
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:int

the maximum number of entries that can be stored in this string cache.

processback to summary
protected abstract T process(String input)

Converts a given string to a value

Parameters
input:String

the input to be converted and stored in the cache

Returns:T

the value generated from the given string/

putback to summary
public void put(String input, T value)

Associates a value to a string

Parameters
input:String

the string to be associated with a given value

value:T

the value associated with the given string

setMaxStringLengthback to summary
public void setMaxStringLength(int maxStringLength)

Returns the maximum length a String key can have to be used as a key in this cache. If the String length exceeds this limit, the value associated with it won't be cached. Defaults to 1024

Parameters
maxStringLength:int

the maximum length a String key can have

setSizeLimitback to summary
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:int

the maximum number of entries that can be stored in this string cache.