Coverage Report - org.apache.tapestry.util.SizeRestrictingIterator
 
Classes in this File Line Coverage Branch Coverage Complexity
SizeRestrictingIterator
0%
0/15
0%
0/4
1.167
 
 1  
 package org.apache.tapestry.util;
 2  
 
 3  
 import org.apache.hivemind.util.Defense;
 4  
 
 5  
 import java.util.Iterator;
 6  
 
 7  
 /**
 8  
  * <p>This class implements an {@link Iterator} which can only return a fixed
 9  
  * number of items.</p>
 10  
  *
 11  
  */
 12  
 public class SizeRestrictingIterator implements Iterator {
 13  
 
 14  
     private static final int DEFAULT_MAX_SIZE = 20;
 15  
 
 16  
     private final int _maxSize;
 17  
     private final Iterator _iterator;
 18  
     private int _currentIndex;
 19  
 
 20  
     /**
 21  
      * Constructs an Iterator which will return at most {@link #DEFAULT_MAX_SIZE} items.
 22  
      *
 23  
      * @param iterator
 24  
      *          The underlying iterator this object will defer to for actual
 25  
      *          iteration.
 26  
      */
 27  
     public SizeRestrictingIterator(Iterator iterator)
 28  
     {
 29  0
         this(iterator, DEFAULT_MAX_SIZE);
 30  0
     }
 31  
 
 32  
     /**
 33  
      * Constructs an Iterator which will return at most as many
 34  
      * items as defined by the user.
 35  
      *
 36  
      * @param iterator
 37  
      *          The underlying iterator this object will defer to for actual
 38  
      *          iteration.
 39  
      * @param maxSize
 40  
      *          How many items to return / filter the list by.
 41  
      */
 42  
     public SizeRestrictingIterator(Iterator iterator, int maxSize)
 43  0
     {
 44  0
         Defense.notNull(iterator, "Iterator source");
 45  
         
 46  0
         _iterator = iterator;
 47  0
         _maxSize = maxSize;
 48  
         
 49  0
         _currentIndex = 0;
 50  0
     }
 51  
     
 52  
     /**
 53  
      * {@inheritDoc}
 54  
      */
 55  
     public boolean hasNext()
 56  
     {
 57  0
         return _currentIndex < _maxSize && _iterator.hasNext();
 58  
     }
 59  
 
 60  
     /**
 61  
      * {@inheritDoc}
 62  
      */
 63  
     public Object next()
 64  
     {
 65  0
         _currentIndex++;
 66  0
         return _iterator.next();
 67  
     }
 68  
 
 69  
     /**
 70  
      * {@inheritDoc}
 71  
      */
 72  
     public void remove()
 73  
     {
 74  0
         _currentIndex--;
 75  0
         _iterator.remove();
 76  0
     }
 77  
     
 78  
     public String toString()
 79  
     {
 80  0
         return "SizeRestrictingIterator[" +
 81  
                "_maxSize=" + _maxSize +
 82  
                '\n' +
 83  
                ", _current=" + _currentIndex +
 84  
                '\n' +
 85  
                ", _iterator=" + _iterator +
 86  
                '\n' +
 87  
                ']';
 88  
     }
 89  
 }