1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.woopi.ant.taskdefs.junit;
18 import java.util.Enumeration;
19 import java.util.NoSuchElementException;
20
21 /***
22 * A couple of methods related to enumerations that might be useful.
23 * This class should probably disappear once the required JDK is set to 1.2
24 * instead of 1.1.
25 *
26 */
27 public final class Enumerations {
28
29 private Enumerations() {
30 }
31
32 /***
33 * creates an enumeration from an array of objects.
34 * @param array the array of object to enumerate.
35 * @return the enumeration over the array of objects.
36 */
37 public static Enumeration fromArray(Object[] array) {
38 return new ArrayEnumeration(array);
39 }
40
41 /***
42 * creates an enumeration from an array of enumeration. The created enumeration
43 * will sequentially enumerate over all elements of each enumeration and skip
44 * <tt>null</tt> enumeration elements in the array.
45 * @param enums the array of enumerations.
46 * @return the enumeration over the array of enumerations.
47 */
48 public static Enumeration fromCompound(Enumeration[] enums) {
49 return new CompoundEnumeration(enums);
50 }
51
52 }
53
54
55 /***
56 * Convenient enumeration over an array of objects.
57 */
58 class ArrayEnumeration implements Enumeration {
59
60 /*** object array */
61 private Object[] array;
62
63 /*** current index */
64 private int pos;
65
66 /***
67 * Initialize a new enumeration that wraps an array.
68 * @param array the array of object to enumerate.
69 */
70 public ArrayEnumeration(Object[] array) {
71 this.array = array;
72 this.pos = 0;
73 }
74 /***
75 * Tests if this enumeration contains more elements.
76 *
77 * @return <code>true</code> if and only if this enumeration object
78 * contains at least one more element to provide;
79 * <code>false</code> otherwise.
80 */
81 public boolean hasMoreElements() {
82 return (pos < array.length);
83 }
84
85 /***
86 * Returns the next element of this enumeration if this enumeration
87 * object has at least one more element to provide.
88 *
89 * @return the next element of this enumeration.
90 * @throws NoSuchElementException if no more elements exist.
91 */
92 public Object nextElement() throws NoSuchElementException {
93 if (hasMoreElements()) {
94 Object o = array[pos];
95 pos++;
96 return o;
97 }
98 throw new NoSuchElementException();
99 }
100 }
101 /***
102 * Convenient enumeration over an array of enumeration. For example:
103 * <pre>
104 * Enumeration e1 = v1.elements();
105 * while (e1.hasMoreElements()) {
106 * // do something
107 * }
108 * Enumeration e2 = v2.elements();
109 * while (e2.hasMoreElements()) {
110 * // do the same thing
111 * }
112 * </pre>
113 * can be written as:
114 * <pre>
115 * Enumeration[] enums = { v1.elements(), v2.elements() };
116 * Enumeration e = Enumerations.fromCompound(enums);
117 * while (e.hasMoreElements()) {
118 * // do something
119 * }
120 * </pre>
121 * Note that the enumeration will skip null elements in the array. The following is
122 * thus possible:
123 * <pre>
124 * Enumeration[] enums = { v1.elements(), null, v2.elements() }; // a null enumeration in the array
125 * Enumeration e = Enumerations.fromCompound(enums);
126 * while (e.hasMoreElements()) {
127 * // do something
128 * }
129 * </pre>
130 */
131 class CompoundEnumeration implements Enumeration {
132
133 /*** enumeration array */
134 private Enumeration[] enumArray;
135
136 /*** index in the enums array */
137 private int index = 0;
138
139 public CompoundEnumeration(Enumeration[] enumarray) {
140 this.enumArray = enumarray;
141 }
142
143 /***
144 * Tests if this enumeration contains more elements.
145 *
146 * @return <code>true</code> if and only if this enumeration object
147 * contains at least one more element to provide;
148 * <code>false</code> otherwise.
149 */
150 public boolean hasMoreElements() {
151 while (index < enumArray.length) {
152 if (enumArray[index] != null && enumArray[index].hasMoreElements()) {
153 return true;
154 }
155 index++;
156 }
157 return false;
158 }
159
160 /***
161 * Returns the next element of this enumeration if this enumeration
162 * object has at least one more element to provide.
163 *
164 * @return the next element of this enumeration.
165 * @throws NoSuchElementException if no more elements exist.
166 */
167 public Object nextElement() throws NoSuchElementException {
168 if (hasMoreElements()) {
169 return enumArray[index].nextElement();
170 }
171 throw new NoSuchElementException();
172 }
173 }
174
175