de.unkrig.commons.lang.protocol
Class ProducerUtil

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

public final class ProducerUtil
extends java.lang.Object

Various Producer-related utility methods.


Method Summary
static
<T> Producer<T>
asProducer(ProducerWhichThrows<? extends T,? extends java.lang.RuntimeException> source)
          Converts a {@link ProducerWhichThrows ProducerWhichThrows<?
static
<T,EX extends java.lang.Throwable>
ProducerWhichThrows<T,EX>
asProducerWhichThrows(Producer<? extends T> source)
          Converts a {@link Producer Producer<?
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> 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<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.Exception>
ProducerWhichThrows<T,EX>
fromIndexTransformer(TransformerWhichThrows<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 Producer<java.lang.Byte> randomByteProducer(long seed)
           
static
<T,ST> Producer<T>
sparingProducer(Producer<T> delegate, Predicate<ST> condition, ST subject)
          The returned 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

sparingProducer

public static <T,ST> Producer<T> sparingProducer(Producer<T> delegate,
                                                 Predicate<ST> condition,
                                                 ST subject)
The returned 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.

Parameters:
subject - The subject argument for the condition

every

public 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. In other words, the interval between two returned true values is never shorter than interval milliseconds.


asProducerWhichThrows

public static <T,EX extends java.lang.Throwable> ProducerWhichThrows<T,EX> asProducerWhichThrows(Producer<? extends T> source)
Converts a Producer<? extends TT> (the "source") into a ProducerWhichThrows<TT, TE> (the "target"), which is always possible.

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

asProducer

public static <T> Producer<T> asProducer(ProducerWhichThrows<? extends T,? extends java.lang.RuntimeException> source)
Converts a ProducerWhichThrows<? extends T, ? extends RuntimeException> (the "source") into a Producer<T> (the "target"), which is always possible.

Type Parameters:
T - The element type

fromElements

public static <T> Producer<T> fromElements(T... elements)
Returns:
A Producer that produced the given elements

fromCollection

public 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. The elements are removed from the collection as they are produced.


fromIterator

public 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.


fromIndexTransformer

public static <T> Producer<T> fromIndexTransformer(Transformer<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', ...


fromIndexTransformer

public static <T,EX extends java.lang.Exception> ProducerWhichThrows<T,EX> fromIndexTransformer(TransformerWhichThrows<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', ...


randomByteProducer

public static Producer<java.lang.Byte> randomByteProducer(long seed)
Returns:
A producer which produces bytes through new java.util.Random(seed).nextInt(0x100)

constantProducer

public static <T> Producer<T> constantProducer(T constant)
Returns:
A producer which always produces the constant