org.w3c.util
Class ArrayDictionary

java.lang.Object
  extended by java.util.Dictionary
      extended by org.w3c.util.ArrayDictionary
All Implemented Interfaces:
java.lang.Cloneable

public class ArrayDictionary
extends java.util.Dictionary
implements java.lang.Cloneable

Random-access dictionary: like a dictionary but with a certain Vector-ness to it Besides all the methods from Dictionary, it also has methods that permit direct access to the nth element or nth key. Should be used with care...it's not too well tested yet, and it is very exposed.

This class does not provide thread-safeness, for the sake of efficiency, again it should be used with care !


Field Summary
protected  int incr
          By how much to grow
protected  java.lang.Object[] keys
          The array of keys
protected  int nelems
          How many real elements are in
protected  java.lang.Object[] values
          The array of corresponding values
 
Constructor Summary
ArrayDictionary()
          Create an ArrayDictionary using default values for initial size and increment.
ArrayDictionary(int init)
          Create an ArrayDictionary using the given initial size.
ArrayDictionary(int init, int incr)
          Create an ArrayDictionary using the given initial size and the given increment for growing the array.
ArrayDictionary(java.lang.Object[] keys, java.lang.Object[] values)
          Create an ArrayDicitonary, using (not copying) the given pair of arrays as keys and values.
ArrayDictionary(java.lang.Object[] keys, java.lang.Object[] values, int incr)
          Create an ArrayDicitonary, using (not copying) the given pair of arrays as keys and values.
ArrayDictionary(java.util.Vector keys, java.util.Vector values)
          Create an ArrayDictionary, contructing the arrays of keys and values from the two given vectors.
ArrayDictionary(java.util.Vector keys, java.util.Vector values, int incr)
          Create an ArrayDictionary, contructing the arrays of keys and values from the two given vectors.
 
Method Summary
 int capacity()
          Returns the maximum number of keys the dictionary can hold without reallocating an array.
 java.lang.Object clone()
          Clone this array dictionary.
 java.lang.Object elementAt(int n)
          Returns the nth element (value).
 java.util.Enumeration elements()
          Returns an enumeration of the elements of the dictionary.
 void ensureCapacity(int minCapacity)
          Increases the capacity of this dictionary to at least the specified number of key/value mappings.
 java.lang.Object get(java.lang.Object key)
          Returns the value that maps to the given key.
 java.lang.Object[] getMany(java.lang.Object[] rKeys)
          "Optimized" method to obtain the values corresponding to several keys, in one swoop.
protected  void grow()
           
protected  void grow(int newCapacity)
           
 boolean isEmpty()
          Are there any entries in the dictionary?
 java.lang.Object keyAt(int n)
          Returns the nth key.
 java.util.Enumeration keys()
          Returns an enumeration of the keys of the dictionary.
static void main(java.lang.String[] args)
          A kludge for testing ArrayDictionary
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Adds a mapping between a key and a value to the dictionary.
 java.lang.Object remove(java.lang.Object key)
          Removes a key (and its value) from the dictionary;
 java.lang.Object removeElementAt(int n)
          Removes the nth mapping (key/value pair) in the dictionary.
 java.lang.Object setElementAt(int n, java.lang.Object newVal)
          Sets the element at the nth place in the array.
 int size()
          Returns the number of elements in the dictionary
 java.lang.String toString()
          Creates a string representation of the dictionary
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

keys

protected java.lang.Object[] keys
The array of keys


values

protected java.lang.Object[] values
The array of corresponding values


nelems

protected int nelems
How many real elements are in


incr

protected int incr
By how much to grow

Constructor Detail

ArrayDictionary

public ArrayDictionary()
Create an ArrayDictionary using default values for initial size and increment.


ArrayDictionary

public ArrayDictionary(int init)
Create an ArrayDictionary using the given initial size. (The increment is set to the same value).

Parameters:
init - The initial size

ArrayDictionary

public ArrayDictionary(int init,
                       int incr)
Create an ArrayDictionary using the given initial size and the given increment for growing the array.

Parameters:
init - the initial size
incr - the increment

ArrayDictionary

public ArrayDictionary(java.util.Vector keys,
                       java.util.Vector values)
Create an ArrayDictionary, contructing the arrays of keys and values from the two given vectors. The two vectors should have the same size. The increment is set to the number of elements.

