Class AttributeMap
This class provides a synchronized wrapper around a LinkedHashMap to store
HTML element attributes as key-value pairs. It maintains insertion order and ensures
thread-safe access to attribute data. All methods are synchronized to prevent
concurrent modification issues when attributes are accessed from multiple threads.
Features:
- Thread-safe access to all map operations
- Maintains insertion order of attributes
- Implements standard
Mapinterface - Supports copying via copy constructor
- Used for HTML element attribute management in the Oorian framework
Thread Safety: All map operations are synchronized, making this class safe for concurrent access from multiple threads. However, iteration over the map's views (keySet, values, entrySet) should still be externally synchronized if the map is modified during iteration.
Usage:
// Create an attribute map
AttributeMap attributes = new AttributeMap();
// Add attributes
attributes.put("class", "btn btn-primary");
attributes.put("id", "submitButton");
attributes.put("data-action", "submit");
// Access attributes
String cssClass = attributes.get("class");
// Copy attributes
AttributeMap copy = new AttributeMap(attributes);
// Iterate (synchronize externally for safety during modification)
synchronized(attributes) {
for (Map.Entry<String, String> entry : attributes.entrySet()) {
String name = entry.getKey();
String value = entry.getValue();
// Process attribute
}
}
- Since:
- 2007
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Nested Class Summary
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs an empty AttributeMap.AttributeMap(AttributeMap copy) Constructs a new AttributeMap as a copy of an existing one. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all attributes from this map.booleancontainsKey(Object key) Returnstrueif this map contains an attribute with the specified name.booleancontainsValue(Object value) Returnstrueif this map contains one or more attributes with the specified value.entrySet()Returns aSetview of the attribute mappings contained in this map.Returns the value of the attribute with the specified name.booleanisEmpty()Returnstrueif this map contains no attributes.keySet()Returns aSetview of the attribute names contained in this map.Associates the specified value with the specified attribute name in this map.voidCopies all of the mappings from the specified map to this attribute map.Removes the attribute with the specified name from this map if present.intsize()Returns the number of attributes in this map.values()Returns aCollectionview of the attribute values contained in this map.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Constructor Details
-
AttributeMap
public AttributeMap()Constructs an empty AttributeMap.Creates a new map with no attributes. Attributes can be added using
put(String, String)orputAll(Map). -
AttributeMap
Constructs a new AttributeMap as a copy of an existing one.Creates a deep copy of the attribute map, copying all key-value pairs from the source map. Changes to the copy will not affect the original, and vice versa.
- Parameters:
copy- the AttributeMap to copy (must not be null)
-
-
Method Details
-
size
public int size()Returns the number of attributes in this map. -
isEmpty
public boolean isEmpty()Returnstrueif this map contains no attributes. -
containsKey
Returnstrueif this map contains an attribute with the specified name.- Specified by:
containsKeyin interfaceMap<String,String> - Parameters:
key- the attribute name to check for- Returns:
trueif this map contains a mapping for the specified key
-
containsValue
Returnstrueif this map contains one or more attributes with the specified value.- Specified by:
containsValuein interfaceMap<String,String> - Parameters:
value- the attribute value to check for- Returns:
trueif this map maps one or more keys to the specified value
-
get
Returns the value of the attribute with the specified name. -
put
Associates the specified value with the specified attribute name in this map.If the map previously contained a mapping for the attribute, the old value is replaced.
-
remove
Removes the attribute with the specified name from this map if present. -
putAll
Copies all of the mappings from the specified map to this attribute map.These mappings will replace any attributes that this map had for any of the keys currently in the specified map.
-
clear
public void clear()Removes all attributes from this map.The map will be empty after this call returns.
-
keySet
Returns aSetview of the attribute names contained in this map.The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined.
-
values
Returns aCollectionview of the attribute values contained in this map.The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress, the results of the iteration are undefined.
-
entrySet
Returns aSetview of the attribute mappings contained in this map.The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined.
-