home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Extras / OSpace / jgl.exe / jgl_2_0 / src / COM / objectspace / jgl / ArrayAdapter.java < prev    next >
Encoding:
Java Source  |  1997-03-14  |  8.9 KB  |  286 lines

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