Class IndirectMap<K,​V>

  • Type Parameters:
    K - the type of keys maintained by this map
    V - the type of mapped values
    All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.util.Map<K,​V>, ChangeTracker, CollectionChangeTracker, IndirectCollection, IndirectContainer

    public class IndirectMap<K,​V>
    extends java.util.Hashtable<K,​V>
    implements CollectionChangeTracker, IndirectCollection
    IndirectMap allows a domain class to take advantage of TopLink indirection without having to declare its instance variable as a ValueHolderInterface.

    To use an IndirectMap:

    • Declare the appropriate instance variable with type Map or Hashtable
    • Send the message #useTransparentMap(String) to the appropriate CollectionMapping.
    EclipseLink will place an IndirectMap in the instance variable when the containing domain object is read from the database. With the first message sent to the IndirectMap, the contents are fetched from the database and normal Hashtable/Map behavior is resumed.
    See Also:
    CollectionMapping, IndirectList, Serialized Form
    Author:
    Big Country
    Since:
    TOPLink/Java 2.5
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.Hashtable<K,​V> delegate
      Reduce type casting
      protected int initialCapacity
      Store initial size for lazy init.
      protected float loadFactor
      Store load factor for lazy init.
      protected ValueHolderInterface valueHolder
      Delegate indirection behavior to a value holder
    • Constructor Summary

      Constructors 
      Constructor Description
      IndirectMap()
      PUBLIC: Construct a new, empty IndirectMap with a default capacity and load factor.
      IndirectMap​(int initialCapacity)
      PUBLIC: Construct a new, empty IndirectMap with the specified initial capacity and default load factor.
      IndirectMap​(int initialCapacity, float loadFactor)
      PUBLIC: Construct a new, empty IndirectMap with the specified initial capacity and load factor.
      IndirectMap​(java.util.Map<? extends K,​? extends V> m)
      PUBLIC: Construct a new IndirectMap with the same mappings as the given Map.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.beans.PropertyChangeListener _persistence_getPropertyChangeListener()
      Return the property change listener for change tracking.
      void _persistence_setPropertyChangeListener​(java.beans.PropertyChangeListener changeListener)
      INTERNAL: Set the property change listener for change tracking.
      protected java.util.Hashtable<K,​V> buildDelegate()
      Return the freshly-built delegate.
      void clear()  
      void clearDeferredChanges()
      INTERNAL: clear any changes that have been deferred to instantiation.
      java.lang.Object clone()  
      V compute​(K key, java.util.function.BiFunction<? super K,​? super V,​? extends V> remappingFunction)  
      V computeIfAbsent​(K key, java.util.function.Function<? super K,​? extends V> mappingFunction)  
      V computeIfPresent​(K key, java.util.function.BiFunction<? super K,​? super V,​? extends V> remappingFunction)  
      boolean contains​(java.lang.Object value)  
      boolean containsKey​(java.lang.Object key)  
      boolean containsValue​(java.lang.Object value)  
      java.util.Enumeration<V> elements()  
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()  
      boolean equals​(java.lang.Object o)  
      void forEach​(java.util.function.BiConsumer<? super K,​? super V> action)  
      V get​(java.lang.Object key)  
      java.util.Collection<java.util.Map.Entry<K,​V>> getAddedElements()
      INTERNAL: Return the elements that have been added before instantiation.
      protected java.util.Hashtable<K,​V> getDelegate()
      INTERNAL: Check whether the contents have been read from the database.
      java.lang.Object getDelegateObject()
      INTERNAL: Return the real collection object.
      V getOrDefault​(java.lang.Object key, V defaultValue)  
      java.util.Collection<java.util.Map.Entry<K,​V>> getRemovedElements()
      INTERNAL: Return the elements that have been removed before instantiation.
      java.lang.String getTrackedAttributeName()
      INTERNAL: Return the mapping attribute name, used to raise change events.
      ValueHolderInterface getValueHolder()
      PUBLIC: Return the valueHolder.
      boolean hasDeferredChanges()
      INTERNAL: Return if any elements that have been added or removed before instantiation.
      int hashCode()  
      boolean hasTrackedPropertyChangeListener()
      INTERNAL: Return if the collection has a property change listener for change tracking.
      protected void initialize​(int initialCapacity, float loadFactor)
      Initialize the instance.
      protected void initialize​(java.util.Map<? extends K,​? extends V> m)
      Initialize the instance.
      boolean isEmpty()  
      boolean isInstantiated()
      PUBLIC: Return whether the contents have been read from the database.
      java.util.Enumeration<K> keys()  
      java.util.Set<K> keySet()  
      V merge​(K key, V value, java.util.function.BiFunction<? super V,​? super V,​? extends V> remappingFunction)  
      V put​(K key, V value)  
      void putAll​(java.util.Map<? extends K,​? extends V> t)  
      V putIfAbsent​(K key, V value)  
      protected void raiseAddChangeEvent​(java.lang.Object key, java.lang.Object value)
      Raise the add change event and relationship maintainence.
      protected void raiseRemoveChangeEvent​(java.lang.Object key, java.lang.Object value)
      Raise the remove change event.
      protected void rehash()  
      V remove​(java.lang.Object key)  
      boolean remove​(java.lang.Object key, java.lang.Object value)  
      V replace​(K key, V value)  
      boolean replace​(K key, V oldValue, V newValue)  
      void replaceAll​(java.util.function.BiFunction<? super K,​? super V,​? extends V> function)  
      void setTrackedAttributeName​(java.lang.String attributeName)
      INTERNAL: Set the mapping attribute name, used to raise change events.
      void setUseLazyInstantiation​(boolean useLazyInstantiation)
      INTERNAL Set whether this collection should attempt do deal with adds and removes without retrieving the collection from the dB
      void setValueHolder​(ValueHolderInterface valueHolder)
      INTERNAL: Set the value holder.
      int size()  
      java.lang.String toString()
      PUBLIC: Use the Hashtable.toString(); but wrap it with braces to indicate there is a bit of indirection.
      java.util.Collection<V> values()  
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • delegate

        protected volatile java.util.Hashtable<K,​V> delegate
        Reduce type casting
      • valueHolder

        protected volatile ValueHolderInterface valueHolder
        Delegate indirection behavior to a value holder
      • initialCapacity

        protected int initialCapacity
        Store initial size for lazy init.
      • loadFactor

        protected float loadFactor
        Store load factor for lazy init.
    • Constructor Detail

      • IndirectMap

        public IndirectMap()
        PUBLIC: Construct a new, empty IndirectMap with a default capacity and load factor.
      • IndirectMap

        public IndirectMap​(int initialCapacity)
        PUBLIC: Construct a new, empty IndirectMap with the specified initial capacity and default load factor.
        Parameters:
        initialCapacity - the initial capacity of the hashtable
      • IndirectMap

        public IndirectMap​(int initialCapacity,
                           float loadFactor)
        PUBLIC: Construct a new, empty IndirectMap with the specified initial capacity and load factor.
        Parameters:
        initialCapacity - the initial capacity of the hashtable
        loadFactor - a number between 0.0 and 1.0
        Throws:
        java.lang.IllegalArgumentException - if the initial capacity is less than or equal to zero, or if the load factor is less than or equal to zero
      • IndirectMap

        public IndirectMap​(java.util.Map<? extends K,​? extends V> m)
        PUBLIC: Construct a new IndirectMap with the same mappings as the given Map. The IndirectMap is created with a capacity of twice the number of entries in the given Map or 11 (whichever is greater), and a default load factor, which is 0.75.
        Parameters:
        m - a map containing the mappings to use
    • Method Detail

      • buildDelegate

        protected java.util.Hashtable<K,​V> buildDelegate()
        Return the freshly-built delegate.
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Map<K,​V>
        Overrides:
        clear in class java.util.Hashtable<K,​V>
        See Also:
        Hashtable.clear()
      • clearDeferredChanges

        public void clearDeferredChanges()
        INTERNAL: clear any changes that have been deferred to instantiation. Indirect collections with change tracking avoid instantiation on add/remove.
        Specified by:
        clearDeferredChanges in interface IndirectCollection
      • clone

        public java.lang.Object clone()
        Overrides:
        clone in class java.util.Hashtable<K,​V>
        See Also:
        This will result in a database query if necessary.
      • contains

        public boolean contains​(java.lang.Object value)
        Overrides:
        contains in class java.util.Hashtable<K,​V>
        See Also:
        Hashtable.contains(java.lang.Object)
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Specified by:
        containsKey in interface java.util.Map<K,​V>
        Overrides:
        containsKey in class java.util.Hashtable<K,​V>
        See Also:
        Hashtable.containsKey(java.lang.Object)
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Specified by:
        containsValue in interface java.util.Map<K,​V>
        Overrides:
        containsValue in class java.util.Hashtable<K,​V>
        See Also:
        Hashtable.containsValue(java.lang.Object)
      • elements

        public java.util.Enumeration<V> elements()
        Overrides:
        elements in class java.util.Hashtable<K,​V>
        See Also:
        Hashtable.elements()
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Specified by:
        entrySet in interface java.util.Map<K,​V>
        Overrides:
        entrySet in class java.util.Hashtable<K,​V>
        See Also:
        Hashtable.entrySet()
      • equals

        public boolean equals​(java.lang.Object o)
        Specified by:
        equals in interface java.util.Map<K,​V>
        Overrides:
        equals in class java.util.Hashtable<K,​V>
        See Also:
        Hashtable.equals(java.lang.Object)
      • get

        public V get​(java.lang.Object key)
        Specified by:
        get in interface java.util.Map<K,​V>
        Overrides:
        get in class java.util.Hashtable<K,​V>
        See Also:
        Hashtable.get(java.lang.Object)
      • getDelegate

        protected java.util.Hashtable<K,​V> getDelegate()
        INTERNAL: Check whether the contents have been read from the database. If they have not, read them and set the delegate. This method used to be synchronized, which caused deadlock.
      • getDelegateObject

        public java.lang.Object getDelegateObject()
        INTERNAL: Return the real collection object. This will force instantiation.
        Specified by:
        getDelegateObject in interface IndirectCollection
      • _persistence_getPropertyChangeListener

        public java.beans.PropertyChangeListener _persistence_getPropertyChangeListener()
        Return the property change listener for change tracking.
        Specified by:
        _persistence_getPropertyChangeListener in interface ChangeTracker
      • getValueHolder

        public ValueHolderInterface getValueHolder()
        PUBLIC: Return the valueHolder. This method used to be synchronized, which caused deadlock.
        Specified by:
        getValueHolder in interface IndirectContainer
        Returns:
        org.eclipse.persistence.indirection.ValueHolderInterface A representation of the valueholder * which this container uses
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Map<K,​V>
        Overrides:
        hashCode in class java.util.Hashtable<K,​V>
        See Also:
        Hashtable.hashCode()
      • hasTrackedPropertyChangeListener

        public boolean hasTrackedPropertyChangeListener()
        INTERNAL: Return if the collection has a property change listener for change tracking.
      • initialize

        protected void initialize​(int initialCapacity,
                                  float loadFactor)
        Initialize the instance.
      • initialize

        protected void initialize​(java.util.Map<? extends K,​? extends V> m)
        Initialize the instance.
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Map<K,​V>
        Overrides:
        isEmpty in class java.util.Hashtable<K,​V>
        See Also:
        Hashtable.isEmpty()
      • isInstantiated

        public boolean isInstantiated()
        PUBLIC: Return whether the contents have been read from the database.
        Specified by:
        isInstantiated in interface IndirectContainer
      • keys

        public java.util.Enumeration<K> keys()
        Overrides:
        keys in class java.util.Hashtable<K,​V>
        See Also:
        Hashtable.keys()
      • keySet

        public java.util.Set<K> keySet()
        Specified by:
        keySet in interface java.util.Map<K,​V>
        Overrides:
        keySet in class java.util.Hashtable<K,​V>
        See Also:
        Hashtable.keySet()
      • put

        public V put​(K key,
                     V value)
        Specified by:
        put in interface java.util.Map<K,​V>
        Overrides:
        put in class java.util.Hashtable<K,​V>
        See Also:
        Hashtable.put(java.lang.Object, java.lang.Object)
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends V> t)
        Specified by:
        putAll in interface java.util.Map<K,​V>
        Overrides:
        putAll in class java.util.Hashtable<K,​V>
        See Also:
        Hashtable.putAll(java.util.Map)
      • compute

        public V compute​(K key,
                         java.util.function.BiFunction<? super K,​? super V,​? extends V> remappingFunction)
        Specified by:
        compute in interface java.util.Map<K,​V>
        Overrides:
        compute in class java.util.Hashtable<K,​V>
      • computeIfAbsent

        public V computeIfAbsent​(K key,
                                 java.util.function.Function<? super K,​? extends V> mappingFunction)
        Specified by:
        computeIfAbsent in interface java.util.Map<K,​V>
        Overrides:
        computeIfAbsent in class java.util.Hashtable<K,​V>
      • computeIfPresent

        public V computeIfPresent​(K key,
                                  java.util.function.BiFunction<? super K,​? super V,​? extends V> remappingFunction)
        Specified by:
        computeIfPresent in interface java.util.Map<K,​V>
        Overrides:
        computeIfPresent in class java.util.Hashtable<K,​V>
      • forEach

        public void forEach​(java.util.function.BiConsumer<? super K,​? super V> action)
        Specified by:
        forEach in interface java.util.Map<K,​V>
        Overrides:
        forEach in class java.util.Hashtable<K,​V>
      • getOrDefault

        public V getOrDefault​(java.lang.Object key,
                              V defaultValue)
        Specified by:
        getOrDefault in interface java.util.Map<K,​V>
        Overrides:
        getOrDefault in class java.util.Hashtable<K,​V>
      • merge

        public V merge​(K key,
                       V value,
                       java.util.function.BiFunction<? super V,​? super V,​? extends V> remappingFunction)
        Specified by:
        merge in interface java.util.Map<K,​V>
        Overrides:
        merge in class java.util.Hashtable<K,​V>
      • putIfAbsent

        public V putIfAbsent​(K key,
                             V value)
        Specified by:
        putIfAbsent in interface java.util.Map<K,​V>
        Overrides:
        putIfAbsent in class java.util.Hashtable<K,​V>
      • remove

        public boolean remove​(java.lang.Object key,
                              java.lang.Object value)
        Specified by:
        remove in interface java.util.Map<K,​V>
        Overrides:
        remove in class java.util.Hashtable<K,​V>
      • replace

        public V replace​(K key,
                         V value)
        Specified by:
        replace in interface java.util.Map<K,​V>
        Overrides:
        replace in class java.util.Hashtable<K,​V>
      • replace

        public boolean replace​(K key,
                               V oldValue,
                               V newValue)
        Specified by:
        replace in interface java.util.Map<K,​V>
        Overrides:
        replace in class java.util.Hashtable<K,​V>
      • replaceAll

        public void replaceAll​(java.util.function.BiFunction<? super K,​? super V,​? extends V> function)
        Specified by:
        replaceAll in interface java.util.Map<K,​V>
        Overrides:
        replaceAll in class java.util.Hashtable<K,​V>
      • rehash

        protected void rehash()
        Overrides:
        rehash in class java.util.Hashtable<K,​V>
        See Also:
        Hashtable.rehash()
      • raiseAddChangeEvent

        protected void raiseAddChangeEvent​(java.lang.Object key,
                                           java.lang.Object value)
        Raise the add change event and relationship maintainence.
      • raiseRemoveChangeEvent

        protected void raiseRemoveChangeEvent​(java.lang.Object key,
                                              java.lang.Object value)
        Raise the remove change event.
      • remove

        public V remove​(java.lang.Object key)
        Specified by:
        remove in interface java.util.Map<K,​V>
        Overrides:
        remove in class java.util.Hashtable<K,​V>
        See Also:
        Hashtable.remove(java.lang.Object)
      • setTrackedAttributeName

        public void setTrackedAttributeName​(java.lang.String attributeName)
        INTERNAL: Set the mapping attribute name, used to raise change events. This is required if the change listener is set.
        Specified by:
        setTrackedAttributeName in interface CollectionChangeTracker
      • _persistence_setPropertyChangeListener

        public void _persistence_setPropertyChangeListener​(java.beans.PropertyChangeListener changeListener)
        INTERNAL: Set the property change listener for change tracking.
        Specified by:
        _persistence_setPropertyChangeListener in interface ChangeTracker
      • size

        public int size()
        Specified by:
        size in interface java.util.Map<K,​V>
        Overrides:
        size in class java.util.Hashtable<K,​V>
        See Also:
        Hashtable.size()
      • setUseLazyInstantiation

        public void setUseLazyInstantiation​(boolean useLazyInstantiation)
        INTERNAL Set whether this collection should attempt do deal with adds and removes without retrieving the collection from the dB
        Specified by:
        setUseLazyInstantiation in interface IndirectCollection
      • getRemovedElements

        public java.util.Collection<java.util.Map.Entry<K,​V>> getRemovedElements()
        INTERNAL: Return the elements that have been removed before instantiation.
        Specified by:
        getRemovedElements in interface IndirectCollection
      • getAddedElements

        public java.util.Collection<java.util.Map.Entry<K,​V>> getAddedElements()
        INTERNAL: Return the elements that have been added before instantiation.
        Specified by:
        getAddedElements in interface IndirectCollection
      • hasDeferredChanges

        public boolean hasDeferredChanges()
        INTERNAL: Return if any elements that have been added or removed before instantiation.
        Specified by:
        hasDeferredChanges in interface IndirectCollection
      • toString

        public java.lang.String toString()
        PUBLIC: Use the Hashtable.toString(); but wrap it with braces to indicate there is a bit of indirection. Don't allow this method to trigger a database read.
        Overrides:
        toString in class java.util.Hashtable<K,​V>
        See Also:
        Hashtable.toString()
      • values

        public java.util.Collection<V> values()
        Specified by:
        values in interface java.util.Map<K,​V>
        Overrides:
        values in class java.util.Hashtable<K,​V>
        See Also:
        Hashtable.values()