public final class ProducerUtil
extends java.lang.Object
Producer-related utility methods.| Modifier and Type | Class and Description |
|---|---|
static interface |
ProducerUtil.FromArrayProducer<T>
Extends the concept of the
Producer by an "index". |
| Modifier and Type | Method and Description |
|---|---|
static Producer<java.lang.Boolean> |
after(long expirationTime)
Returns a "
Producer<Object>" that produces true iff the current time is after the
given expirationTime (in milliseconds). |
static <T> Producer<T> |
alternate(T first,
T second) |
static <T> Producer<T> |
asProducer(ProducerWhichThrows<? extends T,? extends java.lang.RuntimeException> source)
Converts the source into a
Producer<T>. |
static <T,EX extends java.lang.Throwable> |
asProducerWhichThrows(Producer<? extends T> source)
Converts the source into a
ProducerWhichThrows<T, EX>. |
static Producer<java.lang.Boolean> |
atMostEvery(long milliseconds)
Creates and returns a
Producer for which the first product is true, and, for all
following products, the time interval between adjacent true products will (A) be minimal and (B) never
shorter than milliseconds. |
static <T,EX extends java.lang.Throwable> |
cache(ProducerWhichThrows<T,? extends EX> delegate,
ProducerWhichThrows<java.lang.Boolean,? extends EX> condition)
The first product is the first product of the delegate; each following product is the next product
of the delegate if the condition evaluates to
true, otherwise it is the
previous product. |
static <T,EX extends java.lang.Throwable> |
cache(T firstProduct,
ProducerWhichThrows<T,? extends EX> delegate,
ProducerWhichThrows<java.lang.Boolean,? extends EX> condition)
The first product is the firstProduct; each following product is the next product of the
delegate iff the condition evaluates to
true, otherwise it is the
previous product. |
static <T> Producer<T> |
compress(Producer<? extends T> delegate,
Predicate<? super T> compressable,
T compressed)
Discards the elements that the delegate produces while they are compressable.
|
static <T> Producer<T> |
concat(Producer<? extends T> delegate1,
Producer<? extends T> delegate2) |
static <T> Producer<T> |
constantProducer(T constant) |
static Producer<java.lang.Boolean> |
every(long interval)
Returns a
Producer<Boolean> who's first evaluation result is true, and each following result is
true iff the last true result was returned at least the given interval milliseconds ago. |
static <T> ProducerUtil.FromArrayProducer<T> |
fromArray(T[] delegate)
Produces the elements of the delegate array, in ascending index order, and after that an infinite
sequence of
nulls. |
static <T> ProducerUtil.FromArrayProducer<T> |
fromArray(T[] delegate,
int from,
int to)
Produces the elements from ...
|
static <T> Producer<T> |
fromCollection(java.util.Collection<T> delegate)
Produces the elements of the
delegate collection, in its iteration order, or null iff the
collection is empty. |
static <T> Producer<T> |
fromElements(T... elements) |
static <T> Producer<T> |
fromIndexTransformer(Transformer<? super java.lang.Integer,T> indexTransformer)
Produces objects based on the number of preceding invocations, i.e. the
indexTransformer is invoked
with subjects '0', '1', '2', ... |
static <T,EX extends java.lang.Throwable> |
fromIndexTransformer(TransformerWhichThrows<? super java.lang.Integer,T,EX> indexTransformer)
Produces objects based on the number of preceding invocations, i.e. the
indexTransformer is invoked
with subjects '0', '1', '2', ... |
static <T> Producer<T> |
fromIterator(java.util.Iterator<T> iterator)
Produces the products of the
iterator, or null iff the iterator has no more elements. |
static <T,EX extends java.lang.Throwable> |
ignoreExceptions(java.lang.Class<EX> exceptionClass,
ProducerWhichThrows<T,EX> delegate,
T defaultValue)
Wraps the delegate such that its declared exception is caught, ignored, and the
defaultValue is returned.
|
static Producer<java.lang.Integer> |
increasing() |
static Producer<java.lang.Byte> |
randomByteProducer(long seed) |
static <T,ST> Producer<T> |
sparingProducer(Producer<? extends T> delegate,
Predicate<? super ST> condition,
ST subject)
Deprecated.
Use
cache(ProducerWhichThrows, ProducerWhichThrows) instead, which has very similar (but
not identical) semantics. |
@Deprecated public static <T,ST> Producer<T> sparingProducer(Producer<? extends T> delegate, Predicate<? super ST> condition, ST subject)
cache(ProducerWhichThrows, ProducerWhichThrows) instead, which has very similar (but
not identical) semantics.Producer calls the delegate iff the condition returns true,
otherwise it returns the previous product of the delegate, or null iff the delegate has
not yet been called.subject - The subject argument for the conditionpublic static Producer<java.lang.Boolean> every(long interval)
Producer<Boolean> who's first evaluation result is true, and each following result is
true iff the last true result was returned at least the given interval milliseconds ago.
In other words, the interval between two returned true values is never shorter than interval
milliseconds.public static <T,EX extends java.lang.Throwable> ProducerWhichThrows<T,EX> asProducerWhichThrows(Producer<? extends T> source)
ProducerWhichThrows<T, EX>.
This is always possible, because the source is only allowed to throw RuntimeExceptions.
Notice Producer<T> extends ProducerWhichThrows<T, RuntimeException>, thus you don't
need this method to convert to ProducerWhichThrows<T, RuntimeException>.
T - The product typeEX - The target producer's exceptionpublic static <T> Producer<T> asProducer(ProducerWhichThrows<? extends T,? extends java.lang.RuntimeException> source)
Producer<T>.
This is always possible, because both are only allowed to throw RuntimeExceptions.
T - The product typepublic static <T> Producer<T> fromElements(T... elements)
Producer that produced the given elementspublic static <T> Producer<T> fromCollection(java.util.Collection<T> delegate)
delegate collection, in its iteration order, or null iff the
collection is empty. The elements are removed from the collection as they are produced.public static <T> ProducerUtil.FromArrayProducer<T> fromArray(T[] delegate)
nulls.public static <T> ProducerUtil.FromArrayProducer<T> fromArray(T[] delegate, int from, int to)
delegate array, and after that an
infinite sequence of nulls.java.lang.IllegalArgumentException - from is less than 0java.lang.IllegalArgumentException - to is less than fromjava.lang.IllegalArgumentException - to is greater than delegate.lengthpublic static <T> Producer<T> fromIterator(java.util.Iterator<T> iterator)
iterator, or null iff the iterator has no more elements.public static <T> Producer<T> fromIndexTransformer(Transformer<? super java.lang.Integer,T> indexTransformer)
indexTransformer is invoked
with subjects '0', '1', '2', ...public static <T,EX extends java.lang.Throwable> ProducerWhichThrows<T,EX> fromIndexTransformer(TransformerWhichThrows<? super java.lang.Integer,T,EX> indexTransformer)
indexTransformer is invoked
with subjects '0', '1', '2', ...public static Producer<java.lang.Byte> randomByteProducer(long seed)
new java.util.Random(seed).nextInt(0x100)public static <T> Producer<T> constantProducer(T constant)
constantpublic static <T> Producer<T> compress(Producer<? extends T> delegate, Predicate<? super T> compressable, T compressed)
public static <T> Producer<T> alternate(T first, T second)
Producer that produces first, second, first,
second, ...public static Producer<java.lang.Integer> increasing()
Producer that produces 0, 1, 2, 3, ...public static <T> Producer<T> concat(Producer<? extends T> delegate1, Producer<? extends T> delegate2)
null,
the products of delegate2.public static <T,EX extends java.lang.Throwable> ProducerWhichThrows<T,EX> cache(ProducerWhichThrows<T,? extends EX> delegate, ProducerWhichThrows<java.lang.Boolean,? extends EX> condition)
true, otherwise it is the
previous product.
Example:
ProducerUtil.cache(delegate, ProducerUtil.atMostEvery(milliseconds))
caches the products of the delegate for milliseconds' time.
The returned PredicateWhichThrows is not synchronized and therefore not thread-safe.
public static <T,EX extends java.lang.Throwable> ProducerWhichThrows<T,EX> cache(@Nullable T firstProduct, ProducerWhichThrows<T,? extends EX> delegate, ProducerWhichThrows<java.lang.Boolean,? extends EX> condition)
true, otherwise it is the
previous product.
The returned PredicateWhichThrows is not synchronized and therefore not thread-safe.
public static Producer<java.lang.Boolean> atMostEvery(long milliseconds)
Producer for which the first product is true, and, for all
following products, the time interval between adjacent true products will (A) be minimal and (B) never
shorter than milliseconds.public static Producer<java.lang.Boolean> after(long expirationTime)
Producer<Object>" that produces true iff the current time is after the
given expirationTime (in milliseconds).expirationTime - Milliseconds since Jan 1 1970, UTCpublic static <T,EX extends java.lang.Throwable> Producer<T> ignoreExceptions(java.lang.Class<EX> exceptionClass, ProducerWhichThrows<T,EX> delegate, T defaultValue)