Class SingletonMap<K,​V>

  • Type Parameters:
    K - The key type.
    V - The value type.

    public abstract class SingletonMap<K,​V>
    extends Object
    A map from keys to singleton instances.
    • Constructor Detail

      • SingletonMap

        public SingletonMap()
    • Method Detail

      • createSingleton

        public boolean createSingleton​(K key,
                                       LogNode log)
                                throws Exception
        Initialize a value object for this key and return true, if this is the first time this key has been seen, otherwise return false.
        Parameters:
        key - The key for the singleton.
        log - The log.
        Returns:
        The singleton instance.
        Throws:
        Exception - if newInstance(key) throws an exception.
        IllegalArgumentException - if newInstance(key) returns null.
      • getOrCreateSingleton

        public V getOrCreateSingleton​(K key,
                                      LogNode log)
                               throws Exception
        Check if the given key is in the map, and if so, return it. If not, create a singleton value for that key, and return the created value.
        Parameters:
        key - The key for the singleton.
        log - The log.
        Returns:
        The singleton instance.
        Throws:
        Exception - if newInstance(key) throws an exception.
        IllegalArgumentException - if newInstance(key) returns null.
      • newInstance

        public abstract V newInstance​(K key,
                                      LogNode log)
                               throws Exception
        Construct a new singleton instance.
        Parameters:
        key - The key for the singleton.
        log - The log.
        Returns:
        The singleton instance.
        Throws:
        Exception - If something goes wrong.
      • get

        public V get​(K key)
              throws InterruptedException
        Get the singleton for a given key.
        Parameters:
        key - The key for the singleton.
        Returns:
        the new singleton instance, initialized by calling newInstance, or null if createSingleton() or getOrCreateSingleton() has not yet been called. Also returns null if newInstance() threw an exception or returned null while calling either of these methods.
        Throws:
        InterruptedException - If getting the singleton was interrupted.
      • clear

        public void clear()
        Clear the map.