org.apache.commons.collections4.map

## Class SingletonMap<K,V>

- java.lang.Object
- - org.apache.commons.collections4.map.SingletonMap<K,V>

- All Implemented Interfaces:
    Serializable, Cloneable, 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 Object
      implements OrderedMap<K,V>, BoundedMap<K,V>, KeyValue<K,V>, Serializable, Cloneable

  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 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`, see `mapIterator()`
  - the `KeyValue` interface (just cast - no object creation)
  Since:
    3.1
  Version:
    $Id: SingletonMap.java 1533984 2013-10-20 21:12:51Z tn $
  See Also:
    Serialized Form

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from interface java.util.Map

      `Map.Entry<K,V>`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `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<? extends K,? extends V> mapEntry)` Constructor specifying the key and value as a `MapEntry`.
    `SingletonMap(Map<? extends K,? extends V> map)` Constructor copying elements from another map.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `clear()` Unsupported operation.
    `SingletonMap<K,V>` | `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.
    `OrderedMapIterator<K,V>` | `mapIterator()` Obtains an `OrderedMapIterator` 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.
    `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

    - #### SingletonMap

          public SingletonMap()

      Constructor that creates a map of `null` to `null`.

    - #### SingletonMap

          public SingletonMap(K key,
                      V value)

      Constructor specifying the key and value.
      Parameters:
        `key` - the key to use
        `value` - the value to use

    - #### SingletonMap

          public SingletonMap(KeyValue<K,V> keyValue)

      Constructor specifying the key and value as a `KeyValue`.
      Parameters:
        `keyValue` - the key value pair to use

    - #### SingletonMap

          public SingletonMap(Map.Entry<? extends K,? extends V> mapEntry)

      Constructor specifying the key and value as a `MapEntry`.
      Parameters:
        `mapEntry` - the mapEntry to use

    - #### SingletonMap

          public SingletonMap(Map<? extends K,? extends V> map)

      Constructor copying elements from another map.
      Parameters:
        `map` - the map to copy, must be size 1
      Throws:
        `NullPointerException` - if the map is null
        `IllegalArgumentException` - if the size is not 1

  - ### Method Detail

    - #### getKey

          public K getKey()

      Gets the key.
      Specified by:
        `getKey` in interface `KeyValue<K,V>`
      Returns:
        the key

    - #### getValue

          public V getValue()

      Gets the value.
      Specified by:
        `getValue` in interface `KeyValue<K,V>`
      Returns:
        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:
        `isFull` in interface `BoundedMap<K,V>`
      Returns:
        true always

    - #### maxSize

          public int maxSize()

      Gets the maximum size of the map, always 1.
      Specified by:
        `maxSize` in interface `BoundedMap<K,V>`
      Returns:
        1 always

    - #### get

          public V get(Object key)

      Gets the value mapped to the key specified.
      Specified by:
        `get` in interface `Map<K,V>`
      Specified by:
        `get` in interface `Get<K,V>`
      Parameters:
        `key` - the key
      Returns:
        the mapped value, null if no match
      See Also:
        `Map.get(Object)`

    - #### size

          public int size()

      Gets the size of the map, always 1.
      Specified by:
        `size` in interface `Map<K,V>`
      Specified by:
        `size` in interface `Get<K,V>`
      Returns:
        the size of 1
      See Also:
        `Map.size()`

    - #### isEmpty

          public boolean isEmpty()

      Checks whether the map is currently empty, which it never is.
      Specified by:
        `isEmpty` in interface `Map<K,V>`
      Specified by:
        `isEmpty` in interface `Get<K,V>`
      Returns:
        false always
      See Also:
        `Map.isEmpty()`

    - #### containsKey

          public boolean containsKey(Object key)

      Checks whether the map contains the specified key.
      Specified by:
        `containsKey` in interface `Map<K,V>`
      Specified by:
        `containsKey` in interface `Get<K,V>`
      Parameters:
        `key` - the key to search for
      Returns:
        true if the map contains the key
      See Also:
        `Map.containsKey(Object)`

    - #### containsValue

          public boolean containsValue(Object value)

      Checks whether the map contains the specified value.
      Specified by:
        `containsValue` in interface `Map<K,V>`
      Specified by:
        `containsValue` in interface `Get<K,V>`
      Parameters:
        `value` - the value to search for
      Returns:
        true if the map contains the key
      See Also:
        `Map.containsValue(Object)`

    - #### 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:
        `put` in interface `Map<K,V>`
      Specified by:
        `put` in interface `Put<K,V>`
      Parameters:
        `key` - the key to set, must be the key of the map
        `value` - the value to set
      Returns:
        the value previously mapped to this key, null if none
      Throws:
        `IllegalArgumentException` - if the key does not match
      See Also:
        `Map.put(Object, Object)`

    - #### putAll

          public void putAll(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:
        `putAll` in interface `Map<K,V>`
      Specified by:
        `putAll` in interface `Put<K,V>`
      Parameters:
        `map` - the map to add, must be size 0 or 1, and the key must match
      Throws:
        `NullPointerException` - if the map is null
        `IllegalArgumentException` - if the key does not match
      See Also:
        `Map.putAll(Map)`

    - #### remove

          public V remove(Object key)

      Unsupported operation.
      Specified by:
        `remove` in interface `Map<K,V>`
      Specified by:
        `remove` in interface `Get<K,V>`
      Parameters:
        `key` - the mapping to remove
      Returns:
        the value mapped to the removed key, null if key not in map
      Throws:
        `UnsupportedOperationException` - always
      See Also:
        `Map.remove(Object)`

    - #### clear

          public void clear()

      Unsupported operation.
      Specified by:
        `clear` in interface `Map<K,V>`
      Specified by:
        `clear` in interface `Put<K,V>`
      See Also:
        `Map.clear()`

    - #### entrySet

          public Set<Map.Entry<K,V>> entrySet()

      Gets the entrySet view of the map. Changes made via `setValue` affect this map. To simply iterate through the entries, use `mapIterator()`.
      Specified by:
        `entrySet` in interface `Map<K,V>`
      Specified by:
        `entrySet` in interface `Get<K,V>`
      Returns:
        the entrySet view
      See Also:
        `Map.entrySet()`

    - #### keySet

          public 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, use `mapIterator()`.
      Specified by:
        `keySet` in interface `Map<K,V>`
      Specified by:
        `keySet` in interface `Get<K,V>`
      Returns:
        the keySet view
      See Also:
        `Map.keySet()`

    - #### values

          public 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, use `mapIterator()`.
      Specified by:
        `values` in interface `Map<K,V>`
      Specified by:
        `values` in interface `Get<K,V>`
      Returns:
        the values view
      See Also:
        `Map.values()`

    - #### mapIterator

          public OrderedMapIterator<K,V> mapIterator()

      Obtains an `OrderedMapIterator` over the map.

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

      Specified by:
        `mapIterator` in interface `IterableGet<K,V>`
      Specified by:
        `mapIterator` in interface `OrderedMap<K,V>`
      Returns:
        a map iterator

    - #### firstKey

          public K firstKey()

      Gets the first (and only) key in the map.
      Specified by:
        `firstKey` in interface `OrderedMap<K,V>`
      Returns:
        the key

    - #### lastKey

          public K lastKey()

      Gets the last (and only) key in the map.
      Specified by:
        `lastKey` in interface `OrderedMap<K,V>`
      Returns:
        the key

    - #### nextKey

          public K nextKey(K key)

      Gets the next key after the key specified, always null.
      Specified by:
        `nextKey` in interface `OrderedMap<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:
        `previousKey` in interface `OrderedMap<K,V>`
      Parameters:
        `key` - the next key
      Returns:
        null always

    - #### isEqualKey

          protected boolean isEqualKey(Object key)

      Compares the specified key to the stored key.
      Parameters:
        `key` - the key to compare
      Returns:
        true if equal

    - #### isEqualValue

          protected boolean isEqualValue(Object value)

      Compares the specified value to the stored value.
      Parameters:
        `value` - the value to compare
      Returns:
        true if equal

    - #### clone

          public SingletonMap<K,V> clone()

      Clones the map without cloning the key or value.
      Overrides:
        `clone` in class `Object`
      Returns:
        a shallow clone

    - #### equals

          public boolean equals(Object obj)

      Compares this map with another.
      Specified by:
        `equals` in interface `Map<K,V>`
      Overrides:
        `equals` in class `Object`
      Parameters:
        `obj` - the object to compare to
      Returns:
        true if equal

    - #### hashCode

          public int hashCode()

      Gets the standard Map hashCode.
      Specified by:
        `hashCode` in interface `Map<K,V>`
      Overrides:
        `hashCode` in class `Object`
      Returns:
        the hash code defined in the Map interface

    - #### toString

          public String toString()

      Gets the map as a String.
      Overrides:
        `toString` in class `Object`
      Returns:
        a string version of the map

Copyright © 2001–2013 The Apache Software Foundation. All rights reserved.