harpoon.Util.Collections
Class UniqueVector

java.lang.Object
  |
  +--java.util.AbstractCollection
        |
        +--java.util.AbstractList
              |
              +--harpoon.Util.Collections.UniqueVector
All Implemented Interfaces:
Cloneable, Collection, List, Set
Direct Known Subclasses:
UniqueStack

public class UniqueVector
extends AbstractList
implements Set, Cloneable

A unique vector refuses to addElement duplicates.

Conforms to the JDK 1.2 Collections API.

Version:
$Id: UniqueVector.java,v 1.2 2002/02/25 21:09:15 cananian Exp $
Author:
C. Scott Ananian <cananian@alumni.princeton.edu>
See Also:
Vector, Hashtable

Field Summary
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
UniqueVector()
          Constructs an empty UniqueVector.
UniqueVector(Collection c)
          Constructs a vector containing the elements of the specified Collection, in the order they are returned by the collection's iterator.
UniqueVector(int initialCapacity)
          Constructs an empty UniqueVector with the specified initial capacity.
 
Method Summary
 void add(int index, Object element)
          Inserts the specified element at the specified position in this list.
 boolean add(Object obj)
          Adds the specified component to the end of this vector, increasing its size by one, if it doesn't already exist in the vector.
 void addElement(Object obj)
          Adds the specified component to the end of this vector, increasing its size by one, if it doesn't already exist in the vector.
 int capacity()
          Returns the current capacity of this vector.
 void clear()
          Removes all of the elements from this collection.
 Object clone()
          Returns a clone of this vector.
 boolean contains(Object elem)
          Tests if the specified object is a component in this vector.
 void copyInto(Object[] anArray)
          Copies the components of this vector into the specified array.
 Object elementAt(int index)
          Returns the component at the specified index.
 Enumeration elements()
          Returns an enumeration of the components of this vector.
 void ensureCapacity(int minCapacity)
          Increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument.
 Object firstElement()
          Returns the first component of this vector.
 Object get(int index)
          Returns the element at the specified posision in this vector.
 int indexOf(Object elem)
          Returns the first (and only) occurrence of the given argument, testing for equality using the equals method.
 int indexOf(Object elem, int index)
          Returns the first occurrence of the given argument, beginning the search at index, and testing for equality using the equals method.
 void insertElementAt(Object obj, int index)
          Inserts the specified object as a component in this vector at the specified index.
 boolean isEmpty()
          Tests if this vector has no components.
 Object lastElement()
          Returns the last component of the vector.
 int lastIndexOf(Object elem)
          Returns the index of the last (and only) occurrence of the specified object in this vector.
 int lastIndexOf(Object elem, int index)
          Searches backwards for the specified object, starting from the specified index, and returns an index to it.
 Object remove(int index)
          Removes the element at the specified position in this vector.
 void removeAllElements()
          Removes all components from this vector and sets its size to zero.
 boolean removeElement(Object obj)
          Removes the first (and only) occurance of the argument from this vector.
 void removeElementAt(int index)
          Deletes the component at the specified index.
 Object set(int index, Object obj)
          Replaces the element at the specified position in this vector with the specified element.
 void setElementAt(Object obj, int index)
          Sets the component at the specified index of this vector to be the specified object.
 int size()
          Returns the number of components in this vector.
 Object[] toArray()
           
 Object[] toArray(Object[] a)
           
 String toString()
          Returns a string representation of this vector.
 void trimToSize()
          Trims the capacity of this vector to be the vector's current size.
 
Methods inherited from class java.util.AbstractList
addAll, equals, hashCode, iterator, listIterator, listIterator, removeRange, subList
 
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, remove, removeAll, retainAll
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
addAll, containsAll, equals, hashCode, iterator, remove, removeAll, retainAll
 
Methods inherited from interface java.util.List
addAll, containsAll, remove, removeAll, retainAll
 

Constructor Detail

UniqueVector

public UniqueVector()
Constructs an empty UniqueVector.


UniqueVector

public UniqueVector(int initialCapacity)
Constructs an empty UniqueVector with the specified initial capacity.


UniqueVector

public UniqueVector(Collection c)
Constructs a vector containing the elements of the specified Collection, in the order they are returned by the collection's iterator. Duplicate elements are skipped.

Method Detail

addElement

public void addElement(Object obj)
Adds the specified component to the end of this vector, increasing its size by one, if it doesn't already exist in the vector. Duplicate elements are thrown away. The capacity of the vector is increased if necessary.

Parameters:
obj - the component to be added.

add

public void add(int index,
                Object element)
Inserts the specified element at the specified position in this list. To maintain uniqueness, any previous instance of this element in the vector is removed prior to insertion.

Specified by:
add in interface List
Overrides:
add in class AbstractList

add

public boolean add(Object obj)
Adds the specified component to the end of this vector, increasing its size by one, if it doesn't already exist in the vector. Duplicate elements are thrown away. The capacity of the vector is increased if necessary.

Specified by:
add in interface Set
Overrides:
add in class AbstractList
Parameters:
obj - the component to be added.

capacity

public int capacity()
Returns the current capacity of this vector.

Throws:
UnsupportedOperationException - not supported.

clone

public Object clone()
             throws CloneNotSupportedException
Returns a clone of this vector.

Overrides:
clone in class Object
Returns:
a clone of this vector.
Throws:
CloneNotSupportedException - if the UniqueVector cannot be cloned.

contains

public boolean contains(Object elem)
Tests if the specified object is a component in this vector.

Specified by:
contains in interface Set
Overrides:
contains in class AbstractCollection
Parameters:
elem - an object
Returns:
true if the specified object is a component in this vector; false otherwise.

copyInto

