Class ValueProtectingMap<K extends java.io.Serializable,​V extends java.io.Serializable>

  • Type Parameters:
    K - the map key type (must extend Serializable)
    V - the map value type (must extend Serializable)
    All Implemented Interfaces:
    java.io.Serializable, java.util.Map<K,​V>

    public class ValueProtectingMap<K extends java.io.Serializable,​V extends java.io.Serializable>
    extends java.lang.Object
    implements java.util.Map<K,​V>, java.io.Serializable
    A map that protects keys and values from accidental modification.

    Use this map when keys or values need to be protected against client modification. For example, when a component pulls a map from a common resource it can wrap the map with this class to prevent any accidental modification of the shared resource.

    Upon first write to this map , the underlying map will be copied (selectively cloned), the original map handle will be discarded and the copied map will be used. Note that the map copy process will also occur if any mutable value is in danger of being exposed to client modification. Therefore, methods that iterate and retrieve values will also trigger the copy if any values are mutable.

    Since:
    3.4.9, 4.0.1
    Author:
    Derek Hulley
    See Also:
    Serialized Form
    • 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
      static java.util.Set<java.lang.Class<?>> DEFAULT_IMMUTABLE_CLASSES
      Default immutable classes: String BigDecimal BigInteger Byte Double Float Integer Long Short Boolean Date Locale
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()  
      static <K extends java.io.Serializable,​V extends java.io.Serializable>
      java.util.Map<K,​V>
      cloneMap​(java.util.Map<K,​V> map, java.util.Set<java.lang.Class<?>> immutableClasses)
      Utility method to clone a map, preserving immutable instances
      boolean containsKey​(java.lang.Object key)  
      boolean containsValue​(java.lang.Object value)  
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()  
      boolean equals​(java.lang.Object obj)  
      V get​(java.lang.Object key)  
      int hashCode()  
      boolean isEmpty()  
      java.util.Set<K> keySet()  
      static <S extends java.io.Serializable>
      boolean
      mustProtectValue​(S value, java.util.Set<java.lang.Class<?>> immutableClasses)
      Utility method to check if values need to be cloned or not
      static <S extends java.io.Serializable>
      S
      protectValue​(S value, java.util.Set<java.lang.Class<?>> immutableClasses)
      Protect a specific value if it is considered mutable
      V put​(K key, V value)  
      void putAll​(java.util.Map<? extends K,​? extends V> m)  
      V remove​(java.lang.Object key)  
      int size()  
      java.lang.String toString()  
      java.util.Collection<V> values()  
      • Methods inherited from class java.lang.Object

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

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

      • DEFAULT_IMMUTABLE_CLASSES

        public static final java.util.Set<java.lang.Class<?>> DEFAULT_IMMUTABLE_CLASSES
        Default immutable classes:
      • String
      • BigDecimal
      • BigInteger
      • Byte
      • Double
      • Float
      • Integer
      • Long
      • Short
      • Boolean
      • Date
      • Locale
  • Constructor Detail

    • ValueProtectingMap

      public ValueProtectingMap​(java.util.Map<K,​V> protectedMap)
      Construct providing a protected map and using only the default immutable classes
      Parameters:
      protectedMap - the map to safeguard
    • ValueProtectingMap

      public ValueProtectingMap​(java.util.Map<K,​V> protectedMap,
                                java.util.Set<java.lang.Class<?>> immutableClasses)
      Construct providing a protected map, complementing the set of default immutable classes
      Parameters:
      protectedMap - the map to safeguard
      immutableClasses - additional immutable classes over and above the default set (may be null
  • Method Detail

    • protectValue

      public static <S extends java.io.Serializable> S protectValue​(S value,
                                                                    java.util.Set<java.lang.Class<?>> immutableClasses)
      Protect a specific value if it is considered mutable
      Type Parameters:
      S - the type of the value, which must be Serializable
      Parameters:
      value - the value to protect if it is mutable (may be null)
      immutableClasses - a set of classes that can be considered immutable over and above the default set
      Returns:
      a cloned instance (via serialization) or the instance itself, if immutable
    • mustProtectValue

      public static <S extends java.io.Serializable> boolean mustProtectValue​(S value,
                                                                              java.util.Set<java.lang.Class<?>> immutableClasses)
      Utility method to check if values need to be cloned or not
      Type Parameters:
      S - the type of the value, which must be Serializable
      Parameters:
      value - the value to check
      immutableClasses - a set of classes that can be considered immutable over and above the default set
      Returns:
      true if the value must NOT be given to the calling clients
    • cloneMap

      public static <K extends java.io.Serializable,​V extends java.io.Serializable> java.util.Map<K,​V> cloneMap​(java.util.Map<K,​V> map,
                                                                                                                            java.util.Set<java.lang.Class<?>> immutableClasses)
      Utility method to clone a map, preserving immutable instances
      Type Parameters:
      K - the map key type, which must be Serializable
      V - the map value type, which must be Serializable
      Parameters:
      map - the map to copy
      immutableClasses - a set of classes that can be considered immutable over and above the default set
    • size

      public int size()
      Specified by:
      size in interface java.util.Map<K extends java.io.Serializable,​V extends java.io.Serializable>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface java.util.Map<K extends java.io.Serializable,​V extends java.io.Serializable>
    • containsKey

      public boolean containsKey​(java.lang.Object key)
      Specified by:
      containsKey in interface java.util.Map<K extends java.io.Serializable,​V extends java.io.Serializable>
    • containsValue

      public boolean containsValue​(java.lang.Object value)
      Specified by:
      containsValue in interface java.util.Map<K extends java.io.Serializable,​V extends java.io.Serializable>
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface java.util.Map<K extends java.io.Serializable,​V extends java.io.Serializable>
      Overrides:
      hashCode in class java.lang.Object
    • equals

      public boolean equals​(java.lang.Object obj)
      Specified by:
      equals in interface java.util.Map<K extends java.io.Serializable,​V extends java.io.Serializable>
      Overrides:
      equals in class java.lang.Object
    • toString

      public java.lang.String toString()
      Overrides:
      toString in class java.lang.Object
    • get

      public V get​(java.lang.Object key)
      Specified by:
      get in interface java.util.Map<K extends java.io.Serializable,​V extends java.io.Serializable>
    • put

      public V put​(K key,
                   V value)
      Specified by:
      put in interface java.util.Map<K extends java.io.Serializable,​V extends java.io.Serializable>
    • remove

      public V remove​(java.lang.Object key)
      Specified by:
      remove in interface java.util.Map<K extends java.io.Serializable,​V extends java.io.Serializable>
    • putAll

      public void putAll​(java.util.Map<? extends K,​? extends V> m)
      Specified by:
      putAll in interface java.util.Map<K extends java.io.Serializable,​V extends java.io.Serializable>
    • clear

      public void clear()
      Specified by:
      clear in interface java.util.Map<K extends java.io.Serializable,​V extends java.io.Serializable>
    • keySet

      public java.util.Set<K> keySet()
      Specified by:
      keySet in interface java.util.Map<K extends java.io.Serializable,​V extends java.io.Serializable>
    • values

      public java.util.Collection<V> values()
      Specified by:
      values in interface java.util.Map<K extends java.io.Serializable,​V extends java.io.Serializable>
    • entrySet

      public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
      Specified by:
      entrySet in interface java.util.Map<K extends java.io.Serializable,​V extends java.io.Serializable>