ePOST API

rice.p2p.util
Class RedBlackMap

java.lang.Object
  extended by java.util.AbstractMap
      extended by rice.p2p.util.RedBlackMap
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map, java.util.SortedMap

public class RedBlackMap
extends java.util.AbstractMap
implements java.util.SortedMap, java.lang.Cloneable, java.io.Serializable

This class is a modification of the RedBlackMap java class, with the added benefit that iterators do not throw a ConcurrentModificationException when the backing tree changes. The iterator is guaranteed to walk through some version of the tree, and may show nodes which were deleted or which were subsequently added, but it will not die.

See Also:
Serialized Form

Constructor Summary
RedBlackMap()
          Constructs a new, empty map, sorted according to the keys' natural order.
RedBlackMap(java.util.Comparator c)
          Constructs a new, empty map, sorted according to the given comparator.
RedBlackMap(java.util.Map m)
          Constructs a new map containing the same mappings as the given map, sorted according to the keys' natural order.
RedBlackMap(java.util.SortedMap m)
          Constructs a new map containing the same mappings as the given SortedMap, sorted according to the same ordering.
 
Method Summary
 void clear()
          Removes all mappings from this RedBlackMap.
 java.lang.Object clone()
          Returns a shallow copy of this RedBlackMap instance.
 java.util.Comparator comparator()
          Returns the comparator used to order this map, or null if this map uses its keys' natural order.
 boolean containsKey(java.lang.Object key)
          Returns true if this map contains a mapping for the specified key.
 boolean containsValue(java.lang.Object value)
          Returns true if this map maps one or more keys to the specified value.
 java.util.Set entrySet()
          Returns a set view of the mappings contained in this map.
 java.lang.Object firstKey()
          Returns the first (lowest) key currently in this sorted map.
 java.lang.Object get(java.lang.Object key)
          Returns the value to which this map maps the specified key.
 java.lang.Object getKey(java.lang.Object key)
           
 java.util.SortedMap headMap(java.lang.Object toKey)
          Returns a view of the portion of this map whose keys are strictly less than toKey.
 java.util.Set keySet()
          Returns a Set view of the keys contained in this map.
 java.lang.Object lastKey()
          Returns the last (highest) key currently in this sorted map.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Associates the specified value with the specified key in this map.
 void putAll(java.util.Map map)
          Copies all of the mappings from the specified map to this map.
 java.lang.Object remove(java.lang.Object key)
          Removes the mapping for this key from this RedBlackMap if present.
 int size()
          Returns the number of key-value mappings in this map.
 java.util.SortedMap subMap(java.lang.Object fromKey, java.lang.Object toKey)
          Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.
 java.util.SortedMap tailMap(java.lang.Object fromKey)
          Returns a view of the portion of this map whose keys are greater than or equal to fromKey.
 java.util.Collection values()
          Returns a collection view of the values contained in this map.
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, isEmpty, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode, isEmpty
 

Constructor Detail

RedBlackMap

public RedBlackMap()
Constructs a new, empty map, sorted according to the keys' natural order. All keys inserted into the map must implement the Comparable interface. Furthermore, all such keys must be mutually comparable: k1.compareTo(k2) must not throw a ClassCastException for any elements k1 and k2 in the map. If the user attempts to put a key into the map that violates this constraint (for example, the user attempts to put a string key into a map whose keys are integers), the put(Object key, Object value) call will throw a ClassCastException.

See Also:
Comparable

RedBlackMap

public RedBlackMap(java.util.Comparator c)
Constructs a new, empty map, sorted according to the given comparator. All keys inserted into the map must be mutually comparable by the given comparator: comparator.compare(k1, k2) must not throw a ClassCastException for any keys k1 and k2 in the map. If the user attempts to put a key into the map that violates this constraint, the put(Object key, Object value) call will throw a ClassCastException.

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

RedBlackMap

public RedBlackMap(java.util.Map m)
Constructs a new map containing the same mappings as the given map, sorted according to the keys' natural order. All keys inserted into the new map must implement the Comparable interface. Furthermore, all such keys must be mutually comparable: k1.compareTo(k2) must not throw a ClassCastException for any elements k1 and k2 in the map. This method runs in n*log(n) time.

Parameters:
m - the map whose mappings are to be placed in this map.
Throws:
java.lang.ClassCastException - the keys in t are not Comparable, or are not mutually comparable.
java.lang.NullPointerException - if the specified map is null.

RedBlackMap

public RedBlackMap(java.util.SortedMap m)
Constructs a new map containing the same mappings as the given SortedMap, sorted according to the same ordering. This method runs in linear time.

Parameters:
m - the sorted map whose mappings are to be placed in this map, and whose comparator is to be used to sort this map.
Throws:
java.lang.NullPointerException - if the specified sorted map is null.
Method Detail

size

public int size()
Returns the number of key-value mappings in this map.

Specified by:
size in interface java.util.Map
Overrides:
size in class java.util.AbstractMap
Returns:
the number of key-value mappings in this map.

