Class SingletonMap
- java.lang.Object
 - 
- org.apache.commons.collections.map.SingletonMap
 
 
- 
- All Implemented Interfaces:
 java.io.Serializable,java.lang.Cloneable,java.util.Map,BoundedMap,IterableMap,KeyValue,OrderedMap
public class SingletonMap extends java.lang.Object implements OrderedMap, BoundedMap, KeyValue, 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 suceed 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:
 - Commons Collections 3.1
 - See Also:
 - Serialized Form
 
 
- 
- 
Constructor Summary
Constructors Constructor Description SingletonMap()Constructor that creates a map ofnulltonull.SingletonMap(java.lang.Object key, java.lang.Object value)Constructor specifying the key and value.SingletonMap(java.util.Map map)Constructor copying elements from another map.SingletonMap(java.util.Map.Entry mapEntry)Constructor specifying the key and value as aMapEntry.SingletonMap(KeyValue 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.java.lang.Objectclone()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.SetentrySet()Gets the entrySet view of the map.booleanequals(java.lang.Object obj)Compares this map with another.java.lang.ObjectfirstKey()Gets the first (and only) key in the map.java.lang.Objectget(java.lang.Object key)Gets the value mapped to the key specified.java.lang.ObjectgetKey()Gets the key.java.lang.ObjectgetValue()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.SetkeySet()Gets the unmodifiable keySet view of the map.java.lang.ObjectlastKey()Gets the last (and only) key in the map.MapIteratormapIterator()Gets an iterator over the map.intmaxSize()Gets the maximum size of the map, always 1.java.lang.ObjectnextKey(java.lang.Object key)Gets the next key after the key specified, always null.OrderedMapIteratororderedMapIterator()Obtains anOrderedMapIteratorover the map.java.lang.ObjectpreviousKey(java.lang.Object key)Gets the previous key before the key specified, always null.java.lang.Objectput(java.lang.Object key, java.lang.Object value)Puts a key-value mapping into this map where the key must match the existing key.voidputAll(java.util.Map map)Puts the values from the specified map into this map.java.lang.Objectremove(java.lang.Object key)Unsupported operation.java.lang.ObjectsetValue(java.lang.Object value)Sets the value.intsize()Gets the size of the map, always 1.java.lang.StringtoString()Gets the map as a String.java.util.Collectionvalues()Gets the unmodifiable values view of the map. 
 - 
 
- 
- 
Constructor Detail
- 
SingletonMap
public SingletonMap()
Constructor that creates a map ofnulltonull. 
- 
SingletonMap
public SingletonMap(java.lang.Object key, java.lang.Object value)Constructor specifying the key and value.- Parameters:
 key- the key to usevalue- the value to use
 
- 
SingletonMap
public SingletonMap(KeyValue keyValue)
Constructor specifying the key and value as aKeyValue.- Parameters:
 keyValue- the key value pair to use
 
- 
SingletonMap
public SingletonMap(java.util.Map.Entry mapEntry)
Constructor specifying the key and value as aMapEntry.- Parameters:
 mapEntry- the mapEntry to use
 
- 
SingletonMap
public SingletonMap(java.util.Map map)
Constructor copying elements from another map.- Parameters:
 map- the map to copy, must be size 1- Throws:
 java.lang.NullPointerException- if the map is nulljava.lang.IllegalArgumentException- if the size is not 1
 
 - 
 
- 
Method Detail
- 
getKey
public java.lang.Object getKey()
Gets the key. 
- 
getValue
public java.lang.Object getValue()
Gets the value. 
- 
setValue
public java.lang.Object setValue(java.lang.Object 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- Returns:
 - true always
 
 
- 
maxSize
public int maxSize()
Gets the maximum size of the map, always 1.- Specified by:
 maxSizein interfaceBoundedMap- Returns:
 - 1 always
 
 
- 
get
public java.lang.Object get(java.lang.Object key)
Gets the value mapped to the key specified.- Specified by:
 getin interfacejava.util.Map- Parameters:
 key- the key- Returns:
 - the mapped value, null if no match
 
 
- 
size
public int size()
Gets the size of the map, always 1.- Specified by:
 sizein interfacejava.util.Map- Returns:
 - the size of 1
 
 
- 
isEmpty
public boolean isEmpty()
Checks whether the map is currently empty, which it never is.- Specified by:
 isEmptyin interfacejava.util.Map- Returns:
 - false always
 
 
- 
containsKey
public boolean containsKey(java.lang.Object key)
Checks whether the map contains the specified key.- Specified by:
 containsKeyin interfacejava.util.Map- Parameters:
 key- the key to search for- Returns:
 - true if the map contains the key
 
 
- 
containsValue
public boolean containsValue(java.lang.Object value)
Checks whether the map contains the specified value.- Specified by:
 containsValuein interfacejava.util.Map- Parameters:
 value- the value to search for- Returns:
 - true if the map contains the key
 
 
- 
put
public 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.An IllegalArgumentException is thrown if the key does not match as the map is fixed size.
- Specified by:
 putin interfacejava.util.Map- 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
 
- 
putAll
public void putAll(java.util.Map 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- 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
 
- 
remove
public java.lang.Object remove(java.lang.Object key)
Unsupported operation.- Specified by:
 removein interfacejava.util.Map- Parameters:
 key- the mapping to remove- Returns:
 - the value mapped to the removed key, null if key not in map
 - Throws:
 java.lang.UnsupportedOperationException- always
 
- 
clear
public void clear()
Unsupported operation.- Specified by:
 clearin interfacejava.util.Map
 
- 
entrySet
public java.util.Set entrySet()
Gets the entrySet view of the map. Changes made viasetValueaffect this map. To simply iterate through the entries, usemapIterator().- Specified by:
 entrySetin interfacejava.util.Map- Returns:
 - the entrySet view
 
 
- 
keySet
public java.util.Set keySet()
Gets the unmodifiable keySet view of the map. Changes made to the view affect this map. To simply iterate through the keys, usemapIterator().- Specified by:
 keySetin interfacejava.util.Map- Returns:
 - the keySet view
 
 
- 
values
public java.util.Collection values()
Gets the unmodifiable values view of the map. Changes made to the view affect this map. To simply iterate through the values, usemapIterator().- Specified by:
 valuesin interfacejava.util.Map- Returns:
 - the values view
 
 
- 
mapIterator
public MapIterator mapIterator()
Gets an iterator over the map. Changes made to the iterator usingsetValueaffect this map. Theremovemethod 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.
- Specified by:
 mapIteratorin interfaceIterableMap- Returns:
 - the map iterator
 
 
- 
orderedMapIterator
public OrderedMapIterator orderedMapIterator()
Obtains anOrderedMapIteratorover the map.A ordered map iterator is an efficient way of iterating over maps in both directions.
- Specified by:
 orderedMapIteratorin interfaceOrderedMap- Returns:
 - an ordered map iterator
 
 
- 
firstKey
public java.lang.Object firstKey()
Gets the first (and only) key in the map.- Specified by:
 firstKeyin interfaceOrderedMap- Returns:
 - the key
 
 
- 
lastKey
public java.lang.Object lastKey()
Gets the last (and only) key in the map.- Specified by:
 lastKeyin interfaceOrderedMap- Returns:
 - the key
 
 
- 
nextKey
public java.lang.Object nextKey(java.lang.Object key)
Gets the next key after the key specified, always null.- Specified by:
 nextKeyin interfaceOrderedMap- Parameters:
 key- the next key- Returns:
 - null always
 
 
- 
previousKey
public java.lang.Object previousKey(java.lang.Object key)
Gets the previous key before the key specified, always null.- Specified by:
 previousKeyin interfaceOrderedMap- Parameters:
 key- the next key- Returns:
 - null always
 
 
- 
clone
public java.lang.Object 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.- Specified by:
 equalsin interfacejava.util.Map- Overrides:
 equalsin classjava.lang.Object- Parameters:
 obj- the object to compare to- Returns:
 - true if equal
 
 
- 
hashCode
public int hashCode()
Gets the standard Map hashCode.- Specified by:
 hashCodein interfacejava.util.Map- Overrides:
 hashCodein classjava.lang.Object- Returns:
 - the hash code defined in the Map interface
 
 
- 
toString
public java.lang.String toString()
Gets the map as a String.- Overrides:
 toStringin classjava.lang.Object- Returns:
 - a string version of the map
 
 
 - 
 
 -