Coverage Report - net.mtu.eggplant.xml.NodelistElementCollectionAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
NodelistElementCollectionAdapter
61%
13/21
60%
6/10
2
 
 1  
 package net.mtu.eggplant.xml;
 2  
 
 3  
 import java.util.Iterator;
 4  
 import java.util.LinkedList;
 5  
 import java.util.List;
 6  
 import java.util.NoSuchElementException;
 7  
 
 8  
 import org.w3c.dom.Element;
 9  
 import org.w3c.dom.NodeList;
 10  
 
 11  
 /**
 12  
  * An {@link Iterable} for {@link Element}s in a {@link NodeList}.
 13  
  */
 14  0
 public final class NodelistElementCollectionAdapter implements Iterator<Element>, Iterable<Element> {
 15  
 
 16  
   public Iterator<Element> iterator() {
 17  0
     return this;
 18  
   }
 19  
 
 20  
   /**
 21  
    * Walks the iterator and converts to a {@link LinkedList}.
 22  
    */
 23  
   public List<Element> asList() {
 24  0
     final List<Element> retval = new LinkedList<Element>();
 25  0
     for (final Element ele : this) {
 26  0
       retval.add(ele);
 27  
     }
 28  0
     return retval;
 29  
   }
 30  
 
 31  
   private final NodeList nodelist;
 32  
 
 33  
   /**
 34  
    * Index of next element to return.
 35  
    */
 36  
   private int nextIndex;
 37  
 
 38  2
   public NodelistElementCollectionAdapter(final NodeList nodelist) {
 39  2
     this.nodelist = nodelist;
 40  
     // initialize to -1 so that findNext starts looking at 0
 41  2
     this.nextIndex = -1;
 42  2
     findNextElement();
 43  2
   }
 44  
 
 45  
   public boolean hasNext() {
 46  5
     return nextIndex < nodelist.getLength();
 47  
   }
 48  
 
 49  
   public Element next() {
 50  2
     if (hasNext()) {
 51  2
       final Element retval = (Element) nodelist.item(nextIndex);
 52  2
       findNextElement();
 53  2
       return retval;
 54  
     } else {
 55  0
       throw new NoSuchElementException();
 56  
     }
 57  
   }
 58  
 
 59  
   public void remove() {
 60  0
     throw new UnsupportedOperationException();
 61  
   }
 62  
 
 63  
   /**
 64  
    * Starting at nextIndex+1, find the next {@link Element} in the
 65  
    * {@link NodeList}.
 66  
    */
 67  
   private void findNextElement() {
 68  
     do {
 69  4
       ++nextIndex;
 70  
     } while (nextIndex < nodelist.getLength()
 71  4
         && !(nodelist.item(nextIndex) instanceof Element));
 72  4
   }
 73  
 }