Parameters:
keys - the vector of keys
values - the vector of values

ArrayDictionary

public ArrayDictionary(java.util.Vector keys,
                       java.util.Vector values,
                       int incr)
Create an ArrayDictionary, contructing the arrays of keys and values from the two given vectors. The two vectors should have the same size.

Parameters:
keys - the vector of keys
values - the vector of values
incr - the increment for growing the arrays

ArrayDictionary

public ArrayDictionary(java.lang.Object[] keys,
                       java.lang.Object[] values)
Create an ArrayDicitonary, using (not copying) the given pair of arrays as keys and values. The increment is set to the length of the arrays.

Parameters:
keys - the array of keys
values - the array of values

ArrayDictionary

public ArrayDictionary(java.lang.Object[] keys,
                       java.lang.Object[] values,
                       int incr)
Create an ArrayDicitonary, using (not copying) the given pair of arrays as keys and values.

Parameters:
keys - the array of keys
values - the array of values
incr - the increment for growing the arrays
Method Detail

clone

public java.lang.Object clone()
Clone this array dictionary.

As for hashtables, a shallow copy is made, the keys and elements themselves are not cloned.

Overrides:
clone in class java.lang.Object
Returns:
The clone.

grow

protected final void grow()

grow

protected void grow(int newCapacity)

elements

public java.util.Enumeration elements()
Returns an enumeration of the elements of the dictionary.

Specified by:
elements in class java.util.Dictionary
Returns:
the enumeration

get

public java.lang.Object get(java.lang.Object key)
Returns the value that maps to the given key.

Specified by:
get in class java.util.Dictionary
Parameters:
key - the key
Returns:
the value

getMany

public java.lang.Object[] getMany(java.lang.Object[] rKeys)
"Optimized" method to obtain the values corresponding to several keys, in one swoop.

Parameters:
rKeys - An array of requested keys
Returns:
An array of corresponding values

isEmpty

public final boolean isEmpty()
Are there any entries in the dictionary?

Specified by:
isEmpty in class java.util.Dictionary
Returns:
true if there are no entries, false> otherwise.

ensureCapacity

public final void ensureCapacity(int minCapacity)
Increases the capacity of this dictionary to at least the specified number of key/value mappings.

Parameters:
minCapacity - the desired minimum capacity

keys

public java.util.Enumeration keys()
Returns an enumeration of the keys of the dictionary.

Specified by:
keys in class java.util.Dictionary
Returns:
the enumeration

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Adds a mapping between a key and a value to the dictionary. Will grow the arrays if necessary.

Specified by:
put in class java.util.Dictionary
Parameters:
key - the key
value - the corresponding value
Returns:
the previous value corresponding to the key, or null if the key is new.

remove

public java.lang.Object remove(java.lang.Object key)
Removes a key (and its value) from the dictionary;

Specified by:
remove in class java.util.Dictionary
Parameters:
key - the key to remove
Returns:
the value that used to map to that key

size

public final int size()
Returns the number of elements in the dictionary

Specified by:
size in class java.util.Dictionary
Returns:
the number of elements

capacity

public final int capacity()
Returns the maximum number of keys the dictionary can hold without reallocating an array.

Returns:
the capacity of the dictionary

keyAt

public final java.lang.Object keyAt(int n)
Returns the nth key.

Parameters:
n - the index of the desired key
Returns:
the nth key, or null if no key in that place.

elementAt

public final java.lang.Object elementAt(int n)
Returns the nth element (value).

Parameters:
n - the index of the desired element
Returns:
the nth element, or null if no element in that place.

setElementAt

public java.lang.Object setElementAt(int n,
                                     java.lang.Object newVal)
Sets the element at the nth place in the array.

Parameters:
n - the index of the element to change
newVal - the value to change it to
Returns:
the old value

removeElementAt

public java.lang.Object removeElementAt(int n)
Removes the nth mapping (key/value pair) in the dictionary.

Parameters:
n - the index of the element to remove
Returns:
the old value of the element at the nth place

toString

public java.lang.String toString()
Creates a string representation of the dictionary

Overrides:
toString in class java.lang.Object
Returns:
the string representation.

main

public static void main(java.lang.String[] args)
A kludge for testing ArrayDictionary