Top Description Inners Fields Constructors Methods
org.apache.commons.collections4.map

public Class SingletonMap<K, V>

extends Object
implements OrderedMap<K, V>, BoundedMap<K, V>, KeyValue<K, V>, Serializable, Cloneable
Class Inheritance
All Implemented Interfaces
java.lang.Cloneable, java.io.Serializable, org.apache.commons.collections4.KeyValue, org.apache.commons.collections4.BoundedMap, org.apache.commons.collections4.IterableMap, org.apache.commons.collections4.IterableGet, org.apache.commons.collections4.Get, org.apache.commons.collections4.Put, java.util.Map, org.apache.commons.collections4.OrderedMap
Type Parameters
<K>
the type of the keys in this map
<V>
the type of the values in this map
Imports
java.io.Serializable, java.util.AbstractSet, .Collection, .Collections, .Iterator, .Map, .NoSuchElementException, .Set, org.apache.commons.collections4.BoundedMap, .KeyValue, .OrderedMap, .OrderedMapIterator, .ResettableIterator, org.apache.commons.collections4.iterators.SingletonIterator, org.apache.commons.collections4.keyvalue.TiedMapEntry

A 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:

Since
3.1

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
SingletonMap.SingletonMapIterator<K, V>

SingletonMapIterator.

pack-priv static class
SingletonMap.SingletonValues<V>

Values implementation for the SingletonMap.

Field Summary

Modifier and TypeField and Description
private final K
key

Singleton key

private static final long
serialVersionUID

Serialization version

private V
value

Singleton value

Constructor Summary

AccessConstructor and Description
public
SingletonMap()

Constructor that creates a map of null to null.

public
SingletonMap(final K
the key to use
key
,
final V
the value to use
value
)

Constructor specifying the key and value.

public
SingletonMap(final KeyValue<K, V>
the key value pair to use
keyValue
)

Constructor specifying the key and value as a KeyValue.

public
SingletonMap(final Map.Entry<? extends K, ? extends V>
the mapEntry to use
mapEntry
)

Constructor specifying the key and value as a MapEntry.

public
SingletonMap(final Map<? extends K, ? extends V>
the map to copy, must be size 1
map
)

Constructor copying elements from another map.

Method Summary

Modifier and TypeMethod and Description
public void
public SingletonMap<K, V>

Returns:

a shallow clone
clone
()

Overrides java.lang.Object.clone.

Clones the map without cloning the key or value.
public boolean

Returns:

true if the map contains the key
containsKey
(final Object
the key to search for
key
)

Implements java.util.Map.containsKey, org.apache.commons.collections4.Get.containsKey.

Checks whether the map contains the specified key.
public boolean

Returns:

true if the map contains the key
containsValue
(final Object
the value to search for
value
)

Implements java.util.Map.containsValue, org.apache.commons.collections4.Get.containsValue.

Checks whether the map contains the specified value.
public Set<Map.Entry<K, V>>

Returns:

the entrySet view
entrySet
()

Implements java.util.Map.entrySet, org.apache.commons.collections4.Get.entrySet.

Gets the entrySet view of the map.
public boolean

Returns:

true if equal
equals
(final Object
the object to compare to
obj
)

Overrides java.lang.Object.equals.

Implements java.util.Map.equals.

Compares this map with another.
public K

Returns:

the key
firstKey
()

Implements org.apache.commons.collections4.OrderedMap.firstKey.

Gets the first (and only) key in the map.
public V

Returns:

the mapped value, null if no match
get
(final Object
the key
key
)

Implements java.util.Map.get, org.apache.commons.collections4.Get.get.

Gets the value mapped to the key specified.
public K

Returns:

the key
getKey
()

Implements org.apache.commons.collections4.KeyValue.getKey.

Gets the key.
public V

Returns:

the value
getValue
()

Implements org.apache.commons.collections4.KeyValue.getValue.

Gets the value.
public int

Returns:

