Class TypeCache<T>

java.lang.Object
jodd.util.TypeCache<T>

public class TypeCache<T> extends Object
Types cache. Provides several implementations depending on what you need to be addressed. There are two things you should take care off:
  • synchronization - especially on storing items. If not synchronized, one instance of an item may be put more then once into the map. This is usually fine, as it happens only during the initialization and makes not harm if something is created twice
  • weak - if your key classes are replaced during the runtime, you should use weak map, in order to automatically remove obsolete keys.
  • Field Details

  • Constructor Details

    • TypeCache

      private TypeCache(Map<Class<?>,T> backedMap)
  • Method Details

    • create

      public static <A> TypeCache.Builder<A> create()
      Creates a type cache by using a builder.
    • createDefault

      public static <A> TypeCache<A> createDefault()
      Creates default implementation of the type cache.
    • put

      public T put(Class<?> type, T value)
      Add values to the map.
    • get

      public T get(Class<?> key)
      Returns value from the map or null if value does not exist.
    • get

      public <K> T get(Class<K> key, Function<Class<K>, ? extends T> mappingFunction)
      Returns existing value or add default supplied one. Use this method instead of get-nullcheck-put block when thread-safety is of importance.
    • remove

      public T remove(Class<?> type)
      Removes element from the type cache.
    • clear

      public void clear()
      Clears complete cache.
    • size

      public int size()
      Returns cache size.
    • isEmpty

      public boolean isEmpty()
      Returns true if cache is empty.
    • forEachValue

      public void forEachValue(Consumer<? super T> valueConsumer)
      Iterates all cached values.