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

  1. // Copyright(c) 1996 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package jgl;
  5.  
  6. import java.util.Enumeration;
  7.  
  8. /**
  9.  * ShortArray allows a native array of shorts 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 1.1
  14.  * @author ObjectSpace, Inc.
  15.  */
  16.  
  17. public class ShortArray extends ArrayAdapter
  18.   {
  19.   short myArray[];
  20.  
  21.   public ShortArray( short array[] )
  22.     {
  23.     myArray = array;
  24.     }
  25.  
  26.   public ShortArray( ShortArray array )
  27.     {
  28.     myArray = array.myArray;
  29.     }
  30.  
  31.   /**
  32.    * Return a shallow copy of myself.
  33.    */
  34.   public Object clone()
  35.     {
  36.     return new ShortArray( this );
  37.     }
  38.  
  39.   /**
  40.    * Return a string that describes me.
  41.    */
  42.   public String toString()
  43.     {
  44.     return Printing.toString( this, "short[]" );
  45.     }
  46.  
  47.   /**
  48.    * Return true if I'm equal to a specified object.
  49.    * @param object The object to compare myself against.
  50.    * @return true if I'm equal to the specified object.
  51.    */
  52.   public boolean equals( Object object )
  53.     {
  54.     return Comparing.equal( this, (ShortArray) object );
  55.     }
  56.   
  57.   /**
  58.    * Return the number of objects that I contain.
  59.    */
  60.   public int size()
  61.     {
  62.     return myArray.length;
  63.     }
  64.  
  65.   /**
  66.    * Return the maximum number of objects that I can contain.
  67.    */
  68.   public int maxSize()
  69.     {
  70.     return myArray.length;
  71.     }
  72.  
  73.   /**
  74.    * Return an Enumeration of my components.
  75.    */
  76.   public Enumeration elements()
  77.     {
  78.     return ShortIterator.begin( myArray, this );
  79.     }
  80.  
  81.   /**
  82.    * Return an iterator positioned at my first item.
  83.    */
  84.   public ForwardIterator start()
  85.     {
  86.     return ShortIterator.begin( myArray, this );
  87.     }
  88.  
  89.   /**
  90.    * Return an iterator positioned immediately after my last item.
  91.    */
  92.   public ForwardIterator finish()
  93.     {
  94.     return ShortIterator.end( myArray, this );
  95.     }
  96.  
  97.   /**
  98.    * Return the integer at the specified index as a Integer object.
  99.    * @param index The index.
  100.    */
  101.   public Object at( int index )
  102.     {
  103.     return new Integer( myArray[index] );
  104.     }
  105.  
  106.   /**
  107.    * Set the object at a specified index.  The object must be a Integer
  108.    * @param index The index.
  109.    * @param object The object to place at the specified index.
  110.    * @exception java.lang.ClassCastException if object is not a Integer
  111.    * @exception java.lang.IndexOutOfBoundsException if object is not a Integer
  112.    */
  113.   public void put( int index, Object object )
  114.     {
  115.     myArray[index] = (short) ((Integer)object).intValue();
  116.     }
  117.   }
  118.  
  119.