the hash code defined in the Map interface
hashCode
()

Overrides java.lang.Object.hashCode.

Implements java.util.Map.hashCode.

Gets the standard Map hashCode.
public boolean

Returns:

false always
isEmpty
()

Implements java.util.Map.isEmpty, org.apache.commons.collections4.Get.isEmpty.

Checks whether the map is currently empty, which it never is.
protected boolean

Returns:

true if equal
isEqualKey
(final Object
the key to compare
key
)

Compares the specified key to the stored key.

protected boolean

Returns:

true if equal
isEqualValue
(final Object
the value to compare
value
)

Compares the specified value to the stored value.

public boolean

Returns:

true always
isFull
()

Implements org.apache.commons.collections4.BoundedMap.isFull.

Is the map currently full, always true.
public Set<K>

Returns:

the keySet view
keySet
()

Implements java.util.Map.keySet, org.apache.commons.collections4.Get.keySet.

Gets the unmodifiable keySet view of the map.
public K

Returns:

the key
lastKey
()

Implements org.apache.commons.collections4.OrderedMap.lastKey.

Gets the last (and only) key in the map.
public OrderedMapIterator<K, V>
public int

Returns:

1 always
maxSize
()

Implements org.apache.commons.collections4.BoundedMap.maxSize.

Gets the maximum size of the map, always 1.
public K

Returns:

null always
nextKey
(final K
the next key
key
)

Implements org.apache.commons.collections4.OrderedMap.nextKey.

Gets the next key after the key specified, always null.
public K

Returns:

null always
previousKey
(final K
the next key
key
)

Implements org.apache.commons.collections4.OrderedMap.previousKey.

Gets the previous key before the key specified, always null.
public V

Returns:

the value previously mapped to this key, null if none
put
(final K
the key to set, must be the key of the map
key
,
final V
the value to set
value
)

Implements java.util.Map.put, org.apache.commons.collections4.Put.put.

Puts a key-value mapping into this map where the key must match the existing key.
public void
putAll(final Map<? extends K, ? extends V>
the map to add, must be size 0 or 1, and the key must match
map
)

Implements java.util.Map.putAll, org.apache.commons.collections4.Put.putAll.

Puts the values from the specified map into this map.
public V

Returns:

the value mapped to the removed key, null if key not in map
remove
(final Object
the mapping to remove
key
)

Implements java.util.Map.remove, org.apache.commons.collections4.Get.remove.

Unsupported operation.
public V

Returns:

the old value
setValue
(final V
the new value to set
value
)

Sets the value.

public int

Returns:

the size of 1
size
()

Implements java.util.Map.size, org.apache.commons.collections4.Get.size.

Gets the size of the map, always 1.
public String

Returns:

a string version of the map
toString
()

Overrides java.lang.Object.toString.

Gets the map as a String.
public Collection<V>

Returns:

the values view
values
()

Implements java.util.Map.values, org.apache.commons.collections4.Get.values.

Gets the unmodifiable values view of the map.
Inherited from java.lang.Object:
finalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

keyback to summary
private final K key

Singleton key

serialVersionUIDback to summary
private static final long serialVersionUID

Serialization version

valueback to summary
private V value

Singleton value

Constructor Detail

SingletonMapback to summary
public SingletonMap()

Constructor that creates a map of null to null.

SingletonMapback to summary
public SingletonMap(final K key, final V value)

Constructor specifying the key and value.

Parameters
key:K

the key to use

value:V

the value to use

SingletonMapback to summary
public SingletonMap(final KeyValue<K, V> keyValue)

Constructor specifying the key and value as a KeyValue.

Parameters
keyValue:KeyValue<K, V>

the key value pair to use

SingletonMapback to summary
public SingletonMap(final Map.Entry<? extends K, ? extends V> mapEntry)

Constructor specifying the key and value as a MapEntry.

Parameters
mapEntry:Map.Entry<? extends K, ? extends V>

