Class SingletonMap.SingletonHolder<V>

  • Type Parameters:
    V - the singleton type
    Enclosing class:
    SingletonMap<K,​V,​E extends java.lang.Exception>

    private static class SingletonMap.SingletonHolder<V>
    extends java.lang.Object
    Wrapper to allow an object instance to be put into a ConcurrentHashMap using putIfAbsent() without requiring the instance to be initialized first, so that putIfAbsent can be performed without wrapping it with a synchronized lock, and so that initialization work is not wasted if an object is already in the map for the key.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.concurrent.CountDownLatch initialized
      Whether or not the singleton has been initialized (the count will have reached 0 if so).
      private V singleton
      The singleton.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private SingletonHolder()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) V get()
      Get the singleton value.
      (package private) void set​(V singleton)
      Set the singleton value, and decreases the countdown latch to 0.
      • Methods inherited from class java.lang.Object

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

      • singleton

        private volatile V singleton
        The singleton.
      • initialized

        private final java.util.concurrent.CountDownLatch initialized
        Whether or not the singleton has been initialized (the count will have reached 0 if so).
    • Constructor Detail

      • SingletonHolder

        private SingletonHolder()
    • Method Detail

      • set

        void set​(V singleton)
          throws java.lang.IllegalArgumentException
        Set the singleton value, and decreases the countdown latch to 0.
        Parameters:
        singleton - the singleton
        Throws:
        java.lang.IllegalArgumentException - if this method is called more than once (indicating an internal inconsistency).
      • get

        V get()
        throws java.lang.InterruptedException
        Get the singleton value.
        Returns:
        the singleton value.
        Throws:
        java.lang.InterruptedException - if the thread was interrupted while waiting for the value to be set.