de.unkrig.commons.lang.protocol
Class ConsumerUtil

java.lang.Object
  extended by de.unkrig.commons.lang.protocol.ConsumerUtil

public final class ConsumerUtil
extends java.lang.Object

Various Consumer-related utility methods.


Method Summary
static
<T> Consumer<T>
addToCollection(java.util.Collection<T> drain)
           
static
<T,EX extends java.lang.RuntimeException>
Consumer<T>
asConsumer(ConsumerWhichThrows<? super T,? extends java.lang.RuntimeException> delegate)
          Converts a {@link ConsumerWhichThrows ConsumerWhichThrows
static
<T,EX extends java.lang.Exception>
ConsumerWhichThrows<T,EX>
asConsumerWhichThrows(Consumer<T> delegate)
          Converts a {@link Consumer Consumer
static java.io.Writer characterConsumerWriter(ConsumerWhichThrows<java.lang.Character,java.io.IOException> delegate)
           
static
<T> Producer<Consumer<T>>
combine(Consumer<T> target)
          The returned producer is a factory for consumers of T.
static
<T,EX extends java.lang.Exception>
Producer<ConsumerWhichThrows<T,EX>>
combineInOrder(ConsumerWhichThrows<T,EX> target)
          The returned producer is a factory for consumers of T.
static
<E extends java.lang.Exception>
ConsumerWhichThrows<java.lang.Character,E>
lineAggregator(ConsumerWhichThrows<java.lang.String,E> delegate)
           
static ConsumerWhichThrows<java.lang.String,java.io.IOException> lineConsumer(java.io.File file, boolean append)
           
static ConsumerWhichThrows<java.lang.String,java.io.IOException> lineConsumer(java.io.File file, java.lang.String charsetName, boolean append)
           
static Consumer<java.lang.String> lineConsumer(java.io.PrintStream printStream, boolean closeOnFinalize)
           
static ConsumerWhichThrows<java.lang.String,java.io.IOException> lineConsumer(java.io.Writer writer, boolean closeOnFinalize)
           
static
<T,EX extends java.lang.Exception>
java.util.List<ConsumerWhichThrows<T,EX>>
splice(int n, ConsumerWhichThrows<java.util.List<T>,EX> target)
          Returns a list of consumers of size n.
static
<T,EX extends java.lang.Exception>
ConsumerWhichThrows<T,EX>
tee(java.util.Collection<ConsumerWhichThrows<T,EX>> delegates)
           
static
<T,EX extends java.lang.Exception>
ConsumerWhichThrows<T,EX>
tee(ConsumerWhichThrows<T,EX> delegate1, ConsumerWhichThrows<T,EX> delegate2)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

tee

public static <T,EX extends java.lang.Exception> ConsumerWhichThrows<T,EX> tee(ConsumerWhichThrows<T,EX> delegate1,
                                                                               ConsumerWhichThrows<T,EX> delegate2)
Returns:
A consumer that forwards the subjects to both delegate1 and delegate2

tee

public static <T,EX extends java.lang.Exception> ConsumerWhichThrows<T,EX> tee(java.util.Collection<ConsumerWhichThrows<T,EX>> delegates)
Returns:
A consumer that forwards the subjects to all the delegates

asConsumerWhichThrows

public static <T,EX extends java.lang.Exception> ConsumerWhichThrows<T,EX> asConsumerWhichThrows(Consumer<T> delegate)
Converts a Consumer (the "source") into a ConsumerWhichThrows (the "target"), which is always possible.

Type Parameters:
T - The element type
EX - The target consumer's exception

asConsumer

public static <T,EX extends java.lang.RuntimeException> Consumer<T> asConsumer(ConsumerWhichThrows<? super T,? extends java.lang.RuntimeException> delegate)
Converts a ConsumerWhichThrows (the "source") into a Consumer (the "target"), which is always possible. of the source's element type.

Type Parameters:
T - The element type
EX - The source consumer's exception

characterConsumerWriter

public static java.io.Writer characterConsumerWriter(ConsumerWhichThrows<java.lang.Character,java.io.IOException> delegate)
Returns:
A Writer which forwards the characters to a ConsumerWhichThrows<Character, IOException>

lineAggregator

public static <E extends java.lang.Exception> ConsumerWhichThrows<java.lang.Character,E> lineAggregator(ConsumerWhichThrows<java.lang.String,E> delegate)
Returns:
A Consumer<Character> which aggregates characters to lines, which it passes to the delegate

lineConsumer

public static ConsumerWhichThrows<java.lang.String,java.io.IOException> lineConsumer(java.io.File file,
                                                                                     boolean append)
                                                                              throws java.io.IOException
Returns:
A Consumer that writes lines to the given file with the default character encoding
Throws:
java.io.IOException

lineConsumer

public static ConsumerWhichThrows<java.lang.String,java.io.IOException> lineConsumer(java.io.File file,
                                                                                     java.lang.String charsetName,
                                                                                     boolean append)
                                                                              throws java.io.IOException
Returns:
A Consumer that writes lines to the given file with the given encoding
Throws:
java.io.IOException

lineConsumer

public static ConsumerWhichThrows<java.lang.String,java.io.IOException> lineConsumer(java.io.Writer writer,
                                                                                     boolean closeOnFinalize)
Returns:
A Consumer that writes strings to the given Writer, augmented with a line separator

lineConsumer

public static Consumer<java.lang.String> lineConsumer(java.io.PrintStream printStream,
                                                      boolean closeOnFinalize)
Returns:
A Consumer that writes strings to the given Writer, augmented with a line separator

combine

public static <T> Producer<Consumer<T>> combine(Consumer<T> target)
The returned producer is a factory for consumers of T. The subjects sent to these consumers are forwarded immediately to the given target.

See Also:
combineInOrder(ConsumerWhichThrows)

combineInOrder

public static <T,EX extends java.lang.Exception> Producer<ConsumerWhichThrows<T,EX>> combineInOrder(ConsumerWhichThrows<T,EX> target)
The returned producer is a factory for consumers of T. The first subject sent to each of these consumers is passed to the given target in the order the consumers were created (not in the order in which the subjects were sent to the consumers).

See Also:
combine(Consumer)

splice

public static <T,EX extends java.lang.Exception> java.util.List<ConsumerWhichThrows<T,EX>> splice(int n,
                                                                                                  ConsumerWhichThrows<java.util.List<T>,EX> target)
Returns a list of consumers of size n. When all comsumers have consumed their first subject, then these subjects are passed to the target consumer; then again when all consumers have consumed their second subject, and so on.


addToCollection

public static <T> Consumer<T> addToCollection(java.util.Collection<T> drain)
Returns:
A consumer that adds the subjects it consumes to the given collection