Package Torello.Java.ReadOnly
Class ROHashMapBuilder<K,V>
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- java.util.HashMap<K,V>
-
- Torello.Java.ReadOnly.ROHashMapBuilder<K,V>
-
- Type Parameters:
K
- the type of keys maintained by this mapV
- the type of mapped values
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
,java.util.Map<K,V>
public final class ROHashMapBuilder<K,V> extends java.util.HashMap<K,V> implements java.lang.Cloneable, java.io.Serializable
This class was originally copied fromGitHub's Open-JDK
Account. Though the original file has been modified, few changes were applied to the Javadoc Commenting. Method and parameter names & types have not been modified whatsoever. This file may be viewed on the GitHub Archive for Java Packagejava.util.*
The Original'.java'
Source-File's Header-Copyright Information is included here:File Copyright
. Within that Copyright Notice, it is suggested that a copy of theGNU Public License V2
also be included alongside.A Copy of Java'sHashMap
class; used for building aReadOnlyHashMap
. Maintains an internal and inaccessibleHashMap<E>
instance. Upon build completion, this instance is passed to theReadOnlyHashMap
instance and stored there.
The internal data-structure is not exposed by any method provided in this API. It is guaranteed that the contents of aReadOnlyHashMap
will not be modified.
Note: The "RO Builder Classes" are somewhat superfluous (a little)
The art of any solid "Read-Only" Data-Structure Class-API is providing an intelligent means of actually inserting Data into a supposedly-read-only class. In this package, the most direct and efficient way of doing so is just to use one of the myriad constructors offered by the ReadOnly Data-Classes.
Another sound way of creating any "ReadOnly-XX" Data-Structure, is to simply populate the correspondingjava.util.XX
class instance, and pass that instance yourReadOnly-X
Constructor. The constructor actuallycopies
the data out of the original structure, and saves it to its own private & internal data-structure in order to guarantee the immutability contract.
But what about a situation where there is aHashMap
that is being populated by a very large number elements? It would likely be of benefit to skip the data-copying step for performance reasons! If so, then a builder can improve performance quite a bit. The real value of using a "Builder" rather than one of theReadOnlyHashMap
constructors is that this Builder'sbuild()
method doesn't actually have any kind of data-copy step at all!
Also, of some interest, is that this class inherits the original Java-Classjava.util.HashMap
(explained further, below), making it extremely easy to use.
Inheritsjava.util.HashMap
This class may be passed to any Data-Building Method that accepts aHashMap
, because it inherits from that class. Any implementation that can populate aHashMap
can also populate this builder.
Efficiency Improvement:
This class is nothing but a set of wrapper methods. It is being provided as an alternate, and possibly more efficient, way to construct aReadOnlyHashMap
.
This class inherits the Standard JDK Collection-Framework Classjava.util.HashMap
, and adds a singleboolean
field named'built'
that, once switched to'true'
, will block any subsequent attempts to mutate the underlying data-structure.
With aHashMap
having more than, say, 10,000 items, the cost of copying the internalHashMap
(which is necessary to construct any Read-Only Class) would perhaps be much too high. Instead, by making use of classROHashMapBuilder
, there is no need to run any kind of internal-data data-copying step at all.
Simply put all data into the Builder, using any / all standard Java-JDKHashMap
methods, and when thebuild()
method is invoked, an internal flag is set which will wholly prohibit any further mutation of the data in your builder - thereby allowing the Builder, itself, to be used as theReadOnlyHashMap
's internal Data-Structure.- See Also:
ReadOnlyHashMap
, Serialized Form
Hi-Lited Source-Code:- View Here: Torello/Java/ReadOnly/ROHashMapBuilder.java
- Open New Browser-Tab: Torello/Java/ReadOnly/ROHashMapBuilder.java
File Size: 30,680 Bytes Line Count: 700 '\n' Characters Found
-
-
Field Summary
Fields Modifier and Type Field Description protected static long
serialVersionUID
-
Constructor Summary
Constructors Constructor Description ROHashMapBuilder()
Constructs an emptyROHashMapBuilder
with the default initial capacity (16) and the default load factor (0.75).ROHashMapBuilder(int initialCapacity)
Constructs an emptyROHashMapBuilder
with the specified initial capacity and the default load factor (0.75).ROHashMapBuilder(int initialCapacity, float loadFactor)
Constructs an emptyROHashMapBuilder
with the specified initial capacity and load factor.ROHashMapBuilder(Map<? extends K,? extends V> m)
Constructs a newROHashMapBuilder
with the same mappings as the specifiedMap
.
-
Method Summary
Convert this Builder into a ReadOnlyHashMap instance Modifier and Type Method ReadOnlyHashMap<K,V>
build()
Simply transfers'this'
instance' internalHashMap
to theReadOnlyHashMap
Wrapper-Class.Insert Entries into this Read-Only-Map Builder Modifier and Type Method V
put(K key, V value)
Associates the specified value with the specified key in this map.void
putAll(Map<? extends K,? extends V> m)
Copies all of the mappings from the specified map to this map.V
putIfAbsent(K key, V value)
If the specified key is not already associated with a value (or is mapped tonull
) associates it with the given value and returnsnull
, else returns the current value.Remove Entries from this Read-Only-Map Builder Modifier and Type Method void
clear()
Removes all of the mappings from this map.V
remove(Object key)
Removes the mapping for the specified key from this map if present.boolean
remove(Object key, Object value)
Removes the entry for the specified key only if it is currently mapped to the specified value.Compute Entries, and then Insert them into this Map Builder Modifier and Type Method V
compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).V
computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.V
computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.V
merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)
If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value.Replace Map Entries Modifier and Type Method V
replace(K key, V value)
Replaces the entry for the specified key only if it is currently mapped to some value.boolean
replace(K key, V oldValue, V newValue)
Replaces the entry for the specified key only if currently mapped to the specified value.void
replaceAll(BiFunction<? super K,? super V,? extends V> function)
Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.Static-Factory Builder Modifier and Type Method static <K,V>
ROHashMapBuilder<K,V>newROHashMapBuilder(int numMappings)
Creates a new, emptyROHashMapBuilder
suitable for the expected number of mappings.Mutable / Read-Write View Generators - Return Instances with Unmodifiable-Wrappers Modifier and Type Method Set<Map.Entry<K,V>>
entrySet()
Restricted-Access InstanceSet<K>
keySet()
Restricted-Access InstanceCollection<V>
values()
Restricted-Access InstanceMethods: class java.lang.Object Modifier and Type Method boolean
equals(Object o)
Compares the specified Object with this Builder for equality, as per the definition in the original classHashMap
.Methods: interface java.lang.Cloneable Modifier and Type Method ROHashMapBuilder<K,V>
clone()
Clones this instance' ofROHashMapBuilder
.-
Methods inherited from class java.util.HashMap
containsKey, containsValue, forEach, get, getOrDefault, isEmpty, size
-
-
-
-
Field Detail
-
serialVersionUID
protected static final long serialVersionUID
This fulfils the SerialVersion UID requirement for all classes that implement Java'sinterface java.io.Serializable
. Using theSerializable
Implementation offered by java is very easy, and can make saving program state when debugging a lot easier. It can also be used in place of more complicated systems like "hibernate" to store data as well.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
protected static final long serialVersionUID = 1;
-
-
Constructor Detail
-
ROHashMapBuilder
public ROHashMapBuilder(int initialCapacity, float loadFactor)
Constructs an emptyROHashMapBuilder
with the specified initial capacity and load factor.
To create aROHashMapBuilder
with an initial capacity that accommodates an expected number of mappings, usenewROHashMapBuilder
.- Parameters:
initialCapacity
- the initial capacityloadFactor
- the load factor- Throws:
java.lang.IllegalArgumentException
- if the initial capacity is negative or the load factor is nonpositive- Code:
- Exact Constructor Body:
super(initialCapacity, loadFactor);
-
ROHashMapBuilder
public ROHashMapBuilder(int initialCapacity)
Constructs an emptyROHashMapBuilder
with the specified initial capacity and the default load factor (0.75).
To create aROHashMapBuilder
with an initial capacity that accommodates an expected number of mappings, usenewROHashMapBuilder
.- Parameters:
initialCapacity
- the initial capacity.- Throws:
java.lang.IllegalArgumentException
- if the initial capacity is negative.- Code:
- Exact Constructor Body:
super(initialCapacity);
-
ROHashMapBuilder
public ROHashMapBuilder()
Constructs an emptyROHashMapBuilder
with the default initial capacity (16) and the default load factor (0.75).
-
ROHashMapBuilder
public ROHashMapBuilder(java.util.Map<? extends K,? extends V> m)
Constructs a newROHashMapBuilder
with the same mappings as the specifiedMap
. TheROHashMapBuilder
is created with default load factor (0.75) and an initial capacity sufficient to hold the mappings in the specifiedMap
.- Parameters:
m
- the map whose mappings are to be placed in this map- Throws:
java.lang.NullPointerException
- if the specified map is null- Code:
- Exact Constructor Body:
super(m);
-
-
Method Detail
-
build
public ReadOnlyHashMap<K,V> build()
Simply transfers'this'
instance' internalHashMap
to theReadOnlyHashMap
Wrapper-Class.- Returns:
- a newly constructed
ReadOnlyHashMap
"Wrapper-Class", shielding the internal'hashMap'
private-field from any modification. - Code:
- Exact Method Body:
this.built = true; return (size() == 0) ? ReadOnlyHashMap.emptyROHM() : new ReadOnlyHashMap<>(this, friendClassBadge);
-
newROHashMapBuilder
public static <K,V> ROHashMapBuilder<K,V> newROHashMapBuilder (int numMappings)
Creates a new, emptyROHashMapBuilder
suitable for the expected number of mappings. The returned map uses the default load factor of 0.75, and its initial capacity is generally large enough so that the expected number of mappings can be added without resizing the map.- Type Parameters:
K
- the type of keys maintained by the new mapV
- the type of mapped values- Parameters:
numMappings
- the expected number of mappings- Returns:
- the newly created map
- Throws:
java.lang.IllegalArgumentException
- if numMappings is negative- Code:
- Exact Method Body:
if (numMappings < 0) throw new IllegalArgumentException("Negative number of mappings: " + numMappings); return new ROHashMapBuilder<>(calculateHashMapCapacity(numMappings));
-
put
public V put(K key, V value)
Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.
Mutator Method:
This method modifies the contents of this class' internalHashMap
. Note that any method which modifies this internalHashMap
field will throw an exception if invoked after a call tobuild()
.- Specified by:
put
in interfacejava.util.Map<K,V>
- Overrides:
put
in classjava.util.HashMap<K,V>
- Parameters:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key- Returns:
- the previous value associated with
key
, ornull
if there was no mapping forkey
. (Anull
return can also indicate that the map previously associatednull
withkey
.) - Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROHM); return super.put(key, value);
-
putAll
public void putAll(java.util.Map<? extends K,? extends V> m)
Copies all of the mappings from the specified map to this map. These mappings will replace any mappings that this map had for any of the keys currently in the specified map.
Mutator Method:
This method modifies the contents of this class' internalHashMap
. Note that any method which modifies this internalHashMap
field will throw an exception if invoked after a call tobuild()
.- Specified by:
putAll
in interfacejava.util.Map<K,V>
- Overrides:
putAll
in classjava.util.HashMap<K,V>
- Parameters:
m
- mappings to be stored in this map- Throws:
java.lang.NullPointerException
- if the specified map is null- Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROHM); super.putAll(m);
-
remove
public V remove(java.lang.Object key)
Removes the mapping for the specified key from this map if present.
Mutator Method:
This method modifies the contents of this class' internalHashMap
. Note that any method which modifies this internalHashMap
field will throw an exception if invoked after a call tobuild()
.- Specified by:
remove
in interfacejava.util.Map<K,V>
- Overrides:
remove
in classjava.util.HashMap<K,V>
- Parameters:
key
- key whose mapping is to be removed from the map- Returns:
- the previous value associated with
key
, ornull
if there was no mapping forkey
. (Anull
return can also indicate that the map previously associatednull
withkey
.) - Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROHM); return super.remove(key);
-
clear
public void clear()
Removes all of the mappings from this map. The map will be empty after this call returns.
Mutator Method:
This method modifies the contents of this class' internalHashMap
. Note that any method which modifies this internalHashMap
field will throw an exception if invoked after a call tobuild()
.
-
putIfAbsent
public V putIfAbsent(K key, V value)
If the specified key is not already associated with a value (or is mapped tonull
) associates it with the given value and returnsnull
, else returns the current value.
The default implementation is equivalent to, for thismap
:
V v = map.get(key); if (v == null) v = map.put(key, value); return v;
The default implementation makes no guarantees about synchronization or atomicity properties of this method. Any implementation providing atomicity guarantees must override this method and document its concurrency properties.
Mutator Method:
This method modifies the contents of this class' internalHashMap
. Note that any method which modifies this internalHashMap
field will throw an exception if invoked after a call tobuild()
.- Specified by:
putIfAbsent
in interfacejava.util.Map<K,V>
- Overrides:
putIfAbsent
in classjava.util.HashMap<K,V>
- Parameters:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key- Returns:
- the previous value associated with the specified key, or
null
if there was no mapping for the key. (Anull
return can also indicate that the map previously associatednull
with the key, if the implementation supports null values.) - Throws:
java.lang.UnsupportedOperationException
- if theput
operation is not supported by this mapjava.lang.ClassCastException
- if the key or value is of an inappropriate type for this mapjava.lang.NullPointerException
- if the specified key or value is null, and this map does not permit null keys or valuesjava.lang.IllegalArgumentException
- if some property of the specified key or value prevents it from being stored in this map- Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROHM); return super.putIfAbsent(key, value);
-
remove
public boolean remove(java.lang.Object key, java.lang.Object value)
Removes the entry for the specified key only if it is currently mapped to the specified value.
The default implementation is equivalent to, for thismap
:
if (map.containsKey(key) && Objects.equals(map.get(key), value)) { map.remove(key); return true; } return false;
The default implementation makes no guarantees about synchronization or atomicity properties of this method. Any implementation providing atomicity guarantees must override this method and document its concurrency properties.
Mutator Method:
This method modifies the contents of this class' internalHashMap
. Note that any method which modifies this internalHashMap
field will throw an exception if invoked after a call tobuild()
.- Specified by:
remove
in interfacejava.util.Map<K,V>
- Overrides:
remove
in classjava.util.HashMap<K,V>
- Parameters:
key
- key with which the specified value is associatedvalue
- value expected to be associated with the specified key- Returns:
true
if the value was removed- Throws:
java.lang.UnsupportedOperationException
- if theremove
operation is not supported by this mapjava.lang.ClassCastException
- if the key or value is of an inappropriate type for this mapjava.lang.NullPointerException
- if the specified key or value is null, and this map does not permit null keys or values- Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROHM); return super.remove(key, value);
-
replace
public boolean replace(K key, V oldValue, V newValue)
Replaces the entry for the specified key only if currently mapped to the specified value.
The default implementation is equivalent to, for thismap
:
if (map.containsKey(key) && Objects.equals(map.get(key), oldValue)) { map.put(key, newValue); return true; } return false;
The default implementation does not throwNullPointerException
for maps that do not support null values if oldValue is null unless newValue is also null.
The default implementation makes no guarantees about synchronization or atomicity properties of this method. Any implementation providing atomicity guarantees must override this method and document its concurrency properties.
Mutator Method:
This method modifies the contents of this class' internalHashMap
. Note that any method which modifies this internalHashMap
field will throw an exception if invoked after a call tobuild()
.- Specified by:
replace
in interfacejava.util.Map<K,V>
- Overrides:
replace
in classjava.util.HashMap<K,V>
- Parameters:
key
- key with which the specified value is associatedoldValue
- value expected to be associated with the specified keynewValue
- value to be associated with the specified key- Returns:
TRUE
if the value was replaced- Throws:
java.lang.UnsupportedOperationException
- if theput
operation is not supported by this mapjava.lang.ClassCastException
- if the class of a specified key or value prevents it from being stored in this mapjava.lang.NullPointerException
- if a specified key or newValue is null, and this map does not permit null keys or valuesjava.lang.NullPointerException
- if oldValue is null and this map does not permit null valuesjava.lang.IllegalArgumentException
- if some property of a specified key or value prevents it from being stored in this map- Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROHM); return super.replace(key, oldValue, newValue);
-
replace
public V replace(K key, V value)
Replaces the entry for the specified key only if it is currently mapped to some value.
The default implementation is equivalent to, for thismap
:
if (map.containsKey(key)) return map.put(key, value); return null;
The default implementation makes no guarantees about synchronization or atomicity properties of this method. Any implementation providing atomicity guarantees must override this method and document its concurrency properties.
Mutator Method:
This method modifies the contents of this class' internalHashMap
. Note that any method which modifies this internalHashMap
field will throw an exception if invoked after a call tobuild()
.- Specified by:
replace
in interfacejava.util.Map<K,V>
- Overrides:
replace
in classjava.util.HashMap<K,V>
- Parameters:
key
- key with which the specified value is associatedvalue
- value to be associated with the specified key- Returns:
- the previous value associated with the specified key, or
null
if there was no mapping for the key. (Anull
return can also indicate that the map previously associatednull
with the key, if the implementation supports null values.) - Throws:
java.lang.UnsupportedOperationException
- if theput
operation is not supported by this mapjava.lang.ClassCastException
- if the class of the specified key or value prevents it from being stored in this mapjava.lang.NullPointerException
- if the specified key or value is null, and this map does not permit null keys or valuesjava.lang.IllegalArgumentException
- if some property of the specified key or value prevents it from being stored in this map- Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROHM); return super.replace(key, value);
-
computeIfAbsent
public V computeIfAbsent (K key, java.util.function.Function<? super K,? extends V> mappingFunction)
If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.
If the mapping function returns null, no mapping is recorded. If the mapping function itself throws an (unchecked) exception, the exception is rethrown, and no mapping is recorded. The most common usage is to construct a new object serving as an initial mapped value or memoized result, as in:
map.computeIfAbsent(key, k -> new Value(f(k)));
Or to implement a multi-value map,Map<K,Collection<V>>
, supporting multiple values per key:
map.computeIfAbsent(key, k -> new HashSet<V>()).add(v);
The mapping function should not modify this map during computation.
This method will, on a best-effort basis, throw aConcurrentModificationException
if it is detected that the mapping function modifies this map during computation.
Mutator Method:
This method modifies the contents of this class' internalHashMap
. Note that any method which modifies this internalHashMap
field will throw an exception if invoked after a call tobuild()
.- Specified by:
computeIfAbsent
in interfacejava.util.Map<K,V>
- Overrides:
computeIfAbsent
in classjava.util.HashMap<K,V>
- Parameters:
key
- key with which the specified value is to be associatedmappingFunction
- the mapping function to compute a value- Returns:
- the current (existing or computed) value associated with the specified key, or null if the computed value is null
- Throws:
java.util.ConcurrentModificationException
- if it is detected that the mapping function modified this map- Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROHM); return super.computeIfAbsent(key, mappingFunction);
-
computeIfPresent
public V computeIfPresent (K key, java.util.function.BiFunction<? super K,? super V,? extends V> remappingFunction)
If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.
If the remapping function returns null, the mapping is removed. If the remapping function itself throws an (unchecked) exception, the exception is rethrown, and the current mapping is left unchanged.
The remapping function should not modify this map during computation.
This method will, on a best-effort basis, throw aConcurrentModificationException
if it is detected that the remapping function modifies this map during computation.
Mutator Method:
This method modifies the contents of this class' internalHashMap
. Note that any method which modifies this internalHashMap
field will throw an exception if invoked after a call tobuild()
.- Specified by:
computeIfPresent
in interfacejava.util.Map<K,V>
- Overrides:
computeIfPresent
in classjava.util.HashMap<K,V>
- Parameters:
key
- key with which the specified value is to be associatedremappingFunction
- the remapping function to compute a value- Returns:
- the new value associated with the specified key, or null if none
- Throws:
java.util.ConcurrentModificationException
- if it is detected that the remapping function modified this map- Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROHM); return super.computeIfPresent(key, remappingFunction);
-
compute
public V compute (K key, java.util.function.BiFunction<? super K,? super V,? extends V> remappingFunction)
Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping). For example, to either create or append a String msg to a value mapping:
map.compute(key, (k, v) -> (v == null) ? msg : v.concat(msg))
(Method merge() is often simpler to use for such purposes.)
If the remapping function returns null, the mapping is removed (or remains absent if initially absent). If the remapping function itself throws an (unchecked) exception, the exception is rethrown, and the current mapping is left unchanged.
The remapping function should not modify this map during computation.
This method will, on a best-effort basis, throw aConcurrentModificationException
if it is detected that the remapping function modifies this map during computation.
Mutator Method:
This method modifies the contents of this class' internalHashMap
. Note that any method which modifies this internalHashMap
field will throw an exception if invoked after a call tobuild()
.- Specified by:
compute
in interfacejava.util.Map<K,V>
- Overrides:
compute
in classjava.util.HashMap<K,V>
- Parameters:
key
- key with which the specified value is to be associatedremappingFunction
- the remapping function to compute a value- Returns:
- the new value associated with the specified key, or null if none
- Throws:
java.util.ConcurrentModificationException
- if it is detected that the remapping function modified this map- Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROHM); return super.compute(key, remappingFunction);
-
merge
public V merge (K key, V value, java.util.function.BiFunction<? super V,? super V,? extends V> remappingFunction)
If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value. Otherwise, replaces the associated value with the results of the given remapping function, or removes if the result is null. This method may be of use when combining multiple mapped values for a key. For example, to either create or append a String msg to a value mapping:
map.merge(key, msg, String::concat)
If the remapping function returns null, the mapping is removed. If the remapping function itself throws an (unchecked) exception, the exception is rethrown, and the current mapping is left unchanged.
The remapping function should not modify this map during computation.
This method will, on a best-effort basis, throw aConcurrentModificationException
if it is detected that the remapping function modifies this map during computation.
Mutator Method:
This method modifies the contents of this class' internalHashMap
. Note that any method which modifies this internalHashMap
field will throw an exception if invoked after a call tobuild()
.- Specified by:
merge
in interfacejava.util.Map<K,V>
- Overrides:
merge
in classjava.util.HashMap<K,V>
- Parameters:
key
- key with which the resulting value is to be associatedvalue
- the non-null value to be merged with the existing value associated with the key or, if no existing value or a null value is associated with the key, to be associated with the key- Returns:
- the new value associated with the specified key, or null if no value is associated with the key
- Throws:
java.util.ConcurrentModificationException
- if it is detected that the remapping function modified this map- Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROHM); return super.merge(key, value, remappingFunction);
-
replaceAll
public void replaceAll (java.util.function.BiFunction<? super K,? super V,? extends V> function)
Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception. Exceptions thrown by the function are relayed to the caller.
The implementation is equivalent to:
for (Map.Entry<K, V> entry : map.entrySet()) entry.setValue(function.apply(entry.getKey(), entry.getValue()));
No guarantees about synchronization or atomicity properties of this method.
Mutator Method:
This method modifies the contents of this class' internalHashMap
. Note that any method which modifies this internalHashMap
field will throw an exception if invoked after a call tobuild()
.- Specified by:
replaceAll
in interfacejava.util.Map<K,V>
- Overrides:
replaceAll
in classjava.util.HashMap<K,V>
- Parameters:
function
- the function to apply to each entry- Throws:
java.lang.UnsupportedOperationException
- if the set operation is not supported by this map's entry set iterator.java.lang.ClassCastException
- if the class of a replacement value prevents it from being stored in this map (optional)java.lang.NullPointerException
- if the specified function is null, or if a replacement value is null and this map does not permit null values (optional)java.lang.IllegalArgumentException
- if some property of a replacement value prevents it from being stored in this map (optional)java.util.ConcurrentModificationException
- - if an entry is found to be removed during iteration- Code:
- Exact Method Body:
if (this.built) throw new AttemptedModificationException(ROHM); super.replaceAll(function);
-
entrySet
public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
Restricts a back-door into the underlying data-structure.
UnsupportedOperationException Specifics:
This method invokes the Java StandardCollections
class static-method for 'wrapping' this method's returnedSet
in an Immutable-Wrapper. Java's Collections Framework Immutable-Wrappers have been around since prior to JDK 8, and are easy to use. The wrapper utilized here is needed because this particular method returns a Read-Write "View" into the underlyingHashMap
that facilitates modifying data inside this class' instances.
Once'this'
builder instance has been built into a Read-Only Data-Structure, thisSet
(which possesses methods for mutating theHashMap
) would provide a potential back-door for breaking the Immutable-Contract of theReadOnlyHashMap
that is ultimately built.
Remember that all sub-sets and sub-maps returned by the Java Collections Framework guarantee that any changes to the sub-set or sub-map will be reflected and translated back into the originalCollection
from whence they were created.
The returned instance is usable, but any method that would modify thisHashMap
-Builder will, instead, throw a JavaUnsupportedOperationException
.
-
keySet
public java.util.Set<K> keySet()
Restricts a back-door into the underlying data-structure.
UnsupportedOperationException Specifics:
This method invokes the Java StandardCollections
class static-method for 'wrapping' this method's returnedSet
in an Immutable-Wrapper. Java's Collections Framework Immutable-Wrappers have been around since prior to JDK 8, and are easy to use. The wrapper utilized here is needed because this particular method returns a Read-Write "View" into the underlyingHashMap
that facilitates modifying data inside this class' instances.
Once'this'
builder instance has been built into a Read-Only Data-Structure, thisSet
(which possesses methods for mutating theHashMap
) would provide a potential back-door for breaking the Immutable-Contract of theReadOnlyHashMap
that is ultimately built.
Remember that all sub-sets and sub-maps returned by the Java Collections Framework guarantee that any changes to the sub-set or sub-map will be reflected and translated back into the originalCollection
from whence they were created.
The returned instance is usable, but any method that would modify thisHashMap
-Builder will, instead, throw a JavaUnsupportedOperationException
.
-
values
public java.util.Collection<V> values()
Restricts a back-door into the underlying data-structure.
UnsupportedOperationException Specifics:
This method invokes the Java StandardCollections
class static-method for 'wrapping' this method's returnedCollection
in an Immutable-Wrapper. Java's Collections Framework Immutable-Wrappers have been around since prior to JDK 8, and are easy to use. The wrapper utilized here is needed because this particular method returns a Read-Write "View" into the underlyingHashMap
that facilitates modifying data inside this class' instances.
Once'this'
builder instance has been built into a Read-Only Data-Structure, thisCollection
(which possesses methods for mutating theHashMap
) would provide a potential back-door for breaking the Immutable-Contract of theReadOnlyHashMap
that is ultimately built.
Remember that all sub-sets and sub-maps returned by the Java Collections Framework guarantee that any changes to the sub-set or sub-map will be reflected and translated back into the originalCollection
from whence they were created.
The returned instance is usable, but any method that would modify thisHashMap
-Builder will, instead, throw a JavaUnsupportedOperationException
.
-
equals
public boolean equals(java.lang.Object o)
Compares the specified Object with this Builder for equality, as per the definition in the original classjava.util.HashMap
. Ignores the private field'built'
. If'this'
has not been built, but'o'
has been, that fact is ignored during the equality-comparison.- Specified by:
equals
in interfacejava.util.Map<K,V>
- Overrides:
equals
in classjava.util.AbstractMap<K,V>
- Parameters:
o
- object to be compared for equality with thisROHashMapBuilder
instance- Returns:
- true if the specified Object is equal to this Builder
- Code:
- Exact Method Body:
if (this == o) return true; if (! (o instanceof ROHashMapBuilder)) return false; return super.equals((HashMap) o);
-
clone
public ROHashMapBuilder<K,V> clone()
-
-