An enumeration that iterates over an array. : Enumeration Interface « Collections « Java Tutorial






/**
 * 
 * JFreeReport : a free Java reporting library
 * 
 *
 * Project Info:  http://reporting.pentaho.org/
 *
 * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
 * in the United States and other countries.]
 *
 * ------------
 * ArrayEnumeration.java
 * ------------
 * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
 */


import java.util.Enumeration;
import java.util.NoSuchElementException;

/**
 * An enumeration that iterates over an array.
 *
 * @author Thomas Morgner

 */
public class ArrayEnumeration implements Enumeration
{
  /** The base datasource. */
  private Object[] objectarray = null;
  /** The counter holds the current position inside the array. */
  private int counter = 0;

  /**
   * Creates a new enumeration for the given array.
   *
   * @param objectarray the array over which to iterate
   * @throws NullPointerException if the array is null.
   */
  public ArrayEnumeration(final Object[] objectarray)
  {
    if (objectarray == null)
    {
      throw new NullPointerException("The array must not be null.");
    }

    this.objectarray = objectarray;
  }

  /**
   * Returns true if this enumeration has at least one more Element.
   *
   * @return true, if there are more elements, false otherwise.
   */
  public boolean hasMoreElements()
  {
    return (counter < objectarray.length);
  }

  /**
   * Returns the next element in the Array.
   *
   * @return the next element in the array.
   * @throws  NoSuchElementException  if no more elements exist.
   */
  public Object nextElement()
  {
    if (counter >= objectarray.length)
    {
      throw new NoSuchElementException();
    }

    final Object retval = objectarray[counter];
    counter += 1;
    return retval;
  }
}








9.35.Enumeration Interface
9.35.1.The Enumeration Interface
9.35.2.If you prefer a for-loop
9.35.3.Where do we get the enumeration from?
9.35.4.The SequenceInputStream Class
9.35.5.Concatenates the content of two enumerations into one.
9.35.6.Filters enumeration to contain each of the provided elements just once.
9.35.7.Filters some elements out from the input enumeration.
9.35.8.For each element of the input enumeration asks the Processor to provide a replacement
9.35.9.Removes all nulls from the input enumeration.
9.35.10.Returns an enumeration that iterates over provided array.
9.35.11.Support for breadth-first enumerating.
9.35.12.Serializable Enumeration
9.35.13.An enumeration that iterates over an array.
9.35.14.Creating Custom Enumerations