Class RwMapWrapper<K,​V>

  • All Implemented Interfaces:
    IRwMap<K,​V>, java.util.Map<K,​V>

    public class RwMapWrapper<K,​V>
    extends java.lang.Object
    implements IRwMap<K,​V>
    Thread-safe wrapper for maps that uses a read/write lock.
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.Map<K,​V> map
      The wrapped map.
      protected RwAutoLock rwautolock  
    • Constructor Summary

      Constructors 
      Constructor Description
      RwMapWrapper​(java.util.Map<K,​V> map)
      Creates the wrapper.
      RwMapWrapper​(java.util.Map<K,​V> map, boolean fair)
      Creates the wrapper.
      RwMapWrapper​(java.util.Map<K,​V> map, java.util.concurrent.locks.ReadWriteLock lock)
      Creates the wrapper with a specific internal lock.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clears the map.
      boolean containsKey​(java.lang.Object key)
      Returns if the map contains a key.
      boolean containsValue​(java.lang.Object value)
      Returns if the map contains a value.
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
      Returns the entry set.
      V get​(java.lang.Object key)
      Gets the value for the key.
      java.util.concurrent.locks.ReadWriteLock getLock()
      Gets the internal lock.
      java.util.concurrent.locks.Lock getReadLock()
      Gets the read lock for manual locking.
      java.util.concurrent.locks.Lock getWriteLock()
      Gets the write lock for manual locking.
      boolean isEmpty()
      Returns if map is empty.
      java.util.Set<K> keySet()
      Returns the key set.
      V put​(K key, V value)
      Puts a key-value pair.
      void putAll​(java.util.Map<? extends K,​? extends V> m)
      Puts all key-value pairs into map.
      IAutoLock readLock()
      Locks the read lock for resource-based locking.
      V remove​(java.lang.Object key)
      Removes a key-value pair.
      int size()
      Returns the size of the map.
      java.util.Collection<V> values()
      Returns the key values.
      IAutoLock writeLock()
      Locks the write lock for resource-based locking.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Field Detail

      • map

        protected java.util.Map<K,​V> map
        The wrapped map.
    • Constructor Detail

      • RwMapWrapper

        public RwMapWrapper​(java.util.Map<K,​V> map)
        Creates the wrapper.
        Parameters:
        map - The wrapped map.
      • RwMapWrapper

        public RwMapWrapper​(java.util.Map<K,​V> map,
                            java.util.concurrent.locks.ReadWriteLock lock)
        Creates the wrapper with a specific internal lock.
        Parameters:
        map - The wrapped map.
      • RwMapWrapper

        public RwMapWrapper​(java.util.Map<K,​V> map,
                            boolean fair)
        Creates the wrapper.
        Parameters:
        map - The wrapped map.
        fair - Set true for fair-mode lock.
    • Method Detail

      • size

        public int size()
        Returns the size of the map.
        Specified by:
        size in interface java.util.Map<K,​V>
      • isEmpty

        public boolean isEmpty()
        Returns if map is empty.
        Specified by:
        isEmpty in interface java.util.Map<K,​V>
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Returns if the map contains a key.
        Specified by:
        containsKey in interface java.util.Map<K,​V>
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Returns if the map contains a value.
        Specified by:
        containsValue in interface java.util.Map<K,​V>
      • get

        public V get​(java.lang.Object key)
        Gets the value for the key.
        Specified by:
        get in interface java.util.Map<K,​V>
      • put

        public V put​(K key,
                     V value)
        Puts a key-value pair.
        Specified by:
        put in interface java.util.Map<K,​V>
      • remove

        public V remove​(java.lang.Object key)
        Removes a key-value pair.
        Specified by:
        remove in interface java.util.Map<K,​V>
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends V> m)
        Puts all key-value pairs into map.
        Specified by:
        putAll in interface java.util.Map<K,​V>
      • clear

        public void clear()
        Clears the map.
        Specified by:
        clear in interface java.util.Map<K,​V>
      • keySet

        public java.util.Set<K> keySet()
        Returns the key set. Warning: Use manual locking.
        Specified by:
        keySet in interface java.util.Map<K,​V>
      • values

        public java.util.Collection<V> values()
        Returns the key values. Warning: Use manual locking.
        Specified by:
        values in interface java.util.Map<K,​V>
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Returns the entry set. Warning: Use manual locking.
        Specified by:
        entrySet in interface java.util.Map<K,​V>
      • readLock

        public IAutoLock readLock()
        Locks the read lock for resource-based locking.
        Specified by:
        readLock in interface IRwMap<K,​V>
      • writeLock

        public IAutoLock writeLock()
        Locks the write lock for resource-based locking.
        Specified by:
        writeLock in interface IRwMap<K,​V>
      • getReadLock

        public java.util.concurrent.locks.Lock getReadLock()
        Gets the read lock for manual locking.
        Specified by:
        getReadLock in interface IRwMap<K,​V>
      • getWriteLock

        public java.util.concurrent.locks.Lock getWriteLock()
        Gets the write lock for manual locking.
        Specified by:
        getWriteLock in interface IRwMap<K,​V>
      • getLock

        public java.util.concurrent.locks.ReadWriteLock getLock()
        Gets the internal lock.
        Specified by:
        getLock in interface IRwMap<K,​V>
        Returns:
        The lock.