public final class ConsumerUtil
extends java.lang.Object
Consumer-related utility methods.| Modifier and Type | Class and Description |
|---|---|
static interface |
ConsumerUtil.Produmer<PT,CT>
|
| Modifier and Type | Method and Description |
|---|---|
static <T> Consumer<T> |
addToCollection(java.util.Collection<T> drain) |
static <T,EX extends java.lang.RuntimeException> |
asConsumer(ConsumerWhichThrows<? super T,? extends java.lang.RuntimeException> source)
Converts the source into a
Consumer<T>. |
static <T,EX extends java.lang.Throwable> |
asConsumerWhichThrows(Consumer<? super T> source)
Deprecated.
Superseded by
widen2(ConsumerWhichThrows). |
static java.io.Writer |
characterConsumerWriter(ConsumerWhichThrows<? super java.lang.Character,java.io.IOException> delegate) |
static <T> Producer<Consumer<T>> |
combine(Consumer<? super T> target)
The returned producer is a factory for consumers of
T. |
static <T,EX extends java.lang.Throwable> |
combineInOrder(ConsumerWhichThrows<? super T,EX> target)
The returned producer is a factory for consumers of
T. |
static <T> Consumer<T> |
compress(Consumer<? super T> delegate,
Predicate<? super T> compressable,
T compressed)
Replaces sequences of compressable subjects with one compressed subject.
|
static <T> Consumer<T> |
compress(Consumer<T> delegate,
Predicate<T> compressable)
Forwards each subject it consumes to the given delegate, but only iff the
subject is not compressable.
|
static Consumer<java.lang.Long> |
compressExponentially(long initialLimit,
Consumer<? super java.lang.Long> delegate)
Creates and returns a
Consumer<Long> which forwards the quantity to the given delegate, but only if the quantity is equal to or greater than the limit, which starts with initialLimit and increases exponentially. |
static Consumer<java.lang.Number> |
cumulate(Consumer<? super java.lang.Long> delegate,
long initialCount)
Creates and returns a
Consumer which forwards the cumulated quantity to the given delegate. |
static <T,EX extends java.lang.Throwable> |
ignoreExceptions(java.lang.Class<EX> exceptionClass,
ConsumerWhichThrows<T,EX> delegate)
Wraps the delegate such that its declared exception is caught and ignored.
|
static <E extends java.lang.Exception> |
lineAggregator(ConsumerWhichThrows<? super java.lang.String,E> delegate)
Creates and returns a
Consumer<Character> which aggregates characters to lines, which it
passes to the 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> Consumer<T> |
nop() |
static <T,EX extends java.lang.Throwable> |
splice(int n,
ConsumerWhichThrows<? super java.util.List<T>,EX> target)
Returns a list of consumers of size
n. |
static <T> ConsumerUtil.Produmer<T,T> |
store()
The returned
ConsumerUtil.Produmer simply produces the last consumed subject, or null if no subject
has been consumed yet. |
static <T,EX extends java.lang.Throwable> |
tee(java.util.Collection<ConsumerWhichThrows<? super T,EX>> delegates) |
static <T> Consumer<T> |
tee(Consumer<? super T> delegate1,
Consumer<? super T> delegate2) |
static <T,EX extends java.lang.Throwable> |
tee(ConsumerWhichThrows<? super T,EX> delegate1,
ConsumerWhichThrows<? super T,EX> delegate2) |
static <EX extends java.lang.Throwable> |
throwsSubject() |
static <T,EX extends java.lang.Throwable> |
widen(ConsumerWhichThrows<? super T,? extends EX> source)
Deprecated.
Not necessary if you declare variables, fields an parameters as "
ConsumerWhichThrows<? super
consumed-type, ? extends thrown-exception>" |
static <T,EX extends java.lang.Throwable> |
widen2(ConsumerWhichThrows<? super T,? extends java.lang.RuntimeException> source)
Converts the source into a
ConsumerWhichThrows<T, EX>. |
public static <T,EX extends java.lang.Throwable> ConsumerWhichThrows<T,EX> tee(ConsumerWhichThrows<? super T,EX> delegate1, ConsumerWhichThrows<? super T,EX> delegate2)
delegate1 and delegate2public static <T> Consumer<T> tee(Consumer<? super T> delegate1, Consumer<? super T> delegate2)
delegate1 and delegate2public static <T,EX extends java.lang.Throwable> ConsumerWhichThrows<T,EX> tee(java.util.Collection<ConsumerWhichThrows<? super T,EX>> delegates)
delegates@Deprecated public static <T,EX extends java.lang.Throwable> ConsumerWhichThrows<T,EX> asConsumerWhichThrows(Consumer<? super T> source)
widen2(ConsumerWhichThrows).ConsumerWhichThrows<T, EX>.
This is always possible, because the source is only allowed to throw unchecked exceptions.
T - The element typeEX - The target consumer's exception@Deprecated public static <T,EX extends java.lang.Throwable> ConsumerWhichThrows<T,EX> widen(ConsumerWhichThrows<? super T,? extends EX> source)
ConsumerWhichThrows<? super
consumed-type, ? extends thrown-exception>"ConsumerWhichThrows<T, EX>.
This is always possible, because the source consumes a superclass of T, and the source is only allowed to throw a subclass of EX.
T - The element typeEX - The target consumer's exceptionpublic static <T,EX extends java.lang.Throwable> ConsumerWhichThrows<T,EX> widen2(ConsumerWhichThrows<? super T,? extends java.lang.RuntimeException> source)
ConsumerWhichThrows<T, EX>.
This is always possible, because the source consumes a superclass of T, and the
source is only allowed to throw a RuntimeExceptions.
T - The element typeEX - The target consumer's exceptionpublic static <T,EX extends java.lang.RuntimeException> Consumer<T> asConsumer(ConsumerWhichThrows<? super T,? extends java.lang.RuntimeException> source)
Consumer<T>.
This is always possible, because both ar allowed to throw RuntimeExceptions.
T - The element typeEX - The source consumer's exceptionpublic static java.io.Writer characterConsumerWriter(ConsumerWhichThrows<? super java.lang.Character,java.io.IOException> delegate)
Writer which forwards the characters to a ConsumerWhichThrows<Character, IOException>public static <E extends java.lang.Exception> ConsumerWhichThrows<java.lang.Character,E> lineAggregator(ConsumerWhichThrows<? super java.lang.String,E> delegate)
Consumer<Character> which aggregates characters to lines, which it
passes to the delegate.
Notice that iff the last consumed character is neither a CR nor an LF (a.k.a. "last line lacks a line separator"), then that last line will not be sent to the delegate.
public static ConsumerWhichThrows<java.lang.String,java.io.IOException> lineConsumer(java.io.File file, boolean append) throws java.io.IOException
Consumer that writes lines to the given file with the default character encodingjava.io.IOExceptionpublic static ConsumerWhichThrows<java.lang.String,java.io.IOException> lineConsumer(java.io.File file, java.lang.String charsetName, boolean append) throws java.io.IOException
Consumer that writes lines to the given file with the given encodingjava.io.IOExceptionpublic static ConsumerWhichThrows<java.lang.String,java.io.IOException> lineConsumer(java.io.Writer writer, boolean closeOnFinalize)
Consumer that writes strings to the given Writer, augmented with a line separatorpublic static Consumer<java.lang.String> lineConsumer(java.io.PrintStream printStream, boolean closeOnFinalize)
Consumer that writes strings to the given Writer, augmented with a line separatorpublic static <T> Producer<Consumer<T>> combine(Consumer<? super T> target)
T. The subjects sent to these consumers are
forwarded immediately to the given target.combineInOrder(ConsumerWhichThrows)public static <T,EX extends java.lang.Throwable> Producer<ConsumerWhichThrows<T,EX>> combineInOrder(ConsumerWhichThrows<? super T,EX> target)
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).combine(Consumer)public static <T,EX extends java.lang.Throwable> java.util.List<ConsumerWhichThrows<T,EX>> splice(int n, ConsumerWhichThrows<? super java.util.List<T>,EX> target)
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.public static <T> Consumer<T> addToCollection(java.util.Collection<T> drain)
public static <EX extends java.lang.Throwable> ConsumerWhichThrows<EX,EX> throwsSubject()
ConsumerWhichThrows which throws each subject it consumespublic static <T> ConsumerUtil.Produmer<T,T> store()
ConsumerUtil.Produmer simply produces the last consumed subject, or null if no subject
has been consumed yet.public static Consumer<java.lang.Number> cumulate(Consumer<? super java.lang.Long> delegate, long initialCount)
Consumer which forwards the cumulated quantity to the given delegate.initialCount - Initial value for the cumulated quantity, usually 0Lpublic static Consumer<java.lang.Long> compressExponentially(long initialLimit, Consumer<? super java.lang.Long> delegate)
Consumer<Long> which forwards the quantity to the given delegate, but only if the quantity is equal to or greater than the limit, which starts with initialLimit and increases exponentially.public static <T> Consumer<T> compress(Consumer<T> delegate, Predicate<T> compressable)
public static <T> Consumer<T> compress(Consumer<? super T> delegate, Predicate<? super T> compressable, T compressed)
Leading and trailing compressables are discarded.
public static <T> Consumer<T> nop()
public static <T,EX extends java.lang.Throwable> Consumer<T> ignoreExceptions(java.lang.Class<EX> exceptionClass, ConsumerWhichThrows<T,EX> delegate)