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:
-
Nested Class Summary
-
Field Summary
FieldsModifier and TypeFieldDescriptionDefault immutable classes: String BigDecimal BigInteger Byte Double Float Integer Long Short Boolean Date Locale -
Constructor Summary
ConstructorsConstructorDescriptionValueProtectingMap(Map<K, V> protectedMap) Construct providing a protected map and using only thedefault immutable classesConstruct providing a protected map, complementing the set ofdefault immutable classes -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()static <K extends Serializable,V extends Serializable>
Map<K,V> Utility method to clone a map, preserving immutable instancesbooleancontainsKey(Object key) booleancontainsValue(Object value) entrySet()booleaninthashCode()booleanisEmpty()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 mutablevoidintsize()toString()values()Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Field Details
-
DEFAULT_IMMUTABLE_CLASSES
Default immutable classes:- String
- BigDecimal
- BigInteger
- Byte
- Double
- Float
- Integer
- Long
- Short
- Boolean
- Date
- Locale
-
-
Constructor Details
-
ValueProtectingMap
Construct providing a protected map and using only thedefault immutable classes- Parameters:
protectedMap- the map to safeguard
-
ValueProtectingMap
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 Details
-
protectValue
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
- Specified by:
containsKeyin interfaceMap<K extends Serializable,V extends Serializable>
-
containsValue
- 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
- Specified by:
equalsin interfaceMap<K extends Serializable,V extends Serializable> - Overrides:
equalsin classObject
-
toString
-
get
- Specified by:
getin interfaceMap<K extends Serializable,V extends Serializable>
-
put
- Specified by:
putin interfaceMap<K extends Serializable,V extends Serializable>
-
remove
- Specified by:
removein interfaceMap<K extends Serializable,V extends Serializable>
-
putAll
- 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
- Specified by:
keySetin interfaceMap<K extends Serializable,V extends Serializable>
-
values
- Specified by:
valuesin interfaceMap<K extends Serializable,V extends Serializable>
-
entrySet
- Specified by:
entrySetin interfaceMap<K extends Serializable,V extends Serializable>
-