| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| WriteStream |
|
| 0.0;0 | ||||
| WriteStream$1 |
|
| 0.0;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 | /** | |
| 11 | * Represents a stream that has a positionable sequence of values to which new values may | |
| 12 | * be written. The initial sequence values are provided by a sequenced collection that | |
| 13 | * serves as the <dfn>stream backing store</dfn>. | |
| 14 | * | |
| 15 | * @param <E> a restriction on the types of elements being streamed over | |
| 16 | * | |
| 17 | * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a> | |
| 18 | * @version $Id: WriteStream.java,v 1.6 2008/10/03 19:01:23 pholser Exp $ | |
| 19 | */ | |
| 20 | public class WriteStream<E> extends CollectionStream<E, Sequence<E>> | |
| 21 | implements PuttableStream<E> { | |
| 22 | ||
| 23 | /** | |
| 24 | * Creates a new stream over the given backing sequence. | |
| 25 | * | |
| 26 | * @param backingStore the backing sequence to stream over | |
| 27 | * @throws NullPointerException if {@code backingStore} is {@code null} | |
| 28 | */ | |
| 29 | public WriteStream( Sequence<E> backingStore ) { | |
| 30 | 83 | super( backingStore ); |
| 31 | 82 | } |
| 32 | ||
| 33 | /** | |
| 34 | * Creates a new stream over the given backing sequence. | |
| 35 | * | |
| 36 | * @param <T> the type of elements in the backing sequence | |
| 37 | * @param backingStore the backing sequence to stream over | |
| 38 | * @return the new stream | |
| 39 | * @throws NullPointerException if {@code backingStore} is {@code null} | |
| 40 | */ | |
| 41 | public static <T> WriteStream<T> writeStreamOver( Sequence<T> backingStore ) { | |
| 42 | 83 | return new WriteStream<T>( backingStore ); |
| 43 | } | |
| 44 | ||
| 45 | /** | |
| 46 | * {@inheritDoc} | |
| 47 | */ | |
| 48 | public void nextPut( E newElement ) { | |
| 49 | 67 | ensureOpen(); |
| 50 | ||
| 51 | 66 | if ( atEnd() ) |
| 52 | 8 | throw new NoSuchElementException( "can't put past end of stream" ); |
| 53 | ||
| 54 | 58 | contents().putAt( position(), newElement ); |
| 55 | 58 | advance(); |
| 56 | 58 | } |
| 57 | ||
| 58 | /** | |
| 59 | * {@inheritDoc} | |
| 60 | */ | |
| 61 | public void nextPutAll( Collection<? extends E> newElements ) { | |
| 62 | 32 | ensureOpen(); |
| 63 | ||
| 64 | 31 | newElements.forEachDo( new UnaryFunctor<E, Void>() { |
| 65 | 90 | public Void evaluate( E argument ) { |
| 66 | 59 | nextPut( argument ); |
| 67 | 52 | return null; |
| 68 | } | |
| 69 | } ); | |
| 70 | 23 | } |
| 71 | } |