Clover coverage report - JBind Project
Coverage timestamp: Fr Mai 28 2004 11:17:36 CEST
file stats: LOC: 104   Methods: 5
NCLOC: 41   Classes: 2
This license of Clover is provided to support the development of JBind only. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover.
 
 Source file Conditionals Statements Methods TOTAL
FilterIterator.java 83,3% 87,5% 80% 85,2%
 1   
 /*
 2   
  * JBind
 3   
  *
 4   
  * Copyright (c) by Stefan Wachter. All rights reserved.
 5   
  *
 6   
  * Usage, modification, and redistribution is subject to license terms that are
 7   
  * available at 'http://www.jbind.org'. The JBind license is like the
 8   
  * 'Apache Software License V 1.1'.
 9   
  */
 10   
 /*
 11   
  * FilterIterator.java
 12   
  */
 13   
 
 14   
 package org.jbind.util.collection;
 15   
 
 16   
 import java.util.Iterator;
 17   
 import java.util.NoSuchElementException;
 18   
 
 19   
 /**
 20   
  * Filters the objects of an iterator such that all returned objects obey
 21   
  * a condition.
 22   
  * <p>
 23   
  * Usage example: Filter an iteration of strings such that only strings with
 24   
  * a lenght greater than 3 are returned:
 25   
  * <p>
 26   
  * <code>
 27   
  *              for (Iterator i = new FilterIterator(a.iterator(),
 28   
  *                      new FilterIterator.ICondition() {
 29   
  *                              public boolean evaluate(Object anObject) {
 30   
  *                                      return ((String)anObject).length() > 3;
 31   
  *                              }
 32   
  *                      }); i.hasNext(); )
 33   
  *              {
 34   
  *                      System.out.println(i.next());
 35   
  *              }
 36   
  * </code>
 37   
  *
 38   
  * @author  Wachter
 39   
  */
 40   
 public class FilterIterator implements Iterator {
 41   
 
 42   
   private Iterator iterator = null;
 43   
   private ICondition condition = null;
 44   
 
 45   
   private boolean hasNext = false;
 46   
   private Object next = null;
 47   
 
 48   
   /**
 49   
    * Interface which must be implemented in order to decide wheter objects
 50   
    * should be included in the iteration or not.
 51   
    */
 52   
   public static interface ICondition {
 53   
     /**
 54   
      * @return Returns <code>true</code> if the object is to be included
 55   
      * in the iteration and <code>false</code> otherwise.
 56   
      */
 57   
     boolean evaluate(Object anObject);
 58   
   }
 59   
 
 60   
   /**
 61   
    * Constructs a FilterIterator, i.e.&nbsp; an iterator which returns only
 62   
    * objects which obey a condition.
 63   
    *
 64   
    * @param anIterator The iterator that should be filtered.
 65   
    * @param aCondition The condition to be checked.
 66   
    */
 67  987
   public FilterIterator(Iterator anIterator, ICondition aCondition) {
 68  987
     iterator = anIterator;
 69  987
     condition = aCondition;
 70  987
     advance();
 71   
   }
 72   
 
 73   
   /**
 74   
    * Returns the current element and advances the iterator.
 75   
    */
 76  4520
   private Object advance() {
 77  4520
     Object currentElement = next;
 78  4520
     hasNext = false;
 79  4520
     while (iterator.hasNext()) {
 80  4640
       next = iterator.next();
 81  4640
       if (condition.evaluate(next)) {
 82  3533
         hasNext = true;
 83  3533
         break;
 84   
       }
 85   
     }
 86  4520
     return currentElement;
 87   
   }
 88   
 
 89  3533
   public Object next() {
 90  3533
     if (!hasNext) {
 91  0
       throw new NoSuchElementException();
 92   
     }
 93  3533
     return advance();
 94   
   }
 95   
 
 96  4512
   public boolean hasNext() {
 97  4512
     return hasNext;
 98   
   }
 99   
 
 100  0
   public void remove() {
 101  0
     throw new RuntimeException("UnsupportedOperationException");
 102   
   }
 103   
 }
 104