Skip to content

Commit

Permalink
fixed github issue #326
Browse files Browse the repository at this point in the history
  • Loading branch information
jbax committed May 14, 2019
1 parent 0976134 commit 43bed51
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions src/main/java/com/univocity/parsers/common/StringCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,82 @@
import java.util.concurrent.*;

/**
* A simple cache of values associated with strings.
* 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.
*
* @param <T> the type of entry to be stored in the cache
*/
public abstract class StringCache<T> {

private static final int DEFAULT_SIZE_LIMIT = 16384;
private final Map<String, SoftReference<T>> stringCache = new ConcurrentHashMap<String, SoftReference<T>>();
private int sizeLimit = DEFAULT_SIZE_LIMIT;

/**
* Converts a given string to a value
*
* @param input the input to be converted and stored in the cache
* @return the value generated from the given string/
*/
protected abstract T process(String input);

/**
* Tests whether the cache contains the given key
*
* @param input a string that might have a value associated to it.
* @return {@code true} if the cache contains (or contained) a value associated with the given key.
*/
public boolean containsKey(String input){
public boolean containsKey(String input) {
return stringCache.containsKey(input);
}

/**
* Returns the size limit of this string cache. Defaults to 16,384. For simplicity, when
* this limit is reached, the entire cache is cleared.
*
* @return the maximum number of entries that can be stored in this string cache.
*/
public int getSizeLimit() {
return 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.
*
* @param sizeLimit the maximum number of entries that can be stored in this string cache.
*/
public void setSizeLimit(int sizeLimit) {
if (sizeLimit <= 0) {
sizeLimit = DEFAULT_SIZE_LIMIT;
}
this.sizeLimit = sizeLimit;
}

/**
* Associates a value to a string
*
* @param input the string to be associated with a given value
* @param value the value associated with the given string
*/
public void put(String input, T value) {
if (stringCache.size() >= sizeLimit) {
stringCache.clear();
}
stringCache.put(input, new SoftReference<T>(value));
}

/**
* 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 {@link #process(String)}
*
* @param input the string whose associated value will be returned
* @return the value associated with the given string.
*/
public T get(String input) {
if(input == null){
if (input == null) {
return null;
}
SoftReference<T> ref = stringCache.get(input);
Expand All @@ -73,4 +109,8 @@ public T get(String input) {
}
return out;
}

public void clear() {
stringCache.clear();
}
}

0 comments on commit 43bed51

Please sign in to comment.