Package org.alfresco.util
Class ValueProtectingMap<K extends java.io.Serializable,V extends java.io.Serializable>
- java.lang.Object
-
- org.alfresco.util.ValueProtectingMap<K,V>
-
- Type Parameters:
K- the map key type (must extendSerializable)V- the map value type (must extendSerializable)
- 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.SerializableA 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
-
-
Field Summary
Fields Modifier and Type Field Description static java.util.Set<java.lang.Class<?>>DEFAULT_IMMUTABLE_CLASSESDefault immutable classes: String BigDecimal BigInteger Byte Double Float Integer Long Short Boolean Date Locale
-
Constructor Summary
Constructors Constructor Description ValueProtectingMap(java.util.Map<K,V> protectedMap)Construct providing a protected map and using only thedefault immutable classesValueProtectingMap(java.util.Map<K,V> protectedMap, java.util.Set<java.lang.Class<?>> immutableClasses)Construct providing a protected map, complementing the set ofdefault immutable classes
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()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 instancesbooleancontainsKey(java.lang.Object key)booleancontainsValue(java.lang.Object value)java.util.Set<java.util.Map.Entry<K,V>>entrySet()booleanequals(java.lang.Object obj)Vget(java.lang.Object key)inthashCode()booleanisEmpty()java.util.Set<K>keySet()static <S extends java.io.Serializable>
booleanmustProtectValue(S value, java.util.Set<java.lang.Class<?>> immutableClasses)Utility method to check if values need to be cloned or notstatic <S extends java.io.Serializable>
SprotectValue(S value, java.util.Set<java.lang.Class<?>> immutableClasses)Protect a specific value if it is considered mutableVput(K key, V value)voidputAll(java.util.Map<? extends K,? extends V> m)Vremove(java.lang.Object key)intsize()java.lang.StringtoString()java.util.Collection<V>values()
-
-
-
Constructor Detail
-
ValueProtectingMap
public ValueProtectingMap(java.util.Map<K,V> protectedMap)
Construct providing a protected map and using only thedefault 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 ofdefault immutable classes- Parameters:
protectedMap- the map to safeguardimmutableClasses- additional immutable classes over and above thedefault 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 beSerializable- 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 thedefault 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 beSerializable- Parameters:
value- the value to checkimmutableClasses- a set of classes that can be considered immutable over and above thedefault 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 beSerializableV- the map value type, which must beSerializable- Parameters:
map- the map to copyimmutableClasses- a set of classes that can be considered immutable over and above thedefault set
-
size
public int size()
-
isEmpty
public boolean isEmpty()
-
containsKey
public boolean containsKey(java.lang.Object key)
-
containsValue
public boolean containsValue(java.lang.Object value)
-
hashCode
public int hashCode()
-
equals
public boolean equals(java.lang.Object obj)
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
get
public V get(java.lang.Object key)
-
remove
public V remove(java.lang.Object key)
-
clear
public void clear()
-
keySet
public java.util.Set<K> keySet()
-
values
public java.util.Collection<V> values()
-
-