|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.commons.collections.map.SingletonMap<K,V>
public class SingletonMap<K,V>
A Map
implementation that holds a single item and is fixed size.
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)
Nested Class Summary |
---|
Nested classes/interfaces inherited from interface java.util.Map |
---|
Map.Entry<K,V> |
Constructor Summary | |
---|---|
SingletonMap()
Constructor that creates a map of null to null . |
|
SingletonMap(KeyValue<K,V> keyValue)
Constructor specifying the key and value as a KeyValue . |
|
SingletonMap(K key,
V value)
Constructor specifying the key and value. |
|
SingletonMap(Map.Entry<K,V> entry)
Constructor specifying the key and value as a MapEntry . |
|
SingletonMap(Map<? extends K,? extends V> map)
Constructor copying elements from another map. |
Method Summary | |
---|---|
void |
clear()
Unsupported operation. |
Object |
clone()
Clones the map without cloning the key or value. |
boolean |
containsKey(Object key)
Checks whether the map contains the specified key. |
boolean |
containsValue(Object value)
Checks whether the map contains the specified value. |
Set<Map.Entry<K,V>> |
entrySet()
Gets the entrySet view of the map. |
boolean |
equals(Object obj)
Compares this map with another. |
K |
firstKey()
Gets the first (and only) key in the map. |
V |
get(Object key)
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 key)
Compares the specified key to the stored key. |
protected boolean |
isEqualValue(Object value)
Compares the specified value to the stored value. |
boolean |
isFull()
Is the map currently full, always true. |
Set<K> |
keySet()
Gets the unmodifiable keySet view of the map. |
K |
lastKey()
Gets the last (and only) key in the map. |
MapIterator<K,V> |
mapIterator()
Gets an iterator 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. |
OrderedMapIterator<K,V> |
orderedMapIterator()
Obtains an OrderedMapIterator over the map. |
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<? extends K,? extends V> map)
Puts the values from the specified map into this map. |
V |
remove(Object key)
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<V> |
values()
Gets the unmodifiable values view of the map. |
Methods inherited from class java.lang.Object |
---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public SingletonMap()
null
to null
.
public SingletonMap(K key, V value)
key
- the key to usevalue
- the value to usepublic SingletonMap(KeyValue<K,V> keyValue)
KeyValue
.
keyValue
- the key value pair to usepublic SingletonMap(Map.Entry<K,V> entry)
MapEntry
.
entry
- the key value pair to usepublic 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 1Method Detail |
---|
public K getKey()
getKey
in interface KeyValue<K,V>
public V getValue()
getValue
in interface KeyValue<K,V>
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 V get(Object key)
get
in interface Map<K,V>
key
- the key
public int size()
size
in interface Map<K,V>
public boolean isEmpty()
isEmpty
in interface Map<K,V>
public boolean containsKey(Object key)
containsKey
in interface Map<K,V>
key
- the key to search for
public boolean containsValue(Object value)
containsValue
in interface Map<K,V>
value
- the value to search for
public V put(K key, V value)
put
in interface Map<K,V>
key
- the key to set, must be the key of the mapvalue
- the value to set
IllegalArgumentException
- if the key does not matchpublic void putAll(Map<? extends K,? extends V> map)
putAll
in interface Map<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 matchpublic V remove(Object key)
remove
in interface Map<K,V>
key
- the mapping to remove
UnsupportedOperationException
- alwayspublic void clear()
clear
in interface Map<K,V>
public Set<Map.Entry<K,V>> entrySet()
setValue
affect this map.
To simply iterate through the entries, use mapIterator()
.
entrySet
in interface Map<K,V>
public Set<K> keySet()
mapIterator()
.
keySet
in interface Map<K,V>
public Collection<V> values()
mapIterator()
.
values
in interface Map<K,V>
public MapIterator<K,V> 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<K,V>
public OrderedMapIterator<K,V> orderedMapIterator()
OrderedMapIterator
over the map.
A ordered map iterator is an efficient way of iterating over maps
in both directions.
orderedMapIterator
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(Object key)
key
- the key to compare
protected boolean isEqualValue(Object value)
value
- the value to compare
public Object clone()
clone
in class Object
public boolean equals(Object obj)
equals
in interface Map<K,V>
equals
in class Object
obj
- the object to compare to
public int hashCode()
hashCode
in interface Map<K,V>
hashCode
in class Object
public String toString()
toString
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |