Coverage Report - jaggregate.ReadStream
 
Classes in this File Line Coverage Branch Coverage Complexity
ReadStream
98%
51/52
100%
12/12
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 java.util.NoSuchElementException;
 9  
 
 10  
 import static jaggregate.OrderedCollection.*;
 11  
 import static jaggregate.internal.ArgumentChecks.*;
 12  
 
 13  
 /**
 14  
  * Represents a stream that has a positionable sequence of values that can be read. The
 15  
  * sequence values are provided by a sequenced collection that serves as the
 16  
  * <dfn>stream backing store</dfn>.
 17  
  * <p/>
 18  
  * Note that there is nothing to stop a caller to continue to manipulate the sequence
 19  
  * being streamed over via another reference that allows such manipulations: adding to
 20  
  * the sequence, replacing elements in the sequence, and so forth.  However, the stream
 21  
  * will still stop reading elements when it reaches the end of the sequence as it was
 22  
  * set up at the time the stream was created.
 23  
  *
 24  
  * @param <E> a restriction on the types of elements being streamed over
 25  
  *
 26  
  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
 27  
  * @version $Id: ReadStream.java,v 1.5 2008/10/03 19:01:23 pholser Exp $
 28  
  */
 29  0
 public class ReadStream<E> extends CollectionStream<E, ReadOnlySequence<E>>
 30  
     implements GettableStream<E, ReadOnlySequence<E>> {
 31  
 
 32  
     /**
 33  
      * Creates a new stream over the given backing sequence.
 34  
      *
 35  
      * @param backingStore the backing sequence to stream over
 36  
      * @throws NullPointerException if {@code backingStore} is {@code null}
 37  
      */
 38  
     public ReadStream( ReadOnlySequence<E> backingStore ) {
 39  206
         super( backingStore );
 40  205
     }
 41  
 
 42  
     /**
 43  
      * Creates a new stream over the given backing sequence.
 44  
      *
 45  
      * @param <T> the type of elements in the backing sequence
 46  
      * @param backingStore the backing sequence to stream over
 47  
      * @return the new stream
 48  
      * @throws NullPointerException if {@code backingStore} is {@code null}
 49  
      */
 50  
     public static <T> ReadStream<T> readStreamOver( ReadOnlySequence<T> backingStore ) {
 51  206
         return new ReadStream<T>( backingStore );
 52  
     }
 53  
 
 54  
     /**
 55  
      * {@inheritDoc}
 56  
      */
 57  
     public <R> void forEachDo( UnaryFunctor<? super E, ? extends R> operation ) {
 58  16
         ensureOpen();
 59  
 
 60  33
         while ( !atEnd() )
 61  19
             operation.evaluate( next() );
 62  14
     }
 63  
 
 64  
     /**
 65  
      * {@inheritDoc}
 66  
      */
 67  
     public E next() {
 68  143
         ensureOpen();
 69  
 
 70  142
         if ( atEnd() )
 71  6
             throw new NoSuchElementException( "no more elements in the stream" );
 72  
 
 73  136
         E result = contents().at( position() );
 74  136
         advance();
 75  
 
 76  136
         return result;
 77  
     }
 78  
 
 79  
     /**
 80  
      * {@inheritDoc}
 81  
      */
 82  
     public OrderedCollection<E> next( int amount ) {
 83  24
         ensureOpen();
 84  
 
 85  23
         if ( amount < 0 )
 86  1
             throw new IllegalArgumentException( NEGATIVE_AMOUNT + amount );
 87  
 
 88  22
         int numberOfNextElements = maxPosition() - position();
 89  
 
 90  22
         if ( amount > numberOfNextElements )
 91  9
             throw new IllegalArgumentException(
 92  
                 "there are not "
 93  
                 + amount
 94  
                 + " elements left in the stream, only "
 95  
                 + numberOfNextElements );
 96  
 
 97  13
         OrderedCollection<E> nextElements = emptyOrderedCollection();
 98  26
         for ( int i = 0; i < amount; ++i )
 99  13
             nextElements.add( next() );
 100  
 
 101  13
         return nextElements;
 102  
     }
 103  
 
 104  
     /**
 105  
      * {@inheritDoc}
 106  
      */
 107  
     public boolean isNextMatchFor( E target ) {
 108  12
         ensureOpen();
 109  
 
 110  11
         return Objects.areEqual( target, next() );
 111  
     }
 112  
 
 113  
     /**
 114  
      * {@inheritDoc}
 115  
      */
 116  
     public E peek() {
 117  35
         ensureOpen();
 118  
 
 119  34
         return atEnd() ? null : contents().at( position() );
 120  
     }
 121  
 
 122  
     /**
 123  
      * {@inheritDoc}
 124  
      */
 125  
     public boolean isPeekMatchFor( E target ) {
 126  23
         ensureOpen();
 127  
 
 128  22
         return Objects.areEqual( target, peek() );
 129  
     }
 130  
 
 131  
     /**
 132  
      * {@inheritDoc}
 133  
      */
 134  
     public void skip( int amount ) {
 135  15
         ensureOpen();
 136  
 
 137  14
         if ( amount < 0 )
 138  1
             throw new IllegalArgumentException( NEGATIVE_AMOUNT + amount );
 139  
 
 140  13
         int newPosition = position() + amount;
 141  13
         if ( newPosition >= maxPosition() )
 142  9
             setToEnd();
 143  
         else
 144  4
             position( newPosition );
 145  13
     }
 146  
 
 147  
     /**
 148  
      * {@inheritDoc}
 149  
      */
 150  
     public boolean skipTo( E target ) {
 151  15
         ensureOpen();
 152  
 
 153  32
         while ( !atEnd() ) {
 154  26
             if ( Objects.areEqual( target, next() ) )
 155  8
                 return true;
 156  
         }
 157  
 
 158  6
         return false;
 159  
     }
 160  
 
 161  
     /**
 162  
      * {@inheritDoc}
 163  
      */
 164  
     public OrderedCollection<E> upTo( E target ) {
 165  28
         ensureOpen();
 166  
 
 167  28
         OrderedCollection<E> results = emptyOrderedCollection();
 168  
 
 169  66
         while ( !atEnd() ) {
 170  56
             E nextItem = next();
 171  
 
 172  56
             if ( Objects.areEqual( target, nextItem ) )
 173  18
                 return results;
 174  
 
 175  38
             results.add( nextItem );
 176  38
         }
 177  
 
 178  10
         return results;
 179  
     }
 180  
 }