public void copyInto(Object[] anArray)
Copies the components of this vector into the specified array. The array must be big enough to hold all the objects in this vector.

Parameters:
anArray - the array into which the components get copied.

toArray

public Object[] toArray()
Specified by:
toArray in interface Set
Overrides:
toArray in class AbstractCollection

toArray

public Object[] toArray(Object[] a)
Specified by:
toArray in interface Set
Overrides:
toArray in class AbstractCollection

elementAt

public Object elementAt(int index)
Returns the component at the specified index.

Parameters:
index - an index into this vector.
Returns:
the component at the specified index.
Throws:
ArrayIndexOutOfBoundsException - if an invalid index was given.

get

public Object get(int index)
Returns the element at the specified posision in this vector.

Specified by:
get in interface List
Specified by:
get in class AbstractList

elements

public Enumeration elements()
Returns an enumeration of the components of this vector.

Returns:
an enumeration of the components of this vector.

ensureCapacity

public void ensureCapacity(int minCapacity)
Increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument.

Parameters:
minCapacity - the desired minimum capacity.

firstElement

public Object firstElement()
Returns the first component of this vector.

Returns:
the first component of this vector.
Throws:
NoSuchElementException - if this vector has no components.

indexOf

public int indexOf(Object elem)
Returns the first (and only) occurrence of the given argument, testing for equality using the equals method.

Specified by:
indexOf in interface List
Overrides:
indexOf in class AbstractList
Parameters:
elem - an object
Returns:
the index of the first occurrence of the argument in this vector; returns -1 if the object is not found.

indexOf

public int indexOf(Object elem,
                   int index)
Returns the first occurrence of the given argument, beginning the search at index, and testing for equality using the equals method.

Parameters:
elem - an object.
index - the index to start searching from.
Returns:
the index of the first occurrence of the object argument in this vector at position index or later in the vector; returns -1 if the object is not found.

insertElementAt

public void insertElementAt(Object obj,
                            int index)
Inserts the specified object as a component in this vector at the specified index. Each component in this vector with an index greater or equal to the specified index is shifted upward to have an index one greater than the value it had previously.

The index must be a value greater than or equal to 0 and less than or equal to the current size of the vector.

To maintain uniqueness, removed any previous instance of the component in the vector before insertion.

Parameters:
obj - the component to insert.
index - where to insert the new component.
Throws:
ArrayIndexOutOfBoundsException - if the index was invalid.

isEmpty

public boolean isEmpty()
Tests if this vector has no components.

Specified by:
isEmpty in interface Set
Overrides:
isEmpty in class AbstractCollection
Returns:
true if this vector has no components; false otherwise.

lastElement

public Object lastElement()
Returns the last component of the vector.

Returns:
the last component of the vector, i.e., the component at index size()-1.
Throws:
NoSuchElementException - if this vector is empty.

lastIndexOf

public int lastIndexOf(Object elem)
Returns the index of the last (and only) occurrence of the specified object in this vector.

Specified by:
lastIndexOf in interface List
Overrides:
lastIndexOf in class AbstractList
Parameters:
elem - the desired component.
Returns:
the index of the last occurrence of the specified object in this vector; returns -1 if the object is not found.

lastIndexOf

public int lastIndexOf(Object elem,
                       int index)
Searches backwards for the specified object, starting from the specified index, and returns an index to it.

Parameters:
elem - the desired component.
index - the index to start searching from.
Returns:
the index of the last occurrence of the specified object in this vector at position less than index in the vector; -1 if the object is not found.

removeAllElements

public void removeAllElements()
Removes all components from this vector and sets its size to zero.


clear

public void clear()
Removes all of the elements from this collection.

Specified by:
clear in interface Set
Overrides:
clear in class AbstractList

removeElement

public final boolean removeElement(Object obj)
Removes the first (and only) occurance of the argument from this vector. If the object is found in this vector, each component in the vector with an index greater or equal to the object's index is shifted downward to have an index one smaller than the value it had previously.

Parameters:
obj - the component to be removed.
Returns:
true if the argument was a component of this vector; false otherwise.

removeElementAt

public void removeElementAt(int index)
Deletes the component at the specified index. Each component in this vector with an index greater than or equal to the specified index is shifted downward to have an index one smaller than the value it had previously.

The index must be a value greater than or equal to 0 and less than the current size of the vector.

Parameters:
index - the index of the object to remove.
Throws:
ArrayIndexOutOfBoundsException - if the index was invalid.

remove

public Object remove(int index)
Removes the element at the specified position in this vector.

Specified by:
remove in interface List
Overrides:
remove in class AbstractList

setElementAt

public void setElementAt(Object obj,
                         int index)
Sets the component at the specified index of this vector to be the specified object. The previous component at that position is discarded.

The index must be a value greater than or equal to 0 and less than the current size of the vector.

Nothing is done if the component at index is equal to obj. To maintain uniqueness, any component equal to obj is removed before the setElementAt() is done.

Parameters:
obj - what the component is to be set to.
index - the specified index.
Throws:
ArrayIndexOutOfBoundsException - if the index was invalid.

set

public Object set(int index,
                  Object obj)
Replaces the element at the specified position in this vector with the specified element.

Specified by:
set in interface List
Overrides:
set in class AbstractList

size

public int size()
Returns the number of components in this vector.

Specified by:
size in interface Set
Specified by:
size in class AbstractCollection
Returns:
the number of components in this vector.

toString

public String toString()
Returns a string representation of this vector.

Overrides:
toString in class AbstractCollection
Returns:
a string representation of this vector.

trimToSize

public void trimToSize()
Trims the capacity of this vector to be the vector's current size. An application can use this operation to minimize the storage of a vector.