net.sf.molae.pipe.sorted
Class MapAsSortedMap

java.lang.Object
  |
  +--net.sf.molae.pipe.basic.ObjectProxy
        |
        +--net.sf.molae.pipe.basic.MapProxy
              |
              +--net.sf.molae.pipe.sorted.MapAsSortedMap
All Implemented Interfaces:
Map, SortedMap
Direct Known Subclasses:
SortedListAsSortedMap

public class MapAsSortedMap
extends MapProxy
implements SortedMap

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.


Inner classes inherited from class java.util.Map
Map.Entry
 
Field Summary
protected static Object NEGATIVE_INFINITY
          Defines negative infinity regardless of the type of comparison.
protected static Object POSITIVE_INFINITY
          Defines positive infinity regardless of the type of comparison.
 
Constructor Summary
  MapAsSortedMap(Map base, Comparator comparator)
          Constructs a MapAsSortedMap object.
protected MapAsSortedMap(Map base, Comparator comparator, Object fromKey, Object toKey)
          Creates a new MapAsSortedMap with the specified properties.
 
Method Summary
 Comparator comparator()
          Returns the comparator associated with this sorted map, or null if it uses its elements' natural ordering.
protected  int compare(Object o1, Object o2)
          Compares its two arguments for order using a comparator with infinities.
 Object firstKey()
          Returns the first (lowest) key currently in this sorted map.
protected  Comparator getBoundComparator()
          Returns the comparator of this set taking infinity into account.
protected  Object getFromKey()
          Returns the lower bound of this SortedMap.
protected  SortedMap getSubMap(Object fromKey, Object toKey)
          Common method to create head, tail and sub maps.
protected  Object getToKey()
          Returns the upper bound of this SortedMap.
 SortedMap headMap(Object toKey)
          Returns a view of the portion of this sorted map whose keys are strictly less than toKey.
 Object lastKey()
          Returns the last (highest) key currently in this sorted map.
 SortedMap subMap(Object fromKey, Object toKey)
          Returns a view of the portion of this Map whose keys range from fromKey, inclusive, to toKey, exclusive.
 SortedMap tailMap(Object 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, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
clear, containsKey, containsValue, entrySet, equals, get, hashCode, isEmpty, keySet, put, putAll, remove, size, values
 

Field Detail

NEGATIVE_INFINITY

protected static final Object NEGATIVE_INFINITY
Defines negative infinity regardless of the type of comparison.

POSITIVE_INFINITY

protected static final Object POSITIVE_INFINITY
Defines positive infinity regardless of the type of comparison.
Constructor Detail

MapAsSortedMap

protected MapAsSortedMap(Map base,
                         Comparator comparator,
                         Object fromKey,
                         Object 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.
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(Map base,
                      Comparator 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 comparator()
Returns the comparator associated with this sorted map, or null if it uses its elements' natural ordering.
Specified by:
comparator in interface SortedMap
Returns:
the comparator associated with this sorted map, or null if it uses its elements' natural ordering

getBoundComparator

protected Comparator getBoundComparator()
Returns the comparator of this set taking infinity into account.
Returns:
the comparator of this set taking infinity into account

getFromKey

protected final Object 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 Object getToKey()
Returns the upper bound of this SortedMap. Returns POSITIVE_INFINITY if no such bound was specified.
Returns:
the upper bound of this SortedMap

compare

protected final int compare(Object o1,
                            Object 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 getSubMap(Object fromKey,
                              Object 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 subMap(Object fromKey,
                        Object 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
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 headMap(Object 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
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 tailMap(Object 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
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 Object 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
Returns:
the first (lowest) key currently in this sorted map
Throws:
NoSuchElementException - if this map is empty.

lastKey

public Object 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
Returns:
the last (highest) key currently in this sorted map
Throws:
NoSuchElementException - if this map is empty.