Package org.alfresco.util
Class ValueProtectingMap<K extends Serializable,V extends 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:
Serializable,Map<K,V>
public class ValueProtectingMap<K extends Serializable,V extends Serializable> extends Object implements Map<K,V>, 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
-
-
Field Summary
Fields Modifier and Type Field Description static Set<Class<?>>DEFAULT_IMMUTABLE_CLASSESDefault immutable classes: String BigDecimal BigInteger Byte Double Float Integer Long Short Boolean Date Locale
-
Constructor Summary
Constructors Constructor Description ValueProtectingMap(Map<K,V> protectedMap)Construct providing a protected map and using only thedefault immutable classesValueProtectingMap(Map<K,V> protectedMap, Set<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 Serializable,V extends Serializable>
Map<K,V>cloneMap(Map<K,V> map, Set<Class<?>> immutableClasses)Utility method to clone a map, preserving immutable instancesbooleancontainsKey(Object key)booleancontainsValue(Object value)Set<Map.Entry<K,V>>entrySet()booleanequals(Object obj)Vget(Object key)inthashCode()booleanisEmpty()Set<K>keySet()static <S extends Serializable>
booleanmustProtectValue(S value, Set<Class<?>> immutableClasses)Utility method to check if values need to be cloned or notstatic <S extends Serializable>
SprotectValue(S value, Set<Class<?>> immutableClasses)Protect a specific value if it is considered mutableVput(K key, V value)voidputAll(Map<? extends K,? extends V> m)Vremove(Object key)intsize()StringtoString()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
-
-
-
-
Constructor Detail
-
ValueProtectingMap
public ValueProtectingMap(Map<K,V> protectedMap)
Construct providing a protected map and using only thedefault immutable classes- Parameters:
protectedMap- the map to safeguard
-
ValueProtectingMap
public ValueProtectingMap(Map<K,V> protectedMap, Set<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 Serializable> S protectValue(S value, Set<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 Serializable> boolean mustProtectValue(S value, Set<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 Serializable,V extends Serializable> Map<K,V> cloneMap(Map<K,V> map, Set<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()
- Specified by:
sizein interfaceMap<K extends Serializable,V extends Serializable>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfaceMap<K extends Serializable,V extends Serializable>
-
containsKey
public boolean containsKey(Object key)
- Specified by:
containsKeyin interfaceMap<K extends Serializable,V extends Serializable>
-
containsValue
public boolean containsValue(Object value)
- Specified by:
containsValuein interfaceMap<K extends Serializable,V extends Serializable>
-
hashCode
public int hashCode()
- Specified by:
hashCodein interfaceMap<K extends Serializable,V extends Serializable>- Overrides:
hashCodein classObject
-
equals
public boolean equals(Object obj)
- Specified by:
equalsin interfaceMap<K extends Serializable,V extends Serializable>- Overrides:
equalsin classObject
-
get
public V get(Object key)
- Specified by:
getin interfaceMap<K extends Serializable,V extends Serializable>
-
put
public V put(K key, V value)
- Specified by:
putin interfaceMap<K extends Serializable,V extends Serializable>
-
remove
public V remove(Object key)
- Specified by:
removein interfaceMap<K extends Serializable,V extends Serializable>
-
putAll
public void putAll(Map<? extends K,? extends V> m)
- Specified by:
putAllin interfaceMap<K extends Serializable,V extends Serializable>
-
clear
public void clear()
- Specified by:
clearin interfaceMap<K extends Serializable,V extends Serializable>
-
keySet
public Set<K> keySet()
- Specified by:
keySetin interfaceMap<K extends Serializable,V extends Serializable>
-
values
public Collection<V> values()
- Specified by:
valuesin interfaceMap<K extends Serializable,V extends Serializable>
-
entrySet
public Set<Map.Entry<K,V>> entrySet()
- Specified by:
entrySetin interfaceMap<K extends Serializable,V extends Serializable>
-
-