01: /*
02: * Copyright (C) 2004-2007 Stephen Ostermiller
03: * http://ostermiller.org/contact.pl?regarding=Java+Utilities
04: *
05: * This program is free software; you can redistribute it and/or modify
06: * it under the terms of the GNU General Public License as published by
07: * the Free Software Foundation; either version 2 of the License, or
08: * (at your option) any later version.
09: *
10: * This program is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: * GNU General Public License for more details.
14: *
15: * See COPYING.TXT for details.
16: */
17:
18: package com.Ostermiller.util;
19:
20: import java.util.*;
21:
22: /**
23: * Converts an array to an enumerator.
24: * <p>
25: * More information about this class is available from <a target="_top" href=
26: * "http://ostermiller.org/utils/Iterator_Enumeration.html">ostermiller.org</a>.
27: *
28: * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
29: * @param <ElementType> Type of object enumerated
30: * @since ostermillerutils 1.03.00
31: */
32: public class ArrayEnumeration<ElementType> implements
33: Enumeration<ElementType> {
34:
35: /**
36: * Array being converted to enumeration.
37: */
38: private ElementType[] array;
39:
40: /**
41: * Current index into the array.
42: */
43: private int index = 0;
44:
45: /**
46: * Create an Enumeration from an Array.
47: *
48: * @param array of objects on which to enumerate.
49: *
50: * @since ostermillerutils 1.03.00
51: */
52: public ArrayEnumeration(ElementType[] array) {
53: this .array = array;
54: }
55:
56: /**
57: * Tests if this enumeration contains more elements.
58: *
59: * @return true if and only if this enumeration object contains at least
60: * one more element to provide; false otherwise.
61: *
62: * @since ostermillerutils 1.03.00
63: */
64: public boolean hasMoreElements() {
65: return (index < array.length);
66: }
67:
68: /**
69: * Returns the next element of this enumeration if this enumeration
70: * object has at least one more element to provide.
71: *
72: * @return the next element of this enumeration.
73: * @throws NoSuchElementException if no more elements exist.
74: *
75: * @since ostermillerutils 1.03.00
76: */
77: public ElementType nextElement() throws NoSuchElementException {
78: if (index >= array.length)
79: throw new NoSuchElementException("Array index: " + index);
80: ElementType object = array[index];
81: index++;
82: return object;
83: }
84: }
|