containsKey

public boolean containsKey(java.lang.Object key)
Returns true if this map contains a mapping for the specified key.

Specified by:
containsKey in interface java.util.Map
Overrides:
containsKey in class java.util.AbstractMap
Parameters:
key - key whose presence in this map is to be tested.
Returns:
true if this map contains a mapping for the specified key.
Throws:
java.lang.ClassCastException - if the key cannot be compared with the keys currently in the map.
java.lang.NullPointerException - key is null and this map uses natural ordering, or its comparator does not tolerate null keys.

containsValue

public boolean containsValue(java.lang.Object value)
Returns true if this map maps one or more keys to the specified value. More formally, returns true if and only if this map contains at least one mapping to a value v such that (value==null ? v==null : value.equals(v)). This operation will probably require time linear in the Map size for most implementations of Map.

Specified by:
containsValue in interface java.util.Map
Overrides:
containsValue in class java.util.AbstractMap
Parameters:
value - value whose presence in this Map is to be tested.
Returns:
true if a mapping to value exists; false otherwise.
Since:
1.2

get

public java.lang.Object get(java.lang.Object key)
Returns the value to which this map maps the specified key. Returns null if the map contains no mapping for this key. A return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.

Specified by:
get in interface java.util.Map
Overrides:
get in class java.util.AbstractMap
Parameters:
key - key whose associated value is to be returned.
Returns:
the value to which this map maps the specified key, or null if the map contains no mapping for the key.
Throws:
java.lang.ClassCastException - key cannot be compared with the keys currently in the map.
java.lang.NullPointerException - key is null and this map uses natural ordering, or its comparator does not tolerate null keys.
See Also:
containsKey(Object)

comparator

public java.util.Comparator comparator()
Returns the comparator used to order this map, or null if this map uses its keys' natural order.

Specified by:
comparator in interface java.util.SortedMap
Returns:
the comparator associated with this sorted map, or null if it uses its keys' natural sort method.

firstKey

public java.lang.Object firstKey()
Returns the first (lowest) key currently in this sorted map.

Specified by:
firstKey in interface java.util.SortedMap
Returns:
the first (lowest) key currently in this sorted map.
Throws:
java.util.NoSuchElementException - Map is empty.

lastKey

public java.lang.Object lastKey()
Returns the last (highest) key currently in this sorted map.

Specified by:
lastKey in interface java.util.SortedMap
Returns:
the last (highest) key currently in this sorted map.
Throws:
java.util.NoSuchElementException - Map is empty.

putAll

public void putAll(java.util.Map map)
Copies all of the mappings from the specified map to this map. These mappings replace any mappings that this map had for any of the keys currently in the specified map.

Specified by:
putAll in interface java.util.Map
Overrides:
putAll in class java.util.AbstractMap
Parameters:
map - mappings to be stored in this map.
Throws:
java.lang.ClassCastException - class of a key or value in the specified map prevents it from being stored in this map.
java.lang.NullPointerException - if the given map is null or this map does not permit null keys and a key in the specified map is null.

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced.

Specified by:
put in interface java.util.Map
Overrides:
put in class java.util.AbstractMap
Parameters:
key - key with which the specified value is to be associated.
value - value to be associated with the specified key.
Returns:
previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the map previously associated null with the specified key.
Throws:
java.lang.ClassCastException - key cannot be compared with the keys currently in the map.
java.lang.NullPointerException - key is null and this map uses natural order, or its comparator does not tolerate null keys.

remove

public java.lang.Object remove(java.lang.Object key)
Removes the mapping for this key from this RedBlackMap if present.

Specified by:
remove in interface java.util.Map
Overrides:
remove in class java.util.AbstractMap
Parameters:
key - key for which mapping should be removed
Returns:
previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the map previously associated null with the specified key.
Throws:
java.lang.ClassCastException - key cannot be compared with the keys currently in the map.
java.lang.NullPointerException - key is null and this map uses natural order, or its comparator does not tolerate null keys.

getKey

public java.lang.Object getKey(java.lang.Object key)

clear

public void clear()
Removes all mappings from this RedBlackMap.

Specified by:
clear in interface java.util.Map
Overrides:
clear in class java.util.AbstractMap

clone

public java.lang.Object clone()
Returns a shallow copy of this RedBlackMap instance. (The keys and values themselves are not cloned.)

Overrides:
clone in class java.util.AbstractMap
Returns:
a shallow copy of this Map.

keySet

public java.util.Set keySet()
Returns a Set view of the keys contained in this map. The set's iterator will return the keys in ascending order. The map is backed by this RedBlackMap instance, so changes to this map are reflected in the Set, and vice-versa. 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:
keySet in interface java.util.Map
Overrides:
keySet in class java.util.AbstractMap
Returns:
a set view of the keys contained in this RedBlackMap.

values

