Package Torello.Java.ReadOnly
Class ReadOnlyTreeMap<K,V>
- java.lang.Object
-
- Torello.Java.ReadOnly.ReadOnlyTreeMap<K,V>
-
- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values
- All Implemented Interfaces:
java.io.Serializable,ReadOnlyMap<K,V>,ReadOnlyNavigableMap<K,V>,ReadOnlySequencedMap<K,V>,ReadOnlySortedMap<K,V>
public class ReadOnlyTreeMap<K,V> extends java.lang.Object implements ReadOnlyNavigableMap<K,V>, java.io.Serializable
This class was originally copied fromGitHub's Open-JDKAccount. It has been heavily modified as all method bodies have been removed; although few changes were applied to the file's Javadoc Commenting. Method and parameter names & types have not been modified whatsoever - although several methods have been eliminated.
This file contains quite a bit of Wrapper-Code which has been placed around the code from the original Open JDK-21 Source-File of the same (or, rather, highly similar) Class-Name. The original JDK Source-File may be viewed on theJDK-21 GitHubpublic (and, coincidentally, Read-Only) Source-Release 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 V2also be included alongside.Immutable Wrapper forjava.util.TreeMap, found in the "Java Collections Framework".
Immutable Wrapper Class:
This data class contains all of the methods that the standard Java class 'TreeMap' contains - except those which would directly or indirectly modify / mutate the internal data-structure.
In order to guarantee that the contents and values of thisTreeMapremain constant and unchanged, this class retains an internalTreeMap, and assigns it the Java modifier'private'to prevent any outside, unwanted modification. This class' own methods are all trivial, single-line wrappers that invoke the originaljava.utilmethods, but leave out any & all wrappers for methods that have the ability to modify, update, remove or insert values into the internal and privateTreeMap.
Finally, this class offers a Read Only Guarantee, which is enforced by preventing any instantiation of this class from occuring that would allow outside references of its internal, privateTreeMapfield
ImmutableinterfaceInheritance-Tree:
This class implements a variant of the JDK'sjava.util.*Interface-Stack, all of which have been slightly modified (and renamed to add the prefix'ReadOnly'to their names). This decision was necessary because many of the support functions inside Java Collection Framework classes have utility / helper methods that retrieve & generate Sub-Maps and Sub-Sets that are linked to the originalCollection's, Set'sandMap'sfrom whence they were produced.
These Sub-Map / Sub-Set retrieval methods all sing the same refrain - changes to the returnedMap/Setwill be reflected into'this'instance, and vice-versa.
TheseSub-Map'sandSub-Set'swould easily (even though 'accidentally') provide a dangerous back-door means for modifying and updating a Collection that is claiming to be Read-Only & Immutable.
As a result, the decision was made to also port all of thejava.util.*interfaces, such asSet, Map, List & Collection, into the Read-Only Collections Framework by renaming them toReadOnlySet, ReadOnlyMap, & ReadOnlyCollection- and of course to remove / eliminate all Mutator-Methods from those interfaces. This extra step further guarantees the provision that this Data-Class is, truly, Read-Only and un-modifiable.
Self-Referential ReadOnlyTreeMap's
Though likely uncommon in most typical software projects, it is possible for aTreeMapto actually contain a reference to itself. However, in the translation from a standardTreeMapto aReadOnlyTreeMap, there is actually no direct and realizable way to preserve any self-referential pointers inside theReadOnlyTreeMapwhich is being constructed.
Java's Generic Type-System, generally, makes such references nearly (but not completely) impossible. Unless you have assignedjava.lang.Objectto the Data-Class' Generic-Type, there is likely no common Ancestor-Type that would inherit both aReadOnlyTreeMap, and the other objects in your data-set.
As a result, if you happen to pass an instance of data that contains references or pointers to itself, those pointers will not be updated to reference the Read-Only variant of your Data-Structure when that Data-Structure is created. TheReadOnlyTreeMapthat you get back will simply contain pointers to the original Data-Structure you passed to the constructor of this class!- See Also:
- Serialized Form
Hi-Lited Source-Code:- View Here: Torello/Java/ReadOnly/ReadOnlyTreeMap.java
- Open New Browser-Tab: Torello/Java/ReadOnly/ReadOnlyTreeMap.java
File Size: 39,282 Bytes Line Count: 940 '\n' Characters Found
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface Torello.Java.ReadOnly.ReadOnlyMap
ReadOnlyMap.Entry<K,V>
-
-
Field Summary
Serializable ID Modifier and Type Field protected static longserialVersionUID
-
Constructor Summary
Construct an Instance using a Runnable / Tuple2 Combination Constructor ReadOnlyTreeMap(
Tuple2<K,V> refHolder,
Runnable computeNextEntry,
Comparator<? super K> comparator
)Iterable Source: Build an Instance Constructor ReadOnlyTreeMap(
Iterable<? extends K> keys,
Function<? super K,? extends V> valueMapper,
Predicate<? super K> filter,
Comparator<? super K> comparator
)ReadOnlyTreeMap(
Iterable<X> source,
Tuple2<K,V> refHolder,
Consumer<? super X> computeNextEntry,
Predicate<? super X> filter,
Comparator<? super K> comparator
)Map Source: Build Instance Constructor ReadOnlyTreeMap(Map<K,V> map)ReadOnlyTreeMap(
Tuple2<K,V> refHolder,
Map<X,Y> map,
Consumer<Map.Entry<X,Y>> mapTranslator,
Predicate<Map.Entry<X,Y>> filter,
Comparator<? super K> comparator
)Array Source: Build an Instance Constructor ReadOnlyTreeMap(
Predicate<?> filter,
Consumer<?> computeNextEntry,
Tuple2<K,V> refHolder,
Comparator<? super K> comparator,
Object primitiveArray
)ReadOnlyTreeMap(
Tuple2<K,V> refHolder,
Consumer<? super X> computeNextEntry,
Predicate<? super X> filter,
Comparator<? super K> comparator,
X... elements
)
-
Method Summary
Retrieve a TreeMap Value Modifier and Type Method Vget(Object key)Check this TreeMap's Contents Modifier and Type Method booleancontainsKey(Object key)booleancontainsValue(Object value)booleanisEmpty()intsize()Retrieve the Least Map Key Modifier and Type Method KfirstKey()KfloorKey(K key)KlowerKey(K key)Retrieve the Greatest Map Key Modifier and Type Method KceilingKey(K key)KhigherKey(K key)KlastKey()Retrieve the Least Map Key-Value Pair Modifier and Type Method ReadOnlyMap.Entry<K,V>firstEntry()ReadOnlyMap.Entry<K,V>floorEntry(K key)ReadOnlyMap.Entry<K,V>lowerEntry(K key)Retrieve the Greatest Map Key-Value Pair Modifier and Type Method ReadOnlyMap.Entry<K,V>ceilingEntry(K key)ReadOnlyMap.Entry<K,V>higherEntry(K key)ReadOnlyMap.Entry<K,V>lastEntry()Retrieve all Map Key-Value Pairs Modifier and Type Method ReadOnlySet<ReadOnlyMap.Entry<K,
V>>entrySet()Retrieve all Map Keys Modifier and Type Method ReadOnlyNavigableSet<K>descendingKeySet()ReadOnlySet<K>keySet()ReadOnlyNavigableSet<K>navigableKeySet()Retrieve all Map Values Modifier and Type Method ReadOnlyCollection<V>values()Retrieve a Sub-Map as a ReadOnlyNavigableMap Modifier and Type Method ReadOnlyNavigableMap<K,V>headMap(K toKey, boolean inclusive)ReadOnlyNavigableMap<K,V>subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)ReadOnlyNavigableMap<K,V>tailMap(K fromKey, boolean inclusive)Retrieve a Sub-Map as a ReadOnlySortedMap Modifier and Type Method ReadOnlySortedMap<K,V>headMap(K toKey)ReadOnlySortedMap<K,V>subMap(K fromKey, K toKey)ReadOnlySortedMap<K,V>tailMap(K fromKey)Convert 'this' to a Standard 'java.util' Collections-Framework Type Modifier and Type Method Map<K,V>cloneToMap()TreeMap<K,V>cloneToTreeMap()TreeMap<K,V>cloneToTreeMapOLD()Map<K,V>wrapToImmutableMap()Retrieve this Map's Comparator Modifier and Type Method Comparator<? super K>comparator()More Methods Modifier and Type Method ReadOnlyNavigableMap<K,V>descendingMap()voidforEach(BiConsumer<? super K,? super V> action)Retrieve a Singleton Empty-Instance Modifier and Type Method static <X,Y>
ReadOnlyTreeMap<X,Y>emptyROTM()Methods: class java.lang.Object Modifier and Type Method booleanequals(Object o)inthashCode()StringtoString()-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface Torello.Java.ReadOnly.ReadOnlyMap
containsKeyAND, containsKeyAND, containsKeyNAND, containsKeyNAND, containsKeyOR, containsKeyOR, containsKeyXOR, containsKeyXOR, containsValueAND, containsValueAND, containsValueNAND, containsValueNAND, containsValueOR, containsValueOR, containsValueXOR, containsValueXOR, copyIntoMap, getOrDefault
-
Methods inherited from interface Torello.Java.ReadOnly.ReadOnlyNavigableMap
reversed
-
Methods inherited from interface Torello.Java.ReadOnly.ReadOnlySequencedMap
sequencedEntrySet, sequencedKeySet, sequencedValues
-
-
-
-
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 theSerializableImplementation 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
-
ReadOnlyTreeMap
public ReadOnlyTreeMap(java.util.Map<K,V> map)
Copies the contents of parameter'map', and saves saves it, thereby guaranteeing'this'instance is Read-Only and fully-shielded from outside modification.- Parameters:
map- TheMapto be copied into'this'instance internal and private'treeMap'field.- Code:
- Exact Constructor Body:
this.fromBuilderOrTreeMap = false; this.treeMap = (map.size() == 0) ? ((TreeMap<K, V>) EMPTY_TREE_MAP) : new TreeMap<K, V>(map);
-
ReadOnlyTreeMap
public ReadOnlyTreeMap (Tuple2<K,V> refHolder, java.util.Map<X,Y> map, java.util.function.Consumer<java.util.Map.Entry<X,Y>> mapTranslator, java.util.function.Predicate<java.util.Map.Entry<X,Y>> filter, java.util.Comparator<? super K> comparator)
If only a small amount of processing needs to be done on the contents of some Java Map, and using an entire Builder-Class seems disproportionately complex - this constructor can convert any JavaMapinto aReadOnlyTreeMap, using a simple'mapTranslator'.- Type Parameters:
X- The Key-Type of the User-ProvidedMap.Y- The Value-Type of the User-ProvidedMap.- Parameters:
refHolder- This must a non-null instance ofTuple2. The providedConsumeris just that, a'Consumer'rather than a'Function', since the results of each translation must be assigned to the values inside this tuple in order for them to be inserted into thisReadOnlyTreeMap.map- Any JavaMap.mapTranslator- A function for mapping the iterated elements of Map-Types'X'and'Y', into the actualTreeMap'sKey-Type'K', and Value-Type'V'. The results of this translation must be placed into the fields inside'refHolder'.
If this parameter is passed null, this method will throw aNullPointerException.filter- An optional filter that can be used to prevent & prohibit any chosen elements from input'map'from being inserted into'this'ReadOnlyTreeMap.
This parameter may be passed null, and if it is, it will be silently ignored, and all entries present inside'map'will be processed and inserted into'this'comparator- the comparator that will be used to order this set. If null, the natural ordering of the elements will be used.- Throws:
java.lang.NullPointerException- if either parameter'i'or parameter'mapTranslator'is passed null.- Code:
- Exact Constructor Body:
Objects.requireNonNull(refHolder, ROHelpers.NULL_MSG + "'refHolder'"); Objects.requireNonNull(mapTranslator, ROHelpers.NULL_MSG + "'mapTranslator'"); fromBuilderOrTreeMap = false; TreeMap<K, V> treeMap = (comparator != null) ? new TreeMap<>(comparator) : new TreeMap<>(); if (filter == null) for (Map.Entry<X, Y> entry : map.entrySet()) { mapTranslator.accept(entry); treeMap.put(refHolder.a, refHolder.b); } else for (Map.Entry<X, Y> entry : map.entrySet()) { if (! filter.test(entry)) continue; mapTranslator.accept(entry); treeMap.put(refHolder.a, refHolder.b); } // Empty Optimization (throw away, completely, the reference, use static-constant) this.treeMap = (treeMap.size() == 0) ? ((TreeMap<K, V>) EMPTY_TREE_MAP) : treeMap;
-
ReadOnlyTreeMap
public ReadOnlyTreeMap (java.lang.Iterable<? extends K> keys, java.util.function.Function<? super K,? extends V> valueMapper, java.util.function.Predicate<? super K> filter, java.util.Comparator<? super K> comparator)
Constructs an instance ofReadOnlyTreeMapthat contains the keys present in parameter'keys', and values generated by'valueMapper'- using each of the'keys'as input.
Constructor Body:
It is likely of the highest benefit to simply look at the imported Constructor-Body Code-Snippet below to understand the exact mechanics of how this constructor incorporates data into thisReadOnlyTreeMap'sinternalTreeMap.- Parameters:
keys- Any JavaIterableinstance.valueMapper- A user provided function to compute a map value, based on a map key.filter- This parameter may be used to filter some elements returned by the inputIterableinstance.
This parameter may be passed null, and if it is, it will be silently ignored. When it is ignored, all elements from theIterableparameter are processed and inserted.comparator- the comparator that will be used to order this set. If null, the natural ordering of the elements will be used.- Throws:
java.lang.NullPointerException- if either'keys'or'valueMapper'are passed null.- Code:
- Exact Constructor Body:
Objects.requireNonNull(keys, ROHelpers.NULL_MSG + "'keys'"); Objects.requireNonNull(valueMapper, ROHelpers.NULL_MSG + "'valueMapper'"); fromBuilderOrTreeMap = false; TreeMap<K, V> treeMap = (comparator != null) ? new TreeMap<>(comparator) : new TreeMap<>(); if (filter == null) for (K key : keys) treeMap.put(key, valueMapper.apply(key)); else for (K key : keys) if (filter.test(key)) treeMap.put(key, valueMapper.apply(key)); // Empty Optimization (throw away, completely, the reference, use static-constant) this.treeMap = (treeMap.size() == 0) ? ((TreeMap<K, V>) EMPTY_TREE_MAP) : treeMap;
-
ReadOnlyTreeMap
public ReadOnlyTreeMap(Tuple2<K,V> refHolder, java.lang.Runnable computeNextEntry, java.util.Comparator<? super K> comparator)
Constructs an instance ofReadOnlyTreeMapthat has been populated by the Key-Value Pairs left in'refHolder'by each invocation of theRunnableparameter'computeNextEntry'. Key-Value Pairs are inserted until an invocation of theRunnableleaves null inrefHolder.aandrefHolder.b
Constructor Body:
It is likely of the highest benefit to simply look at the imported Constructor-Body Code-Snippet below to understand the exact mechanics of how this constructor incorporates data into thisReadOnlyTreeMap'sinternalTreeMap.- Parameters:
refHolder- Any instance ofTuple2may be passed to this parameter. This reference may not be null, or aNullPointerExceptionwill throw immediately.
This parameter must be used to populate thisReadOnlyTreeMapinstance.
The'computeNextEntry'instance provided to this method, when invoked, should place each Key-Value Pair that it computes into the two fields of this'refHolder'. After invoking the'computeNextEntry'consumer, the contents of thisTuple2are inserted into thisReadOnlyMapinstance.
Please review the Source-Code in the window below. It, hopefully, elucidates how this method retrieves the data to be placed into thisReadOnlyTreeMapusing this'refHolder'.computeNextEntry- ThisRunnableinstance will be called to compute the Key-Value Pairs that are to be inserted into thisReadOnlyTreeMapinstance. The Lambda-Expression or Method provided to thisRunnablemust place each Key-Value Pair into the'refHolder'instance.
After each insertion that is performed, the logic will check if the Key and Value ofTuple2instance have been passed null. When both the Key and the Value are null, this Constructor will cease inserting data into this instance internalTreeMap, and exit.
NOTE: It is acceptabe for thisRunnableto return multiple values for a single Key. If redundant Keys are placed into the'refHolder', the old value assigned to the key will be over-written by the latest value. If both a key and a value placed into'refHolder'are duplicates, both will be reinserted a second time, which doesn't have any effect on the internally storedTreeMap.
Again, for a detailed understanding of how to use this parameter, it is best just to look at the imported Constructor-Body below!comparator- the comparator that will be used to order this set. If null, the natural ordering of the elements will be used.- Throws:
java.lang.NullPointerException- if either'refHolder'or'computeNextEntry'are passed null.- Code:
- Exact Constructor Body:
Objects.requireNonNull(refHolder, ROHelpers.NULL_MSG + "'refHolder'"); Objects.requireNonNull(computeNextEntry, ROHelpers.NULL_MSG + "'computeNextEntry'"); fromBuilderOrTreeMap = false; TreeMap<K, V> treeMap = (comparator != null) ? new TreeMap<>(comparator) : new TreeMap<>(); do { computeNextEntry.run(); if ((refHolder.a == null) && (refHolder.b == null)) break; treeMap.put(refHolder.a, refHolder.b); } while (true); // Empty Optimization (throw away, completely, the reference, use static-constant) this.treeMap = (treeMap.size() == 0) ? ((TreeMap<K, V>) EMPTY_TREE_MAP) : treeMap;
-
ReadOnlyTreeMap
public ReadOnlyTreeMap (java.lang.Iterable<X> source, Tuple2<K,V> refHolder, java.util.function.Consumer<? super X> computeNextEntry, java.util.function.Predicate<? super X> filter, java.util.Comparator<? super K> comparator)
Populates an instance ofReadOnlyTreeMapby iterating the input'source'iterable, and passing each value returned by thatIteratorto the'computeNextEntry'JavaConsumer.
It is the programmer's responsibility to properly place each Key-Value Pair that is intending to be inserted into the finalMapinstance into the'refHolder'instance. After each invocation of'computeNextEntry', this constructor's logic will retrieve the values within'refHolder.a'and'refHolder.b'and insert them into this instance internalTreeMap.
Constructor Body:
It is likely of the highest benefit to simply look at the imported Constructor-Body Code-Snippet below to understand the exact mechanics of how this constructor incorporates data into thisReadOnlyTreeMap'sinternalTreeMap.- Type Parameters:
X- The type of the elements inside'source'- Parameters:
source- Any JavaIterableinstance.refHolder- Any instance ofTuple2may be passed to this parameter. This reference may not be null, or aNullPointerExceptionwill throw immediately.
This parameter must be used to populate thisReadOnlyTreeMapinstance.
The'computeNextEntry'instance provided to this method, when invoked, should place each Key-Value Pair that it computes into the two fields of this'refHolder'. After invoking the'computeNextEntry'consumer, the contents of thisTuple2are inserted into thisReadOnlyMapinstance.
Please review the Source-Code in the window below. It, hopefully, elucidates how this method retrieves the data to be placed into thisReadOnlyTreeMapusing this'refHolder'.computeNextEntry- ThisConsumerinstance will be called to compute the Key-Value Pairs that are to be inserted into thisReadOnlyTreeMapinstance. The Lambda-Expression or Method provided to thisConsumermust place each Key-Value Pair into the'refHolder'instance.
Passed as input to thisConsumerwill simply be the elements from the input'source'parameterIterable-instance (which is iterated in this constructor's body).
NOTE: It is acceptabe for thisConsumerto return multiple values for a single Key. If redundant keys are placed into the'refHolder', the old value assigned to the key will be over-written by the latest value. If both a key and a value placed into'refHolder'are duplicates, both will be reinserted a second time, which doesn't have any effect on the internally storedTreeMap.
Again, for a detailed understanding of how to use this parameter, it is best just to look at the imported Constructor-Body below!filter- This parameter may be used to filter some elements returned by the inputIterableinstance.
This parameter may be passed null, and if it is, it will be silently ignored. When it is ignored, all elements from theIterableparameter are processed and inserted.comparator- the comparator that will be used to order this set. If null, the natural ordering of the elements will be used.- Throws:
java.lang.NullPointerException- if either'refHolder','computeNextEntry'or'source'are passed null.- Code:
- Exact Constructor Body:
Objects.requireNonNull(refHolder, ROHelpers.NULL_MSG + "'refHolder'"); Objects.requireNonNull(computeNextEntry, ROHelpers.NULL_MSG + "'computeNextEntry'"); fromBuilderOrTreeMap = false; TreeMap<K, V> treeMap = (comparator != null) ? new TreeMap<>(comparator) : new TreeMap<>(); X x; // temp var Iterator<X> iter = source.iterator(); if (filter == null) while (iter.hasNext()) { computeNextEntry.accept(iter.next()); treeMap.put(refHolder.a, refHolder.b); } else while (iter.hasNext()) if (filter.test(x = iter.next())) { computeNextEntry.accept(x); treeMap.put(refHolder.a, refHolder.b); } // Empty Optimization (throw away, completely, the reference, use static-constant) this.treeMap = (treeMap.size() == 0) ? ((TreeMap<K, V>) EMPTY_TREE_MAP) : treeMap;
-
ReadOnlyTreeMap
public ReadOnlyTreeMap (Tuple2<K,V> refHolder, java.util.function.Consumer<? super X> computeNextEntry, java.util.function.Predicate<? super X> filter, java.util.Comparator<? super K> comparator, X... elements)
Retrieves elements from the VarArgs Generic-Array parameter'elements', and subsequently invokes the'computeNextEntry'processor to populate thisReadOnlyTreeMap.- Type Parameters:
X- The type of array parameter'elements'- Parameters:
refHolder- Any instance ofTuple2may be passed to this parameter. This reference may not be null, or aNullPointerExceptionwill throw immediately.
This parameter must be used to populate thisReadOnlyTreeMapinstance.
The'computeNextEntry'instance provided to this method, when invoked, should place each Key-Value Pair that it computes into the two fields of this'refHolder'. After invoking the'computeNextEntry'consumer, the contents of thisTuple2are inserted into thisReadOnlyMapinstance.
Please review the Source-Code in the window below. It, hopefully, elucidates how this method retrieves the data to be placed into thisReadOnlyTreeMapusing this'refHolder'.computeNextEntry- ThisConsumerinstance will be called to compute the Key-Value Pairs that are to be inserted into thisReadOnlyTreeMapinstance. The Lambda-Expression or Method provided to thisConsumermust place each Key-Value Pair into the'refHolder'instance.
Passed as input to thisConsumerwill simply be the elements from the user-provided input-array.
NOTE: It is acceptabe for thisConsumerto return multiple values for a single Key. If redundant keys are placed into the'refHolder', the old value assigned to the key will be over-written by the latest value. If both a key and a value placed into'refHolder'are duplicates, both will be reinserted a second time, which doesn't have any effect on the internally storedTreeMap.
Again, for a detailed understanding of how to use this parameter, it is best just to look at the imported Constructor-Body below!filter- This parameter may be used to filter elements from the array-instance, preventing them from being inserted into theReadOnlyTreeMap.
This parameter may be passed null, and if it is, it will be silently ignored. When it is ignored, all elements from the array parameter are processed and inserted.comparator- the comparator that will be used to order this set. If null, the natural ordering of the elements will be used.elements- Any Generic VarArgs-Array- Throws:
java.lang.ClassCastException- If'primitiveArray'cannot be cast to a One-Dimensional, Java Primitive-Array.java.lang.NullPointerException- if either'refHolder'or'computeNextEntry'are passed null- Code:
- Exact Constructor Body:
Objects.requireNonNull(refHolder, ROHelpers.NULL_MSG + "'refHolder'"); Objects.requireNonNull(computeNextEntry, ROHelpers.NULL_MSG + "'computeNextEntry'"); this.fromBuilderOrTreeMap = false; TreeMap<K, V> treeMap = (comparator != null) ? new TreeMap<>(comparator) : new TreeMap<>(); if (filter == null) for (X e : elements) { computeNextEntry.accept(e); treeMap.put(refHolder.a, refHolder.b); } else for (X x : elements) if (filter.test(x)) { computeNextEntry.accept(x); treeMap.put(refHolder.a, refHolder.b); } // Empty Optimization (throw away, completely, the reference, use static-constant) this.treeMap = (treeMap.size() == 0) ? ((TreeMap<K, V>) EMPTY_TREE_MAP) : treeMap;
-
ReadOnlyTreeMap
public ReadOnlyTreeMap(java.util.function.Predicate<?> filter, java.util.function.Consumer<?> computeNextEntry, Tuple2<K,V> refHolder, java.util.Comparator<? super K> comparator, java.lang.Object primitiveArray)
Retrieves elements from the Java Primitive-Array parameter'primitiveArray', and subsequently invokes the'computeNextEntry'processor to populate thisReadOnlyTreeMap.- Parameters:
filter- The JavaPredicatepassed to this parameter must have atest(...)method that can handle input-references of the Java Boxed-Type that corresponds to the Java-Primitive Type used in parameter'primitiveArray'.
In English this means, If you have pased anint[]-Array to'primitiveArray', and would like to filter some of the elements out of yourReadOnlyTreeMap, then you should pass aPredicate<java.lang.Integer>to this parameter.
If you have passed achar[]-Array, then this parameter should be aPredicate<java.lang.Character>- and so on and so forth...
NOTE: This is, indeed, a Run-Time rather than a Compile-Time Type-Solution. If you would like to know what that means (in plain English), it is as follows: There isn't a way to request that the Java-Compiler ensure, and print error-messages on error, that you have passed aPredicate<Integer>for anint[]-Array and aPredicate<Float>for afloat[]-Array.
If an erroneousPredicateis passed to parameter'filter', then, during Run-Time, aClassCastExceptionwill throw - rather than having a Compile-Time Error generated.computeNextEntry- ThisConsumerinstance will be called to compute the Key-Value Pairs that are to be inserted into thisReadOnlyTreeMapinstance. The Lambda-Expression or Method provided to thisConsumermust place each Key-Value Pair into the'refHolder'instance.
Passed as input to thisConsumerwill simply be the elements from the user-provided input-array.
NOTE: It is acceptabe for thisConsumerto return multiple values for a single Key. If redundant keys are placed into the'refHolder', the old value assigned to the key will be over-written by the latest value. If both a key and a value placed into'refHolder'are duplicates, both will be reinserted a second time, which doesn't have any effect on the internally storedTreeMap.
Again, for a detailed understanding of how to use this parameter, it is best just to look at the imported Constructor-Body below!refHolder- Any instance ofTuple2may be passed to this parameter. This reference may not be null, or aNullPointerExceptionwill throw immediately.
This parameter must be used to populate thisReadOnlyTreeMapinstance.
The'computeNextEntry'instance provided to this method, when invoked, should place each Key-Value Pair that it computes into the two fields of this'refHolder'. After invoking the'computeNextEntry'consumer, the contents of thisTuple2are inserted into thisReadOnlyMapinstance.
Please review the Source-Code in the window below. It, hopefully, elucidates how this method retrieves the data to be placed into thisReadOnlyTreeMapusing this'refHolder'.comparator- the comparator that will be used to order this set. If null, the natural ordering of the elements will be used.primitiveArray- Any Java Primitive-Array- Throws:
java.lang.ClassCastException- If'primitiveArray'cannot be cast to a One-Dimensional, Java Primitive-Array.java.lang.NullPointerException- if either'refHolder'or'computeNextEntry'are passed null- Code:
- Exact Constructor Body:
Objects.requireNonNull(refHolder, ROHelpers.NULL_MSG + "'refHolder'"); Objects.requireNonNull(computeNextEntry, ROHelpers.NULL_MSG + "'computeNextEntry'"); this.fromBuilderOrTreeMap = false; TreeMap<K, V> treeMap = ROHelperPrimitiveArrays.buildROMap( primitiveArray, (int arrayLen) -> (comparator != null) ? new TreeMap<>(comparator) : new TreeMap<>(), filter, refHolder, computeNextEntry ); // Empty Optimization (throw away, completely, the reference, use static-constant) this.treeMap = (treeMap.size() == 0) ? ((TreeMap<K, V>) EMPTY_TREE_MAP) : treeMap;
-
-
Method Detail
-
emptyROTM
public static <X,Y> ReadOnlyTreeMap<X,Y> emptyROTM()
Returns an empty, singleton, instance ofReadOnlyTreeMap.- Type Parameters:
X- ReturnedReadOnlyMap's Key-Type.Y- ReturnedReadOnlyMap's Value-Type.- Returns:
- An empty map. Since this map is both empty & read-only, a raw-type singleton
will suffice for all operations offered by this clas.
NOTE: The instance that is returned is astatic, finalconstant which is constructed during the loading of this class by the Class-Loader. It is fully Thread-Safe, unless it is going to be used as an Object-Monitor to help lock a Synchronized Code-Block! - Code:
- Exact Method Body:
return (ReadOnlyTreeMap<X, Y>) EMPTY_READONLY_TREE_MAP;
-
cloneToTreeMapOLD
public java.util.TreeMap<K,V> cloneToTreeMapOLD()
Clone's'this'instance internalTreeMap<K, V>field, and returns it. This method generates a Read / Write Cloned Version of'this'instance' internalReadOnlyTreeMap. The instance that's returned by this method is a clone, and this decision preserves the immutable (Read-Only) protection offered by this class.
Standard JDK TreeMap
The returned object is a Standard JDKTreeMapdata-structure (the one from packagejava.util). It is mutable, but again because it is a clone of'this'instance's internalTreeMap, it is not actually 'linked' to the internalTreeMap
This does mean that changes to the returnedTreeMapinstance will not be reflected to the'this' ReadOnlyTreeMap. They are, in fact, two independent and unlinked copies of the internal data.- Returns:
- An independent, mutable copy of
'this'instance internalTreeMap<K, V>data-structure. - Code:
- Exact Method Body:
if (! fromBuilderOrTreeMap) return (TreeMap<K, V>) this.treeMap.clone(); TreeMap<K, V> ret = new TreeMap<K, V>(); for (Map.Entry<K, V> e : ((ROTreeMapBuilder<K, V>) this.treeMap)._entrySet(friendClassBadge)) ret.put(e.getKey(), e.getValue()); return ret;
-
size
public int size()
Returns the number of key-value mappings in this map.- Specified by:
sizein interfaceReadOnlyMap<K,V>- Returns:
- the number of key-value mappings in this map
- Code:
- Exact Method Body:
return this.treeMap.size();
-
containsKey
public boolean containsKey(java.lang.Object key)
ReturnsTRUEif this map contains a mapping for the specified key.- Specified by:
containsKeyin interfaceReadOnlyMap<K,V>- Parameters:
key- key whose presence in this map is to be tested- Returns:
TRUEif this map contains a mapping for the specified key- Throws:
java.lang.ClassCastException- if the specified key cannot be compared with the keys currently in the mapjava.lang.NullPointerException- if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys- Code:
- Exact Method Body:
return this.treeMap.containsKey(key);
-
containsValue
public boolean containsValue(java.lang.Object value)
ReturnsTRUEif this map maps one or more keys to the specified value. More formally, returnsTRUEif and only if this map contains at least one mapping to a valuevsuch that(value==null ? v==null : value.equals(v)).- Specified by:
containsValuein interfaceReadOnlyMap<K,V>- Parameters:
value- value whose presence in this map is to be tested- Returns:
TRUEif a mapping tovalueexists;FALSEotherwise- Code:
- Exact Method Body:
return this.treeMap.containsValue(value);
-
get
public V get(java.lang.Object key)
Returns the value to which the specified key is mapped, ornullif this map contains no mapping for the key.
More formally, if this map contains a mapping from a keykto a valuevsuch thatkeycompares equal tokaccording to the map's ordering, then this method returnsv; otherwise it returnsnull. (There can be at most one such mapping.)
A return value ofnulldoes not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key tonull. ThecontainsKeyoperation may be used to distinguish these two cases.- Specified by:
getin interfaceReadOnlyMap<K,V>- Parameters:
key- the key whose associated value is to be returned- Returns:
- the value to which the specified key is mapped, or
nullif this map contains no mapping for the key - Throws:
java.lang.ClassCastException- if the specified key cannot be compared with the keys currently in the mapjava.lang.NullPointerException- if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys- Code:
- Exact Method Body:
return this.treeMap.get(key);
-
comparator
public java.util.Comparator<? super K> comparator()
Description copied from interface:ReadOnlySortedMapReturns the comparator used to order the keys in this map, ornullif this map uses thenatural orderingof its keys.- Specified by:
comparatorin interfaceReadOnlySortedMap<K,V>- Returns:
- the comparator used to order the keys in this map, or
nullif this map uses the natural ordering of its keys - Code:
- Exact Method Body:
return this.treeMap.comparator();
-
firstKey
public K firstKey()
Description copied from interface:ReadOnlySortedMapReturns the first (lowest) key currently in this map.- Specified by:
firstKeyin interfaceReadOnlySortedMap<K,V>- Returns:
- the first (lowest) key currently in this map
- Throws:
java.util.NoSuchElementException- Code:
- Exact Method Body:
return this.treeMap.firstKey();
-
lastKey
public K lastKey()
Description copied from interface:ReadOnlySortedMapReturns the last (highest) key currently in this map.- Specified by:
lastKeyin interfaceReadOnlySortedMap<K,V>- Returns:
- the last (highest) key currently in this map
- Throws:
java.util.NoSuchElementException- Code:
- Exact Method Body:
return this.treeMap.lastKey();
-
firstEntry
public ReadOnlyMap.Entry<K,V> firstEntry()
Description copied from interface:ReadOnlyNavigableMapReturns a key-value mapping associated with the least key in this map, ornullif the map is empty.- Specified by:
firstEntryin interfaceReadOnlyNavigableMap<K,V>- Specified by:
firstEntryin interfaceReadOnlySequencedMap<K,V>- Returns:
- an entry with the least key, or
nullif this map is empty - Code:
- Exact Method Body:
return new EntryImpl<>(this.treeMap.firstEntry());
-
lastEntry
public ReadOnlyMap.Entry<K,V> lastEntry()
Description copied from interface:ReadOnlyNavigableMapReturns a key-value mapping associated with the greatest key in this map, ornullif the map is empty.- Specified by:
lastEntryin interfaceReadOnlyNavigableMap<K,V>- Specified by:
lastEntryin interfaceReadOnlySequencedMap<K,V>- Returns:
- an entry with the greatest key, or
nullif this map is empty - Code:
- Exact Method Body:
return new EntryImpl<>(this.treeMap.lastEntry());
-
lowerEntry
public ReadOnlyMap.Entry<K,V> lowerEntry(K key)
Description copied from interface:ReadOnlyNavigableMapReturns a key-value mapping associated with the greatest key strictly less than the given key, ornullif there is no such key.- Specified by:
lowerEntryin interfaceReadOnlyNavigableMap<K,V>- Parameters:
key- the key- Returns:
- an entry with the greatest key less than
key, ornullif there is no such key - Throws:
java.lang.ClassCastException- if the specified key cannot be compared with the keys currently in the mapjava.lang.NullPointerException- if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys- Code:
- Exact Method Body:
return new EntryImpl<>(this.treeMap.lowerEntry(key));
-
lowerKey
public K lowerKey(K key)
Description copied from interface:ReadOnlyNavigableMapReturns the greatest key strictly less than the given key, ornullif there is no such key.- Specified by:
lowerKeyin interfaceReadOnlyNavigableMap<K,V>- Parameters:
key- the key- Returns:
- the greatest key less than
key, ornullif there is no such key - Throws:
java.lang.ClassCastException- if the specified key cannot be compared with the keys currently in the mapjava.lang.NullPointerException- if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys- Code:
- Exact Method Body:
return this.treeMap.lowerKey(key);
-
floorEntry
public ReadOnlyMap.Entry<K,V> floorEntry(K key)
Description copied from interface:ReadOnlyNavigableMapReturns a key-value mapping associated with the greatest key less than or equal to the given key, ornullif there is no such key.- Specified by:
floorEntryin interfaceReadOnlyNavigableMap<K,V>- Parameters:
key- the key- Returns:
- an entry with the greatest key less than or equal to
key, ornullif there is no such key - Throws:
java.lang.ClassCastException- if the specified key cannot be compared with the keys currently in the mapjava.lang.NullPointerException- if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys- Code:
- Exact Method Body:
return new EntryImpl<>(this.treeMap.floorEntry(key));
-
floorKey
public K floorKey(K key)
Description copied from interface:ReadOnlyNavigableMapReturns the greatest key less than or equal to the given key, ornullif there is no such key.- Specified by:
floorKeyin interfaceReadOnlyNavigableMap<K,V>- Parameters:
key- the key- Returns:
- the greatest key less than or equal to
key, ornullif there is no such key - Throws:
java.lang.ClassCastException- if the specified key cannot be compared with the keys currently in the mapjava.lang.NullPointerException- if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys- Code:
- Exact Method Body:
return this.treeMap.floorKey(key);
-
ceilingEntry
public ReadOnlyMap.Entry<K,V> ceilingEntry(K key)
Description copied from interface:ReadOnlyNavigableMapReturns a key-value mapping associated with the least key greater than or equal to the given key, ornullif there is no such key.- Specified by:
ceilingEntryin interfaceReadOnlyNavigableMap<K,V>- Parameters:
key- the key- Returns:
- an entry with the least key greater than or equal to
key, ornullif there is no such key - Throws:
java.lang.ClassCastException- if the specified key cannot be compared with the keys currently in the mapjava.lang.NullPointerException- if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys- Code:
- Exact Method Body:
return new EntryImpl<>(this.treeMap.ceilingEntry(key));
-
ceilingKey
public K ceilingKey(K key)
Description copied from interface:ReadOnlyNavigableMapReturns the least key greater than or equal to the given key, ornullif there is no such key.- Specified by:
ceilingKeyin interfaceReadOnlyNavigableMap<K,V>- Parameters:
key- the key- Returns:
- the least key greater than or equal to
key, ornullif there is no such key - Throws:
java.lang.ClassCastException- if the specified key cannot be compared with the keys currently in the mapjava.lang.NullPointerException- if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys- Code:
- Exact Method Body:
return this.treeMap.ceilingKey(key);
-
higherEntry
public ReadOnlyMap.Entry<K,V> higherEntry(K key)
Description copied from interface:ReadOnlyNavigableMapReturns a key-value mapping associated with the least key strictly greater than the given key, ornullif there is no such key.- Specified by:
higherEntryin interfaceReadOnlyNavigableMap<K,V>- Parameters:
key- the key- Returns:
- an entry with the least key greater than
key, ornullif there is no such key - Throws:
java.lang.ClassCastException- if the specified key cannot be compared with the keys currently in the mapjava.lang.NullPointerException- if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys- Code:
- Exact Method Body:
return new EntryImpl<>(this.treeMap.higherEntry(key));
-
higherKey
public K higherKey(K key)
Description copied from interface:ReadOnlyNavigableMapReturns the least key strictly greater than the given key, ornullif there is no such key.- Specified by:
higherKeyin interfaceReadOnlyNavigableMap<K,V>- Parameters:
key- the key- Returns:
- the least key greater than
key, ornullif there is no such key - Throws:
java.lang.ClassCastException- if the specified key cannot be compared with the keys currently in the mapjava.lang.NullPointerException- if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys- Code:
- Exact Method Body:
return this.treeMap.higherKey(key);
-
keySet
public ReadOnlySet<K> keySet()
Returns aReadOnlySetview of the keys contained in this map.- Specified by:
keySetin interfaceReadOnlyMap<K,V>- Specified by:
keySetin interfaceReadOnlySortedMap<K,V>- Returns:
- a set view of the keys contained in this map, sorted in ascending order
- Code:
- Exact Method Body:
return new JavaHTMLReadOnlySet<>( fromBuilderOrTreeMap ? ((ROTreeMapBuilder<K, V>) this.treeMap)._keySet(friendClassBadge) : this.treeMap.keySet() );
-
navigableKeySet
public ReadOnlyNavigableSet<K> navigableKeySet()
Description copied from interface:ReadOnlyNavigableMapReturns aReadOnlyNavigableSetview of the keys contained in this map. The set's iterator returns the keys in ascending order.- Specified by:
navigableKeySetin interfaceReadOnlyNavigableMap<K,V>- Returns:
- a navigable set view of the keys in this map
- Code:
- Exact Method Body:
return new JavaHTMLReadOnlyNavigableSet<>( fromBuilderOrTreeMap ? ((ROTreeMapBuilder<K, V>) this.treeMap)._navigableKeySet(friendClassBadge) : this.treeMap.navigableKeySet() );
-
descendingKeySet
public ReadOnlyNavigableSet<K> descendingKeySet()
Description copied from interface:ReadOnlyNavigableMapReturns a reverse orderReadOnlyNavigableSetview of the keys contained in this map. The set's iterator returns the keys in descending order.- Specified by:
descendingKeySetin interfaceReadOnlyNavigableMap<K,V>- Returns:
- a reverse order navigable set view of the keys in this map
- Code:
- Exact Method Body:
return new JavaHTMLReadOnlyNavigableSet<>( fromBuilderOrTreeMap ? ((ROTreeMapBuilder<K, V>) this.treeMap)._descendingKeySet(friendClassBadge) : this.treeMap.descendingKeySet() );
-
values
public ReadOnlyCollection<V> values()
Returns aReadOnlyCollectionview of the values contained in this map.- Specified by:
valuesin interfaceReadOnlyMap<K,V>- Specified by:
valuesin interfaceReadOnlySortedMap<K,V>- Returns:
- a collection view of the values contained in this map, sorted in ascending key order
- Code:
- Exact Method Body:
return new JavaHTMLReadOnlyCollection<>( fromBuilderOrTreeMap ? ((ROTreeMapBuilder<K, V>) this.treeMap)._values(friendClassBadge) : this.treeMap.values() );
-
entrySet
public ReadOnlySet<ReadOnlyMap.Entry<K,V>> entrySet()
Returns aReadOnlySetview of the mappings contained in this map.- Specified by:
entrySetin interfaceReadOnlyMap<K,V>- Specified by:
entrySetin interfaceReadOnlySortedMap<K,V>- Returns:
- At the moment, a
ReadOnlyTreeSetis arbitrarily used for the type of the returned instance - Code:
- Exact Method Body:
return ROHelperEntrySet.toReadOnlyEntrySet( fromBuilderOrTreeMap ? ((ROTreeMapBuilder<K, V>) this.treeMap)._entrySet(friendClassBadge) : this.treeMap.entrySet() );
-
descendingMap
public ReadOnlyNavigableMap<K,V> descendingMap()
Description copied from interface:ReadOnlyNavigableMapReturns a reverse order view of the mappings contained in this map.- Specified by:
descendingMapin interfaceReadOnlyNavigableMap<K,V>- Returns:
- a reverse order view of this map
- Code:
- Exact Method Body:
return new JavaHTMLReadOnlyNavigableMap<>( fromBuilderOrTreeMap ? ((ROTreeMapBuilder<K, V>) this.treeMap)._descendingMap(friendClassBadge) : this.treeMap.descendingMap() );
-
subMap
public ReadOnlyNavigableMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)
Description copied from interface:ReadOnlyNavigableMapReturns a view of the portion of this map whose keys range fromfromKeytotoKey. IffromKeyandtoKeyare equal, the returned map is empty unlessfromInclusiveandtoInclusiveare both true.
The returned map supports all optional map operations that this map supports.- Specified by:
subMapin interfaceReadOnlyNavigableMap<K,V>- Parameters:
fromKey- low endpoint of the keys in the returned mapfromInclusive-TRUEif the low endpoint is to be included in the returned viewtoKey- high endpoint of the keys in the returned maptoInclusive-TRUEif the high endpoint is to be included in the returned view- Returns:
- a view of the portion of this map whose keys range from
fromKeytotoKey - Throws:
java.lang.ClassCastException- iffromKeyandtoKeycannot be compared to one another using this map's comparator (or, if the map has no comparator, using natural ordering). Implementations may, but are not required to, throw this exception iffromKeyortoKeycannot be compared to keys currently in the map.java.lang.NullPointerException- iffromKeyortoKeyis null and this map uses natural ordering, or its comparator does not permit null keysjava.lang.IllegalArgumentException- iffromKeyis greater thantoKey; or if this map itself has a restricted range, andfromKeyortoKeylies outside the bounds of the range- Code:
- Exact Method Body:
return new JavaHTMLReadOnlyNavigableMap<>( fromBuilderOrTreeMap ? ((ROTreeMapBuilder<K, V>) this.treeMap)._subMap (fromKey, fromInclusive, toKey, toInclusive, friendClassBadge) : this.treeMap.subMap(fromKey, fromInclusive, toKey, toInclusive) );
-
headMap
public ReadOnlyNavigableMap<K,V> headMap(K toKey, boolean inclusive)
Description copied from interface:ReadOnlyNavigableMapReturns a view of the portion of this map whose keys are less than (or equal to, ifinclusiveis true)toKey. The returned map supports all optional map operations that this map supports.- Specified by:
headMapin interfaceReadOnlyNavigableMap<K,V>- Parameters:
toKey- high endpoint of the keys in the returned mapinclusive-TRUEif the high endpoint is to be included in the returned view- Returns:
- a view of the portion of this map whose keys are less than (or equal to, if
inclusiveis true)toKey - Throws:
java.lang.NullPointerException- iftoKeyis nullvand this map uses natural ordering, or its comparator does not permit null keysjava.lang.IllegalArgumentException- if this map itself has a restricted range, andtoKeylies outside the bounds of the range- Code:
- Exact Method Body:
return new JavaHTMLReadOnlyNavigableMap<>( fromBuilderOrTreeMap ? ((ROTreeMapBuilder<K, V>) this.treeMap)._headMap (toKey, inclusive, friendClassBadge) : this.treeMap.headMap(toKey, inclusive) );
-
tailMap
public ReadOnlyNavigableMap<K,V> tailMap(K fromKey, boolean inclusive)
Description copied from interface:ReadOnlyNavigableMapReturns a view of the portion of this map whose keys are greater than (or equal to, ifinclusiveis true)fromKey. The returned map supports all optional map operations that this map supports.- Specified by:
tailMapin interfaceReadOnlyNavigableMap<K,V>- Parameters:
fromKey- low endpoint of the keys in the returned mapinclusive-TRUEif the low endpoint is to be included in the returned view- Returns:
- a view of the portion of this map whose keys are greater than (or equal to, if
inclusiveis true)fromKey - Throws:
java.lang.ClassCastException- iffromKeyis not compatible with this map's comparator (or, if the map has no comparator, iffromKeydoes not implementComparable). Implementations may, but are not required to, throw this exception iffromKeycannot be compared to keys currently in the map.java.lang.NullPointerException- iffromKeyis null and this map uses natural ordering, or its comparator does not permit null keysjava.lang.IllegalArgumentException- if this map itself has a restricted range, andfromKeylies outside the bounds of the range- Code:
- Exact Method Body:
return new JavaHTMLReadOnlyNavigableMap<>( fromBuilderOrTreeMap ? ((ROTreeMapBuilder<K, V>) this.treeMap)._tailMap (fromKey, inclusive, friendClassBadge) : this.treeMap.tailMap(fromKey, inclusive) );
-
subMap
public ReadOnlySortedMap<K,V> subMap(K fromKey, K toKey)
Description copied from interface:ReadOnlyNavigableMapReturns a view of the portion of this map whose keys range fromfromKey, inclusive, totoKey, exclusive. (IffromKeyandtoKeyare equal, the returned map is empty.) The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.
The returned map will throw anIllegalArgumentExceptionon an attempt to insert a key outside its range.
Equivalent tosubMap(fromKey, true, toKey, false).- Specified by:
subMapin interfaceReadOnlyNavigableMap<K,V>- Specified by:
subMapin interfaceReadOnlySortedMap<K,V>- Parameters:
fromKey- low endpoint (inclusive) of the keys in the returned maptoKey- high endpoint (exclusive) of the keys in the returned map- Returns:
- a view of the portion of this map whose keys range from
fromKey, inclusive, totoKey, exclusive - Throws:
java.lang.ClassCastException- iffromKeyandtoKeycannot be compared to one another using this map's comparator (or, if the map has no comparator, using natural ordering). Implementations may, but are not required to, throw this exception iffromKeyortoKeycannot be compared to keys currently in the map.java.lang.NullPointerException- iffromKeyortoKeyis null and this map uses natural ordering, or its comparator does not permit null keysjava.lang.IllegalArgumentException- iffromKeyis greater thantoKey; or if this map itself has a restricted range, andfromKeyortoKeylies outside the bounds of the range- Code:
- Exact Method Body:
return new JavaHTMLReadOnlySortedMap<>( fromBuilderOrTreeMap ? ((ROTreeMapBuilder<K, V>) this.treeMap)._subMap (fromKey, toKey, friendClassBadge) : this.treeMap.subMap(fromKey, toKey) );
-
headMap
public ReadOnlySortedMap<K,V> headMap(K toKey)
Description copied from interface:ReadOnlyNavigableMapReturns a view of the portion of this map whose keys are strictly less thantoKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.
The returned map will throw anIllegalArgumentExceptionon an attempt to insert a key outside its range.
Equivalent toheadMap(toKey, false).- Specified by:
headMapin interfaceReadOnlyNavigableMap<K,V>- Specified by:
headMapin interfaceReadOnlySortedMap<K,V>- Parameters:
toKey- high endpoint (exclusive) of the keys in the returned map- Returns:
- a view of the portion of this map whose keys are strictly less than
toKey - Throws:
java.lang.ClassCastException- iftoKeyis not compatible with this map's comparator (or, if the map has no comparator, iftoKeydoes not implementComparable). Implementations may, but are not required to, throw this exception iftoKeycannot be compared to keys currently in the map.java.lang.NullPointerException- iftoKeyis null and this map uses natural ordering, or its comparator does not permit null keysjava.lang.IllegalArgumentException- if this map itself has a restricted range, andtoKeylies outside the bounds of the range- Code:
- Exact Method Body:
return new JavaHTMLReadOnlySortedMap<>( fromBuilderOrTreeMap ? ((ROTreeMapBuilder<K, V>) this.treeMap)._headMap(toKey, friendClassBadge) : this.treeMap.headMap(toKey) );
-
tailMap
public ReadOnlySortedMap<K,V> tailMap(K fromKey)
Description copied from interface:ReadOnlyNavigableMapReturns a view of the portion of this map whose keys are greater than or equal tofromKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.
The returned map will throw anIllegalArgumentExceptionon an attempt to insert a key outside its range.
Equivalent totailMap(fromKey, true).- Specified by:
tailMapin interfaceReadOnlyNavigableMap<K,V>- Specified by:
tailMapin interfaceReadOnlySortedMap<K,V>- Parameters:
fromKey- low endpoint (inclusive) of the keys in the returned map- Returns:
- a view of the portion of this map whose keys are greater than or equal to
fromKey - Throws:
java.lang.ClassCastException- iffromKeyis not compatible with this map's comparator (or, if the map has no comparator, iffromKeydoes not implementComparable). Implementations may, but are not required to, throw this exception iffromKeycannot be compared to keys currently in the map.java.lang.NullPointerException- iffromKeyis null and this map uses natural ordering, or its comparator does not permit null keysjava.lang.IllegalArgumentException- if this map itself has a restricted range, andfromKeylies outside the bounds of the range- Code:
- Exact Method Body:
return new JavaHTMLReadOnlySortedMap<>( fromBuilderOrTreeMap ? ((ROTreeMapBuilder<K, V>) this.treeMap)._tailMap(fromKey, friendClassBadge) : this.treeMap.tailMap(fromKey) );
-
forEach
public void forEach (java.util.function.BiConsumer<? super K,? super V> action)
Description copied from interface:ReadOnlyMapPerforms the given action for each entry in this map until all entries have been processed or the action throws an exception. Unless otherwise specified by the implementing class, actions are performed in the order of entry set iteration (if an iteration order is specified.) Exceptions thrown by the action are relayed to the caller.
The default implementation is equivalent to, for thismap:
for (ReadOnlyMap.Entry<K, V> entry : map.entrySet()) action.accept(entry.getKey(), entry.getValue());
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.- Specified by:
forEachin interfaceReadOnlyMap<K,V>- Parameters:
action- The action to be performed for each entry- Code:
- Exact Method Body:
this.treeMap.forEach(action);
-
isEmpty
public boolean isEmpty()
Description copied from interface:ReadOnlyMapReturnsTRUEif this map contains no key-value mappings.- Specified by:
isEmptyin interfaceReadOnlyMap<K,V>- Returns:
TRUEif this map contains no key-value mappings- Code:
- Exact Method Body:
return this.treeMap.isEmpty();
-
cloneToTreeMap
public java.util.TreeMap<K,V> cloneToTreeMap()
Clone's'this'instance internalTreeMap<K, V>field, and returns it. This method generates a Read / Write Cloned Version of'this'instance' internalReadOnlyTreeMap. The instance that's returned by this method is a clone, and this decision preserves the immutable (Read-Only) protection offered by this class.
Standard JDK TreeMap
The returned object is a Standard JDKTreeMapdata-structure (the one from packagejava.util). It is mutable, but again because it is a clone of'this'instance's internalTreeMap, it is not actually 'linked' to the internalTreeMap
This does mean that changes to the returnedTreeMapinstance will not be reflected to the'this' ReadOnlyTreeMap. They are, in fact, two independent and unlinked copies of the internal data.- Returns:
- An independent, mutable copy of
'this'instance' internalTreeMap<K, V>data-structure. - Code:
- Exact Method Body:
return fromBuilderOrTreeMap ? new TreeMap<K, V>(this.treeMap) : (TreeMap<K, V>) this.treeMap.clone();
-
cloneToMap
public java.util.Map<K,V> cloneToMap()
Converts this ReadOnlyTreeMap to a java.util.TreeMapSame: Identical to the method cloneToTreeMap().Returns: Has Return-Type Map<K, V>, instead.Implements: Parent-Class ReadOnlyMap.cloneToMap()- Specified by:
cloneToMapin interfaceReadOnlyMap<K,V>- Returns:
- For the sub-class
ReadOnlyHashMap, this will return a cloned instance of the internaljava.util.HashMap.
- For the sub-class
ReadOnlyTreeMap, this will return a cloned instance of the internaljava.util.TreeMap.
- For the sub-class
ReadOnlyHashtable, this will return a cloned instance of the internaljava.util.Hashtable.
- For the sub-class
ReadOnlyProperties, this will return a cloned instance of the internaljava.util.Properties.
- For the sub-class
- Code:
- Exact Method Body:
return cloneToTreeMap();
-
wrapToImmutableMap
public java.util.Map<K,V> wrapToImmutableMap()
Description copied from interface:ReadOnlyMapInvokesjava.util.Collections.unmodifiableMapon the internalMap
Standard JDKMap:
There may be situations where the TypeReadOnlyMapis simply not usable with an API that can't be adapted to accept Type's from the Java-HTMLReadOnlyPackage. If such a case arises, this method can be used to generate an immutable instance of a JDK-Standardjava.utilType, which does not inherit fromTorello.Java.ReadOnly
Classjava.util.Collectionsoffers a method that wraps a type into an Unmodifiable-Wrapper that implements the standardjava.util.Map, but throws an exception if any attempts are made to invoke Mutator-Methods.
Unmodifiable-Collections throw Exceptions instead:
The returnedjava.util.Mapinstance will hopefully provide an acceptable substitute whenReadOnlyType's cannot match an API's requirements. Java's Immutable-Wrappers work on a different principle - these classes retain any / all Mutator-Methods amongst their Type-Members, but instead throw anUnsupportedOperationExceptionif any of those methods are invoked!
Also, if a Java Collections FrameworkUnmodifiable-Class has any Member-Methods that return a "View" into the underlying Data-Structure, the Sub-Lists and Sub-Maps returned by those methods are also, subsequently, wrapped in anUnmodifiable-Wrapper. These returned instances will also throw anUnsupportedOperationExceptionif any of those methods are invoked!
This does provide a shield or layer of protection against modification to guarantee the "Immutability-Contract" offered by these classes, all-the-while returning to native Java API Collection Types, rather thanTorello.Java.ReadOnlyTypes, which may not always be compatible with a given API or Interface requirement!
Remember, many of the Data-Classes insidejava.utilhave methods whose documentation state very clearly that "any changes made the this [View] will be pushed back into the original type."- Specified by:
wrapToImmutableMapin interfaceReadOnlyMap<K,V>- Returns:
- A
Setwhich adheres to the JDK interfacejava.util.Set, but throws anUnsupportedOperationExceptionif a user attempts to invoke a Mutator-Method on the returned instance. - Code:
- Exact Method Body:
return Collections.unmodifiableMap(this.treeMap);
-
toString
public java.lang.String toString()
Returns aStringrepresentation of this map. TheStringrepresentation consists of a list of key-value mappings in the order returned by the map's entrySet view's iterator, enclosed in braces ("{}"). Adjacent mappings are separated by the characters", "(comma and space). Each key-value mapping is rendered as the key followed by an equals sign ("=") followed by the associated value. Keys and values are converted toString'sas byString.valueOf(Object).- Overrides:
toStringin classjava.lang.Object- Returns:
- a
Stringrepresentation of thisTreeMap - Code:
- Exact Method Body:
return ROHelperEntrySet.toString( this.treeMap, // if the map contains itself, it is needed for printing purposes fromBuilderOrTreeMap ? ((ROTreeMapBuilder<K, V>) this.treeMap)._entrySet(friendClassBadge) : this.treeMap.entrySet() );
-
equals
public boolean equals(java.lang.Object o)
Compares the specified Object with this Map for equality, as per the definition in the classjava.util.TreeMap.- Specified by:
equalsin interfaceReadOnlyMap<K,V>- Overrides:
equalsin classjava.lang.Object- Parameters:
o- object to be compared for equality with thisReadOnlyTreeMap.- Returns:
TRUEif the specified Object is equal to this map- Code:
- Exact Method Body:
return ROHelperEquals.roMapEq(this, o);
-
hashCode
public int hashCode()
Returns the hash code value for this Map as per the definition in the classjava.util.TreeMap.- Specified by:
hashCodein interfaceReadOnlyMap<K,V>- Overrides:
hashCodein classjava.lang.Object- Returns:
- the hash code value for this map
- See Also:
ReadOnlyMap.Entry.hashCode(),ReadOnlyMap.equals(Object)- Code:
- Exact Method Body:
return this.treeMap.hashCode();
-
-