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