public java.util.Collection values()
Returns a collection view of the values contained in this map. The collection's iterator will return the values in the order that their corresponding keys appear in the tree. The collection is backed by this RedBlackMap instance, so changes to this map are reflected in the collection, and vice-versa. The collection supports element removal, which removes the corresponding mapping from the map through the Iterator.remove, Collection.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Specified by:
values in interface java.util.Map
Overrides:
values in class java.util.AbstractMap
Returns:
a collection view of the values contained in this map.

entrySet

public java.util.Set entrySet()
Returns a set view of the mappings contained in this map. The set's iterator returns the mappings in ascending key order. Each element in the returned set is a Map.Entry. The set is backed by this map, so changes to this map are reflected in the set, and vice-versa. The set supports element removal, which removes the corresponding mapping from the RedBlackMap, through the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

Specified by:
entrySet in interface java.util.Map
Specified by:
entrySet in class java.util.AbstractMap
Returns:
a set view of the mappings contained in this map.
See Also:
Map.Entry

subMap

public java.util.SortedMap subMap(java.lang.Object fromKey,
                                  java.lang.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.) The returned sorted map is backed by this map, so changes in the returned sorted map are reflected in this map, and vice-versa. The returned sorted map supports all optional map operations.

The sorted map returned by this method will throw an IllegalArgumentException if the user attempts to insert a key less than fromKey or greater than or equal to toKey.

Note: this method always returns a half-open range (which includes its low endpoint but not its high endpoint). If you need a closed range (which includes both endpoints), and the key type allows for calculation of the successor a given key, merely request the subrange from lowEndpoint to successor(highEndpoint). For example, suppose that m is a sorted map whose keys are strings. The following idiom obtains a view containing all of the key-value mappings in m whose keys are between low and high, inclusive:

    SortedMap sub = m.submap(low, high+"\0");
A similar technique can be used to generate an open range (which contains neither endpoint). The following idiom obtains a view containing all of the key-value mappings in m whose keys are between low and high, exclusive:
    SortedMap sub = m.subMap(low+"\0", high);

Specified by:
subMap in interface java.util.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:
java.lang.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).
java.lang.IllegalArgumentException - if fromKey is greater than toKey.
java.lang.NullPointerException - if fromKey or toKey is null and this map uses natural order, or its comparator does not tolerate null keys.

headMap

public java.util.SortedMap headMap(java.lang.Object toKey)
Returns a view of the portion of this map whose keys are strictly less than toKey. The returned sorted map is backed by this map, so changes in the returned sorted map are reflected in this map, and vice-versa. The returned sorted map supports all optional map operations.

The sorted map returned by this method will throw an IllegalArgumentException if the user attempts to insert a key greater than or equal to toKey.

Note: this method always returns a view that does not contain its (high) endpoint. If you need a view that does contain this endpoint, and the key type allows for calculation of the successor a given key, merely request a headMap bounded by successor(highEndpoint). For example, suppose that suppose that m is a sorted map whose keys are strings. The following idiom obtains a view containing all of the key-value mappings in m whose keys are less than or equal to high:

     SortedMap head = m.headMap(high+"\0");
 

Specified by:
headMap in interface java.util.SortedMap
Parameters:
toKey - high endpoint (exclusive) of the headMap.
Returns:
a view of the portion of this map whose keys are strictly less than toKey.
Throws:
java.lang.ClassCastException - if toKey is not compatible with this map's comparator (or, if the map has no comparator, if toKey does not implement Comparable).
java.lang.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.
java.lang.NullPointerException - if toKey is null and this map uses natural order, or its comparator does not tolerate null keys.

tailMap

public java.util.SortedMap tailMap(java.lang.Object fromKey)
Returns a view of the portion of this map whose keys are greater than or equal to fromKey. The returned sorted map is backed by this map, so changes in the returned sorted map are reflected in this map, and vice-versa. The returned sorted map supports all optional map operations.

The sorted map returned by this method will throw an IllegalArgumentException if the user attempts to insert a key less than fromKey.

Note: this method always returns a view that contains its (low) endpoint. If you need a view that does not contain this endpoint, and the element type allows for calculation of the successor a given value, merely request a tailMap bounded by successor(lowEndpoint). For For example, suppose that suppose that m is a sorted map whose keys are strings. The following idiom obtains a view containing all of the key-value mappings in m whose keys are strictly greater than low:

     SortedMap tail = m.tailMap(low+"\0");
 

Specified by:
tailMap in interface java.util.SortedMap
Parameters:
fromKey - low endpoint (inclusive) of the tailMap.
Returns:
a view of the portion of this map whose keys are greater than or equal to fromKey.
Throws:
java.lang.ClassCastException - if fromKey is not compatible with this map's comparator (or, if the map has no comparator, if fromKey does not implement Comparable).
java.lang.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.
java.lang.NullPointerException - if fromKey is null and this map uses natural order, or its comparator does not tolerate null keys.

ePOST API

Copyright © 2001-2005 - Rice Pastry.