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 / IntArray.java < prev    next >
Encoding:
Java Source  |  1997-03-14  |  3.2 KB  |  137 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.  * IntArray allows a native array of ints to be accessed like a Container.
  10.  * It is particularly useful for applying generic algorithms like Sorting.sort()
  11.  * to a native array.
  12.  * <p>
  13.  * @version 2.0.2
  14.  * @author ObjectSpace, Inc.
  15.  */
  16.  
  17. public class IntArray extends ArrayAdapter
  18.   {
  19.   int myArray[];
  20.  
  21.   public IntArray()
  22.     {
  23.     myArray = new int[ 0 ];
  24.     }
  25.  
  26.   public IntArray( int array[] )
  27.     {
  28.     myArray = array;
  29.     }
  30.  
  31.   public IntArray( IntArray array )
  32.     {
  33.     myArray = array.myArray;
  34.     }
  35.  
  36.   /**
  37.    * Return a shallow copy of myself.
  38.    */
  39.   public synchronized Object clone()
  40.     {
  41.     return new IntArray( this );
  42.     }
  43.  
  44.   /**
  45.    * Return a string that describes me.
  46.    */
  47.   public synchronized String toString()
  48.     {
  49.     return Printing.toString( this, "int[]" );
  50.     }
  51.  
  52.   /**
  53.    * Return true if I'm equal to a specified object.
  54.    * @param object The object to compare myself against.
  55.    * @return true if I'm equal to the specified object.
  56.    */
  57.   public boolean equals( Object object )
  58.     {
  59.     return object instanceof IntArray && equals( (IntArray)object );
  60.     }
  61.  
  62.   /**
  63.    * Return true if I contain the same items in the same order as
  64.    * another IntArray. Use equals() to compare the individual elements.
  65.    * @param array The IntArray to compare myself against.
  66.    * @return true if I'm equal to the specified object.
  67.    */
  68.   public synchronized boolean equals( IntArray object )
  69.     {
  70.     synchronized( object )
  71.       {
  72.       return Comparing.equal( this, object );
  73.       }
  74.     }
  75.  
  76.   /**
  77.    * Return the number of objects that I contain.
  78.    */
  79.   public int size()
  80.     {
  81.     return myArray.length;
  82.     }
  83.  
  84.   /**
  85.    * Return the maximum number of objects that I can contain.
  86.    */
  87.   public int maxSize()
  88.     {
  89.     return myArray.length;
  90.     }
  91.  
  92.   /**
  93.    * Return an Enumeration of my components.
  94.    */
  95.   public synchronized Enumeration elements()
  96.     {
  97.     return IntIterator.begin( myArray, this );
  98.     }
  99.  
  100.   /**
  101.    * Return an iterator positioned at my first item.
  102.    */
  103.   public synchronized ForwardIterator start()
  104.     {
  105.     return IntIterator.begin( myArray, this );
  106.     }
  107.  
  108.   /**
  109.    * Return an iterator positioned immediately after my last item.
  110.    */
  111.   public synchronized ForwardIterator finish()
  112.     {
  113.     return IntIterator.end( myArray, this );
  114.     }
  115.  
  116.   /**
  117.    * Return the integer at the specified index as a Integer object.
  118.    * @param index The index.
  119.    */
  120.   public synchronized Object at( int index )
  121.     {
  122.     return new Integer( myArray[index] );
  123.     }
  124.  
  125.   /**
  126.    * Set the object at a specified index.  The object must be a Integer
  127.    * @param index The index.
  128.    * @param object The object to place at the specified index.
  129.    * @exception java.lang.ClassCastException if object is not a Integer
  130.    * @exception java.lang.IndexOutOfBoundsException if object is not a Integer
  131.    */
  132.   public synchronized void put( int index, Object object )
  133.     {
  134.     myArray[index] = ((Integer)object).intValue();
  135.     }
  136.   }
  137.