saker.util Documentation TaskDoc JavaDoc Packages
package saker.util
public abstract class TransformingMap<MK, MV, K, Vextends AbstractMap<K, V>
TransformingNavigableMap<MK, MV, K, V>, TransformingSortedMap<MK, MV, K, V>
Pseudo-Map implementation that dynamically generates the entries for it based on the entries of a subject map.

This map is immutable, and designed to provide a map implementation that is only used to iterate over its entries. Each entry in this map is dynamically generated based on an entry in the underlying map.

Any method that is not the size(), isEmpty(), Map.forEach(BiConsumer<super K, ? super V>), AbstractMap.values(), AbstractMap.keySet(), entrySet() and their iterator() functions, may throw an UnsupportedOperationException any time.

Important: Implementations should ensure that the transformed keys still stay unique in the map, as they was in the subject map. Violating this may result in undefined behaviour in some implementations.

An use-case for this kind of map is to create a new Map with the given entries without pre-allocating the transformed entries beforehand.

Example:

 Map<Integer, Integer> ints = ...;
 Map<Integer, String> nmap = new TreeMap<>(new TransformingMap<Integer, Integer, Integer, String>(ints) {
 	@Override
 	protected Entry<Integer, String> transformEntry(Integer key, Integer value) {
 		return ImmutableUtils.makeImmutableMapEntry(key + value, Integer.toString(key * value));
 	}
 });
 
Constructing a map in this way instead of calling Map.put(K, V) for every entry can be more efficient, as new maps can allocate or construct their instances more efficiently.
MKThe key type of the source map.
MVThe value type of the source map.
KThe key type of this map.
VThe value type of this map.
Fields
protected final Map<extends MK, ? extends MV>
The backing map of the transforming map.
Constructors
public
TransformingMap(Map<extends MK, ? extends MV> map)
Creates a new instance with the given map.
Methods
public Set<Entry<K, V>>
Returns a Set view of the mappings contained in this map.
public boolean
Returns true if this map contains no key-value mappings.
public int
Returns the number of key-value mappings in this map.
protected abstract Entry<K, V>
transformEntry(MK key, MV value)
Transforms the source entry key-value pair to the map entry.
protected final Map<extends MK, ? extends MV> map
The backing map of the transforming map.
public TransformingMap(Map<extends MK, ? extends MV> map) throws NullPointerException
Creates a new instance with the given map.
mapThe subject map.
NullPointerExceptionIf the map is null.
public Set<Entry<K, V>> entrySet()
Overridden from: Map
Returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.
a set view of the mappings contained in this map
public boolean isEmpty()
Overridden from: Map
Returns true if this map contains no key-value mappings.
true if this map contains no key-value mappings
public int size()
Overridden from: Map
Returns the number of key-value mappings in this map. If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
the number of key-value mappings in this map
protected abstract Entry<K, V> transformEntry(MK key, MV value)
Transforms the source entry key-value pair to the map entry.
keyThe source entry key.
valueThe source entry value.
The transformed entry.