home *** CD-ROM | disk | FTP | other *** search
/ ActiveX Programming Unleashed CD / AXU.iso / jgl_1_1 / jgl_1_1.exe / src / ArrayAdapter.java < prev    next >
Encoding:
Java Source  |  1996-09-17  |  7.5 KB  |  260 lines

  1. // Copyright(c) 1996 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package jgl;
  5.  
  6. /**
  7.  * ArrayAdapter is the base class of all array adapters, including those
  8.  * that adapt the JDK Vector and Java native arrays.
  9.  * <p>
  10.  * @see jgl.BooleanArray
  11.  * @see jgl.ByteArray
  12.  * @see jgl.CharArray
  13.  * @see jgl.DoubleArray
  14.  * @see jgl.FloatArray
  15.  * @see jgl.IntArray
  16.  * @see jgl.LongArray
  17.  * @see jgl.ObjectArray
  18.  * @see jgl.ShortArray
  19.  * @see jgl.VectorArray
  20.  * @version 1.1
  21.  * @author ObjectSpace, Inc.
  22.  */
  23.  
  24. abstract public class ArrayAdapter implements Sequence
  25.   {
  26.   // needed for Visual J++ bug workaround
  27.   public Object clone()
  28.     {
  29.     return null;
  30.     }
  31.  
  32.   // needed for Visual J++ bug workaround
  33.   public boolean equals( Object object )
  34.     {
  35.     return false;
  36.     }
  37.  
  38.   /**
  39.    * Return my hash code for support of hashing containers
  40.    */
  41.   public int hashCode()
  42.     {
  43.     return Hashing.orderedHash( this );
  44.     }
  45.  
  46.   /**
  47.    * Return true if I contain no objects.
  48.    */
  49.   public boolean isEmpty()
  50.     {
  51.     return size() == 0;
  52.     }
  53.  
  54.   /**
  55.    * Return my last element.
  56.    */
  57.   public Object back()
  58.     {
  59.     return at( size() - 1 );
  60.     }
  61.  
  62.   /**
  63.    * Return my first element.
  64.    */
  65.   public Object front()
  66.     {
  67.     return at( 0 );
  68.     }
  69.  
  70.   /**
  71.    * Return the number of objects that match a specified object.
  72.    * @param object The object to count.
  73.    */
  74.   public int count( Object object ) 
  75.     {
  76.     return count( 0, size() - 1, object );
  77.     }
  78.     
  79.   /**
  80.    * Return the number of objects within a specified range of that match a
  81.    * particular value.  the range is inclusive
  82.    * @param first The index of the first object to consider.
  83.    * @param last The index of the last object to consider.
  84.    * @exception java.lang.IndexOutOfBoundsException If either index is invalid.
  85.    */
  86.   public int count( int first, int last, Object object )
  87.     {
  88.     int count = 0;
  89.     
  90.     for( int i=first; i <= last; i++ )
  91.       if ( at( i ).equals( object ) )
  92.         count++;
  93.     
  94.     return count;
  95.     }
  96.  
  97.   /**
  98.    * Replace all elements that match a particular object with a new value and return
  99.    * the number of objects that were replaced.
  100.    * @param oldValue The object to be replaced.
  101.    * @param newValue The value to substitute.
  102.    */
  103.   public int replace( Object oldValue, Object newValue )
  104.     {
  105.     return replace( 0, size() - 1, oldValue, newValue );
  106.     }
  107.  
  108.   /**
  109.    * Replace all elements within a specified range that match a particular object
  110.    * with a new value and return the number of objects that were replaced.
  111.    * @param first The index of the first object to be considered.
  112.    * @param last The index of the last object to be considered.
  113.    * @param oldValue The object to be replaced.
  114.    * @param newValue The value to substitute.
  115.    * @exception java.lang.IndexOutOfBoundsException If either index is invalid.
  116.    */
  117.   public int replace( int first, int last, Object oldValue, Object newValue )
  118.     {
  119.     int count = 0;
  120.     
  121.     for( int i=first; i <= last; i++ )
  122.       if ( at( i ).equals( oldValue ) )
  123.         {
  124.         put( i, newValue );
  125.         count++;
  126.         }
  127.     
  128.     return count;
  129.     }
  130.  
  131.   /**
  132.    * Return true if I contain a particular object using .equals()
  133.    * @param object The object in question.
  134.    */
  135.   public boolean contains( Object object )
  136.     {
  137.     for( int i=0; i < size(); i++ )
  138.       if ( at( i ).equals( object ) )
  139.         {
  140.         return true;
  141.         }
  142.     return false;
  143.     }
  144.  
  145.   /**
  146.    * Return the index of the first object that matches a particular value, or 
  147.    * -1 if the object is not found.  Uses .equals() to find a match
  148.    * @param object The object to find.
  149.    * @exception java.lang.ClassCastException if objects are not Boolean
  150.    */
  151.   public int indexOf( Object object )
  152.     {
  153.     return indexOf( 0, size()-1, object );
  154.     }
  155.  
  156.  
  157.   /**
  158.    * Return an index positioned at the first object within a specified range that 
  159.    * matches a particular object, or -1 if the object is not found.
  160.    * @param first The index of the first object to consider.
  161.    * @param last The index of the last object to consider.
  162.    * @param object The object to find.
  163.    * @exception java.lang.IndexOutOfBoundsException If either index is invalid.
  164.    * @exception java.lang.ClassCastException if objects are not Boolean
  165.    */
  166.   public int indexOf( int first, int last, Object object )
  167.     {
  168.     for( int i=first; i <= last; i++ )
  169.       if ( at( i ).equals( object ) )
  170.         {
  171.         return i;
  172.         }
  173.     return -1;
  174.     }
  175.  
  176.   /**
  177.    * Remove all of my objects. By default, this method throws an exception.
  178.    * @exception jgl.InvalidOperationException Thrown by default.
  179.    */
  180.   public void clear()
  181.     {
  182.     throw new InvalidOperationException( "cannot execute clear() on a native array" );
  183.     }
  184.  
  185.   /**
  186.    * Add an object to myself. By default, this method throws an exception.
  187.    * @param object The object to add.
  188.    * @exception jgl.InvalidOperationException Thrown by default.
  189.    */
  190.   public Object add( Object object )
  191.     {
  192.     throw new InvalidOperationException( "cannot execute add() on a native array" );
  193.     }
  194.  
  195.   /**
  196.    * Insert an object in front of my first element. By default, this method throws
  197.    * an exception.
  198.    * @param object The object to insert.
  199.    * @exception jgl.InvalidOperationException Thrown by default.
  200.    */
  201.   public void pushFront( Object object ) 
  202.     {
  203.     throw new InvalidOperationException( "cannot execute pushFront() on a native array" );
  204.     }
  205.  
  206.   /**
  207.    * Remove and return my first element. By default, this method throws an exception.
  208.    * @exception jgl.InvalidOperationException Thrown by default.
  209.    */
  210.   public Object popFront()
  211.     {
  212.     throw new InvalidOperationException( "cannot execute popFront() on a native array" );
  213.     }
  214.  
  215.   /**
  216.    * Add an object at my end. By default, this method throws an exception.
  217.    * @param object The object to add.
  218.    * @exception jgl.InvalidOperationException Thrown by default.
  219.    */
  220.   public void pushBack( Object object )
  221.     {
  222.     throw new InvalidOperationException( "cannot execute pushBack() on a native array" );
  223.     }
  224.  
  225.   /**
  226.    * Remove and return my last element. By default, this method throws an exception.
  227.    * @exception jgl.InvalidOperationException Thrown by default.
  228.    */
  229.   public Object popBack()
  230.     {
  231.     throw new InvalidOperationException( "cannot execute popBack() on a native array" );
  232.     }
  233.  
  234.   /**
  235.    * Remove all elements that match a specified object and return the number of
  236.    * objects that were removed. By default, this method throws an exception.
  237.    * @param object The object to remove.
  238.    * @exception jgl.InvalidOperationException Thrown by default.
  239.    */
  240.   public int remove( Object object )
  241.     {
  242.     throw new InvalidOperationException( "cannot execute remove() on a native array" );
  243.     }
  244.  
  245.   /**
  246.    * Remove all elements within a specified range that match a particular object
  247.    * and return the number of objects that were removed. By default, this method throws
  248.    * an exception.
  249.    * @param first The index of the first object to remove.
  250.    * @param last The index of the last object to remove.
  251.    * @param object The object to remove.
  252.    * @exception java.lang.IndexOutOfBoundsException Thrown by default.
  253.    */
  254.   public int remove( int first, int last, Object object )
  255.     {
  256.     throw new InvalidOperationException( "cannot execute remove() on a native array" );
  257.     }
  258.   } 
  259.  
  260.