KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > junit > Enumerations


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.taskdefs.optional.junit;
19
20 import java.util.Enumeration JavaDoc;
21 import java.util.NoSuchElementException JavaDoc;
22
23 /**
24  * A couple of methods related to enumerations that might be useful.
25  * This class should probably disappear once the required JDK is set to 1.2
26  * instead of 1.1.
27  *
28  */

29 public final class Enumerations {
30
31         private Enumerations() {
32         }
33
34         /**
35          * creates an enumeration from an array of objects.
36          * @param array the array of object to enumerate.
37          * @return the enumeration over the array of objects.
38          */

39         public static Enumeration JavaDoc fromArray(Object JavaDoc[] array) {
40                 return new ArrayEnumeration(array);
41         }
42
43         /**
44         * creates an enumeration from an array of enumeration. The created enumeration
45         * will sequentially enumerate over all elements of each enumeration and skip
46         * <tt>null</tt> enumeration elements in the array.
47         * @param enums the array of enumerations.
48         * @return the enumeration over the array of enumerations.
49          */

50         public static Enumeration JavaDoc fromCompound(Enumeration JavaDoc[] enums) {
51                 return new CompoundEnumeration(enums);
52         }
53
54 }
55
56
57 /**
58  * Convenient enumeration over an array of objects.
59  */

60 class ArrayEnumeration implements Enumeration JavaDoc {
61
62         /** object array */
63         private Object JavaDoc[] array;
64
65         /** current index */
66         private int pos;
67
68         /**
69          * Initialize a new enumeration that wraps an array.
70          * @param array the array of object to enumerate.
71          */

72         public ArrayEnumeration(Object JavaDoc[] array) {
73                 this.array = array;
74                 this.pos = 0;
75         }
76         /**
77          * Tests if this enumeration contains more elements.
78          *
79          * @return <code>true</code> if and only if this enumeration object
80          * contains at least one more element to provide;
81          * <code>false</code> otherwise.
82          */

83         public boolean hasMoreElements() {
84                 return (pos < array.length);
85         }
86
87         /**
88          * Returns the next element of this enumeration if this enumeration
89          * object has at least one more element to provide.
90          *
91          * @return the next element of this enumeration.
92          * @throws NoSuchElementException if no more elements exist.
93          */

94         public Object JavaDoc nextElement() throws NoSuchElementException JavaDoc {
95                 if (hasMoreElements()) {
96                         Object JavaDoc o = array[pos];
97                         pos++;
98                         return o;
99                 }
100                 throw new NoSuchElementException JavaDoc();
101         }
102 }
103 /**
104  * Convenient enumeration over an array of enumeration. For example:
105  * <pre>
106  * Enumeration e1 = v1.elements();
107  * while (e1.hasMoreElements()) {
108  * // do something
109  * }
110  * Enumeration e2 = v2.elements();
111  * while (e2.hasMoreElements()) {
112  * // do the same thing
113  * }
114  * </pre>
115  * can be written as:
116  * <pre>
117  * Enumeration[] enums = { v1.elements(), v2.elements() };
118  * Enumeration e = Enumerations.fromCompound(enums);
119  * while (e.hasMoreElements()) {
120  * // do something
121  * }
122  * </pre>
123  * Note that the enumeration will skip null elements in the array. The following is
124  * thus possible:
125  * <pre>
126  * Enumeration[] enums = { v1.elements(), null, v2.elements() }; // a null enumeration in the array
127  * Enumeration e = Enumerations.fromCompound(enums);
128  * while (e.hasMoreElements()) {
129  * // do something
130  * }
131  * </pre>
132  */

133  class CompoundEnumeration implements Enumeration JavaDoc {
134
135         /** enumeration array */
136         private Enumeration JavaDoc[] enumArray;
137
138         /** index in the enums array */
139         private int index = 0;
140
141     public CompoundEnumeration(Enumeration JavaDoc[] enumarray) {
142                 this.enumArray = enumarray;
143     }
144
145         /**
146          * Tests if this enumeration contains more elements.
147          *
148          * @return <code>true</code> if and only if this enumeration object
149          * contains at least one more element to provide;
150          * <code>false</code> otherwise.
151          */

152     public boolean hasMoreElements() {
153                 while (index < enumArray.length) {
154                         if (enumArray[index] != null && enumArray[index].hasMoreElements()) {
155                                 return true;
156                         }
157                         index++;
158                 }
159                 return false;
160     }
161
162         /**
163          * Returns the next element of this enumeration if this enumeration
164          * object has at least one more element to provide.
165          *
166          * @return the next element of this enumeration.
167          * @throws NoSuchElementException if no more elements exist.
168          */

169     public Object JavaDoc nextElement() throws NoSuchElementException JavaDoc {
170                 if (hasMoreElements()) {
171                         return enumArray[index].nextElement();
172                 }
173                 throw new NoSuchElementException JavaDoc();
174     }
175 }
176
177
178
Popular Tags