org.compass.core.util.backport.java.util
Class TreeMap

java.lang.Object
  extended by java.util.AbstractMap
      extended by org.compass.core.util.backport.java.util.AbstractMap
          extended by org.compass.core.util.backport.java.util.TreeMap
All Implemented Interfaces:
Serializable, Map, SortedMap, NavigableMap

public class TreeMap
extends AbstractMap
implements NavigableMap, Serializable

Sorted map implementation based on a red-black tree and implementing all the methods from the NavigableMap interface.

Author:
Dawid Kurzyniec
See Also:
Serialized Form

Nested Class Summary
(package private)  class TreeMap.DescendingEntryIterator
           
(package private)  class TreeMap.DescendingEntrySet
           
(package private)  class TreeMap.DescendingKeyIterator
           
(package private)  class TreeMap.DescendingKeySet
           
(package private)  class TreeMap.DescendingValueIterator
           
static class TreeMap.Entry
           
(package private)  class TreeMap.EntryIterator
           
(package private)  class TreeMap.EntrySet
           
(package private) static class TreeMap.IOIterator
           
(package private) static class TreeMap.IteratorIOException
           
(package private) static class TreeMap.IteratorNoClassException
           
(package private)  class TreeMap.KeyIterator
           
(package private)  class TreeMap.KeySet
           
(package private)  class TreeMap.SubDescendingEntryIterator
           
(package private)  class TreeMap.SubEntryIterator
           
(package private)  class TreeMap.ValueIterator
           
(package private)  class TreeMap.ValueSet
           
 
Nested classes/interfaces inherited from class org.compass.core.util.backport.java.util.AbstractMap
AbstractMap.SimpleEntry, AbstractMap.SimpleImmutableEntry
 
Field Summary
 
Fields inherited from class org.compass.core.util.backport.java.util.AbstractMap
keySet
 
Constructor Summary
TreeMap()
           
TreeMap(Comparator comparator)
           
TreeMap(Map map)
           
TreeMap(SortedMap map)
           
 
Method Summary
(package private)  void buildFromSorted(Iterator itr, int size)
           
 Map.Entry ceilingEntry(Object key)
          Returns a key-value mapping associated with the least key greater than or equal to the given key, or null if there is no such key.
 Object ceilingKey(Object key)
          Returns the least key greater than or equal to the given key, or null if there is no such key.
 void clear()
           
 Object clone()
           
(package private) static boolean colorOf(TreeMap.Entry p)
          Return color of node p, or BLACK if p is null (In the CLR version, they use a special dummy `nil' node for such purposes, but that doesn't work well here, since it could lead to creating one such special node per real node.)
 Comparator comparator()
           
 boolean containsKey(Object key)
           
 boolean containsValue(Object value)
           
 Set descendingEntrySet()
          Returns a Set view of the mappings contained in this map.
 Set descendingKeySet()
          Returns a Set view of the keys contained in this map.
 Set entrySet()
           
 Map.Entry firstEntry()
          Returns a key-value mapping associated with the least key in this map, or null if the map is empty.
 Object firstKey()
           
 Map.Entry floorEntry(Object key)
          Returns a key-value mapping associated with the greatest key less than or equal to the given key, or null if there is no such key.
 Object floorKey(Object key)
          Returns the greatest key less than or equal to the given key, or null if there is no such key.
 Object get(Object key)
          
 SortedMap headMap(Object toKey)
           
 Map.Entry higherEntry(Object key)
          Returns a key-value mapping associated with the least key strictly greater than the given key, or null if there is no such key.
 Object higherKey(Object key)
          Returns the least key strictly greater than the given key, or null if there is no such key.
 boolean isEmpty()
           
 Map.Entry lastEntry()
          Returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.
 Object lastKey()
           
 Map.Entry lowerEntry(Object key)
          Returns a key-value mapping associated with the greatest key strictly less than the given key, or null if there is no such key.
 Object lowerKey(Object key)
          Returns the greatest key strictly less than the given key, or null if there is no such key.
 NavigableMap navigableHeadMap(Object toKey)
          Returns a view of the portion of this map whose keys are strictly less than toKey.
 NavigableMap navigableSubMap(Object fromKey, Object toKey)
          Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.
 NavigableMap navigableTailMap(Object fromKey)
          Returns a view of the portion of this map whose keys are greater than or equal to fromKey.
(package private) static TreeMap.Entry parentOf(TreeMap.Entry p)
          return parent of node p, or null if p is null
 Map.Entry pollFirstEntry()
          Removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty.
 Map.Entry pollLastEntry()
          Removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.
 Object put(Object key, Object value)
           
 void putAll(Map map)
           
 Object remove(Object key)
           
 int size()
           
 SortedMap subMap(Object fromKey, Object toKey)
           
 SortedMap tailMap(Object fromKey)
           
 
Methods inherited from class org.compass.core.util.backport.java.util.AbstractMap
keySet
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode, keySet, values
 

Constructor Detail

TreeMap

public TreeMap()

TreeMap

public TreeMap(Comparator comparator)

TreeMap

public TreeMap(SortedMap map)

TreeMap

public TreeMap(Map map)
Method Detail

size

public int size()
Specified by:
size in interface Map
Overrides:
size in class AbstractMap

clear

public void clear()
Specified by:
clear in interface Map
Overrides:
clear in class AbstractMap

clone

public Object clone()
Overrides:
clone in class AbstractMap

put

public Object put(Object key,
                  Object value)
Specified by:
put in interface Map
Overrides:
put in class AbstractMap

get

public Object get(Object key)

Specified by:
get in interface Map
Overrides:
get in class AbstractMap

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map
Overrides:
containsKey in class AbstractMap

entrySet

public Set entrySet()
Specified by:
entrySet in interface Map
Specified by:
entrySet in class AbstractMap

buildFromSorted

void buildFromSorted(Iterator itr,
                     int size)

colorOf

static boolean colorOf(TreeMap.Entry p)
Return color of node p, or BLACK if p is null (In the CLR version, they use a special dummy `nil' node for such purposes, but that doesn't work well here, since it could lead to creating one such special node per real node.)


