Source code: com/eireneh/util/ArrayEnumeration.java
1
2 package com.eireneh.util;
3
4 import java.util.Enumeration;
5
6 /**
7 * Helper class to enumerate through the objects in an array.
8 *
9 * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
10 * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
11 * Distribution Licence:<br />
12 * Project B is free software; you can redistribute it
13 * and/or modify it under the terms of the GNU General Public License,
14 * version 2 as published by the Free Software Foundation.<br />
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.<br />
19 * The License is available on the internet
20 * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
21 * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
23 * The copyright to this program is held by it's authors.
24 * </font></td></tr></table>
25 * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
26 * @see docs.Licence
27 * @author Joe Walker
28 */
29 public class ArrayEnumeration implements Enumeration
30 {
31 /**
32 * Setup the Enumeration with an array to iterate through
33 * @param array The array to Enumerate over
34 */
35 public ArrayEnumeration(Object[] array)
36 {
37 this(array, 0, array.length);
38 }
39
40 /**
41 * Setup the Enumeration with an array to iterate through
42 * @param array The array to Enumerate over
43 */
44 public ArrayEnumeration(Object[] array, int start)
45 {
46 this(array, start, array.length);
47 }
48
49 /**
50 * Setup the Enumeration with an array to iterate through
51 * @param array The array to Enumerate over
52 */
53 public ArrayEnumeration(Object[] array, int start, int end)
54 {
55 if (array == null)
56 throw new NullPointerException("Array must not be null.");
57 if (start < 0)
58 throw new IllegalArgumentException("Start must not be less than 0.");
59 if (end > array.length)
60 throw new IllegalArgumentException("End must not be greater than the array length.");
61
62 this.array = array;
63 this.pos = start;
64 this.end = end;
65 }
66
67 /**
68 * Are there more items in the database?
69 * @return true if we are not finished yet
70 */
71 public boolean hasMoreElements()
72 {
73 return pos < end;
74 }
75
76 /**
77 * Get the next item from the database
78 * @return The next object in the list
79 */
80 public Object nextElement()
81 {
82 return array[pos++];
83 }
84
85 /** The array to iterate through */
86 protected Object[] array;
87
88 /** The current position in the array */
89 protected int pos;
90
91 /** The place to stop when we reach */
92 protected int end;
93 }