home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-09-10 | 4.8 KB | 220 lines |
- // Copyright(c) 1996 ObjectSpace, Inc.
- // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
-
- package jgl;
-
- import java.util.Enumeration;
- import java.util.Vector;
-
- /**
- * VectorArray allows a java.util.Vector to be accessed like a Container.
- * It is particularly useful for applying generic algorithms like Sorting.sort()
- * to a java.util.Vector.
- * <p>
- * @version 1.1
- * @author ObjectSpace, Inc.
- */
-
- public class VectorArray extends ArrayAdapter
- {
- java.util.Vector myVector;
-
- public VectorArray( java.util.Vector vector )
- {
- myVector = vector;
- }
-
- public VectorArray( VectorArray vector )
- {
- myVector = vector.myVector;
- }
-
- /**
- * Return a shallow copy of myself.
- */
- public Object clone()
- {
- return new VectorArray( this );
- }
-
- /**
- * Return a string that describes me.
- */
- public String toString()
- {
- return myVector.toString();
- }
-
- /**
- * Return true if I'm equal to a specified object.
- * @param object The object to compare myself against.
- * @return true if I'm equal to the specified object.
- */
- public boolean equals( Object object )
- {
- return myVector.equals( ((VectorArray) object).myVector );
- }
-
- /**
- * Return the number of objects that I contain.
- */
- public int size()
- {
- return myVector.size();
- }
-
- /**
- * Return the maximum number of objects that I can contain.
- */
- public int maxSize()
- {
- return Allocator.maxSize();
- }
-
- /**
- * Return an Enumeration of my elements.
- */
- public Enumeration elements()
- {
- return VectorIterator.begin( myVector, this );
- }
-
- /**
- * Return an iterator positioned at my first item.
- */
- public ForwardIterator start()
- {
- return VectorIterator.begin( myVector, this );
- }
-
- /**
- * Return an iterator positioned immediately after my last item.
- */
- public ForwardIterator finish()
- {
- return VectorIterator.end( myVector, this );
- }
-
- /**
- * Return the object at the specified index.
- * @param index The index.
- */
- public Object at( int index )
- {
- return myVector.elementAt( index );
- }
-
- /**
- * Set the object at a specified index. The object must be a Integer
- * @param index The index.
- * @param object The object to place at the specified index.
- * @exception java.lang.IndexOutOfBoundsException if index is not in range.
- */
- public void put( int index, Object object )
- {
- myVector.setElementAt( object, index );
- }
-
- /**
- * Remove all of my objects.
- */
- public void clear()
- {
- myVector.removeAllElements();
- }
-
- /**
- * Add an object to myself.
- */
- public Object add( Object object )
- {
- myVector.addElement( object );
- return null;
- }
-
- /**
- * Insert an object in front of my first element.
- * @param object The object to insert.
- */
- public void pushFront( Object object )
- {
- myVector.insertElementAt( object, 0 );
- }
-
- /**
- * Remove and return my first element.
- */
- public Object popFront()
- {
- Object r = myVector.firstElement();
- myVector.removeElementAt( 0 );
- return r;
- }
-
- /**
- * Add an object at my end.
- * @param object The object to add.
- */
- public void pushBack( Object object )
- {
- myVector.addElement( object );
- }
-
- /**
- * Remove and return my last element.
- */
- public Object popBack()
- {
- Object r = myVector.lastElement();
- myVector.removeElementAt( myVector.size() - 1 );
- return r;
- }
-
- /**
- * Remove all elements that match a specified object and return the number of
- * objects that were removed.
- * @param object The object to remove.
- */
- public int remove( Object object )
- {
- int count = 0;
- while( myVector.removeElement( object ) )
- count++;
-
- return count;
- }
-
- /**
- * Remove all elements within a specified range that match a particular object
- * and return the number of objects that were removed.
- * @param first The index of the first object to remove.
- * @param last The index of the last object to remove.
- * @param object The object to remove.
- * @exception java.lang.IndexOutOfBoundsException If either index is invalid.
- */
- public int remove( int first, int last, Object object )
- {
- if ( ( first < 0 ) || ( last > myVector.size() - 1 ) )
- throw new IndexOutOfBoundsException( "index out of range for this Vector." );
-
- int count = 0;
- int index = first;
-
- for( int i = first; i < last; i++ )
- {
- if( ( myVector.elementAt( index ) ).equals( object ) )
- {
- myVector.removeElementAt( index );
- count++;
- }
- else
- {
- index++;
- }
- }
-
- return count;
- }
- }
-
-