the mapEntry to use

SingletonMapback to summary
public SingletonMap(final Map<? extends K, ? extends V> map)

Constructor copying elements from another map.

Parameters
map:Map<? extends K, ? extends V>

the map to copy, must be size 1

Exceptions
NullPointerException:
if the map is null
IllegalArgumentException:
if the size is not 1

Method Detail

clearback to summary
public void clear()

Implements java.util.Map.clear, org.apache.commons.collections4.Put.clear.

Unsupported operation.

Annotations
@Override
cloneback to summary
public SingletonMap<K, V> clone()

Overrides java.lang.Object.clone.

Clones the map without cloning the key or value.

Returns:SingletonMap<K, V>

a shallow clone

Annotations
@Override
@SuppressWarnings:unchecked
containsKeyback to summary
public boolean containsKey(final Object key)

Implements java.util.Map.containsKey, org.apache.commons.collections4.Get.containsKey.

Checks whether the map contains the specified key.

Parameters
key:Object

the key to search for

Returns:boolean

true if the map contains the key

Annotations
@Override
containsValueback to summary
public boolean containsValue(final Object value)

Implements java.util.Map.containsValue, org.apache.commons.collections4.Get.containsValue.

Checks whether the map contains the specified value.

Parameters
value:Object

the value to search for

Returns:boolean

true if the map contains the key

Annotations
@Override
entrySetback to summary
public Set<Map.Entry<K, V>> entrySet()

Implements java.util.Map.entrySet, org.apache.commons.collections4.Get.entrySet.

Gets the entrySet view of the map. Changes made via setValue affect this map. To simply iterate through the entries, use mapIterator().

Returns:Set<Map.Entry<K, V>>

the entrySet view

Annotations
@Override
equalsback to summary
public boolean equals(final Object obj)

Overrides java.lang.Object.equals.

Implements java.util.Map.equals.

Compares this map with another.

Parameters
obj:Object

the object to compare to

Returns:boolean

true if equal

Annotations
@Override
firstKeyback to summary
public K firstKey()

Implements org.apache.commons.collections4.OrderedMap.firstKey.

Gets the first (and only) key in the map.

Returns:K

the key

Annotations
@Override
getback to summary
public V get(final Object key)

Implements java.util.Map.get, org.apache.commons.collections4.Get.get.

Gets the value mapped to the key specified.

Parameters
key:Object

the key

Returns:V

the mapped value, null if no match

Annotations
@Override
getKeyback to summary
public K getKey()

Implements org.apache.commons.collections4.KeyValue.getKey.

Gets the key.

Returns:K

the key

Annotations
@Override
getValueback to summary
public V getValue()

Implements org.apache.commons.collections4.KeyValue.getValue.

Gets the value.

Returns:V

the value

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Implements java.util.Map.hashCode.

Gets the standard Map hashCode.

Returns:int

the hash code defined in the Map interface

Annotations
@Override
isEmptyback to summary
public boolean isEmpty()

Implements java.util.Map.isEmpty, org.apache.commons.collections4.Get.isEmpty.

Checks whether the map is currently empty, which it never is.

Returns:boolean

false always

Annotations
@Override
isEqualKeyback to summary
protected boolean isEqualKey(final Object key)

Compares the specified key to the stored key.

Parameters
key:Object

the key to compare

Returns:boolean

true if equal

isEqualValueback to summary
protected boolean isEqualValue(final Object value)

Compares the specified value to the stored value.

Parameters
value:Object

the value to compare

Returns:boolean

true if equal

isFullback to summary
public boolean isFull()

Implements org.apache.commons.collections4.BoundedMap.isFull.

Is the map currently full, always true.

Returns:boolean

true always

Annotations
@Override
keySetback to summary
public Set<K> keySet()

Implements java.util.Map.keySet, org.apache.commons.collections4.Get.keySet.

Gets the unmodifiable keySet view of the map. Changes made to the view affect this map. To simply iterate through the keys, use mapIterator().

