All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class COM.objectspace.jgl.OrderedMap

java.lang.Object
   |
   +----java.util.Dictionary
           |
           +----COM.objectspace.jgl.Map
                   |
                   +----COM.objectspace.jgl.OrderedMap

public class OrderedMap
extends Map
An OrderedMap is an associative container that manages a set of ordered key/value pairs. The pairs are ordered by key, using a comparator. By default, a comparator is used which orders keys based on their hash value. By default, only one value may be associated with a particular key. An OrderedMap's underlying data structure allows you to very efficiently find all of the values associated with a particular key.

An OrderedMap is useful for implementing a collection of one-to-one and one-to-many mappings.

Strictly speaking, there is no reason why null is not a valid key. However, most comparators (including the default one) will fail and throw an exception if you attempt to add a null key because they cast the key to a class and then activate one of its methods. It is perfectly possible to hand-craft a comparator that will accept null as a valid key.

There are many different approaches that could be used to implementing an associative container. For example, most of the older libraries used a hashing scheme for positioning and retrieving items. This implementation uses a data structure called a red-black tree. A red-black tree is a binary search tree that uses an extra field in every node to store the node's color. Red-black trees constrain the way that nodes may be colored in such a way that the tree remains reasonably balanced. This property is important for ensuring a good overall performance - red-black trees guarantee that the worst case performance for the most common dynamic set operations is O( log N ). One conseqence of using a binary tree for storage of data is the items remain in a sorted order. This allows JGL users to iterate through an associative container and access its elements in a sequenced manner. Each node of the red-black tree holds a Pair( key, value ). The comparator is used to order the Pairs based only on their keys.

Insertion does not affect iterators or references.

Removal only invalidates the iterators and references to the removed elements.

See Also:
BinaryPredicate, OrderedMapExamples

Constructor Index

 o OrderedMap()
Construct myself to be an empty OrderedMap that orders its keys based on their hash value and does not allow duplicates.
 o OrderedMap(BinaryPredicate)
Construct myself to be an empty OrderedMap that orders its keys using a specified binary predicate and does not allow duplicates.
 o OrderedMap(BinaryPredicate, boolean)
Construct myself to be an empty OrderedMap that orders its keys using a specified binary predicate and conditionally allows duplicates.
 o OrderedMap(boolean)
Construct myself to be an empty OrderedMap that orders its keys based on their hash value and conditionally allows duplicates.
 o OrderedMap(OrderedMap)
Construct myself to be a shallow copy of an existing OrderedMap.

Method Index

 o add(Object)
Assume that the specified object is a Pair whose first field is a key and whose second field is a value.
 o add(Object, Object)
If the key doesn't exist or duplicates are allowed, associate the value with the key and return null, otherwise don't modify the map and return the current value associated with the key.
 o allowsDuplicates()
Return true if duplicates are allowed.
 o begin()
Return an iterator positioned at my first pair.
 o clear()
Remove all of my elements.
 o clone()
Return a shallow copy of myself.
 o copy(OrderedMap)
Become a shallow copy of an existing OrderedMap.
 o count(Object)
Return the number of key/value pairs that match a particular key.
 o countValues(Object)
Return the number of values that match a given object.
 o elements()
Return an Enumeration of my values
 o end()
Return an iterator positioned immediately after my last pair.
 o equalRange(Object)
Return a pair of iterators whose first element is equal to lowerBound() and whose second element is equal to upperBound().
 o equals(Object)
Return true if I'm equal to another object.
 o equals(OrderedMap)
Return true if I contain the same items in the same order as another OrderedMap.
 o find(Object)
Find a key/value pair based on its key and return its position.
 o finish()
Return an iterator positioned immediately afer my last pair.
 o get(Object)
Return the value associated with key, or null if the key does not exist.
 o getComparator()
Return my comparator.
 o hashCode()
Return my hash code for support of hashing containers
 o isEmpty()
Return true if I contain no entries.
 o keys()
Return an Enumeration of all my keys.
 o keys(Object)
Return an Enumeration of all my keys that are associated with a particular value.
 o lowerBound(Object)
Return an iterator positioned at the first location that a pair with a specified key could be inserted without violating the ordering criteria.
 o maxSize()
Return the maximum number of entries that I can contain.
 o put(Object, Object)
If the key doesn't exist, associate the value with the key and return null, otherwise replace the first value associated with the key and return the old value.
 o remove(Enumeration)
