public class SingletonMap extends java.lang.Object implements OrderedMap, BoundedMap, KeyValue, java.io.Serializable, java.lang.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 suceed 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 keyValue)
Constructor specifying the key and value as a
KeyValue . |
SingletonMap(java.util.Map.Entry mapEntry)
Constructor specifying the key and value as a
MapEntry . |
SingletonMap(java.util.Map map)
Constructor copying elements from another map.
|
SingletonMap(java.lang.Object key,
java.lang.Object value)
Constructor specifying the key and value.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Unsupported operation.
|
java.lang.Object |
clone()
Clones the map without cloning the key or value.
|
boolean |
containsKey(java.lang.Object key)
Checks whether the map contains the specified key.
|
boolean |
containsValue(java.lang.Object value)
Checks whether the map contains the specified value.
|
java.util.Set |
entrySet()
Gets the entrySet view of the map.
|
boolean |
equals(java.lang.Object obj)
Compares this map with another.
|
java.lang.Object |
firstKey()
Gets the first (and only) key in the map.
|
java.lang.Object |
get(java.lang.Object key)
Gets the value mapped to the key specified.
|
java.lang.Object |
getKey()
Gets the key.
|
java.lang.Object |
getValue()
Gets the value.
|
int |
hashCode()
Gets the standard Map hashCode.
|
boolean |
isEmpty()
Checks whether the map is currently empty, which it never is.
|
boolean |
isFull()
Is the map currently full, always true.
|
java.util.Set |
keySet()
Gets the unmodifiable keySet view of the map.
|
java.lang.Object |
lastKey()
Gets the last (and only) key in the map.
|
MapIterator |
mapIterator()
Gets an iterator over the map.
|
int |
maxSize()
Gets the maximum size of the map, always 1.
|
java.lang.Object |
nextKey(java.lang.Object key)
Gets the next key after the key specified, always null.
|
OrderedMapIterator |
orderedMapIterator()
Obtains an
OrderedMapIterator over the map. |
java.lang.Object |
previousKey(java.lang.Object key)
Gets the previous key before the key specified, always null.
|
java.lang.Object |
put(java.lang.Object key,
java.lang.Object value)
Puts a key-value mapping into this map where the key must match the existing key.
|
void |
putAll(java.util.Map map)
Puts the values from the specified map into this map.
|
java.lang.Object |
remove(java.lang.Object key)
Unsupported operation.
|
java.lang.Object |
setValue(java.lang.Object value)
Sets the value.
|
int |
size()
Gets the size of the map, always 1.
|
java.lang.String |
toString()
Gets the map as a String.
|
java.util.Collection |
values()
Gets the unmodifiable values view of the map.
|
public SingletonMap()
null
to null
.public SingletonMap(java.lang.Object key, java.lang.Object value)
key
- the key to usevalue
- the value to usepublic SingletonMap(KeyValue keyValue)
KeyValue
.keyValue
- the key value pair to usepublic SingletonMap(java.util.Map.Entry mapEntry)
MapEntry
.mapEntry
- the mapEntry to usepublic SingletonMap(java.util.Map map)
map
- the map to copy, must be size 1java.lang.NullPointerException
- if the map is nulljava.lang.IllegalArgumentException
- if the size is not 1public java.lang.Object getKey()
public java.lang.Object getValue()
public java.lang.Object setValue(java.lang.Object value)
value
- the new value to setpublic boolean isFull()
isFull
in interface BoundedMap
public int maxSize()
maxSize
in interface BoundedMap
public java.lang.Object get(java.lang.Object key)
get
in interface java.util.Map
key
- the keypublic int size()
size
in interface java.util.Map
public boolean isEmpty()
isEmpty
in interface java.util.Map
public boolean containsKey(java.lang.Object key)
containsKey
in interface java.util.Map
key
- the key to search forpublic boolean containsValue(java.lang.Object value)
containsValue
in interface java.util.Map
value
- the value to search forpublic java.lang.Object put(java.lang.Object key, java.lang.Object value)
An IllegalArgumentException is thrown if the key does not match as the map is fixed size.
put
in interface java.util.Map
key
- the key to set, must be the key of the mapvalue
- the value to setjava.lang.IllegalArgumentException
- if the key does not matchpublic void putAll(java.util.Map 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 java.util.Map
map
- the map to add, must be size 0 or 1, and the key must matchjava.lang.NullPointerException
- if the map is nulljava.lang.IllegalArgumentException
- if the key does not matchpublic java.lang.Object remove(java.lang.Object key)
remove
in interface java.util.Map
key
- the mapping to removejava.lang.UnsupportedOperationException
- alwayspublic void clear()
clear
in interface java.util.Map
public java.util.Set entrySet()
setValue
affect this map.
To simply iterate through the entries, use mapIterator()
.entrySet
in interface java.util.Map
public java.util.Set keySet()
mapIterator()
.keySet
in interface java.util.Map
public java.util.Collection values()
mapIterator()
.values
in interface java.util.Map
public MapIterator mapIterator()
setValue
affect this map.
The remove
method is unsupported.
A MapIterator returns the keys in the map. It also provides convenient methods to get the key and value, and set the value. It avoids the need to create an entrySet/keySet/values object. It also avoids creating the Map Entry object.
mapIterator
in interface IterableMap
public OrderedMapIterator orderedMapIterator()
OrderedMapIterator
over the map.
A ordered map iterator is an efficient way of iterating over maps in both directions.
orderedMapIterator
in interface OrderedMap
public java.lang.Object firstKey()
firstKey
in interface OrderedMap
public java.lang.Object lastKey()
lastKey
in interface OrderedMap
public java.lang.Object nextKey(java.lang.Object key)
nextKey
in interface OrderedMap
key
- the next keypublic java.lang.Object previousKey(java.lang.Object key)
previousKey
in interface OrderedMap
key
- the next keypublic java.lang.Object clone()
clone
in class java.lang.Object
public boolean equals(java.lang.Object obj)
equals
in interface java.util.Map
equals
in class java.lang.Object
obj
- the object to compare topublic int hashCode()
hashCode
in interface java.util.Map
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
"Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"