ch.javasoft.util.map
Class JoinedMultiValueMap<K,V>

java.lang.Object
  extended by ch.javasoft.util.map.AbstractMultiValueMap<K,V>
      extended by ch.javasoft.util.map.JoinedMultiValueMap<K,V>
Type Parameters:
K - the key type of the one side of the join
V - the value type of the many side of the join
All Implemented Interfaces:
MultiValueMap<K,V>, Serializable

public class JoinedMultiValueMap<K,V>
extends AbstractMultiValueMap<K,V>
implements Serializable

The JoinedMultiValueMap class joins two instances of MultiValueMap. The resulting multi value map is immutable.

Joining multi value maps nests the joined maps and does not copy the values. However, for a large number of accesses, it might be more efficient to materialize the joined map using flatten() beforehand.

See Also:
Serialized Form

Constructor Summary
JoinedMultiValueMap(MultiValueMap<K,?> one, MultiValueMap<?,V> many)
          Cconstructor to join two multi value maps
 
Method Summary
 boolean contains(Object key)
          Returns true if the value collection is non-empty
 boolean contains(Object key, V value)
          Returns true if the value collection contains at least one occurrence of the given value
 int count(Object key)
          Returns the number of values in the value collection
protected  Collection<V> createCollection(Collection<V> values)
          Returns a new collection instance.
 MultiValueMap<K,V> flatten()
          Materializes this virtual multi value map into a new instance of DefaultMultiValueMap, a mutable map, by copying the content of this map into the new instance.
 Collection<V> get(Object key)
          Returns a collection for all values belonging to the specified key, or an empty collection if no such key collection exists yet.
 V getFirst(Object key)
          Returns the first value of the value collection, or null if no such value exists
static
<K,V> JoinedMultiValueMap<K,V>
join(MultiValueMap<K,?> one, MultiValueMap<?,V> many)
          Alternative to the constructor, for convenience only.
static
<K,V> JoinedMultiValueMap<K,V>
join(MultiValueMap<K,?> first, MultiValueMap<?,V> last, MultiValueMap<?,?>... intermediate)
          Joins several multi value maps resulting in a new virtual multi value map.
 Set<K> keySet()
          Returns an unmodifiable set with the keys
 Iterable<V> values()
          Returns an unmodifiable iterator for all values
 
Methods inherited from class ch.javasoft.util.map.AbstractMultiValueMap
add, addAll, addAll, addAll, addAll, addAllNested, asCollectionMap, asSingleValueMap, clear, equals, hashCode, invert, isEmpty, keySize, remove, remove, toString, valueSize
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JoinedMultiValueMap

public JoinedMultiValueMap(MultiValueMap<K,?> one,
                           MultiValueMap<?,V> many)
Cconstructor to join two multi value maps

Parameters:
one - the one side of the join
many - the many side of the join
Method Detail

join

public static <K,V> JoinedMultiValueMap<K,V> join(MultiValueMap<K,?> one,
                                                  MultiValueMap<?,V> many)
Alternative to the constructor, for convenience only. Joins the two multi value maps resulting in a new virtual multi value map.

Type Parameters:
K - the key type of the one side of the join
V - the value type of the many side of the join
Parameters:
one - the one side of the join
many - the many side of the join
Returns:
the joined multi value map

join

public static <K,V> JoinedMultiValueMap<K,V> join(MultiValueMap<K,?> first,
                                                  MultiValueMap<?,V> last,
                                                  MultiValueMap<?,?>... intermediate)
Joins several multi value maps resulting in a new virtual multi value map. The first map defines the key type of the resulting map, the last map the value type.

Type Parameters:
K - the key type of the head map of the join
V - the value type of the tail map of the join
Parameters:
first - the head map of the join
last - the tail map of the join
intermediate - intermediary maps of the join, in join order
Returns:
the joined multi value map

flatten

public MultiValueMap<K,V> flatten()
Materializes this virtual multi value map into a new instance of DefaultMultiValueMap, a mutable map, by copying the content of this map into the new instance.

Returns:
a DefaultMultiValueMap instance containing the same key/collection-of-value pairs as this multi value map

contains

public boolean contains(Object key)
Description copied from interface: MultiValueMap
Returns true if the value collection is non-empty

Specified by:
contains in interface MultiValueMap<K,V>
Overrides:
contains in class AbstractMultiValueMap<K,V>
Parameters:
key - the key which identifies the value collection
Returns:
true if at least one value exists in the value collection

contains

public boolean contains(Object key,
                        V value)
Description copied from interface: MultiValueMap
Returns true if the value collection contains at least one occurrence of the given value

Specified by:
contains in interface MultiValueMap<K,V>
Parameters:
key - the key which identifies the value collection
value - the value to look for
Returns:
true if at least one such value exists in the value collection

count

public int count(Object key)
Description copied from interface: MultiValueMap
Returns the number of values in the value collection

Specified by:
count in interface MultiValueMap<K,V>
Parameters:
key - the key which identifies the value collection
Returns:
the number of values in the value collection

get

public Collection<V> get(Object key)
Description copied from interface: MultiValueMap
Returns a collection for all values belonging to the specified key, or an empty collection if no such key collection exists yet. For existing keys, the returned collection is non-empty.

Specified by:
get in interface MultiValueMap<K,V>
Parameters:
key - the key which identifies the value collection
Returns:
the value collection for the given key, never null, and non-empty for existing keys

getFirst

public V getFirst(Object key)
Description copied from interface: MultiValueMap
Returns the first value of the value collection, or null if no such value exists

Specified by:
getFirst in interface MultiValueMap<K,V>
Overrides:
getFirst in class AbstractMultiValueMap<K,V>
Parameters:
key - the key which identifies the value collection
Returns:
the first value of the value collection, or null if no such value exists

keySet

public Set<K> keySet()
Returns an unmodifiable set with the keys

Specified by:
keySet in interface MultiValueMap<K,V>
Returns:
the set of keys identifying non-empty value collections
See Also:
MultiValueMap.keySet()

values

public Iterable<V> values()
Description copied from class: AbstractMultiValueMap
Returns an unmodifiable iterator for all values

Specified by:
values in interface MultiValueMap<K,V>
Overrides:
values in class AbstractMultiValueMap<K,V>
Returns:
an iterator for all values contained in this mapping
See Also:
MultiValueMap.values()

createCollection

protected Collection<V> createCollection(Collection<V> values)
Returns a new collection instance. By default, an ArrayList with the given values is created, or an empty list if values is null.

Parameters:
values - the values to be added to the created list, or null if an empty list should be returned
Returns:
the new array list instance, or an empty list if values is null