001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs.optional.junit;
019:
020: import java.util.Enumeration;
021: import java.util.NoSuchElementException;
022:
023: /**
024: * A couple of methods related to enumerations that might be useful.
025: * This class should probably disappear once the required JDK is set to 1.2
026: * instead of 1.1.
027: *
028: */
029: public final class Enumerations {
030:
031: private Enumerations() {
032: }
033:
034: /**
035: * creates an enumeration from an array of objects.
036: * @param array the array of object to enumerate.
037: * @return the enumeration over the array of objects.
038: */
039: public static Enumeration fromArray(Object[] array) {
040: return new ArrayEnumeration(array);
041: }
042:
043: /**
044: * creates an enumeration from an array of enumeration. The created enumeration
045: * will sequentially enumerate over all elements of each enumeration and skip
046: * <tt>null</tt> enumeration elements in the array.
047: * @param enums the array of enumerations.
048: * @return the enumeration over the array of enumerations.
049: */
050: public static Enumeration fromCompound(Enumeration[] enums) {
051: return new CompoundEnumeration(enums);
052: }
053:
054: }
055:
056: /**
057: * Convenient enumeration over an array of objects.
058: */
059: class ArrayEnumeration implements Enumeration {
060:
061: /** object array */
062: private Object[] array;
063:
064: /** current index */
065: private int pos;
066:
067: /**
068: * Initialize a new enumeration that wraps an array.
069: * @param array the array of object to enumerate.
070: */
071: public ArrayEnumeration(Object[] array) {
072: this .array = array;
073: this .pos = 0;
074: }
075:
076: /**
077: * Tests if this enumeration contains more elements.
078: *
079: * @return <code>true</code> if and only if this enumeration object
080: * contains at least one more element to provide;
081: * <code>false</code> otherwise.
082: */
083: public boolean hasMoreElements() {
084: return (pos < array.length);
085: }
086:
087: /**
088: * Returns the next element of this enumeration if this enumeration
089: * object has at least one more element to provide.
090: *
091: * @return the next element of this enumeration.
092: * @throws NoSuchElementException if no more elements exist.
093: */
094: public Object nextElement() throws NoSuchElementException {
095: if (hasMoreElements()) {
096: Object o = array[pos];
097: pos++;
098: return o;
099: }
100: throw new NoSuchElementException();
101: }
102: }
103:
104: /**
105: * Convenient enumeration over an array of enumeration. For example:
106: * <pre>
107: * Enumeration e1 = v1.elements();
108: * while (e1.hasMoreElements()) {
109: * // do something
110: * }
111: * Enumeration e2 = v2.elements();
112: * while (e2.hasMoreElements()) {
113: * // do the same thing
114: * }
115: * </pre>
116: * can be written as:
117: * <pre>
118: * Enumeration[] enums = { v1.elements(), v2.elements() };
119: * Enumeration e = Enumerations.fromCompound(enums);
120: * while (e.hasMoreElements()) {
121: * // do something
122: * }
123: * </pre>
124: * Note that the enumeration will skip null elements in the array. The following is
125: * thus possible:
126: * <pre>
127: * Enumeration[] enums = { v1.elements(), null, v2.elements() }; // a null enumeration in the array
128: * Enumeration e = Enumerations.fromCompound(enums);
129: * while (e.hasMoreElements()) {
130: * // do something
131: * }
132: * </pre>
133: */
134: class CompoundEnumeration implements Enumeration {
135:
136: /** enumeration array */
137: private Enumeration[] enumArray;
138:
139: /** index in the enums array */
140: private int index = 0;
141:
142: public CompoundEnumeration(Enumeration[] enumarray) {
143: this .enumArray = enumarray;
144: }
145:
146: /**
147: * Tests if this enumeration contains more elements.
148: *
149: * @return <code>true</code> if and only if this enumeration object
150: * contains at least one more element to provide;
151: * <code>false</code> otherwise.
152: */
153: public boolean hasMoreElements() {
154: while (index < enumArray.length) {
155: if (enumArray[index] != null
156: && enumArray[index].hasMoreElements()) {
157: return true;
158: }
159: index++;
160: }
161: return false;
162: }
163:
164: /**
165: * Returns the next element of this enumeration if this enumeration
166: * object has at least one more element to provide.
167: *
168: * @return the next element of this enumeration.
169: * @throws NoSuchElementException if no more elements exist.
170: */
171: public Object nextElement() throws NoSuchElementException {
172: if (hasMoreElements()) {
173: return enumArray[index].nextElement();
174: }
175: throw new NoSuchElementException();
176: }
177: }
|