public class SingletonMap<K,V> extends Objectimplements OrderedMap <K ,V>, BoundedMap <K ,V>, KeyValue <K ,V>, Serializable , Cloneable
Map implementation that holds a single item and is fixed size.
The single key/value pair is specified at creation. The map is fixed size so any action that would change the size is disallowed. However, the put or setValue methods can change the value associated with the key.
If trying to remove or clear the map, an UnsupportedOperationException is thrown. If trying to put a new mapping into the map, an IllegalArgumentException is thrown. The put method will only succeed if the key specified is the same as the singleton key.
The key and value can be obtained by:
MapIterator, see mapIterator() KeyValue interface (just cast - no object creation) | Constructor and Description |
|---|
SingletonMap()
Constructor that creates a map of
null to
null.
|
SingletonMap(KeyValue
Constructor specifying the key and value as a
KeyValue.
|
SingletonMap(K key, V value)
Constructor specifying the key and value.
|
SingletonMap(Map
Constructor specifying the key and value as a
MapEntry.
|
SingletonMap(Map
Constructor copying elements from another map.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Unsupported operation.
|
SingletonMap |
clone()
Clones the map without cloning the key or value.
|
boolean |
containsKey(Object
Checks whether the map contains the specified key.
|
boolean |
containsValue(Object
Checks whether the map contains the specified value.
|
Set |
entrySet()
Gets the entrySet view of the map.
|
boolean |
equals(Object
Compares this map with another.
|
K |
firstKey()
Gets the first (and only) key in the map.
|
V |
get(Object
Gets the value mapped to the key specified.
|
K |
getKey()
Gets the key.
|
V |
getValue()
Gets the value.
|
int |
hashCode()
Gets the standard Map hashCode.
|
boolean |
isEmpty()
Checks whether the map is currently empty, which it never is.
|
protected boolean |
isEqualKey(Object
Compares the specified key to the stored key.
|
protected boolean |
isEqualValue(Object
Compares the specified value to the stored value.
|
boolean |
isFull()
Is the map currently full, always true.
|
Set |
keySet()
Gets the unmodifiable keySet view of the map.
|
K |
lastKey()
Gets the last (and only) key in the map.
|
OrderedMapIterator |
mapIterator()
Obtains an
OrderedMapIterator over the map.
|
int |
maxSize()
Gets the maximum size of the map, always 1.
|
K |
nextKey(K key)
Gets the next key after the key specified, always null.
|
K |
previousKey(K key)
Gets the previous key before the key specified, always null.
|
V |
put(K key, V value)
Puts a key-value mapping into this map where the key must match the existing key.
|
void |
putAll(Map
Puts the values from the specified map into this map.
|
V |
remove(Object
Unsupported operation.
|
V |
setValue(V value)
Sets the value.
|
int |
size()
Gets the size of the map, always 1.
|
String |
toString()
Gets the map as a String.
|
Collection |
values()
Gets the unmodifiable values view of the map.
|
finalize, getClass, notify, notifyAll, wait, wait, waitcompute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAllpublic SingletonMap()
null to
null.
public SingletonMap(K key, V value)
key - the key to use
value - the value to use
public SingletonMap(KeyValue<K ,V> keyValue)
KeyValue.
keyValue - the key value pair to use
public SingletonMap(Map.Entry <? extends K ,? extends V> mapEntry)
MapEntry.
mapEntry - the mapEntry to use
public SingletonMap(Map<? extends K ,? extends V> map)
map - the map to copy, must be size 1
NullPointerException - if the map is null
IllegalArgumentException - if the size is not 1
public K getKey()
public V getValue()
public V setValue(V value)
value - the new value to set
public boolean isFull()
isFull in interface
BoundedMap<K,V>
public int maxSize()
maxSize in interface
BoundedMap<K,V>
public int size()
public boolean isEmpty()
public boolean containsKey(Objectkey)
containsKey in interface
Map<K,V>
containsKey in interface
Get<K,V>
key - the key to search for
Map.containsKey(Object)
public boolean containsValue(Objectvalue)
containsValue in interface
Map<K,V>
containsValue in interface
Get<K,V>
value - the value to search for
Map.containsValue(Object)
public V put(K key, V value)
An IllegalArgumentException is thrown if the key does not match as the map is fixed size.
put in interface
Map<K,V>
put in interface
Put<K,V>
key - the key to set, must be the key of the map
value - the value to set
IllegalArgumentException - if the key does not match
Map.put(Object, Object)
public void putAll(Map<? extends K ,? extends V> map)
The map must be of size 0 or size 1. If it is size 1, the key must match the key of this map otherwise an IllegalArgumentException is thrown.
putAll in interface
Map<K,V>
putAll in interface
Put<K,V>
map - the map to add, must be size 0 or 1, and the key must match
NullPointerException - if the map is null
IllegalArgumentException - if the key does not match
Map.putAll(Map)
public V remove(Objectkey)
remove in interface
Map<K,V>
remove in interface
Get<K,V>
key - the mapping to remove
UnsupportedOperationException - always
Map.remove(Object)
public void clear()
public Set<Map .Entry <K ,V>> entrySet()
setValue affect this map. To simply iterate through the entries, use
mapIterator().
public Set<K> keySet()
mapIterator().
public Collection<V> values()
mapIterator().
public OrderedMapIterator<K ,V> mapIterator()
OrderedMapIterator over the map.
A ordered map iterator is an efficient way of iterating over maps in both directions.
mapIterator in interface
IterableGet<K,V>
mapIterator in interface
OrderedMap<K,V>
public K firstKey()
firstKey in interface
OrderedMap<K,V>
public K lastKey()
lastKey in interface
OrderedMap<K,V>
public K nextKey(K key)
nextKey in interface
OrderedMap<K,V>
key - the next key
public K previousKey(K key)
previousKey in interface
OrderedMap<K,V>
key - the next key
protected boolean isEqualKey(Objectkey)
key - the key to compare
protected boolean isEqualValue(Objectvalue)
value - the value to compare
public SingletonMap<K ,V> clone()
public boolean equals(Objectobj)
public int hashCode()