Class AttributeHolder

  • All Implemented Interfaces:

    
    public class AttributeHolder
    
                        

    App level, default ttl=12H, unbounded thread safe cache. Need manually register and remove (or publish AttributeRidEvent)

    Since:

    2021-03-30

    Author:

    trydofor

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
      public final static int TtlDefault
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      static <K, V> AttributeCache<K, V> getCache(@NotNull() TypedReg<K, V> reg)
      static Set<TypedReg<out Object, out Object>> holders()
      static <K, V> void regLoader(@NotNull() TypedReg<K, V> reg, @NotNull() Function<K, V> loader) Registering a typed key-value and its loader
      static <K, V> void putAttr(@NotNull() TypedReg<K, V> reg, @NotNull() K key, @NotNull() V value) Put an attribute value to the typed key.
      static <K, V> void putAttr(@NotNull() TypedReg<K, V> reg, @NotNull() K key, @NotNull() V value, int ttl) Put an attribute value to the typed key.
      static <K, V> void putAttrs(@NotNull() TypedReg<K, V> reg, @NotNull() Map<K, V> map) Put all attribute value from map to the typed key.
      static <K, V> void putAttrs(@NotNull() TypedReg<K, V> reg, @NotNull() Map<K, V> map, int ttl) Put all attribute value from map to the typed key.
      static <K, V> V tryAttr(@NotNull() TypedReg<K, V> reg, @NotNull() K key, V elze) Try to get an attribute by typed key, load it if not found, return `elze` if the result is null.
      static <K, V> V tryAttr(@NotNull() TypedReg<K, V> reg, @NotNull() K key, V elze, int ttl) Try to get an attribute by typed key, load it if not found, return `elze` if the result is null.
      static <K, V> V tryAttr(@NotNull() TypedReg<K, V> reg, @NotNull() K key) Try to get an attribute by typed key, load it if not found, throw NPE if the result is null.
      static <K, V> V tryAttr(@NotNull() TypedReg<K, V> reg, @NotNull() K key, int ttl) Try to get an attribute by typed key, load it if not found, throw NPE if the result is null.
      static <K, V> V tryAttr(@NotNull() TypedReg<K, V> reg, @NotNull() K key, boolean notnull) Try to get an attribute by typed key, load it if not found, throw NPE if notnull and the result is null.
      static <K, V> V tryAttr(@NotNull() TypedReg<K, V> reg, @NotNull() K key, boolean notnull, int ttl) Try to get an attribute by typed key, load it if not found, throw NPE if notnull and the result is null.
      static <K, V> V getAttr(@NotNull() TypedReg<K, V> reg, @NotNull() K key) Get an attribute by typed key, and NOT load if not found.
      static <K, V> Map<K, V> getAttrs(@NotNull() TypedReg<K, V> reg, @NotNull() Collection<K> key) Get all attributes by typed keys, and NOT load if not found.
      static <K> void ridAttr(TypedReg<K, out Object> reg, K key) remove an attribute by key
      static <K> void ridAttrs(TypedReg<K, out Object> reg, Collection<out K> key) remove all attribute by keys
      static void ridAttrAll(Array<TypedReg<out Object, out Object>> reg) remove all attribute of type
      static void ridAttrAll(Collection<out TypedReg<out Object, out Object>> reg) remove all attribute of type
      static void ridLoader(Array<TypedReg<out Object, out Object>> reg) remove the loader
      static void ridLoader(Collection<out TypedReg<out Object, out Object>> reg) remove the loader
      static void unregister(Array<TypedReg<out Object, out Object>> reg) unregister from the event
      • Methods inherited from class java.lang.Object

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

      • AttributeHolder

        AttributeHolder()
    • Method Detail

      • regLoader

         static <K, V> void regLoader(@NotNull() TypedReg<K, V> reg, @NotNull() Function<K, V> loader)

        Registering a typed key-value and its loader

        Parameters:
        reg - Type to register
        loader - returns `null` is not cached and is called every time, so it is recommended to return `nonnull`.
      • putAttr

         static <K, V> void putAttr(@NotNull() TypedReg<K, V> reg, @NotNull() K key, @NotNull() V value)

        Put an attribute value to the typed key.

        Parameters:
        reg - Type to register
        key - unique key, e.g.
        value - value
      • putAttr

         static <K, V> void putAttr(@NotNull() TypedReg<K, V> reg, @NotNull() K key, @NotNull() V value, int ttl)

        Put an attribute value to the typed key.

        Parameters:
        reg - Type to register
        key - unique key, e.g.
        value - value
        ttl - ttl in second
      • putAttrs

         static <K, V> void putAttrs(@NotNull() TypedReg<K, V> reg, @NotNull() Map<K, V> map)

        Put all attribute value from map to the typed key.

        Parameters:
        reg - Type to register
        map - map of attribute
      • putAttrs

         static <K, V> void putAttrs(@NotNull() TypedReg<K, V> reg, @NotNull() Map<K, V> map, int ttl)

        Put all attribute value from map to the typed key.

        Parameters:
        reg - Type to register
        map - map of attribute
        ttl - ttl in second
      • tryAttr

        @Contract(value = "_,_,!null->!null") static <K, V> V tryAttr(@NotNull() TypedReg<K, V> reg, @NotNull() K key, V elze)

        Try to get an attribute by typed key, load it if not found, return `elze` if the result is null.

        Parameters:
        reg - Type to register
        key - unique key, e.g.
        elze - return `elze` if result is null
      • tryAttr

        @Contract(value = "_,_,!null,_->!null") static <K, V> V tryAttr(@NotNull() TypedReg<K, V> reg, @NotNull() K key, V elze, int ttl)

        Try to get an attribute by typed key, load it if not found, return `elze` if the result is null.

        Parameters:
        reg - Type to register
        key - unique key, e.g.
        elze - return `elze` if result is null
        ttl - ttl in second
      • tryAttr

        @NotNull() static <K, V> V tryAttr(@NotNull() TypedReg<K, V> reg, @NotNull() K key)

        Try to get an attribute by typed key, load it if not found, throw NPE if the result is null.

        Parameters:
        reg - Type to register
        key - unique key, e.g.
      • tryAttr

        @NotNull() static <K, V> V tryAttr(@NotNull() TypedReg<K, V> reg, @NotNull() K key, int ttl)

        Try to get an attribute by typed key, load it if not found, throw NPE if the result is null.

        Parameters:
        reg - Type to register
        key - unique key, e.g.
        ttl - ttl in second
      • tryAttr

        @Contract(value = "_,_,true ->!null") static <K, V> V tryAttr(@NotNull() TypedReg<K, V> reg, @NotNull() K key, boolean notnull)

        Try to get an attribute by typed key, load it if not found, throw NPE if notnull and the result is null.

        Parameters:
        reg - Type to register
        key - unique key, e.g.
        notnull - whether notnull
      • tryAttr

        @Contract(value = "_,_,true,_ ->!null") static <K, V> V tryAttr(@NotNull() TypedReg<K, V> reg, @NotNull() K key, boolean notnull, int ttl)

        Try to get an attribute by typed key, load it if not found, throw NPE if notnull and the result is null.

        Parameters:
        reg - Type to register
        key - unique key, e.g.
        notnull - whether notnull
        ttl - ttl in second
      • getAttr

        @Nullable() static <K, V> V getAttr(@NotNull() TypedReg<K, V> reg, @NotNull() K key)

        Get an attribute by typed key, and NOT load if not found.

        Parameters:
        reg - Type to register
        key - unique key, e.g.
      • getAttrs

        @NotNull() static <K, V> Map<K, V> getAttrs(@NotNull() TypedReg<K, V> reg, @NotNull() Collection<K> key)

        Get all attributes by typed keys, and NOT load if not found.

        Parameters:
        reg - Type to register
        key - unique key, e.g.
      • ridAttr

         static <K> void ridAttr(TypedReg<K, out Object> reg, K key)

        remove an attribute by key

        Parameters:
        reg - Type to register
        key - unique key, e.g.
      • ridAttrs

         static <K> void ridAttrs(TypedReg<K, out Object> reg, Collection<out K> key)

        remove all attribute by keys

        Parameters:
        reg - Type to register
        key - unique key, e.g.
      • ridAttrAll

         static void ridAttrAll(Array<TypedReg<out Object, out Object>> reg)

        remove all attribute of type

        Parameters:
        reg - Type to register
      • ridLoader

         static void ridLoader(Array<TypedReg<out Object, out Object>> reg)

        remove the loader

        Parameters:
        reg - Type to register