net.sf.molae.pipe.sorted
Class MapAsSortedMap<B extends Map<K,V>,K,V>

java.lang.Object
  extended by net.sf.molae.pipe.basic.ObjectProxy<B>
      extended by net.sf.molae.pipe.basic.MapProxy<B,K,V>
          extended by net.sf.molae.pipe.sorted.MapAsSortedMap<B,K,V>
All Implemented Interfaces:
Map<K,V>, SortedMap<K,V>
Direct Known Subclasses:
SortedListAsSortedMap

public class MapAsSortedMap<B extends Map<K,V>,K,V>
extends MapProxy<B,K,V>
implements SortedMap<K,V>

If it can be guaranteed that the iterator of a map returns elements in sorted key order, this wrapper can be used to create a SortedMap implementation.


Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Constructor Summary
  MapAsSortedMap(B base, Comparator<? super K> comparator)
          Constructs a MapAsSortedMap object.
protected MapAsSortedMap(B base, Comparator<? super K> comparator, Comparator<Bound<K>> boundComparator, Bound<K> fromKey, Bound<K> toKey)
          Creates a new MapAsSortedMap with the specified properties.
 
Method Summary
 Comparator<? super K> comparator()
          Returns the comparator associated with this sorted map, or null if it uses its elements' natural ordering.
protected  int compare(Bound<K> o1, Bound<K> o2)
          Compares its two arguments for order using a comparator with infinities.
 K firstKey()
          Returns the first (lowest) key currently in this sorted map.
protected  Comparator<Bound<K>> getBoundComparator()
          Returns the comparator of this set taking infinity into account.
protected  Bound<K> getFromKey()
          Returns the lower bound of this SortedMap.
protected  SortedMap<K,V> getSubMap(Bound<K> fromKey, Bound<K> toKey)
          Common method to create head, tail and sub maps.
protected  Bound<K> getToKey()
          Returns the upper bound of this SortedMap.
 SortedMap<K,V> headMap(K toKey)
          Returns a view of the portion of this sorted map whose keys are strictly less than toKey.
 K lastKey()
          Returns the last (highest) key currently in this sorted map.
static
<K,V> SortedMap<K,V>
newInstance(Map<K,V> base, Comparator<? super K> comparator)
          Constructs a MapAsSortedMap object.
 SortedMap<K,V> subMap(K fromKey, K toKey)
          Returns a view of the portion of this Map whose keys range from fromKey, inclusive, to toKey, exclusive.
 SortedMap<K,V> tailMap(K fromKey)
          Returns a view of the portion of this sorted map whose keys are greater than or equal to fromKey.
 
Methods inherited from class net.sf.molae.pipe.basic.MapProxy
clear, containsKey, containsValue, entrySet, equals, get, isEmpty, keySet, put, putAll, remove, size, values
 
Methods inherited from class net.sf.molae.pipe.basic.ObjectProxy
assertNotNull, getBase, hashCode, toString, toVerboseString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.SortedMap
entrySet, keySet, values
 
Methods inherited from interface java.util.Map
clear, containsKey, containsValue, equals, get, hashCode, isEmpty, put, putAll, remove, size
 

Constructor Detail

MapAsSortedMap

protected MapAsSortedMap(B base,
                         Comparator<? super K> comparator,
                         Comparator<Bound<K>> boundComparator,
                         Bound<K> fromKey,
                         Bound<K> toKey)
Creates a new MapAsSortedMap with the specified properties. This constructor is intended for classes that implement both the SortedMap and its submaps.

Parameters:
base - the underlying base map
comparator - the comparator that will be used to sort this map A null value indicates that the keys' natural ordering should be used.
boundComparator - the comparator that will be used to compare bounds - used for submaps
fromKey - low endpoint (inclusive) of the subMap.
toKey - high endpoint (exclusive) of the subMap.
Throws:
ClassCastException - if FromKey and toKey cannot be compared to one another using this Map's comparator (or, if the Map has no comparator, using natural ordering).
IllegalArgumentException - if fromKey is greater than toKey.
NullPointerException - if fromKey or toKey is null and this Map uses natural order, or its comparator does not tolerate null Keys or if the base map is null.

MapAsSortedMap

public MapAsSortedMap(B base,
                      Comparator<? super K> comparator)
Constructs a MapAsSortedMap object.

Parameters:
base - the underlying base map
comparator - the comparator that will be used to sort this map A null value indicates that the keys' natural ordering should be used.
Method Detail

comparator

public final Comparator<? super K> comparator()
Returns the comparator associated with this sorted map, or null if it uses its elements' natural ordering.

