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 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 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

      private final Map<String,SoftReference<T>> stringCache
    • sizeLimit

      private int sizeLimit
    • maxStringLength

      private int maxStringLength
  • Constructor Details

    • StringCache

      public StringCache()
  • Method Details

    • process

      protected abstract T process(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(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(String input, T value)
      Associates a value to a string
      Parameters:
      input - the string to be associated with a given value
      value - the value associated with the given string
    • get

      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 - 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 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:
      the maximum length a String key can have
    • setMaxStringLength

      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 - the maximum length a String key can have