home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-09-17 | 7.5 KB | 260 lines |
- // Copyright(c) 1996 ObjectSpace, Inc.
- // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
-
- package jgl;
-
- /**
- * ArrayAdapter is the base class of all array adapters, including those
- * that adapt the JDK Vector and Java native arrays.
- * <p>
- * @see jgl.BooleanArray
- * @see jgl.ByteArray
- * @see jgl.CharArray
- * @see jgl.DoubleArray
- * @see jgl.FloatArray
- * @see jgl.IntArray
- * @see jgl.LongArray
- * @see jgl.ObjectArray
- * @see jgl.ShortArray
- * @see jgl.VectorArray
- * @version 1.1
- * @author ObjectSpace, Inc.
- */
-
- abstract public class ArrayAdapter implements Sequence
- {
- // needed for Visual J++ bug workaround
- public Object clone()
- {
- return null;
- }
-
- // needed for Visual J++ bug workaround
- public boolean equals( Object object )
- {
- return false;
- }
-
- /**
- * Return my hash code for support of hashing containers
- */
- public int hashCode()
- {
- return Hashing.orderedHash( this );
- }
-
- /**
- * Return true if I contain no objects.
- */
- public boolean isEmpty()
- {
- return size() == 0;
- }
-
- /**
- * Return my last element.
- */
- public Object back()
- {
- return at( size() - 1 );
- }
-
- /**
- * Return my first element.
- */
- public Object front()
- {
- return at( 0 );
- }
-
- /**
- * Return the number of objects that match a specified object.
- * @param object The object to count.
- */
- public int count( Object object )
- {
- return count( 0, size() - 1, object );
- }
-
- /**
- * Return the number of objects within a specified range of that match a
- * particular value. the range is inclusive
- * @param first The index of the first object to consider.
- * @param last The index of the last object to consider.
- * @exception java.lang.IndexOutOfBoundsException If either index is invalid.
- */
- public int count( int first, int last, Object object )
- {
- int count = 0;
-
- for( int i=first; i <= last; i++ )
- if ( at( i ).equals( object ) )
- count++;
-
- return count;
- }
-
- /**
- * Replace all elements that match a particular object with a new value and return
- * the number of objects that were replaced.
- * @param oldValue The object to be replaced.
- * @param newValue The value to substitute.
- */
- public int replace( Object oldValue, Object newValue )
- {
- return replace( 0, size() - 1, oldValue, newValue );
- }
-
- /**
- * Replace all elements within a specified range that match a particular object
- * with a new value and return the number of objects that were replaced.
- * @param first The index of the first object to be considered.
- * @param last The index of the last object to be considered.
- * @param oldValue The object to be replaced.
- * @param newValue The value to substitute.
- * @exception java.lang.IndexOutOfBoundsException If either index is invalid.
- */
- public int replace( int first, int last, Object oldValue, Object newValue )
- {
- int count = 0;
-
- for( int i=first; i <= last; i++ )
- if ( at( i ).equals( oldValue ) )
- {
- put( i, newValue );
- count++;
- }
-
- return count;
- }
-
- /**
- * Return true if I contain a particular object using .equals()
- * @param object The object in question.
- */
- public boolean contains( Object object )
- {
- for( int i=0; i < size(); i++ )
- if ( at( i ).equals( object ) )
- {
- return true;
- }
- return false;
- }
-
- /**
- * Return the index of the first object that matches a particular value, or
- * -1 if the object is not found. Uses .equals() to find a match
- * @param object The object to find.
- * @exception java.lang.ClassCastException if objects are not Boolean
- */
- public int indexOf( Object object )
- {
- return indexOf( 0, size()-1, object );
- }
-
-
- /**
- * Return an index positioned at the first object within a specified range that
- * matches a particular object, or -1 if the object is not found.
- * @param first The index of the first object to consider.
- * @param last The index of the last object to consider.
- * @param object The object to find.
- * @exception java.lang.IndexOutOfBoundsException If either index is invalid.
- * @exception java.lang.ClassCastException if objects are not Boolean
- */
- public int indexOf( int first, int last, Object object )
- {
- for( int i=first; i <= last; i++ )
- if ( at( i ).equals( object ) )
- {
- return i;
- }
- return -1;
- }
-
- /**
- * Remove all of my objects. By default, this method throws an exception.
- * @exception jgl.InvalidOperationException Thrown by default.
- */
- public void clear()
- {
- throw new InvalidOperationException( "cannot execute clear() on a native array" );
- }
-
- /**
- * Add an object to myself. By default, this method throws an exception.
- * @param object The object to add.
- * @exception jgl.InvalidOperationException Thrown by default.
- */
- public Object add( Object object )
- {
- throw new InvalidOperationException( "cannot execute add() on a native array" );
- }
-
- /**
- * Insert an object in front of my first element. By default, this method throws
- * an exception.
- * @param object The object to insert.
- * @exception jgl.InvalidOperationException Thrown by default.
- */
- public void pushFront( Object object )
- {
- throw new InvalidOperationException( "cannot execute pushFront() on a native array" );
- }
-
- /**
- * Remove and return my first element. By default, this method throws an exception.
- * @exception jgl.InvalidOperationException Thrown by default.
- */
- public Object popFront()
- {
- throw new InvalidOperationException( "cannot execute popFront() on a native array" );
- }
-
- /**
- * Add an object at my end. By default, this method throws an exception.
- * @param object The object to add.
- * @exception jgl.InvalidOperationException Thrown by default.
- */
- public void pushBack( Object object )
- {
- throw new InvalidOperationException( "cannot execute pushBack() on a native array" );
- }
-
- /**
- * Remove and return my last element. By default, this method throws an exception.
- * @exception jgl.InvalidOperationException Thrown by default.
- */
- public Object popBack()
- {
- throw new InvalidOperationException( "cannot execute popBack() on a native array" );
- }
-
- /**
- * Remove all elements that match a specified object and return the number of
- * objects that were removed. By default, this method throws an exception.
- * @param object The object to remove.
- * @exception jgl.InvalidOperationException Thrown by default.
- */
- public int remove( Object object )
- {
- throw new InvalidOperationException( "cannot execute remove() on a native array" );
- }
-
- /**
- * Remove all elements within a specified range that match a particular object
- * and return the number of objects that were removed. By default, this method throws
- * an exception.
- * @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 Thrown by default.
- */
- public int remove( int first, int last, Object object )
- {
- throw new InvalidOperationException( "cannot execute remove() on a native array" );
- }
- }
-
-