parentOf

static TreeMap.Entry parentOf(TreeMap.Entry p)
return parent of node p, or null if p is null


lowerEntry

public Map.Entry lowerEntry(Object key)
Description copied from interface: NavigableMap
Returns a key-value mapping associated with the greatest key strictly less than the given key, or null if there is no such key.

Specified by:
lowerEntry in interface NavigableMap
Parameters:
key - the key
Returns:
an entry with the greatest key less than key, or null if there is no such key
Since:
1.6

lowerKey

public Object lowerKey(Object key)
Description copied from interface: NavigableMap
Returns the greatest key strictly less than the given key, or null if there is no such key.

Specified by:
lowerKey in interface NavigableMap
Parameters:
key - the key
Returns:
the greatest key less than key, or null if there is no such key
Since:
1.6

floorEntry

public Map.Entry floorEntry(Object key)
Description copied from interface: NavigableMap
Returns a key-value mapping associated with the greatest key less than or equal to the given key, or null if there is no such key.

Specified by:
floorEntry in interface NavigableMap
Parameters:
key - the key
Returns:
an entry with the greatest key less than or equal to key, or null if there is no such key
Since:
1.6

floorKey

public Object floorKey(Object key)
Description copied from interface: NavigableMap
Returns the greatest key less than or equal to the given key, or null if there is no such key.

Specified by:
floorKey in interface NavigableMap
Parameters:
key - the key
Returns:
the greatest key less than or equal to key, or null if there is no such key
Since:
1.6

ceilingEntry

public Map.Entry ceilingEntry(Object key)
Description copied from interface: NavigableMap
Returns a key-value mapping associated with the least key greater than or equal to the given key, or null if there is no such key.

Specified by:
ceilingEntry in interface NavigableMap
Parameters:
key - the key
Returns:
an entry with the least key greater than or equal to key, or null if there is no such key
Since:
1.6

ceilingKey

public Object ceilingKey(Object key)
Description copied from interface: NavigableMap
Returns the least key greater than or equal to the given key, or null if there is no such key.

Specified by:
ceilingKey in interface NavigableMap
Parameters:
key - the key
Returns:
the least key greater than or equal to key, or null if there is no such key
Since:
1.6

higherEntry

public Map.Entry higherEntry(Object key)
Description copied from interface: NavigableMap
Returns a key-value mapping associated with the least key strictly greater than the given key, or null if there is no such key.

Specified by:
higherEntry in interface NavigableMap
Parameters:
key - the key
Returns:
an entry with the least key greater than key, or null if there is no such key
Since:
1.6

higherKey

