| 1 | |
|
| 2 | |
|
| 3 | |
|
| 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 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | 0 | public class ReadStream<E> extends CollectionStream<E, ReadOnlySequence<E>> |
| 30 | |
implements GettableStream<E, ReadOnlySequence<E>> { |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
public ReadStream( ReadOnlySequence<E> backingStore ) { |
| 39 | 206 | super( backingStore ); |
| 40 | 205 | } |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
public static <T> ReadStream<T> readStreamOver( ReadOnlySequence<T> backingStore ) { |
| 51 | 206 | return new ReadStream<T>( backingStore ); |
| 52 | |
} |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 106 | |
|
| 107 | |
public boolean isNextMatchFor( E target ) { |
| 108 | 12 | ensureOpen(); |
| 109 | |
|
| 110 | 11 | return Objects.areEqual( target, next() ); |
| 111 | |
} |
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
public E peek() { |
| 117 | 35 | ensureOpen(); |
| 118 | |
|
| 119 | 34 | return atEnd() ? null : contents().at( position() ); |
| 120 | |
} |
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
public boolean isPeekMatchFor( E target ) { |
| 126 | 23 | ensureOpen(); |
| 127 | |
|
| 128 | 22 | return Objects.areEqual( target, peek() ); |
| 129 | |
} |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
} |