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