001 /*
002 * LAPIS lightweight structured text processing system
003 *
004 * Copyright (C) 1998-2002 Carnegie Mellon University,
005 * Copyright (C) 2003 Massachusetts Institute of Technology.
006 * All rights reserved.
007 *
008 * This library is free software; you can redistribute it
009 * and/or modify it under the terms of the GNU General
010 * Public License as published by the Free Software
011 * Foundation, version 2.
012 *
013 * LAPIS homepage: http://graphics.lcs.mit.edu/lapis/
014 */
015
016
017 package lapisx.enum;
018 import java.util.Enumeration;
019 import java.util.NoSuchElementException;
020
021 /**
022 * Enumeration of an arbitrary array.
023 */
024 public class ArrayEnumeration implements Enumeration {
025 Object[] array;
026 int i = 0;
027
028 /**
029 * Make an ArrayEnumeration.
030 * @param array Array to enumerate. May be null, if desired.
031 * (This is sometimes useful for producing an empty enumeration.)
032 */
033 public ArrayEnumeration (Object[] array) {
034 this.array = array;
035 }
036
037 /**
038 * Test if enumeration has reached end.
039 * @return true if more elements are available to be enumerated, false
040 * if all elements have been yielded by nextElement()
041 */
042 public boolean hasMoreElements () {
043 return array != null ? i < array.length : false;
044 }
045
046 /**
047 * Get next element of enumeration.
048 * @return next element of enumeration, advancing the enumeration pointer
049 * @exception java.util.NoSuchElementException if no more elements
050 */
051 public Object nextElement () {
052 if (array == null || i >= array.length)
053 throw new NoSuchElementException ();
054 return array[i++];
055 }
056 }