Remove the element at a particular position and return its value.
 o remove(Enumeration, Enumeration)
Remove the elements within a specified range, returning the first value of the key in that range.
 o remove(Object)
If I contain key/value pair(s) that matches a particular key, remove them all and return the number of pairs removed.
 o remove(Object, int)
If I contain key/value pair(s) that matches a particular key, remove at most a given number and return the number of pairs removed.
 o size()
Return the number of entries that I contain.
 o start()
Return an iterator positioned at my first pair.
 o swap(OrderedMap)
Swap my contents with another OrderedMap.
 o toString()
Return a string that describes me.
 o upperBound(Object)
Return an iterator positioned at the last location that a pair with a specified key could be inserted without violating the ordering criteria.
 o values(Object)
Return an Enumeration of all my values that are associated with a particular key.

Constructors

 o OrderedMap
 public OrderedMap()
Construct myself to be an empty OrderedMap that orders its keys based on their hash value and does not allow duplicates.

 o OrderedMap
 public OrderedMap(boolean allowDuplicates)
Construct myself to be an empty OrderedMap that orders its keys based on their hash value and conditionally allows duplicates.

Parameters:
allowDuplicates - true if duplicates are allowed.
 o OrderedMap
 public OrderedMap(BinaryPredicate comparator)
Construct myself to be an empty OrderedMap that orders its keys using a specified binary predicate and does not allow duplicates.

Parameters:
comparator - The predicate for ordering keys.
 o OrderedMap
 public OrderedMap(BinaryPredicate comparator,
                   boolean allowDuplicates)
Construct myself to be an empty OrderedMap that orders its keys using a specified binary predicate and conditionally allows duplicates.

Parameters:
comparator - The predicate for ordering keys.
allowDuplicates - true if duplicates are allowed.
 o OrderedMap
 public OrderedMap(OrderedMap map)
Construct myself to be a shallow copy of an existing OrderedMap.

Parameters:
map - The OrderedMap to copy.

Methods

 o allowsDuplicates
 public boolean allowsDuplicates()
Return true if duplicates are allowed.

 o clone
 public synchronized Object clone()
Return a shallow copy of myself.

Overrides:
clone in class Map
 o copy
 public synchronized void copy(OrderedMap map)
Become a shallow copy of an existing OrderedMap.

Parameters:
map - The OrderedMap that I shall become a shallow copy of.
 o toString
 public synchronized String toString()
Return a string that describes me.

Overrides:
toString in class Map
 o elements
 public synchronized Enumeration elements()
Return an Enumeration of my values

Overrides:
elements in class Map
 o start
 public ForwardIterator start()
Return an iterator positioned at my first pair.

Overrides:
start in class Map
 o finish
 public ForwardIterator finish()
Return an iterator positioned immediately afer my last pair.

Overrides:
finish in class Map
 o begin
 public synchronized OrderedMapIterator begin()
Return an iterator positioned at my first pair.

 o end
 public synchronized OrderedMapIterator end()
Return an iterator positioned immediately after my last pair.

 o isEmpty
 public boolean isEmpty()
Return true if I contain no entries.

Overrides:
isEmpty in class Map
 o size
 public int size()
Return the number of entries that I contain.

Overrides:
size in class Map
 o maxSize
 public int maxSize()
Return the maximum number of entries that I can contain.

Overrides:
maxSize in class Map
 o equals
 public boolean equals(Object object)
Return true if I'm equal to another object.

Parameters:
object - The object to compare myself against.
Overrides:
equals in class Map
 o equals
 public synchronized boolean equals(OrderedMap map)
Return true if I contain the same items in the same order as another OrderedMap. Use equals() to compare the individual elements.

Parameters:
map - The OrderedMap to compare myself against.
 o hashCode
 public synchronized int hashCode()
Return my hash code for support of hashing containers

Overrides:
hashCode in class Object
 o swap
 public synchronized void swap(OrderedMap map)
Swap my contents with another OrderedMap.

Parameters:
map - The OrderedMap that I will swap my contents with.
 o clear
 public synchronized void clear()
Remove all of my elements.

Overrides:
clear in class Map
 o remove
 public synchronized Object remove(Object object)
If I contain key/value pair(s) that matches a particular key, remove them all and return the number of pairs removed.