Returns:Set<K>

the keySet view

Annotations
@Override
lastKeyback to summary
public K lastKey()

Implements org.apache.commons.collections4.OrderedMap.lastKey.

Gets the last (and only) key in the map.

Returns:K

the key

Annotations
@Override
mapIteratorback to summary
public OrderedMapIterator<K, V> mapIterator()

Implements org.apache.commons.collections4.OrderedMap.mapIterator, org.apache.commons.collections4.IterableGet.mapIterator.

Doc from org.apache.commons.collections4.OrderedMap.mapIterator.

Obtains an OrderedMapIterator over the map.

A ordered map iterator is an efficient way of iterating over maps in both directions.

Returns:OrderedMapIterator<K, V>

a map iterator

Annotations
@Override
maxSizeback to summary
public int maxSize()

Implements org.apache.commons.collections4.BoundedMap.maxSize.

Gets the maximum size of the map, always 1.

Returns:int

1 always

Annotations
@Override
nextKeyback to summary
public K nextKey(final K key)

Implements org.apache.commons.collections4.OrderedMap.nextKey.

Gets the next key after the key specified, always null.

Parameters
key:K

the next key

Returns:K

null always

Annotations
@Override
previousKeyback to summary
public K previousKey(final K key)

Implements org.apache.commons.collections4.OrderedMap.previousKey.

Gets the previous key before the key specified, always null.

Parameters
key:K

the next key

Returns:K

null always

Annotations
@Override
putback to summary
public V put(final K key, final V value)

Implements java.util.Map.put, org.apache.commons.collections4.Put.put.

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.

Parameters
key:K

the key to set, must be the key of the map

value:V

the value to set

Returns:V

the value previously mapped to this key, null if none

Annotations
@Override
Exceptions
IllegalArgumentException:
if the key does not match
putAllback to summary
public void putAll(final Map<? extends K, ? extends V> map)

Implements java.util.Map.putAll, org.apache.commons.collections4.Put.putAll.

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.

Parameters
map:Map<? extends K, ? extends V>

the map to add, must be size 0 or 1, and the key must match

Annotations
@Override
Exceptions
NullPointerException:
if the map is null
IllegalArgumentException:
if the key does not match
removeback to summary
public V remove(final Object key)

Implements java.util.Map.remove, org.apache.commons.collections4.Get.remove.

Unsupported operation.

Parameters
key:Object

the mapping to remove

Returns:V

the value mapped to the removed key, null if key not in map

Annotations
@Override
Exceptions
UnsupportedOperationException:
always
setValueback to summary
public V setValue(final V value)

Sets the value.

Parameters
value:V

the new value to set

Returns:V

the old value

sizeback to summary
public int size()

Implements java.util.Map.size, org.apache.commons.collections4.Get.size.

Gets the size of the map, always 1.

Returns:int

the size of 1

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Gets the map as a String.

Returns:String

a string version of the map

Annotations
@Override
valuesback to summary
public Collection<V> values()

Implements java.util.Map.values, org.apache.commons.collections4.Get.values.

Gets the unmodifiable values view of the map. Changes made to the view affect this map. To simply iterate through the values, use mapIterator().

Returns:Collection<V>

the values view

Annotations
@Override
org.apache.commons.collections4.map back to summary

pack-priv Class SingletonMap.SingletonMapIterator<K, V>

extends Object
implements OrderedMapIterator<K, V>, ResettableIterator<K>
Class Inheritance
  • java.lang.Object
  • org.apache.commons.collections4.map.SingletonMap.SingletonMapIterator
All Implemented Interfaces
org.apache.commons.collections4.ResettableIterator, java.util.Iterator, org.apache.commons.collections4.OrderedMapIterator, org.apache.commons.collections4.OrderedIterator, org.apache.commons.collections4.MapIterator

SingletonMapIterator.

Field Summary

