Module iu.util
Package edu.iu

Class IuCachedValue<V>

java.lang.Object
edu.iu.IuCachedValue<V>
Type Parameters:
V - value type

public class IuCachedValue<V> extends Object
Holds a single value by SoftReference with timed expiration.

A cached value could be cleared by the garbage collector in order to prevent OutOfMemoryError due to insufficient heap space.

  • Constructor Details

    • IuCachedValue

      public IuCachedValue(V value, Duration timeToLive, UnsafeRunnable onExpire)
      Constructor.
      Parameters:
      value - value
      timeToLive - maximum length of time for the cached value to remain valid
      onExpire - thunk to invoke when the reference expires; should execute quickly, i.e., to remove a map entry relative the provided key
  • Method Details

    • get

      public V get()
      Gets the cached value.
      Returns:
      cached value; may be null if the cached value is null, the reference was cleared by the garbage collector, or the expiration time is in the past.
    • isValid

      public boolean isValid()
      Determines whether or not the cached value is still valid.
      Returns:
      true if the reference is still valid; false if it has been cleared by the garbage collection or the expiration time is in the past.
    • has

      public boolean has(Object o)
      Determines if an object is equal to the referent.
      Parameters:
      o - object
      Returns:
      true if the reference is still valid and the referent is equal to the object.
    • clear

      public void clear()
      Invalidates the cached value, invokes the onExpire thunk, and clears all related references and resource.

      This method has no effect if invoked on an invalid reference.

    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object