1 18 package org.apache.tools.ant.taskdefs.optional.junit; 19 20 import java.util.Enumeration ; 21 import java.util.NoSuchElementException ; 22 23 29 public final class Enumerations { 30 31 private Enumerations() { 32 } 33 34 39 public static Enumeration fromArray(Object [] array) { 40 return new ArrayEnumeration(array); 41 } 42 43 50 public static Enumeration fromCompound(Enumeration [] enums) { 51 return new CompoundEnumeration(enums); 52 } 53 54 } 55 56 57 60 class ArrayEnumeration implements Enumeration { 61 62 63 private Object [] array; 64 65 66 private int pos; 67 68 72 public ArrayEnumeration(Object [] array) { 73 this.array = array; 74 this.pos = 0; 75 } 76 83 public boolean hasMoreElements() { 84 return (pos < array.length); 85 } 86 87 94 public Object nextElement() throws NoSuchElementException { 95 if (hasMoreElements()) { 96 Object o = array[pos]; 97 pos++; 98 return o; 99 } 100 throw new NoSuchElementException (); 101 } 102 } 103 133 class CompoundEnumeration implements Enumeration { 134 135 136 private Enumeration [] enumArray; 137 138 139 private int index = 0; 140 141 public CompoundEnumeration(Enumeration [] enumarray) { 142 this.enumArray = enumarray; 143 } 144 145 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 169 public Object nextElement() throws NoSuchElementException { 170 if (hasMoreElements()) { 171 return enumArray[index].nextElement(); 172 } 173 throw new NoSuchElementException (); 174 } 175 } 176 177 178 | Popular Tags |