// Copyright(c) 1996,1997 ObjectSpace, Inc.
// Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.

package COM.objectspace.jgl;

import java.util.Enumeration;

/**
 * BooleanArray allows a native array of booleans to be accessed like a Container.
 * It is particularly useful for applying generic algorithms like Sorting.sort()
 * to a native array.
 * <p>
 * @version 2.0.2
 * @author ObjectSpace, Inc.
 */

public class BooleanArray extends ArrayAdapter
  {
  boolean myArray[];

  public BooleanArray()
    {
    myArray = new boolean[ 0 ];
    }

  public BooleanArray( boolean array[] )
    {
    myArray = array;
    }

  public BooleanArray( BooleanArray array )
    {
    myArray = array.myArray;
    }

  /**
   * Return a shallow copy of myself.
   */
  public synchronized Object clone()
    {
    return new BooleanArray( this );
    }

  /**
   * Return a string that describes me.
   */
  public synchronized String toString()
    {
    return Printing.toString( this, "boolean[]" );
    }

  /**
   * Return true if I'm equal to a specified object.
   * @param object The object to compare myself against.
   * @return true if I'm equal to the specified object.
   */
  public boolean equals( Object object )
    {
    return object instanceof BooleanArray && equals( (BooleanArray)object );
    }

  /**
   * Return true if I contain the same items in the same order as
   * another BooleanArray. Use equals() to compare the individual elements.
   * @param array The BooleanArray to compare myself against.
   * @return true if I'm equal to the specified object.
   */
  public synchronized boolean equals( BooleanArray object )
    {
    synchronized( object )
      {
      return Comparing.equal( this, object );
      }
    }

  /**
   * Return the number of objects that I contain.
   */
  public int size()
    {
    return myArray.length;
    }

  /**
   * Return the maximum number of objects that I can contain.
   */
  public int maxSize()
    {
    return myArray.length;
    }

  /**
   * Return an Enumeration to my components.
   */
  public synchronized Enumeration elements()
    {
    return BooleanIterator.begin( myArray, this );
    }

  /**
   * Return an iterator positioned at my first item.
   */
  public synchronized ForwardIterator start()
    {
    return BooleanIterator.begin( myArray, this );
    }

  /**
   * Return an iterator positioned immediately after my last item.
   */
  public synchronized ForwardIterator finish()
    {
    return BooleanIterator.end( myArray, this );
    }

  /**
   * Return the boolean at the specified index as a Boolean object.
   * @param index The index.
   */
  public synchronized Object at( int index )
    {
    return new Boolean( myArray[ index ] );
    }

  /**
   * Set the object at a specified index. The object must be a Boolean
   * @param index The index.
   * @param object The object to place at the specified index.
   * @exception java.lang.ClassCastException if object is not a Boolean
   * @exception java.lang.IndexOutOfBoundsException if object is not a Boolean
   */
  public synchronized void put( int index, Object object )
    {
    myArray[ index ] = ((Boolean) object).booleanValue();
    }
  }
