Class AbstractMap<K,V>
- Type Parameters:
K
- the type of keys maintained by the map.V
- the type of mapped values.
- All Implemented Interfaces:
Map<K,
V>
- Direct Known Subclasses:
AuthorityCodes
,CharacteristicMap
,CharacteristicTypeMap
,FilteredCodes
,LinearTransformBuilder.ControlPoints
,MergedProperties
,Properties
,StaxDataStore.Config
java.util.AbstractMap
using different implementation strategies.
Instead of providing default method implementations on top of entrySet()
, this base class uses more
often the Map.get(Object)
method with the assumption that the map cannot contain null values, or use a
special-purpose entryIterator()
which can reduce the amount of object creations.
This base class is for Apache SIS internal purpose only. Do not use!
This class is less robust than the JDK one (e.g. does not accept null values), forces subclasses to implement
more methods, uses a non-standard entryIterator()
, and may change in any future SIS version.
This AbstractMap
implementation makes the following assumptions.
Do not use this class if any of those assumptions do not hold!
- The map cannot contain
null
value. - The map cannot contain references to itself, directly or indirectly, in the keys or in the values.
Read-only subclasses need to implement the following methods:
size()
(not mandatory but recommended)Map.get(Object)
entryIterator()
(non-standard method)
Read/write subclasses can implement those additional methods:
clear()
remove(Object)
put(Object,Object)
addKey(Object)
(non-standard, optional method)addValue(Object)
(non-standard, optional method)
- Since:
- 0.5
- Version:
- 0.8
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
Iterator over the entries, used only whenentryIterator()
perform recycling.protected static class
An iterator over the entries in the enclosing map.private static class
Base class of iterators overs keys, values or entries.protected static class
An implementation ofAbstractMap.EntryIterator
which delegates its work to a standard iterator.protected final class
ConvenienceEntryIterator
implementation which iterates over a list of key candidates.private static final class
Iterator over the keys.private static final class
Iterator over the values. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
Adds the given key in this map.protected boolean
Adds the given value in this map.void
clear()
Removes all entries in this map.boolean
containsKey
(Object key) Returnstrue
if this map contains a value for the given name.boolean
containsValue
(Object value) Returnstrue
if this map contains the given value.protected abstract AbstractMap.EntryIterator
<K, V> Returns an iterator over the entries in this map.entrySet()
Returns a view over the entries in this map.boolean
Compares this map with the given object for equality.getOrDefault
(Object key, V defaultValue) Returns the value for the given key, ordefaultValue
if none.int
hashCode()
Computes a hash code value for this map.boolean
isEmpty()
Returnstrue
if this map contains no element.keySet()
Returns a view over the keys in this map.(package private) static String
message
(boolean add) The message to gives to the exception to be thrown in case of unsupported operation.Adds an entry for the given key in this map.void
Puts all entries of the given map in this map.Removes the entry for the given key in this map.int
size()
Returns the number of key-value mappings in this map.toString()
Returns a string representation of this map.values()
Returns a view over the values in this map.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, get, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Constructor Details
-
AbstractMap
protected AbstractMap()For subclass constructors.
-
-
Method Details
-
size
public int size()Returns the number of key-value mappings in this map. The default implementation count the number of values returned byentryIterator()
. Subclasses should implement a more efficient method. -
isEmpty
public boolean isEmpty()Returnstrue
if this map contains no element. -
containsKey
Returnstrue
if this map contains a value for the given name. The default implementation assumes that the map cannot containnull
values.- Specified by:
containsKey
in interfaceMap<K,
V> - Parameters:
key
- the key for which to test the presence of a value.- Returns:
true
if the map contains a non-null value for the given key.
-
containsValue
Returnstrue
if this map contains the given value. The default implementation iterates over all values using theentryIterator()
.- Specified by:
containsValue
in interfaceMap<K,
V> - Parameters:
value
- the value for which to test the presence.- Returns:
true
if the map contains the given value.
-
getOrDefault
Returns the value for the given key, ordefaultValue
if none. The default implementation assumes that the map cannot containnull
values.- Specified by:
getOrDefault
in interfaceMap<K,
V> - Parameters:
key
- the key for which to get the value.defaultValue
- the value to return if this map does not have an entry for the given key.- Returns:
- the value for the given key, or
defaultValue
if none.
-
message
The message to gives to the exception to be thrown in case of unsupported operation.- Parameters:
add
-true
if this method is invoked fromaddKey(Object)
oraddValue(Object)
.
-
clear
Removes all entries in this map. The default operation throwsUnsupportedOperationException
.- Specified by:
clear
in interfaceMap<K,
V> - Throws:
UnsupportedOperationException
-
remove
Removes the entry for the given key in this map. The default operation throwsUnsupportedOperationException
.- Specified by:
remove
in interfaceMap<K,
V> - Parameters:
key
- the key of the entry to remove.- Returns:
- the previous value, or
null
if none. - Throws:
UnsupportedOperationException
-
put
Adds an entry for the given key in this map. The default operation throwsUnsupportedOperationException
.- Specified by:
put
in interfaceMap<K,
V> - Parameters:
key
- the key of the entry to remove.value
- the value to associate to the given key.- Returns:
- the previous value, or
null
if none. - Throws:
UnsupportedOperationException
-
putAll
Puts all entries of the given map in this map.- Specified by:
putAll
in interfaceMap<K,
V> - Parameters:
map
- the other map from which to copy the entries.- Throws:
UnsupportedOperationException
-
addKey
Adds the given key in this map. Implementation of this method shall generate a corresponding value. The default operation throwsUnsupportedOperationException
.- Parameters:
key
- the key to add.- Returns:
true
if this map changed as a result of this operation.- Throws:
UnsupportedOperationException
-
addValue
Adds the given value in this map. Implementation of this method shall generate a corresponding key. The default operation throwsUnsupportedOperationException
.- Parameters:
value
- the value to add.- Returns:
true
if this map changed as a result of this operation.- Throws:
UnsupportedOperationException
-
keySet
Returns a view over the keys in this map. The returned set supports theSet.add(Object)
operation if the enclosing map implements theaddKey(Object)
method.The default implementation does not cache the set on the assumption that it is very quick to create and usually not retained for a long time (we often want only its iterator). Caching the set would require a
volatile
field for thread safety, which also has cost. -
values
Returns a view over the values in this map. The returned collection supports theCollection.add(Object)
operation if the enclosing map implements theaddValue(Object)
method.The default implementation does not cache the collection on the assumption that it is very quick to create and usually not retained for a long time.
-
entrySet
Returns a view over the entries in this map.The default implementation does not cache the set on the assumption that it is very quick to create and usually not retained for a long time.
-
entryIterator
Returns an iterator over the entries in this map. It is okay (but not required) to returnnull
if the map is empty.Tip:AbstractMap<K,
provides a convenient implementation for simple cases.V>.KeyIterator - Returns:
- an iterator over the entries in this map, or
null
.
-
equals
Compares this map with the given object for equality. -
hashCode
public int hashCode()Computes a hash code value for this map. -
toString
Returns a string representation of this map. The default implementation is different than thejava.util.AbstractMap
one, as it uses a tabular format rather than formatting all entries on a single line.
-