| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
package jaggregate; |
| 7 | |
|
| 8 | |
import jaggregate.internal.SequenceIndexOutOfBoundsException; |
| 9 | |
import static jaggregate.internal.ArgumentChecks.*; |
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 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 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 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 | |
|
| 46 | |
|
| 47 | |
public boolean atEnd() { |
| 48 | 413 | ensureOpen(); |
| 49 | 411 | return position() >= maxPosition(); |
| 50 | |
} |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
public S contents() { |
| 58 | 372 | ensureOpen(); |
| 59 | 370 | return backingStore; |
| 60 | |
} |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
public void close() { |
| 66 | 35 | backingStore = null; |
| 67 | 35 | } |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
public boolean isEmpty() { |
| 73 | 95 | ensureOpen(); |
| 74 | 95 | return contents().isEmpty(); |
| 75 | |
} |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
public int position() { |
| 81 | 852 | ensureOpen(); |
| 82 | 850 | return position; |
| 83 | |
} |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 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 | |
|
| 102 | |
|
| 103 | |
public void reset() { |
| 104 | 17 | ensureOpen(); |
| 105 | 15 | position = 0; |
| 106 | 15 | } |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
public void setToEnd() { |
| 112 | 35 | ensureOpen(); |
| 113 | 33 | position = maxPosition; |
| 114 | 33 | } |
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
protected final int maxPosition() { |
| 122 | 602 | return maxPosition; |
| 123 | |
} |
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
protected final void advance() { |
| 129 | 194 | ++position; |
| 130 | 194 | } |
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
protected final void ensureOpen() { |
| 138 | 2291 | if ( backingStore == null ) |
| 139 | 22 | throw new IllegalStateException( "stream is closed" ); |
| 140 | 2269 | } |
| 141 | |
} |