Modifier and TypeField and Description
private boolean
private boolean
private final SingletonMap<K, V>

Constructor Summary

AccessConstructor and Description
pack-priv
SingletonMapIterator(final SingletonMap<K, V> parent)

Method Summary

Modifier and TypeMethod and Description
public K
getKey()

Implements org.apache.commons.collections4.MapIterator.getKey.

Gets the current key, which is the key returned by the last call to next().
public V
getValue()

Implements org.apache.commons.collections4.MapIterator.getValue.

Gets the current value, which is the value associated with the last key returned by next().
public boolean
hasNext()

Implements org.apache.commons.collections4.MapIterator.hasNext, java.util.Iterator.hasNext.

Checks to see if there are more entries still to be iterated.
public boolean
hasPrevious()

Implements org.apache.commons.collections4.OrderedMapIterator.hasPrevious.

Checks to see if there is a previous entry that can be iterated to.
public K
public K
previous()

Implements org.apache.commons.collections4.OrderedMapIterator.previous.

Gets the previous key from the Map.
public void
remove()

Overrides default java.util.Iterator.remove.

Implements org.apache.commons.collections4.MapIterator.remove.

Removes the last returned key from the underlying Map (optional operation).
public void
reset()

Implements org.apache.commons.collections4.ResettableIterator.reset.

Resets the iterator back to the position at which the iterator was created.
public V
setValue(final V
the new value
value
)

Implements org.apache.commons.collections4.MapIterator.setValue.

Sets the value associated with the current key (optional operation).
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

canGetSetback to summary
private boolean canGetSet
hasNextback to summary
private boolean hasNext
parentback to summary
private final SingletonMap<K, V> parent

Constructor Detail

SingletonMapIteratorback to summary
pack-priv SingletonMapIterator(final SingletonMap<K, V> parent)

Method Detail

getKeyback to summary
public K getKey()

Implements org.apache.commons.collections4.MapIterator.getKey.

Doc from org.apache.commons.collections4.MapIterator.getKey.

Gets the current key, which is the key returned by the last call to next().

Returns:K

the current key

Annotations
@Override
getValueback to summary
public V getValue()

Implements org.apache.commons.collections4.MapIterator.getValue.

Doc from org.apache.commons.collections4.MapIterator.getValue.

Gets the current value, which is the value associated with the last key returned by next().

Returns:V

the current value

Annotations
@Override
hasNextback to summary
public boolean hasNext()

Implements org.apache.commons.collections4.MapIterator.hasNext, java.util.Iterator.hasNext.

Doc from org.apache.commons.collections4.MapIterator.hasNext.

Checks to see if there are more entries still to be iterated.

Returns:boolean

true if the iterator has more elements

Annotations
@Override
hasPreviousback to summary
public boolean hasPrevious()

Implements org.apache.commons.collections4.OrderedMapIterator.hasPrevious.

Doc from org.apache.commons.collections4.OrderedMapIterator.hasPrevious.

Checks to see if there is a previous entry that can be iterated to.

Returns:boolean

true if the iterator has a previous element

Annotations
@Override
nextback to summary
public K next()

Implements org.apache.commons.collections4.MapIterator.next, java.util.Iterator.next.

Doc from org.apache.commons.collections4.MapIterator.next.

Gets the next key from the Map.

Returns:K

the next key in the iteration

Annotations
@Override
previousback to summary
public K previous()

Implements org.apache.commons.collections4.OrderedMapIterator.previous.

Doc from org.apache.commons.collections4.OrderedMapIterator.previous.

Gets the previous key from the Map.

Returns:K

the previous key in the iteration

Annotations
@Override
removeback to summary
public void remove()

Overrides default java.util.Iterator.remove.

Implements org.apache.commons.collections4.MapIterator.remove.

Doc from org.apache.commons.collections4.MapIterator.remove.

Removes the last returned key from the underlying Map (optional operation).

This method can be called once per call to next().

