Class SingletonMap<K,​V,​E extends java.lang.Exception>

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

    public abstract class SingletonMap<K,​V,​E extends java.lang.Exception>
    extends java.lang.Object
    A map from keys to singleton instances. Allows you to create object instance singletons and add them to a ConcurrentMap on demand, based on a key value. Works the same as concurrentMap.computeIfAbsent(key, key -> newInstance(key)), except that it also works on JDK 7.
    • Constructor Detail

      • SingletonMap

        public SingletonMap()
    • Method Detail

      • newInstance

        public abstract V newInstance​(K key,
                                      LogNode log)
                               throws E extends java.lang.Exception,
                                      java.lang.InterruptedException
        Construct a new singleton instance.
        Parameters:
        key - The key for the singleton.
        log - The log.
        Returns:
        The singleton instance. This method must either return a non-null value, or throw an exception of type E.
        Throws:
        E - If something goes wrong while instantiating the new object instance.
        java.lang.InterruptedException - if the thread was interrupted while instantiating the singleton.
        E extends java.lang.Exception
      • values

        public java.util.List<V> values()
                                 throws java.lang.InterruptedException
        Get all valid singleton values in the map.
        Returns:
        the singleton values in the map, skipping over any value for which newInstance() threw an exception or returned null.
        Throws:
        java.lang.InterruptedException - If getting the values was interrupted.
      • isEmpty

        public boolean isEmpty()
        Returns true if the map is empty.
        Returns:
        true, if the map is empty
      • entries

        public java.util.List<java.util.Map.Entry<K,​V>> entries()
                                                               throws java.lang.InterruptedException
        Get the map entries.
        Returns:
        the map entries.
        Throws:
        java.lang.InterruptedException - if interrupted.
      • remove

        public V remove​(K key)
                 throws java.lang.InterruptedException
        Remove the singleton for a given key.
        Parameters:
        key - the key
        Returns:
        the old singleton from the map, if one was present, otherwise null.
        Throws:
        java.lang.InterruptedException - if interrupted.
      • clear

        public void clear()
        Clear the map.