org.apache.poi.util
public final class: BinaryTree [javadoc |
source]
java.lang.Object
java.util.AbstractMap
org.apache.poi.util.BinaryTree
All Implemented Interfaces:
Map
Red-Black tree-based implementation of Map. This class guarantees
that the map will be in both ascending key order and ascending
value order, sorted according to the natural order for the key's
and value's classes.
This Map is intended for applications that need to be able to look
up a key-value pairing by either key or value, and need to do so
with equal efficiency.
While that goal could be accomplished by taking a pair of TreeMaps
and redirecting requests to the appropriate TreeMap (e.g.,
containsKey would be directed to the TreeMap that maps values to
keys, containsValue would be directed to the TreeMap that maps keys
to values), there are problems with that implementation,
particularly when trying to keep the two TreeMaps synchronized with
each other. And if the data contained in the TreeMaps is large, the
cost of redundant storage becomes significant.
This solution keeps the data properly synchronized and minimizes
the data storage. The red-black algorithm is based on TreeMap's,
but has been modified to simultaneously map a tree node by key and
by value. This doubles the cost of put operations (but so does
using two TreeMaps), and nearly doubles the cost of remove
operations (there is a savings in that the lookup of the node to be
removed only has to be performed once). And since only one node
contains the key and value, storage is significantly less than that
required by two TreeMaps.
There are some limitations placed on data kept in this Map. The
biggest one is this:
When performing a put operation, neither the key nor the value may
already exist in the Map. In the java.util Map implementations
(HashMap, TreeMap), you can perform a put with an already mapped
key, and neither cares about duplicate values at all ... but this
implementation's put method with throw an IllegalArgumentException
if either the key or the value is already in the Map.
Obviously, that same restriction (and consequence of failing to
heed that restriction) applies to the putAll method.
The Map.Entry instances returned by the appropriate methods will
not allow setValue() and will throw an
UnsupportedOperationException on attempts to call that method.
New methods are added to take advantage of the fact that values are
kept sorted independently of their keys:
Object getKeyForValue(Object value) is the opposite of get; it
takes a value and returns its key, if any.
Object removeValue(Object value) finds and removes the specified
value and returns the now un-used key.
Set entrySetByValue() returns the Map.Entry's in a Set whose
iterator will iterate over the Map.Entry's in ascending order by
their corresponding values.
Set keySetByValue() returns the keys in a Set whose iterator will
iterate over the keys in ascending order by their corresponding
values.
Collection valuesByValue() returns the values in a Collection whose
iterator will iterate over the values in ascending order.
- author:
Marc - Johnson (mjohnson at apache dot org)
| Constructor: |
public BinaryTree() {
}
Construct a new BinaryTree |
public BinaryTree(Map map) throws IllegalArgumentException, NullPointerException, ClassCastException {
putAll(map);
}
Constructs a new BinaryTree from an existing Map, with keys and
values sorted Parameters:
map - the map whose mappings are to be placed in this map.
Throws:
ClassCastException - if the keys in the map are not
Comparable, or are not mutually
comparable; also if the values in
the map are not Comparable, or
are not mutually Comparable
NullPointerException - if any key or value in the map
is null
IllegalArgumentException - if there are duplicate keys
or duplicate values in the
map
- exception:
ClassCastException - if the keys in the map are not
Comparable, or are not mutually
comparable; also if the values in
the map are not Comparable, or
are not mutually Comparable
- exception:
NullPointerException - if any key or value in the map
is null
- exception:
IllegalArgumentException - if there are duplicate keys
or duplicate values in the
map
|
| Method from org.apache.poi.util.BinaryTree Summary: |
|---|
|
clear, containsKey, containsValue, entrySet, entrySetByValue, get, getKeyForValue, keySet, keySetByValue, put, remove, removeValue, size, values, valuesByValue |
| Methods from java.util.AbstractMap: |
|---|
|
clear, containsKey, containsValue, entrySet, equals, get, hashCode, isEmpty, keySet, put, putAll, remove, size, toString, values |
| Method from org.apache.poi.util.BinaryTree Detail: |
public void clear() {
modify();
_size = 0;
_root[ _KEY ] = null;
_root[ _VALUE ] = null;
}
Removes all mappings from this map |
public boolean containsKey(Object key) throws NullPointerException, ClassCastException {
checkKey(key);
return lookup(( Comparable ) key, _KEY) != null;
}
Returns true if this map contains a mapping for the specified
key. |
public boolean containsValue(Object value) {
checkValue(value);
return lookup(( Comparable ) value, _VALUE) != null;
}
Returns true if this map maps one or more keys to the
specified value. |
public Set entrySet() {
if (_entry_set[ _KEY ] == null)
{
_entry_set[ _KEY ] = new AbstractSet()
{
public Iterator iterator()
{
return new BinaryTreeIterator(_KEY)
{
protected Object doGetNext()
{
return _last_returned_node;
}
};
}
public boolean contains(Object o)
{
if (!(o instanceof Map.Entry))
{
return false;
}
Map.Entry entry = ( Map.Entry ) o;
Object value = entry.getValue();
Node node = lookup(( Comparable ) entry.getKey(),
_KEY);
return (node != null)
&& node.getData(_VALUE).equals(value);
}
public boolean remove(Object o)
{
if (!(o instanceof Map.Entry))
{
return false;
}
Map.Entry entry = ( Map.Entry ) o;
Object value = entry.getValue();
Node node = lookup(( Comparable ) entry.getKey(),
_KEY);
if ((node != null) && node.getData(_VALUE).equals(value))
{
doRedBlackDelete(node);
return true;
}
return false;
}
public int size()
{
return BinaryTree.this.size();
}
public void clear()
{
BinaryTree.this.clear();
}
};
}
return _entry_set[ _KEY ];
}
Returns a set view of the mappings contained in this map. Each
element in the returned set is a Map.Entry. 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, 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. |
public Set entrySetByValue() {
if (_entry_set[ _VALUE ] == null)
{
_entry_set[ _VALUE ] = new AbstractSet()
{
public Iterator iterator()
{
return new BinaryTreeIterator(_VALUE)
{
protected Object doGetNext()
{
return _last_returned_node;
}
};
}
public boolean contains(Object o)
{
if (!(o instanceof Map.Entry))
{
return false;
}
Map.Entry entry = ( Map.Entry ) o;
Object key = entry.getKey();
Node node = lookup(( Comparable ) entry.getValue(),
_VALUE);
return (node != null) && node.getData(_KEY).equals(key);
}
public boolean remove(Object o)
{
if (!(o instanceof Map.Entry))
{
return false;
}
Map.Entry entry = ( Map.Entry ) o;
Object key = entry.getKey();
Node node = lookup(( Comparable ) entry.getValue(),
_VALUE);
if ((node != null) && node.getData(_KEY).equals(key))
{
doRedBlackDelete(node);
return true;
}
return false;
}
public int size()
{
return BinaryTree.this.size();
}
public void clear()
{
BinaryTree.this.clear();
}
};
}
return _entry_set[ _VALUE ];
}
Returns a set view of the mappings contained in this map. Each
element in the returned set is a Map.Entry. 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, 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.
The difference between this method and entrySet is that
entrySet's iterator() method returns an iterator that iterates
over the mappings in ascending order by key. This method's
iterator method iterates over the mappings in ascending order
by value. |
public Object get(Object key) throws NullPointerException, ClassCastException {
return doGet(( Comparable ) key, _KEY);
}
Returns the value to which this map maps the specified
key. Returns null if the map contains no mapping for this key. |
public Object getKeyForValue(Object value) throws NullPointerException, ClassCastException {
return doGet(( Comparable ) value, _VALUE);
}
Returns the key to which this map maps the specified value.
Returns null if the map contains no mapping for this value. |
public Set keySet() {
if (_key_set[ _KEY ] == null)
{
_key_set[ _KEY ] = new AbstractSet()
{
public Iterator iterator()
{
return new BinaryTreeIterator(_KEY)
{
protected Object doGetNext()
{
return _last_returned_node.getData(_KEY);
}
};
}
public int size()
{
return BinaryTree.this.size();
}
public boolean contains(Object o)
{
return containsKey(o);
}
public boolean remove(Object o)
{
int old_size = _size;
BinaryTree.this.remove(o);
return _size != old_size;
}
public void clear()
{
BinaryTree.this.clear();
}
};
}
return _key_set[ _KEY ];
}
Returns a set view of the keys contained in this map. 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, 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. |
public Set keySetByValue() {
if (_key_set[ _VALUE ] == null)
{
_key_set[ _VALUE ] = new AbstractSet()
{
public Iterator iterator()
{
return new BinaryTreeIterator(_VALUE)
{
protected Object doGetNext()
{
return _last_returned_node.getData(_KEY);
}
};
}
public int size()
{
return BinaryTree.this.size();
}
public boolean contains(Object o)
{
return containsKey(o);
}
public boolean remove(Object o)
{
int old_size = _size;
BinaryTree.this.remove(o);
return _size != old_size;
}
public void clear()
{
BinaryTree.this.clear();
}
};
}
return _key_set[ _VALUE ];
}
Returns a set view of the keys contained in this map. 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, 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.
The difference between this method and keySet is that keySet's
iterator() method returns an iterator that iterates over the
keys in ascending order by key. This method's iterator method
iterates over the keys in ascending order by value. |
public Object put(Object key,
Object value) throws IllegalArgumentException, NullPointerException, ClassCastException {
checkKeyAndValue(key, value);
Node node = _root[ _KEY ];
if (node == null)
{
Node root = new Node(( Comparable ) key, ( Comparable ) value);
_root[ _KEY ] = root;
_root[ _VALUE ] = root;
grow();
}
else
{
while (true)
{
int cmp = compare(( Comparable ) key, node.getData(_KEY));
if (cmp == 0)
{
throw new IllegalArgumentException(
"Cannot store a duplicate key (\"" + key
+ "\") in this Map");
}
else if (cmp < 0)
{
if (node.getLeft(_KEY) != null)
{
node = node.getLeft(_KEY);
}
else
{
Node newNode = new Node(( Comparable ) key,
( Comparable ) value);
insertValue(newNode);
node.setLeft(newNode, _KEY);
newNode.setParent(node, _KEY);
doRedBlackInsert(newNode, _KEY);
grow();
break;
}
}
else
{ // cmp > 0
if (node.getRight(_KEY) != null)
{
node = node.getRight(_KEY);
}
else
{
Node newNode = new Node(( Comparable ) key,
( Comparable ) value);
insertValue(newNode);
node.setRight(newNode, _KEY);
newNode.setParent(node, _KEY);
doRedBlackInsert(newNode, _KEY);
grow();
break;
}
}
}
}
return null;
}
Associates the specified value with the specified key in this
map. |
public Object remove(Object key) {
return doRemove(( Comparable ) key, _KEY);
}
Removes the mapping for this key from this map if present |
public Object removeValue(Object value) {
return doRemove(( Comparable ) value, _VALUE);
}
Removes the mapping for this value from this map if present |
public int size() {
return _size;
}
Returns the number of key-value mappings in this map. If the
map contains more than Integer.MAX_VALUE elements, returns
Integer.MAX_VALUE. |
public Collection values() {
if (_value_collection[ _KEY ] == null)
{
_value_collection[ _KEY ] = new AbstractCollection()
{
public Iterator iterator()
{
return new BinaryTreeIterator(_KEY)
{
protected Object doGetNext()
{
return _last_returned_node.getData(_VALUE);
}
};
}
public int size()
{
return BinaryTree.this.size();
}
public boolean contains(Object o)
{
return containsValue(o);
}
public boolean remove(Object o)
{
int old_size = _size;
removeValue(o);
return _size != old_size;
}
public boolean removeAll(Collection c)
{
boolean modified = false;
Iterator iter = c.iterator();
while (iter.hasNext())
{
if (removeValue(iter.next()) != null)
{
modified = true;
}
}
return modified;
}
public void clear()
{
BinaryTree.this.clear();
}
};
}
return _value_collection[ _KEY ];
}
Returns a collection view of the values contained in this
map. The collection is backed by the map, so changes to the map
are reflected in the collection, and vice-versa. If the map is
modified while an iteration over the collection is in progress,
the results of the iteration are undefined. The collection
supports element removal, which removes the corresponding
mapping from the map, via the Iterator.remove,
Collection.remove, removeAll, retainAll and clear operations.
It does not support the add or addAll operations. |
public Collection valuesByValue() {
if (_value_collection[ _VALUE ] == null)
{
_value_collection[ _VALUE ] = new AbstractCollection()
{
public Iterator iterator()
{
return new BinaryTreeIterator(_VALUE)
{
protected Object doGetNext()
{
return _last_returned_node.getData(_VALUE);
}
};
}
public int size()
{
return BinaryTree.this.size();
}
public boolean contains(Object o)
{
return containsValue(o);
}
public boolean remove(Object o)
{
int old_size = _size;
removeValue(o);
return _size != old_size;
}
public boolean removeAll(Collection c)
{
boolean modified = false;
Iterator iter = c.iterator();
while (iter.hasNext())
{
if (removeValue(iter.next()) != null)
{
modified = true;
}
}
return modified;
}
public void clear()
{
BinaryTree.this.clear();
}
};
}
return _value_collection[ _VALUE ];
}
Returns a collection view of the values contained in this
map. The collection is backed by the map, so changes to the map
are reflected in the collection, and vice-versa. If the map is
modified while an iteration over the collection is in progress,
the results of the iteration are undefined. The collection
supports element removal, which removes the corresponding
mapping from the map, via the Iterator.remove,
Collection.remove, removeAll, retainAll and clear operations.
It does not support the add or addAll operations.
The difference between this method and values is that values's
iterator() method returns an iterator that iterates over the
values in ascending order by key. This method's iterator method
iterates over the values in ascending order by key. |