public Object higherKey(Object key)
Description copied from interface: NavigableMap
Returns the least key strictly greater than the given key, or null if there is no such key.

Specified by:
higherKey in interface NavigableMap
Parameters:
key - the key
Returns:
the least key greater than key, or null if there is no such key
Since:
1.6

firstEntry

public Map.Entry firstEntry()
Description copied from interface: NavigableMap
Returns a key-value mapping associated with the least key in this map, or null if the map is empty.

Specified by:
firstEntry in interface NavigableMap
Returns:
an entry with the least key, or null if this map is empty
Since:
1.6

lastEntry

public Map.Entry lastEntry()
Description copied from interface: NavigableMap
Returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.

Specified by:
lastEntry in interface NavigableMap
Returns:
an entry with the greatest key, or null if this map is empty
Since:
1.6

pollFirstEntry

public Map.Entry pollFirstEntry()
Description copied from interface: NavigableMap
Removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty.

Specified by:
pollFirstEntry in interface NavigableMap
Returns:
the removed first entry of this map, or null if this map is empty
Since:
1.6

pollLastEntry

public Map.Entry pollLastEntry()
Description copied from interface: NavigableMap
Removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.

Specified by:
pollLastEntry in interface NavigableMap
Returns:
the removed last entry of this map, or null if this map is empty
Since:
1.6

descendingKeySet

public Set descendingKeySet()
Description copied from interface: NavigableMap
Returns a Set view of the keys contained in this map. The set's iterator returns the keys in descending order. 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), 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.

Specified by:
descendingKeySet in interface NavigableMap
Returns:
a set view of the keys contained in this map, sorted in descending order

descendingEntrySet

public Set descendingEntrySet()
Description copied from interface: NavigableMap
Returns a Set view of the mappings contained in this map. The set's iterator returns the entries in descending key order. 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.

Specified by:
descendingEntrySet in interface NavigableMap
Returns:
a set view of the mappings contained in this map, sorted in descending key order

navigableSubMap

public NavigableMap navigableSubMap(Object fromKey,
                                    Object toKey)
Description copied from interface: NavigableMap
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 map is empty.) The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.

The returned map will throw an IllegalArgumentException on an attempt to insert a key outside its range.

Specified by:
navigableSubMap in interface NavigableMap
Parameters:
fromKey - low endpoint (inclusive) of the keys in the returned map
toKey - high endpoint (exclusive) of the keys in the returned map
Returns:
a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive

navigableHeadMap

public NavigableMap navigableHeadMap(Object toKey)
Description copied from interface: NavigableMap
Returns a view of the portion of this map whose keys are strictly less than toKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.

The returned map will throw an IllegalArgumentException on an attempt to insert a key outside its range.

Specified by:
navigableHeadMap in interface NavigableMap
Parameters:
toKey - high endpoint (exclusive) of the keys in the returned map
Returns:
a view of the portion of this map whose keys are strictly less than toKey

navigableTailMap

public NavigableMap navigableTailMap(Object fromKey)
Description copied from interface: NavigableMap
Returns a view of the portion of this map whose keys are greater than or equal to fromKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.

The returned map will throw an IllegalArgumentException on an attempt to insert a key outside its range.

Specified by:
navigableTailMap in interface NavigableMap
Parameters:
fromKey - low endpoint (inclusive) of the keys in the returned map
Returns:
a view of the portion of this map whose keys are greater than or equal to fromKey

comparator

public Comparator comparator()
Specified by:
comparator in interface SortedMap

subMap

public SortedMap subMap(Object fromKey,
                        Object toKey)
Specified by:
subMap in interface SortedMap

headMap

public SortedMap headMap(Object toKey)
Specified by:
headMap in interface SortedMap

tailMap

public SortedMap tailMap(Object fromKey)
Specified by:
tailMap in interface SortedMap

firstKey

public Object firstKey()
Specified by:
firstKey in interface SortedMap

lastKey

public Object lastKey()
Specified by:
lastKey in interface SortedMap

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map
Overrides:
isEmpty in class AbstractMap

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map
Overrides:
containsValue in class AbstractMap

remove

public Object remove(Object key)
Specified by:
remove in interface Map
Overrides:
remove in class AbstractMap

putAll

public void putAll(Map map)
Specified by:
putAll in interface Map
Overrides:
putAll in class AbstractMap


Copyright (c) 2004-2006 The Compass Project.