Class WrapperObservableMap<K,​V>

  • All Implemented Interfaces:
    ObservableMap<K,​V>, Map<K,​V>
    Direct Known Subclasses:
    ObservableSplittingBackingMap, WrapperConcurrentMap

    public class WrapperObservableMap<K,​V>
    extends AbstractKeySetBasedMap<K,​V>
    implements ObservableMap<K,​V>
    A simple implementation of ObservableMap interface built as a wrapper around any Map implementation. It also provides an implementation of CacheStatistics interface.

    Note: as of Coherence 3.0 the CacheStatistics implementation has to be turned on explicitly by calling the setCollectStats(boolean) method.

    Note: if the underlying (wrapped) Map is an ObservableMap by itself, as of Coherence 3.2 the WrapperObservableMap implementation does not translate events generated the wrapped map by default. The translation can be turned on explicitly by calling the setTranslateEvents(boolean) method.

    Since:
    Coherence 2.3
    Author:
    gg 2003.10.01
    • Field Detail

      • m_map

        protected Map<K,​V> m_map
        The (wrapped) map containing all the resources.
      • m_listenerInternal

        protected MapListener<K,​V> m_listenerInternal
        The MapListener used to listen to the wrapped ObservableMap.
      • m_stats

        protected SimpleCacheStatistics m_stats
        The CacheStatistics object maintained by this wrapper.
      • m_fTranslateEvents

        protected boolean m_fTranslateEvents
        Specifies whether or not events are translated by this wrapper.
      • m_fDeferredEvent

        protected boolean m_fDeferredEvent
        Specifies whether or not fabricated events could be deferred.
    • Constructor Detail

      • WrapperObservableMap

        public WrapperObservableMap​(Map<K,​V> map)
        Construct an ObservableMap wrapper based on the specified map.

        Note: it is assumed that while the WrapperObservableMap exists, the contents of the underlying wrapped map are not directly manipulated.

        Parameters:
        map - the Map that will be wrapped by this WrapperObservableMap
      • WrapperObservableMap

        public WrapperObservableMap​(Map<K,​V> map,
                                    boolean fDeferredEvent)
        Construct an ObservableMap wrapper based on the specified map.

        Note: it is assumed that while the WrapperObservableMap exists, the contents of the underlying wrapped map are not directly manipulated.

        Parameters:
        map - the Map that will be wrapped by this WrapperObservableMap
        fDeferredEvent - true iff if the value contained in the fabricated cache events could be lazily populated. Deferred events should only be raised to listeners that will process events synchronously
    • Method Detail

      • containsValue

        public boolean containsValue​(Object oValue)
        Returns true if this Map maps one or more keys to the specified value.
        Specified by:
        containsValue in interface Map<K,​V>
        Overrides:
        containsValue in class AbstractKeyBasedMap<K,​V>
        Returns:
        true if this Map maps one or more keys to the specified value, false otherwise
      • get

        public V get​(Object oKey)
        Returns the value to which this map maps the specified key.
        Specified by:
        get in interface Map<K,​V>
        Specified by:
        get in class AbstractKeyBasedMap<K,​V>
        Parameters:
        oKey - the key object
        Returns:
        the value to which this map maps the specified key, or null if the map contains no mapping for this key
      • put

        public V put​(K oKey,
                     V oValue)
        Associates the specified value with the specified key in this map.
        Specified by:
        put in interface Map<K,​V>
        Overrides:
        put in class AbstractKeyBasedMap<K,​V>
        Parameters:
        oKey - key with which the specified value is to be associated
        oValue - value to be associated with the specified key
        Returns:
        previous value associated with specified key, or null if there was no mapping for key
      • putAll

        public void putAll​(Map<? extends K,​? extends V> map)
        Copies all of the mappings from the specified map to this map. The effect of this call is equivalent to that of calling AbstractKeyBasedMap.put(K, V) on this map once for each mapping in the passed map. The behavior of this operation is unspecified if the passed map is modified while the operation is in progress.
        Specified by:
        putAll in interface Map<K,​V>
        Overrides:
        putAll in class AbstractKeyBasedMap<K,​V>
        Parameters:
        map - the Map containing the key/value pairings to put into this Map
      • remove

        public V remove​(Object oKey)
        Removes the mapping for this key from this map if present. Expensive: updates both the underlying cache and the local cache.
        Specified by:
        remove in interface Map<K,​V>
        Overrides:
        remove in class AbstractKeyBasedMap<K,​V>
        Parameters:
        oKey - key whose mapping is to be removed from the map
        Returns:
        previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the map previously associated null with the specified key, if the implementation supports null values.
      • getInternalKeySet

        protected Set<K> getInternalKeySet()
        Obtain a set of keys that are represented by this Map.

        The AbstractKeySetBasedMap only utilizes the internal key set as a read-only resource.

        Specified by:
        getInternalKeySet in class AbstractKeySetBasedMap<K,​V>
        Returns:
        an internal Set of keys that are contained by this Map
      • removeBlind

        protected boolean removeBlind​(Object oKey)
        Removes the mapping for this key from this map if present. This method exists to allow sub-classes to optimize remove functionality for situations in which the original value is not required.
        Overrides:
        removeBlind in class AbstractKeyBasedMap<K,​V>
        Parameters:
        oKey - key whose mapping is to be removed from the map
        Returns:
        true iff the Map changed as the result of this operation
      • addMapListener

        public void addMapListener​(MapListener<? super K,​? super V> listener)
        Add a standard map listener that will receive all events (inserts, updates, deletes) that occur against the map, with the key, old-value and new-value included. This has the same result as the following call:
           addMapListener(listener, (Filter) null, false);
         
        Specified by:
        addMapListener in interface ObservableMap<K,​V>
        Parameters:
        listener - the MapEvent listener to add
      • removeMapListener

        public void removeMapListener​(MapListener<? super K,​? super V> listener)
        Remove a standard map listener that previously signed up for all events. This has the same result as the following call:
           removeMapListener(listener, (Filter) null);
         
        Specified by:
        removeMapListener in interface ObservableMap<K,​V>
        Parameters:
        listener - the listener to remove
      • addMapListener

        public void addMapListener​(MapListener<? super K,​? super V> listener,
                                   K oKey,
                                   boolean fLite)
        Add a map listener for a specific key.

        The listeners will receive MapEvent objects, but if fLite is passed as true, they might not contain the OldValue and NewValue properties.

        To unregister the MapListener, use the ObservableMap.removeMapListener(MapListener, Object) method.

        Specified by:
        addMapListener in interface ObservableMap<K,​V>
        Parameters:
        listener - the MapEvent listener to add
        oKey - the key that identifies the entry for which to raise events
        fLite - true to indicate that the MapEvent objects do not have to include the OldValue and NewValue property values in order to allow optimizations
      • removeMapListener

        public void removeMapListener​(MapListener<? super K,​? super V> listener,
                                      K oKey)
        Remove a map listener that previously signed up for events about a specific key.
        Specified by:
        removeMapListener in interface ObservableMap<K,​V>
        Parameters:
        listener - the listener to remove
        oKey - the key that identifies the entry for which to raise events
      • addMapListener

        public void addMapListener​(MapListener<? super K,​? super V> listener,
                                   Filter filter,
                                   boolean fLite)
        Add a map listener that receives events based on a filter evaluation.

        The listeners will receive MapEvent objects, but if fLite is passed as true, they might not contain the OldValue and NewValue properties.

        To unregister the MapListener, use the ObservableMap.removeMapListener(MapListener, Filter) method.

        Specified by:
        addMapListener in interface ObservableMap<K,​V>
        Parameters:
        listener - the MapEvent listener to add
        filter - a filter that will be passed MapEvent objects to select from; a MapEvent will be delivered to the listener only if the filter evaluates to true for that MapEvent (see MapEventFilter); null is equivalent to a filter that alway returns true
        fLite - true to indicate that the MapEvent objects do not have to include the OldValue and NewValue property values in order to allow optimizations
      • removeMapListener

        public void removeMapListener​(MapListener<? super K,​? super V> listener,
                                      Filter filter)
        Remove a map listener that previously signed up for events based on a filter evaluation.
        Specified by:
        removeMapListener in interface ObservableMap<K,​V>
        Parameters:
        listener - the listener to remove
        filter - the filter that was passed into the corresponding addMapListener() call
      • getMap

        public Map<K,​V> getMap()
        Get the Map that is wrapped by this wrapper.

        Note: direct modifications of the returned map may cause an unpredictable behavior of the wrapper map.

        Returns:
        the wrapped Map
      • getCacheStatistics

        public CacheStatistics getCacheStatistics()
        Return the CacheStatistics for this cache.
        Returns:
        a CacheStatistics object
      • isCollectStats

        public boolean isCollectStats()
        Check whether or not statistics are collected by the wrapper.
        Returns:
        true if this wrapper collects cache statistics; false otherwise
        Since:
        Coherence 3.0
      • setCollectStats

        public void setCollectStats​(boolean fCollectStats)
        Specify whether or not statistics are to be collected by the wrapper.
        Parameters:
        fCollectStats - true if this wrapper should collect cache statistics; false otherwise
        Since:
        Coherence 3.0
      • isTranslateEvents

        public boolean isTranslateEvents()
        Check whether or not an event source has to be translated by the wrapper.

        Note: this setting is only meaningful if the underlying map is an ObservableMap itself.

        Returns:
        true if this wrapper translates an event source; false otherwise
        Since:
        Coherence 3.3
      • setTranslateEvents

        public void setTranslateEvents​(boolean fTranslate)
        Specify whether or not an event source has to be translated by the wrapper.

        Note: this setting is only meaningful if the underlying map is an ObservableMap itself.

        Parameters:
        fTranslate - true if this wrapper should translate an event source; false otherwise
        Since:
        Coherence 3.3
      • getDescription

        protected String getDescription()
        Assemble a human-readable description.
        Returns:
        a description of this Map
      • toString

        public String toString()
        Returns a string representation of this Map. The string representation consists of a list of key-value mappings in the order returned by the Map's entrySet view's iterator, enclosed in braces ("{}"). Adjacent mappings are separated by the characters ", " (comma and space). Each key-value mapping is rendered as the key followed by an equals sign ("=") followed by the associated value. Keys and values are converted to strings as by String.valueOf(Object).
        Overrides:
        toString in class AbstractKeyBasedMap<K,​V>
        Returns:
        a String representation of this Map
      • getMapListenerSupport

        protected MapListenerSupport getMapListenerSupport()
        Accessor for the MapListenerSupport for sub-classes.
        Returns:
        the MapListenerSupport, or null if there are no listeners
      • ensureMapListenerSupport

        protected MapListenerSupport ensureMapListenerSupport()
        Obtain the MapListenerSupport, creating it if necessary.
        Returns:
        the MapListenerSupport; never null
      • hasListeners

        protected boolean hasListeners()
        Determine if the OverflowMap has any listeners at all.
        Returns:
        true iff this OverflowMap has at least one MapListener
      • isEventFabricator

        protected boolean isEventFabricator()
        Determine if this ObservableMap has to fabricate events itself.
        Returns:
        true if events are expected, but the wrapped Map does not generate any events itself
      • isSynthetic

        protected static <K,​V> boolean isSynthetic​(MapEvent<K,​V> evt)
        Helper method to determine if an event is synthetic.
        Type Parameters:
        K - the key type
        V - the value type
        Parameters:
        evt - a Map Event
        Returns:
        true if the event is a synthetic cache event
      • dispatchPendingEvent

        protected void dispatchPendingEvent​(K oKey,
                                            int nId,
                                            V oNewValue,
                                            boolean fSynthetic)
        Dispatch an event that has not yet occurred, allowing the cache to potentially avoid reading of the "original value" information.
        Parameters:
        oKey - the key which the event is related to
        nId - the event ID
        oNewValue - the new value
        fSynthetic - true if the event is synthetic
      • dispatchEvent

        protected void dispatchEvent​(MapEvent<? extends K,​? extends V> evt)
        Dispatch the passed event.
        Parameters:
        evt - a CacheEvent object
      • hashCode

        public int hashCode()
        Description copied from class: AbstractKeyBasedMap
        Returns the hash code value for this Map. The hash code of a Map is defined to be the sum of the hash codes of each entry in the Map's entrySet() view. This ensures that t1.equals(t2) implies that t1.hashCode()==t2.hashCode() for any two maps t1 and t2, as required by the general contract of Object.hashCode.
        Specified by:
        hashCode in interface Map<K,​V>
        Overrides:
        hashCode in class AbstractKeyBasedMap<K,​V>
        Returns:
        the hash code value for this Map
      • equals

        public boolean equals​(Object o)
        Description copied from class: AbstractKeyBasedMap
        Compares the specified object with this map for equality. Returns true if the given object is also a map and the two maps represent the same mappings. More formally, two maps t1 and t2 represent the same mappings if t1.keySet().equals(t2.keySet()) and for every key k in t1.keySet(), (t1.get(k)==null ? t2.get(k)==null : t1.get(k).equals(t2.get(k))) . This ensures that the equals method works properly across different implementations of the map interface.
        Specified by:
        equals in interface Map<K,​V>
        Overrides:
        equals in class AbstractKeyBasedMap<K,​V>
        Parameters:
        o - object to be compared for equality with this Map
        Returns:
        true if the specified object is equal to this Map
      • ensureInternalListener

        protected MapListener<K,​V> ensureInternalListener()
        Obtain the internal MapListener, creating one if necessary.
        Returns:
        an instance of MapListener; never null
      • instantiateInternalListener

        protected MapListener<K,​V> instantiateInternalListener()
        Instantiate a MapListener to listen to the wrapped map.
        Returns:
        an instance of MapListener