Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 217   Methods: 9
NCLOC: 54   Classes: 3
 
 Source file Conditionals Statements Methods TOTAL
Enumerations.java 75% 89.5% 88.9% 86.1%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 package org.apache.tools.ant.taskdefs.optional.junit;
 55   
 
 56   
 import java.util.Enumeration;
 57   
 import java.util.NoSuchElementException;
 58   
 
 59   
 /**
 60   
  * A couple of methods related to enumerations that might be useful.
 61   
  * This class should probably disappear once the required JDK is set to 1.2
 62   
  * instead of 1.1.
 63   
  *
 64   
  * @author <a href="mailto:sbailliez@imediation.com">Stephane Bailliez</a>
 65   
  */
 66   
 public final class Enumerations {
 67   
         
 68  0
         private Enumerations(){
 69   
         }
 70   
 
 71   
         /**
 72   
          * creates an enumeration from an array of objects.
 73   
          * @param       array   the array of object to enumerate.
 74   
          * @return the enumeration over the array of objects.
 75   
          */
 76  1
         public static Enumeration fromArray(Object[] array){
 77  1
                 return new ArrayEnumeration(array);
 78   
         }
 79   
 
 80   
         /**
 81   
         * creates an enumeration from an array of enumeration. The created enumeration
 82   
         * will sequentially enumerate over all elements of each enumeration and skip
 83   
         * <tt>null</tt> enumeration elements in the array.
 84   
         * @param        enums   the array of enumerations.
 85   
         * @return the enumeration over the array of enumerations.
 86   
          */
 87  5
         public static Enumeration fromCompound(Enumeration[] enums){
 88  5
                 return new CompoundEnumeration(enums);
 89   
         }
 90   
 
 91   
 }
 92   
 
 93   
 
 94   
 /**
 95   
  * Convenient enumeration over an array of objects.
 96   
  * @author <a href="mailto:sbailliez@imediation.com">Stephane Bailliez</a>
 97   
  */
 98   
 class ArrayEnumeration implements Enumeration {
 99   
         
 100   
         /** object array */
 101   
         private Object[] array;
 102   
         
 103   
         /** current index */
 104   
         private int pos;
 105   
         
 106   
         /**
 107   
          * Initialize a new enumeration that wraps an array.
 108   
          * @param       array   the array of object to enumerate.
 109   
          */
 110  1
         public ArrayEnumeration(Object[] array){
 111  1
                 this.array = array;
 112  1
                 this.pos = 0;
 113   
         }
 114   
         /**
 115   
          * Tests if this enumeration contains more elements.
 116   
          *
 117   
          * @return  <code>true</code> if and only if this enumeration object
 118   
          *           contains at least one more element to provide;
 119   
          *          <code>false</code> otherwise.
 120   
          */
 121  409
         public boolean hasMoreElements() {
 122  409
                 return (pos < array.length);
 123   
         }
 124   
 
 125   
         /**
 126   
          * Returns the next element of this enumeration if this enumeration
 127   
          * object has at least one more element to provide.
 128   
          *
 129   
          * @return     the next element of this enumeration.
 130   
          * @throws  NoSuchElementException  if no more elements exist.
 131   
          */
 132  136
         public Object nextElement() throws NoSuchElementException {
 133  136
                 if (hasMoreElements()) {
 134  136
                         Object o = array[pos];
 135  136
                         pos++;
 136  136
                         return o;
 137   
                 }
 138  0
                 throw new NoSuchElementException();
 139   
         }
 140   
 }
 141   
 /**
 142   
  * Convenient enumeration over an array of enumeration. For example:
 143   
  * <pre>
 144   
  * Enumeration e1 = v1.elements();
 145   
  * while (e1.hasMoreElements()){
 146   
  *    // do something
 147   
  * }
 148   
  * Enumeration e2 = v2.elements();
 149   
  * while (e2.hasMoreElements()){
 150   
  *    // do the same thing
 151   
  * }
 152   
  * </pre>
 153   
  * can be written as:
 154   
  * <pre>
 155   
  * Enumeration[] enums = { v1.elements(), v2.elements() };
 156   
  * Enumeration e = Enumerations.fromCompound(enums);
 157   
  * while (e.hasMoreElements()){
 158   
  *    // do something
 159   
  * }
 160   
  * </pre>
 161   
  * Note that the enumeration will skip null elements in the array. The following is
 162   
  * thus possible:
 163   
  * <pre>
 164   
  * Enumeration[] enums = { v1.elements(), null, v2.elements() }; // a null enumeration in the array
 165   
  * Enumeration e = Enumerations.fromCompound(enums);
 166   
  * while (e.hasMoreElements()){
 167   
  *    // do something
 168   
  * }
 169   
  * </pre>
 170   
  * @author <a href="mailto:sbailliez@imediation.com">Stephane Bailliez</a>
 171   
  */
 172   
  class CompoundEnumeration implements Enumeration {
 173   
         
 174   
         /** enumeration array */
 175   
         private Enumeration[] enumArray;
 176   
         
 177   
         /** index in the enums array */
 178   
         private int index = 0;
 179   
 
 180  5
     public CompoundEnumeration(Enumeration[] enumarray) {
 181  5
                 this.enumArray = enumarray;
 182   
     }
 183   
 
 184   
         /**
 185   
          * Tests if this enumeration contains more elements.
 186   
          *
 187   
          * @return  <code>true</code> if and only if this enumeration object
 188   
          *           contains at least one more element to provide;
 189   
          *          <code>false</code> otherwise.
 190   
          */
 191  285
     public boolean hasMoreElements() {
 192  285
                 while (index < enumArray.length) {
 193  290
                         if (enumArray[index] != null && enumArray[index].hasMoreElements()) {
 194  280
                                 return true;
 195   
                         }
 196  10
                         index++;
 197   
                 }
 198  5
                 return false;
 199   
     }
 200   
 
 201   
         /**
 202   
          * Returns the next element of this enumeration if this enumeration
 203   
          * object has at least one more element to provide.
 204   
          *
 205   
          * @return     the next element of this enumeration.
 206   
          * @throws  NoSuchElementException  if no more elements exist.
 207   
          */
 208  140
     public Object nextElement() throws NoSuchElementException {
 209  140
                 if (hasMoreElements()) {
 210  140
                         return enumArray[index].nextElement();
 211   
                 }
 212  0
                 throw new NoSuchElementException();
 213   
     }
 214   
 }
 215   
 
 216   
 
 217