Parameters:
key - The key of the pair(s) to be removed.
Returns:
The first key/value pair removed or null if the container is unchanged.
Overrides:
remove in class Dictionary
 o remove
 public synchronized int remove(Object key,
                                int count)
If I contain key/value pair(s) that matches a particular key, remove at most a given number and return the number of pairs removed.

Parameters:
key - The key of the pair(s) to be removed.
count - The maximum number of objects to remove.
Returns:
The number of pairs removed.
 o remove
 public synchronized Object remove(Enumeration pos)
Remove the element at a particular position and return its value.

Parameters:
pos - An Enumeration positioned at the element to remove.
Returns:
The value that was removed or null if none.
Throws: IllegalArgumentException
is the Enumeration isn't an OrderedMapIterator for this OrderedMap object.
Overrides:
remove in class Map
 o remove
 public synchronized int remove(Enumeration first,
                                Enumeration last)
Remove the elements within a specified range, returning the first value of the key in that range.

Parameters:
first - An iterator positioned at the first element to remove.
last - An iterator positioned immediately after the last element to remove.
Returns:
Return the number of pairs removed.
Throws: IllegalArgumentException
is the Enumeration isn't an OrderedMapIterator for this OrderedMap object.
Overrides:
remove in class Map
 o find
 public synchronized OrderedMapIterator find(Object key)
Find a key/value pair based on its key and return its position. If the pair is not found, return end().

Parameters:
object - The object to locate.
 o count
 public synchronized int count(Object key)
Return the number of key/value pairs that match a particular key.

Parameters:
key - The key to match against.
Overrides:
count in class Map
 o countValues
 public synchronized int countValues(Object value)
Return the number of values that match a given object.

Parameters:
value - The value to match against.
Overrides:
countValues in class Map
 o lowerBound
 public synchronized OrderedMapIterator lowerBound(Object key)
Return an iterator positioned at the first location that a pair with a specified key could be inserted without violating the ordering criteria. If no such location is found, return an iterator positioned at end().

Parameters:
key - The key.
 o upperBound
 public synchronized OrderedMapIterator upperBound(Object key)
Return an iterator positioned at the last location that a pair with a specified key could be inserted without violating the ordering criteria. If no such location is found, return an iterator positioned at end().

Parameters:
key - The key.
 o equalRange
 public synchronized Range equalRange(Object object)
Return a pair of iterators whose first element is equal to lowerBound() and whose second element is equal to upperBound().

Parameters:
object - The object whose bounds are to be found.
 o getComparator
 public BinaryPredicate getComparator()
Return my comparator.

 o get
 public synchronized Object get(Object key)
Return the value associated with key, or null if the key does not exist.

Parameters:
key - The key to search against.
Overrides:
get in class Dictionary
 o put
 public synchronized Object put(Object key,
                                Object value)
If the key doesn't exist, associate the value with the key and return null, otherwise replace the first value associated with the key and return the old value.

Parameters:
key - The key.
value - The value.
Throws: NullPointerException
If the key or value is null.
Overrides:
put in class Dictionary
 o add
 public Object add(Object object)
Assume that the specified object is a Pair whose first field is a key and whose second field is a value. If the key doesn't exist or duplicates are allowed, associate the value with the key and return null, otherwise don't modify the map and return the current value associated with the key.

Parameters:
object - The pair to add.
Throws: IllegalArgumentException
If the object is not a Pair
Throws: NullPointerException
If the object is null or if the first or second items in the pair are null.
Overrides:
add in class Map
 o add
 public synchronized Object add(Object key,
                                Object value)
If the key doesn't exist or duplicates are allowed, associate the value with the key and return null, otherwise don't modify the map and return the current value associated with the key.

Parameters:
key - The key.
value - The value.
Throws: NullPointerException
If the key or value is null.
 o keys
 public synchronized Enumeration keys()
Return an Enumeration of all my keys.

Overrides:
keys in class Dictionary
 o keys
 public synchronized Enumeration keys(Object value)
Return an Enumeration of all my keys that are associated with a particular value.

Parameters:
value - The value to match.
Overrides:
keys in class Map
 o values
 public synchronized Enumeration values(Object key)
Return an Enumeration of all my values that are associated with a particular key.

Parameters:
key - The key to match.
Overrides:
values in class Map

All Packages  Class Hierarchy  This Package  Previous  Next  Index