Class NestedCache<T>

java.lang.Object
org.opentrafficsim.core.gtu.NestedCache<T>
Type Parameters:
T - value type

public class NestedCache<T>
extends java.lang.Object
Utility class to cache data based on a variable (between cache instances) number of keys of any type. This replaces nested Maps.

Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See OpenTrafficSim License.

Version:
$Revision$, $LastChangedDate$, by $Author$, initial version 20 apr. 2018
Author:
Alexander Verbraeck, Peter Knoppers, Wouter Schakel
  • Constructor Summary

    Constructors 
    Constructor Description
    NestedCache​(java.lang.Class<?>... types)
    Constructor.
  • Method Summary

    Modifier and Type Method Description
    java.lang.Object clear​(java.lang.Object key)
    Clears the cache for the given key.
    NestedCache<T> getChild​(java.lang.Object key)
    Return branch for key.
    java.util.Set<java.lang.Object> getKeys()
    Return set of key objects on this level.
    T getValue​(java.lang.Object key)
    Return value for key.
    T getValue​(java.util.function.Supplier<T> supplier, java.lang.Object... keys)
    Returns a value.
    java.lang.String toString()

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • NestedCache

      public NestedCache​(java.lang.Class<?>... types)
      Constructor.
      Parameters:
      types - Class<?>...; types
  • Method Details

    • getValue

      public T getValue​(java.util.function.Supplier<T> supplier, java.lang.Object... keys)
      Returns a value.
      Parameters:
      supplier - Supplier<T>; supplier of T for if it wasn't cached yet
      keys - Object...; list of key objects
      Returns:
      T; value
    • getKeys

      public java.util.Set<java.lang.Object> getKeys()
      Return set of key objects on this level.
      Returns:
      Set; set of key objects on this level
    • getChild

      public NestedCache<T> getChild​(java.lang.Object key) throws java.lang.IllegalStateException
      Return branch for key.
      Parameters:
      key - Object; key
      Returns:
      NestedCache; branch for key
      Throws:
      java.lang.IllegalStateException - if this is not a branch level
    • getValue

      public T getValue​(java.lang.Object key) throws java.lang.IllegalStateException
      Return value for key.
      Parameters:
      key - Object; key
      Returns:
      T; value for key
      Throws:
      java.lang.IllegalStateException - if this is not a leaf level
    • clear

      public java.lang.Object clear​(java.lang.Object key)
      Clears the cache for the given key.
      Parameters:
      key - Object; key to clear the cache for
      Returns:
      Object; object that was previously cached to the key, or null if it was not cached.
    • toString

      public java.lang.String toString()
      Overrides:
      toString in class java.lang.Object