Package jodd.util

Class TypeCache<T>


  • public class TypeCache<T>
    extends java.lang.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.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  TypeCache.Builder<A>  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clears complete cache.
      static <A> TypeCache.Builder<A> create()
      Creates a type cache by using a builder.
      static <A> TypeCache<A> createDefault()
      Creates default implementation of the type cache.
      void forEachValue​(java.util.function.Consumer<? super T> valueConsumer)
      Iterates all cached values.
      T get​(java.lang.Class<?> key)
      Returns value from the map or null if value does not exist.
      <K> T get​(java.lang.Class<K> key, java.util.function.Function<java.lang.Class<K>,​? extends T> mappingFunction)
      Returns existing value or add default supplied one.
      boolean isEmpty()
      Returns true if cache is empty.
      T put​(java.lang.Class<?> type, T value)
      Add values to the map.
      T remove​(java.lang.Class<?> type)
      Removes element from the type cache.
      int size()
      Returns cache size.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • 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​(java.lang.Class<?> type,
                     T value)
        Add values to the map.
      • get

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

        public <K> T get​(java.lang.Class<K> key,
                         java.util.function.Function<java.lang.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​(java.lang.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​(java.util.function.Consumer<? super T> valueConsumer)
        Iterates all cached values.