Class SingletonMap<K,V>
- java.lang.Object
-
- org.apache.commons.collections4.map.SingletonMap<K,V>
-
- Type Parameters:
K- the type of the keys in this mapV- the type of the values in this map
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable,java.util.Map<K,V>,BoundedMap<K,V>,Get<K,V>,IterableGet<K,V>,IterableMap<K,V>,KeyValue<K,V>,OrderedMap<K,V>,Put<K,V>
public class SingletonMap<K,V> extends java.lang.Object implements OrderedMap<K,V>, BoundedMap<K,V>, KeyValue<K,V>, java.io.Serializable, java.lang.Cloneable
AMapimplementation 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
putorsetValuemethods 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:
- normal Map methods and views
- the
MapIterator, seemapIterator() - the
KeyValueinterface (just cast - no object creation)
- Since:
- 3.1
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description SingletonMap()Constructor that creates a map ofnulltonull.SingletonMap(java.util.Map.Entry<? extends K,? extends V> mapEntry)Constructor specifying the key and value as aMapEntry.SingletonMap(java.util.Map<? extends K,? extends V> map)Constructor copying elements from another map.SingletonMap(K key, V value)Constructor specifying the key and value.SingletonMap(KeyValue<K,V> keyValue)Constructor specifying the key and value as aKeyValue.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Unsupported operation.SingletonMap<K,V>clone()Clones the map without cloning the key or value.booleancontainsKey(java.lang.Object key)Checks whether the map contains the specified key.booleancontainsValue(java.lang.Object value)Checks whether the map contains the specified value.java.util.Set<java.util.Map.Entry<K,V>>entrySet()Gets the entrySet view of the map.booleanequals(java.lang.Object obj)Compares this map with another.KfirstKey()Gets the first (and only) key in the map.Vget(java.lang.Object key)Gets the value mapped to the key specified.KgetKey()Gets the key.VgetValue()Gets the value.inthashCode()Gets the standard Map hashCode.booleanisEmpty()Checks whether the map is currently empty, which it never is.booleanisFull()Is the map currently full, always true.java.util.Set<K>keySet()Gets the unmodifiable keySet view of the map.KlastKey()Gets the last (and only) key in the map.OrderedMapIterator<K,V>mapIterator()Obtains anOrderedMapIteratorover the map.intmaxSize()Gets the maximum size of the map, always 1.KnextKey(K key)Gets the next key after the key specified, always null.KpreviousKey(K key)Gets the previous key before the key specified, always null.Vput(K key, V value)Puts a key-value mapping into this map where the key must match the existing key.voidputAll(java.util.Map<? extends K,? extends V> map)Puts the values from the specified map into this map.Vremove(java.lang.Object key)Unsupported operation.VsetValue(V value)Sets the value.intsize()Gets the size of the map, always 1.java.lang.StringtoString()Gets the map as a String.java.util.Collection<V>values()Gets the unmodifiable values view of the map.
-
-
-
Constructor Detail
-
SingletonMap
public SingletonMap()
Constructor that creates a map ofnulltonull.
-
SingletonMap
public SingletonMap(K key, V value)
Constructor specifying the key and value.- Parameters:
key- the key to usevalue- the value to use
-
SingletonMap
public SingletonMap(KeyValue<K,V> keyValue)
Constructor specifying the key and value as aKeyValue.- Parameters:
keyValue- the key value pair to use
-
SingletonMap
public SingletonMap(java.util.Map.Entry<? extends K,? extends V> mapEntry)
Constructor specifying the key and value as aMapEntry.- Parameters:
mapEntry- the mapEntry to use
-
-
Method Detail
-
getKey
public K getKey()
Gets the key.
-
getValue
public V getValue()
Gets the value.
-
setValue
public V setValue(V value)
Sets the value.- Parameters:
value- the new value to set- Returns:
- the old value
-
isFull
public boolean isFull()
Is the map currently full, always true.- Specified by:
isFullin interfaceBoundedMap<K,V>- Returns:
- true always
-
maxSize
public int maxSize()
Gets the maximum size of the map, always 1.- Specified by:
maxSizein interfaceBoundedMap<K,V>- Returns:
- 1 always
-
get
public V get(java.lang.Object key)
Gets the value mapped to the key specified.
-
size
public int size()
Gets the size of the map, always 1.
-
isEmpty
public boolean isEmpty()
Checks whether the map is currently empty, which it never is.
-
containsKey
public boolean containsKey(java.lang.Object key)
Checks whether the map contains the specified key.
-
containsValue
public boolean containsValue(java.lang.Object value)
Checks whether the map contains the specified value.
-
put
public V put(K key, V value)
Puts a key-value mapping into this map where the key must match the existing key.An IllegalArgumentException is thrown if the key does not match as the map is fixed size.
- Specified by:
putin interfacejava.util.Map<K,V>- Specified by:
putin interfacePut<K,V>- Parameters:
key- the key to set, must be the key of the mapvalue- the value to set- Returns:
- the value previously mapped to this key, null if none
- Throws:
java.lang.IllegalArgumentException- if the key does not match- See Also:
Map.put(Object, Object)
-
putAll
public void putAll(java.util.Map<? extends K,? extends V> map)
Puts the values from the specified map into this 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.
- Specified by:
putAllin interfacejava.util.Map<K,V>- Specified by:
putAllin interfacePut<K,V>- Parameters:
map- the map to add, must be size 0 or 1, and the key must match- Throws:
java.lang.NullPointerException- if the map is nulljava.lang.IllegalArgumentException- if the key does not match- See Also:
Map.putAll(Map)
-
remove
public V remove(java.lang.Object key)
Unsupported operation.
-
clear
public void clear()
Unsupported operation.
-
entrySet
public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
Gets the entrySet view of the map. Changes made viasetValueaffect this map. To simply iterate through the entries, usemapIterator().
-
keySet
public java.util.Set<K> keySet()
Gets the unmodifiable keySet view of the map. Changes made to the view affect this map. To simply iterate through the keys, usemapIterator().
-
values
public java.util.Collection<V> values()
Gets the unmodifiable values view of the map. Changes made to the view affect this map. To simply iterate through the values, usemapIterator().
-
mapIterator
public OrderedMapIterator<K,V> mapIterator()
Obtains anOrderedMapIteratorover the map.A ordered map iterator is an efficient way of iterating over maps in both directions.
- Specified by:
mapIteratorin interfaceIterableGet<K,V>- Specified by:
mapIteratorin interfaceOrderedMap<K,V>- Returns:
- a map iterator
-
firstKey
public K firstKey()
Gets the first (and only) key in the map.- Specified by:
firstKeyin interfaceOrderedMap<K,V>- Returns:
- the key
-
lastKey
public K lastKey()
Gets the last (and only) key in the map.- Specified by:
lastKeyin interfaceOrderedMap<K,V>- Returns:
- the key
-
nextKey
public K nextKey(K key)
Gets the next key after the key specified, always null.- Specified by:
nextKeyin interfaceOrderedMap<K,V>- Parameters:
key- the next key- Returns:
- null always
-
previousKey
public K previousKey(K key)
Gets the previous key before the key specified, always null.- Specified by:
previousKeyin interfaceOrderedMap<K,V>- Parameters:
key- the next key- Returns:
- null always
-
clone
public SingletonMap<K,V> clone()
Clones the map without cloning the key or value.- Returns:
- a shallow clone
-
equals
public boolean equals(java.lang.Object obj)
Compares this map with another.
-
hashCode
public int hashCode()
Gets the standard Map hashCode.
-
toString
public java.lang.String toString()
Gets the map as a String.- Overrides:
toStringin classjava.lang.Object- Returns:
- a string version of the map
-
-