@Override public T map( T t ) { counter.map( t ); if( counter.count % interval == 0 ) { logProgress(); } return t; }
@Override public <SenderThrowableType extends Throwable> void receiveFrom( Sender<? extends T, SenderThrowableType> sender ) throws ReceiverThrowableType, SenderThrowableType { sender.sendTo( receiver ); } };
@Test public void testIOString() throws Exception { StringBuilder sb = new StringBuilder(); iterable( byteCollection() ).transferTo( map( valueSerialization.serialize(), text( sb ) ) ); String output = sb.toString(); List<Byte> list = new ArrayList<Byte>(); text( output ).transferTo( map( valueSerialization.deserialize( Byte.class ), collection( list ) ) ); assertEquals( byteCollection(), list ); }
@Override public <ReceiverThrowableType extends Throwable> void transferTo( Output<? super String, ReceiverThrowableType> output ) throws RuntimeException, ReceiverThrowableType { output.receiveFrom( new Sender<String, RuntimeException>() { @Override public <Receiver2ThrowableType extends Throwable> void sendTo( Receiver<? super String, Receiver2ThrowableType> receiver ) throws Receiver2ThrowableType, RuntimeException { Scanner scanner = new Scanner( source ); while( scanner.hasNextLine() ) { receiver.receive( scanner.nextLine() ); } } } ); } };
public ProgressLog( long interval ) { this.interval = interval; counter = new Counter<>(); }
/** * Read lines from a UTF-8 encoded textfile. * * If the filename ends with .gz, then the data is automatically unzipped when read. * * @param source textfile with lines separated by \n character * * @return Input that provides lines from the textfiles as strings */ public static Input<String, IOException> text( final File source ) // END SNIPPET: method { return text( source, "UTF-8" ); }
/** * Write lines to a text file with UTF-8 encoding. Separate each line with a newline ("\n" character). If the writing or sending fails, * the file is deleted. * <p> * If the filename ends with .gz, then the data is automatically GZipped. * </p> * @param file the file to save the text to * * @return an Output for storing text in a file */ public static Output<String, IOException> text( final File file ) // END SNIPPET: method { return text( file, "UTF-8" ); }
protected void logProgress() { if( log != null ) { log.map( counter.count + "" ); } } }
/** * Do nothing. Use this if you have all logic in filters and/or specifications * * @param <T> The item type. * * @return An Output instance that ignores all data. */ public static <T> Output<T, RuntimeException> noop() // END SNIPPET: method { return withReceiver( new Receiver<T, RuntimeException>() { @Override public void receive( T item ) throws RuntimeException { // Do nothing } } ); }
@Override public <SenderThrowableType extends Throwable> void receiveFrom( Sender<? extends String, SenderThrowableType> sender ) throws IOException, SenderThrowableType { sender.sendTo( new Receiver<String, IOException>() { @Override public void receive( String item ) throws IOException { builder.append( item ).append( "\n" ); } } ); }
@Override public <ReceiverThrowableType extends Throwable> void transferTo( Output<? super String, ReceiverThrowableType> output ) throws RuntimeException, ReceiverThrowableType { output.receiveFrom( new Sender<String, RuntimeException>() { @Override public <Receiver2ThrowableType extends Throwable> void sendTo( Receiver<? super String, Receiver2ThrowableType> receiver ) throws Receiver2ThrowableType, RuntimeException { Scanner scanner = new Scanner( source ); while( scanner.hasNextLine() ) { receiver.receive( scanner.nextLine() ); } } } ); } };
@Override public <SenderThrowableType extends Throwable> void receiveFrom( Sender<?, SenderThrowableType> sender ) throws RuntimeException, SenderThrowableType { sender.sendTo( new Receiver<Object, RuntimeException>() { @Override public void receive( Object item ) { System.err.println( item ); } } ); } };
@Override public <ReceiverThrowableType extends Throwable> void transferTo( Output<? super T, ReceiverThrowableType> output ) throws RuntimeException, ReceiverThrowableType { output.receiveFrom( new Sender<T, RuntimeException>() { @Override public <Receiver2ThrowableType extends Throwable> void sendTo( Receiver<? super T, Receiver2ThrowableType> receiver ) throws Receiver2ThrowableType, RuntimeException { for( T item : iterable ) { receiver.receive( item ); } } } ); } };
@Override public <SenderThrowableType extends Throwable> void receiveFrom( Sender<? extends String, SenderThrowableType> sender ) throws IOException, SenderThrowableType { sender.sendTo( new Receiver<String, IOException>() { @Override public void receive( String item ) throws IOException { writer.append( item ).append( "\n" ); } } ); }
@Override public <SenderThrowableType extends Throwable> void receiveFrom( Sender<?, SenderThrowableType> sender ) throws RuntimeException, SenderThrowableType { sender.sendTo( new Receiver<Object, RuntimeException>() { @Override public void receive( Object item ) { System.out.println( item ); } } ); } };
@Override public <SenderThrowableType extends Throwable> void receiveFrom( Sender<? extends T, SenderThrowableType> sender ) throws RuntimeException, SenderThrowableType { sender.sendTo( new Receiver<T, RuntimeException>() { @Override public void receive( T item ) throws RuntimeException { collection.add( item ); } } ); } };
@Override public <SenderThrowableType extends Throwable> void receiveFrom(Sender<? extends TransactionApplicationEvents, SenderThrowableType> sender) throws ApplicationEventReplayException, SenderThrowableType { sender.sendTo( new Receiver<TransactionApplicationEvents, ApplicationEventReplayException>() { @Override public void receive( TransactionApplicationEvents item ) throws ApplicationEventReplayException { for (ApplicationEvent applicationEvent : events( item )) { if (specification.satisfiedBy( applicationEvent )) player.playEvent( applicationEvent, eventHandler ); } } } ); } };
@Override public <SenderThrowableType extends Throwable> void receiveFrom( Sender<? extends DomainEventValue, SenderThrowableType> sender ) throws T, SenderThrowableType { sender.sendTo( new Receiver<DomainEventValue, T>() { @Override public void receive( DomainEventValue item ) throws T { for( Map.Entry<Specification<DomainEventValue>, Receiver<DomainEventValue, T>> specificationReceiverEntry : routeEvent.entrySet() ) { if( specificationReceiverEntry.getKey().satisfiedBy( item ) ) { specificationReceiverEntry.getValue().receive( item ); return; } } // No match, use default defaultReceiver.receive( item ); } } ); }
@Override public <SenderThrowableType extends Throwable> void receiveFrom( Sender<? extends EntityState, SenderThrowableType> sender ) throws RuntimeException, SenderThrowableType { sender.sendTo( this ); reindexState(); }