bcds.tools
Class MapToConst<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by bcds.tools.MapToConst<K,V>
All Implemented Interfaces:
java.util.Map<K,V>

public class MapToConst<K,V>
extends java.util.AbstractMap<K,V>

Maps each and every key to a (single) constant value. The map itself is not constant, and keys can be added and removed. Although put and putAll cannot be called through the map, they can be called through the key set passed to the constructor, which is shared with the map object.

MapToConst extends AbstractMap to avoid reimplementing hashCode and equals.

Several methods are overriden (for example size) because otherwise their implementation in AbstractMap produce calls to entrySet, quite unnecessarily in this case.

Author:
Juan Segovia S.

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V>
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Constructor Summary
MapToConst(java.util.Set<K> keys, V const_value)
          Creates an instance in which the values in keys map to const_value, and any other value maps to null.
 
Method Summary
 void clear()
          Clears the key set.
 boolean containsKey(java.lang.Object key)
          Returns true if key is mapped by this object.
 boolean containsValue(java.lang.Object value)
          Returns true if value is equal to the constant value to which all elements in this object map.
 java.util.Set<java.util.Map.Entry<K,V>> entrySet()
          Returns a set of immutable entries.
 V get(java.lang.Object key)
          Returns the constant value associated to this map if key exists.
 boolean isEmpty()
          Returns true if the key set is empty.
 java.util.Set<K> keySet()
          Returns a reference to the key set.
static
<K,V> MapToConst<K,V>
make(java.util.Set<K> keys, V const_value)
          A convenience "factory" method.
 V remove(java.lang.Object key)
          Removes key from the map and returns the constant value.
 int size()
          Returns the number of elements of the key set.
 java.lang.String toString()
          Returns a string the list of keys, an arrow and the constant value.
 java.util.Collection<V> values()
          Returns the same constant value repeated |keys| times so that the number of keys and the number of values match.
 
Methods inherited from class java.util.AbstractMap
clone, equals, hashCode, put, putAll
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MapToConst

public MapToConst(java.util.Set<K> keys,
                  V const_value)
Creates an instance in which the values in keys map to const_value, and any other value maps to null.

The set keys is not copied by this object, so that any change in that set performed by the caller will affect this map. This is a deliberate decision, so that the user can insert or remove keys if so is desired.

Parameters:
const_value - may be null.
keys - must not be null.
Method Detail

make

public static <K,V> MapToConst<K,V> make(java.util.Set<K> keys,
                                         V const_value)
A convenience "factory" method.


clear

public void clear()
Clears the key set. Beware that the key set is shared with the caller.

Specified by:
clear in interface java.util.Map<K,V>
Overrides:
clear in class java.util.AbstractMap<K,V>

containsKey

public boolean containsKey(java.lang.Object key)
Returns true if key is mapped by this object.

Specified by:
containsKey in interface java.util.Map<K,V>
Overrides:
containsKey in class java.util.AbstractMap<K,V>

containsValue

public boolean containsValue(java.lang.Object value)
Returns true if value is equal to the constant value to which all elements in this object map.

Specified by:
containsValue in interface java.util.Map<K,V>
Overrides:
containsValue in class java.util.AbstractMap<K,V>

entrySet

public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
Returns a set of immutable entries. These are "artificial" entries (created only to satisfy this method) for this class does not need or use map entries. The entries are of type AbstractMap.SimpleImmutableEntry.

Specified by:
entrySet in interface java.util.Map<K,V>
Specified by:
entrySet in class java.util.AbstractMap<K,V>

get

public V get(java.lang.Object key)
Returns the constant value associated to this map if key exists. Otherwise, it returns null. Note that if the constant value itself is null, the value returned cannot be used as an indication of the existance of a key.

Specified by:
get in interface java.util.Map<K,V>
Overrides:
get in class java.util.AbstractMap<K,V>

isEmpty

public boolean isEmpty()
Returns true if the key set is empty.

Specified by:
isEmpty in interface java.util.Map<K,V>
Overrides:
isEmpty in class java.util.AbstractMap<K,V>

keySet

public java.util.Set<K> keySet()
Returns a reference to the key set.

Specified by:
keySet in interface java.util.Map<K,V>
Overrides:
keySet in class java.util.AbstractMap<K,V>

remove

public V remove(java.lang.Object key)
Removes key from the map and returns the constant value. If the key does not exist, null is returned.

Specified by:
remove in interface java.util.Map<K,V>
Overrides:
remove in class java.util.AbstractMap<K,V>

size

public int size()
Returns the number of elements of the key set.

Specified by:
size in interface java.util.Map<K,V>
Overrides:
size in class java.util.AbstractMap<K,V>

toString

public java.lang.String toString()
Returns a string the list of keys, an arrow and the constant value.

Overrides:
toString in class java.util.AbstractMap<K,V>

values

public java.util.Collection<V> values()
Returns the same constant value repeated |keys| times so that the number of keys and the number of values match.

Specified by:
values in interface java.util.Map<K,V>
Overrides:
values in class java.util.AbstractMap<K,V>