Specified by:
comparator in interface SortedMap<K,V>
Returns:
the comparator associated with this sorted map, or null if it uses its elements' natural ordering

getBoundComparator

protected Comparator<Bound<K>> getBoundComparator()
Returns the comparator of this set taking infinity into account.

Returns:
the comparator of this set taking infinity into account

getFromKey

protected final Bound<K> getFromKey()
Returns the lower bound of this SortedMap. Returns negative infinity if no such bound was specified.

Returns:
the lower bound of this SortedMap.

getToKey

protected final Bound<K> getToKey()
Returns the upper bound of this SortedMap. Returns positive infinity if no such bound was specified.

Returns:
the upper bound of this SortedMap

newInstance

public static <K,V> SortedMap<K,V> newInstance(Map<K,V> base,
                                               Comparator<? super K> comparator)
Constructs a MapAsSortedMap object.

Parameters:
base - the underlying base map
comparator - the comparator that will be used to sort this map A null value indicates that the keys' natural ordering should be used.

compare

protected final int compare(Bound<K> o1,
                            Bound<K> o2)
Compares its two arguments for order using a comparator with infinities.

Parameters:
o1 - first key to compare
o2 - second key to compare
Returns:
a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

getSubMap

protected SortedMap<K,V> getSubMap(Bound<K> fromKey,
                                   Bound<K> toKey)
Common method to create head, tail and sub maps. Overwrite this method for specific implementations.

Parameters:
fromKey - low endpoint (inclusive) of the subMap.
toKey - high endpoint (exclusive) of the subMap.
Returns:
a view of the portion of this Map whose keys range from fromKey, inclusive, to toKey, exclusive.

subMap

public SortedMap<K,V> subMap(K fromKey,
                             K toKey)
Returns a view of the portion of this Map whose keys range from fromKey, inclusive, to toKey, exclusive. (If fromKey and toKey are equal, the returned sorted Map is empty.) This implementation calls getSubMap.

Specified by:
subMap in interface SortedMap<K,V>
Parameters:
fromKey - low endpoint (inclusive) of the subMap.
toKey - high endpoint (exclusive) of the subMap.
Returns:
a view of the portion of this Map whose keys range from fromKey, inclusive, to toKey, exclusive.
Throws:
ClassCastException - if FromKey and toKey cannot be compared to one another using this Map's comparator (or, if the Map has no comparator, using natural ordering).
IllegalArgumentException - if fromKey is greater than toKey.
NullPointerException - if fromKey or toKey is null and this Map uses natural order, or its comparator does not tolerate null Keys.

headMap

public SortedMap<K,V> headMap(K toKey)
Returns a view of the portion of this sorted map whose keys are strictly less than toKey. This implementation calls getSubMap() with the the fromKey of this SortedMap and the specified toKey.

Specified by:
headMap in interface SortedMap<K,V>
Parameters:
toKey - high endpoint (exclusive) of the headMap.
Returns:
a view of the specified initial range of this sorted map.
Throws:
ClassCastException - if toKey is not compatible with this map's comparator (or, if the map has no comparator, if toKey does not implement Comparable).
IllegalArgumentException - if this map is itself a subMap, headMap, or tailMap, and toKey is not within the specified range of the subMap, headMap, or tailMap.
NullPointerException - if toKey is null and this sorted map does not tolerate null keys.

tailMap

public SortedMap<K,V> tailMap(K fromKey)
Returns a view of the portion of this sorted map whose keys are greater than or equal to fromKey. This implementation calls getSubMap() with the specified fromKey and the toKey of this SortedMap.

Specified by:
tailMap in interface SortedMap<K,V>
Parameters:
fromKey - low endpoint (inclusive) of the tailMap.
Returns:
a view of the specified final range of this sorted map.
Throws:
ClassCastException - if fromKey is not compatible with this map's comparator (or, if the map has no comparator, if fromKey does not implement Comparable).
IllegalArgumentException - if this map is itself a subMap, headMap, or tailMap, and fromKey is not within the specified range of the subMap, headMap, or tailMap.
NullPointerException - if fromKey is null and this sorted map does not tolerate null keys.

firstKey

public K firstKey()
Returns the first (lowest) key currently in this sorted map. This implementation returns the first element from the keySet iterator.

Specified by:
firstKey in interface SortedMap<K,V>
Returns:
the first (lowest) key currently in this sorted map
Throws:
NoSuchElementException - if this map is empty.

lastKey

public K lastKey()
Returns the last (highest) key currently in this sorted map. This implementation returns the last element from the keySet iterator.

Specified by:
lastKey in interface SortedMap<K,V>
Returns:
the last (highest) key currently in this sorted map
Throws:
NoSuchElementException - if this map is empty.