Annotations
@Override
resetback to summary
public void reset()

Implements org.apache.commons.collections4.ResettableIterator.reset.

Doc from org.apache.commons.collections4.ResettableIterator.reset.

Resets the iterator back to the position at which the iterator was created.

Annotations
@Override
setValueback to summary
public V setValue(final V value)

Implements org.apache.commons.collections4.MapIterator.setValue.

Doc from org.apache.commons.collections4.MapIterator.setValue.

Sets the value associated with the current key (optional operation).

Parameters
value:V

the new value

Returns:V

the previous value

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
org.apache.commons.collections4.map back to summary

pack-priv Class SingletonMap.SingletonValues<V>

extends AbstractSet<V>
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable

Values implementation for the SingletonMap. This class is needed as values is a view that must update as the map updates.

Field Summary

Modifier and TypeField and Description
private final SingletonMap<?, V>
private static final long

Constructor Summary

AccessConstructor and Description
pack-priv
SingletonValues(final SingletonMap<?, V> parent)

Method Summary

Modifier and TypeMethod and Description
public void
clear()

Overrides java.util.AbstractCollection.clear.

Implements java.util.Set.clear, java.util.Collection.clear.

Removes all of the elements from this set (optional operation).
public boolean
contains(final Object
element whose presence in this set is to be tested
object
)

Overrides java.util.AbstractCollection.contains.

Implements java.util.Set.contains, java.util.Collection.contains.

Returns true if this set contains the specified element.
public boolean
isEmpty()

Overrides java.util.AbstractCollection.isEmpty.

Implements java.util.Set.isEmpty, java.util.Collection.isEmpty.

Returns true if this set contains no elements.
public Iterator<V>
iterator()

Implements abstract java.util.AbstractCollection.iterator.

Implements java.util.Set.iterator, java.util.Collection.iterator.

Returns an iterator over the elements in this set.
public int
size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.Set.size, java.util.Collection.size.

Returns the number of elements in this set (its cardinality).
Inherited from java.util.AbstractSet:
equalshashCoderemoveAll

Field Detail

parentback to summary
private final SingletonMap<?, V> parent
serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

SingletonValuesback to summary
pack-priv SingletonValues(final SingletonMap<?, V> parent)

Method Detail

clearback to summary
public void clear()

Overrides java.util.AbstractCollection.clear.

Implements java.util.Set.clear, java.util.Collection.clear.

Doc from java.util.Set.clear.

Removes all of the elements from this set (optional operation). The set will be empty after this call returns.

Annotations
@Override
containsback to summary
public boolean contains(final Object object)

Overrides java.util.AbstractCollection.contains.

Implements java.util.Set.contains, java.util.Collection.contains.

Doc from java.util.Set.contains.

Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that Objects.equals(o, e).

Parameters
object:Object

element whose presence in this set is to be tested

Returns:boolean

true if this set contains the specified element

Annotations
@Override
isEmptyback to summary
public boolean isEmpty()

Overrides java.util.AbstractCollection.isEmpty.

Implements java.util.Set.isEmpty, java.util.Collection.isEmpty.

Doc from java.util.Set.isEmpty.

Returns true if this set contains no elements.

Returns:boolean

true if this set contains no elements

Annotations
@Override
iteratorback to summary
public Iterator<V> iterator()

Implements abstract java.util.AbstractCollection.iterator.

Implements java.util.Set.iterator, java.util.Collection.iterator.

Doc from java.util.Set.iterator.

Returns an iterator over the elements in this set. The elements are returned in no particular order (unless this set is an instance of some class that provides a guarantee).

Returns:Iterator<V>

an iterator over the elements in this set

Annotations
@Override
sizeback to summary
public int size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.Set.size, java.util.Collection.size.

Doc from java.util.Set.size.

Returns the number of elements in this set (its cardinality). If this set contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Returns:int

the number of elements in this set (its cardinality)

Annotations
@Override