Coverage Report - jaggregate.CollectionStream
 
Classes in this File Line Coverage Branch Coverage Complexity
CollectionStream
100%
35/35
100%
4/4
0
 
 1  
 /*
 2  
  Copyright 2004-2008 Paul R. Holser, Jr.  All rights reserved.
 3  
  Licensed under the Academic Free License version 3.0
 4  
  */
 5  
 
 6  
 package jaggregate;
 7  
 
 8  
 import jaggregate.internal.SequenceIndexOutOfBoundsException;
 9  
 import static jaggregate.internal.ArgumentChecks.*;
 10  
 
 11  
 /**
 12  
  * Represents a stream that has a {@link ReadOnlySequence} as its <dfn>stream backing
 13  
  * store</dfn>.
 14  
  *
 15  
  * @param <E> the type of elements in the stream
 16  
  * @param <S> the type of the stream's backing store
 17  
  *
 18  
  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
 19  
  * @version $Id: CollectionStream.java,v 1.5 2008/10/03 19:01:23 pholser Exp $
 20  
  */
 21  
 public abstract class CollectionStream<E, S extends ReadOnlySequence<E>>
 22  
     implements Stream<E, S> {
 23  
 
 24  
     private static final String POSITIONING = "positioning";
 25  
 
 26  
     private S backingStore;
 27  
     private int position;
 28  
     private int maxPosition;
 29  
 
 30  
     /**
 31  
      * Creates a new stream over a given backing sequence.
 32  
      *
 33  
      * @param backingStore the sequence to stream over
 34  
      * @throws NullPointerException if {@code backingStore} is {@code null}
 35  
      */
 36  289
     protected CollectionStream( S backingStore ) {
 37  289
         ensureNotNull( backingStore, "backing store" );
 38  
 
 39  287
         this.backingStore = backingStore;
 40  287
         position = 0;
 41  287
         maxPosition = backingStore.size();
 42  287
     }
 43  
 
 44  
     /**
 45  
      * {@inheritDoc}
 46  
      */
 47  
     public boolean atEnd() {
 48  413
         ensureOpen();
 49  411
         return position() >= maxPosition();
 50  
     }
 51  
 
 52  
     /**
 53  
      * {@inheritDoc}
 54  
      * <p/>
 55  
      * Answers this stream's backing store.
 56  
      */
 57  
     public S contents() {
 58  372
         ensureOpen();
 59  370
         return backingStore;
 60  
     }
 61  
 
 62  
     /**
 63  
      * {@inheritDoc}
 64  
      */
 65  
     public void close() {
 66  35
         backingStore = null;
 67  35
     }
 68  
 
 69  
     /**
 70  
      * {@inheritDoc}
 71  
      */
 72  
     public boolean isEmpty() {
 73  95
         ensureOpen();
 74  95
         return contents().isEmpty();
 75  
     }
 76  
 
 77  
     /**
 78  
      * {@inheritDoc}
 79  
      */
 80  
     public int position() {
 81  852
         ensureOpen();
 82  850
         return position;
 83  
     }
 84  
 
 85  
     /**
 86  
      * {@inheritDoc}
 87  
      */
 88  
     public void position( int amount ) {
 89  97
         ensureOpen();
 90  
 
 91  95
         if ( amount < 0 )
 92  9
             throw new SequenceIndexOutOfBoundsException( POSITIONING, amount );
 93  
 
 94  86
         if ( !isEmpty() && amount >= maxPosition() )
 95  12
             throw new SequenceIndexOutOfBoundsException( POSITIONING, amount );
 96  
 
 97  74
         position = Math.min( amount, maxPosition() );
 98  74
     }
 99  
 
 100  
     /**
 101  
      * {@inheritDoc}
 102  
      */
 103  
     public void reset() {
 104  17
         ensureOpen();
 105  15
         position = 0;
 106  15
     }
 107  
 
 108  
     /**
 109  
      * {@inheritDoc}
 110  
      */
 111  
     public void setToEnd() {
 112  35
         ensureOpen();
 113  33
         position = maxPosition;
 114  33
     }
 115  
 
 116  
     /**
 117  
      * Answers the position beyond which this stream will not stream any longer.
 118  
      *
 119  
      * @return this stream's maximum position
 120  
      */
 121  
     protected final int maxPosition() {
 122  602
         return maxPosition;
 123  
     }
 124  
 
 125  
     /**
 126  
      * Increments this stream's position by one.  Guaranteed not to overrun the maximum.
 127  
      */
 128  
     protected final void advance() {
 129  194
         ++position;
 130  194
     }
 131  
 
 132  
     /**
 133  
      * Checks whether a caller has previously {@link #close() closed} this stream.
 134  
      *
 135  
      * @throws IllegalStateException if this stream has been previously closed
 136  
      */
 137  
     protected final void ensureOpen() {
 138  2291
         if ( backingStore == null )
 139  22
             throw new IllegalStateException( "stream is closed" );
 140  2269
     }
 141  
 }