Package de.scravy.bedrock
Class Control
java.lang.Object
de.scravy.bedrock.Control
public class Control extends Object
Missing control structures for Java.
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classControl.Async<In,Out>static classControl.TypeOfBranch<A,T>static classControl.TypeOfVoidBranch<A>static classControl.ValueOfBranch<A,T>static classControl.ValueOfVoidBranch<A> -
Constructor Summary
Constructors Constructor Description Control() -
Method Summary
Modifier and Type Method Description static <A> Control.Async<A,A>async()static <In, Out> Control.Async<In,Out>async(ThrowingBiConsumer<In,Callback<Out>> function)static <T> Supplier<T>atomic(Supplier<T> supplier)Turns a supplier into a thread safe supplier, that is the invocation of get will be guarded by synchronization.static <A> Aexhaustively(UnaryOperator<A> operation, A startValue)Applies the given function on a value until that value does not change anymore (i.e.static <K, T> Optional<T>findFirstNonNull(K k, Function<? super K,? extends T>... fs)Evaluates the given functions one by one, returning the result of the first function that does not yield null, without executing the remaining functions.static voidforever(ThrowingRunnable runnable)static voidforever(Thread.UncaughtExceptionHandler exceptionHandler, ThrowingRunnable runnable)static <A> Aiterate(UnaryOperator<A> operation, BiPredicate<A,A> exitCriterion, A startValue)Iterates a function on a given value until a certain exit criterion is met.static <A, R> Function<A,R>memoizing(Function<A,R> function)Turns a function into a memoizing function which caches result values for the given arguments.static <R> IntFunction<R>memoizing(IntFunction<R> function)static <R> LongFunction<R>memoizing(LongFunction<R> function)static <T> Function0<T>memoizing(Supplier<T> supplier)Turns a supplier into a memoizing supplier, which invokes the actual supplier only once, caches the result, and returns that henceforth.static <T> ThrowingConsumer<Callback<Seq<T>>>parallel(ThrowingConsumer<Callback<T>>... actions)static voidparallel(Executor executor, ThrowingRunnable... runnables)static <T> Seq<T>parallel(Executor executor, Callable<? extends T>... runnables)static voidreport(Object err)static voidsleep(Duration duration)static voidswap(boolean[] array, int i, int j)static voidswap(byte[] array, int i, int j)static voidswap(char[] array, int i, int j)static voidswap(double[] array, int i, int j)static voidswap(float[] array, int i, int j)static voidswap(int[] array, int i, int j)static voidswap(long[] array, int i, int j)static voidswap(short[] array, int i, int j)static <E> voidswap(E[] array, int i, int j)static voidtimes(int n, ThrowingIntConsumer runnable)static ThrowabletoThrowable(Object object)static <A, T> Control.TypeOfBranch<A,T>type(Class<A> clazz, ThrowingFunction<A,T> f)static <A> Control.TypeOfVoidBranch<A>type_(Class<A> clazz, ThrowingConsumer<A> f)static <T> TtypeOf(Object value, Control.TypeOfBranch<?,? extends T>... typeOfBranches)static voidtypeOf(Object value, Control.TypeOfVoidBranch<?>... branches)static Thread.UncaughtExceptionHandleruncaughtExceptionHandler()static <A, T> Control.ValueOfBranch<A,T>value(A value, ThrowingFunction<A,T> f)static <A> Control.ValueOfVoidBranch<A>value_(A value, ThrowingConsumer<A> f)static <T> TvalueOf(Object value, Control.ValueOfBranch<?,? extends T>... typeOfBranches)static voidvalueOf(Object value, Control.ValueOfVoidBranch<?>... branches)static booleanwait(Object monitor)static <A> Control.Async<A,A>waterfall(Seq<ThrowingBiConsumer<A,Callback<A>>> fs)static <A> Control.Async<A,A>waterfall(ThrowingBiConsumer<A,Callback<A>>... fs)static <A> Control.Async<A,A>waterfall(List<ThrowingBiConsumer<A,Callback<A>>> fs)
-
Constructor Details
-
Control
public Control()
-
-
Method Details
-
type
@Nonnull public static <A, T> Control.TypeOfBranch<A,T> type(@Nonnull Class<A> clazz, @Nonnull ThrowingFunction<A,T> f) -
type_
@Nonnull public static <A> Control.TypeOfVoidBranch<A> type_(@Nonnull Class<A> clazz, @Nonnull ThrowingConsumer<A> f) -
typeOf
@SafeVarargs public static <T> T typeOf(Object value, Control.TypeOfBranch<?,? extends T>... typeOfBranches) -
typeOf
-
value
@Nonnull public static <A, T> Control.ValueOfBranch<A,T> value(@Nonnull A value, @Nonnull ThrowingFunction<A,T> f) -
value_
@Nonnull public static <A> Control.ValueOfVoidBranch<A> value_(@Nonnull A value, @Nonnull ThrowingConsumer<A> f) -
valueOf
@SafeVarargs public static <T> T valueOf(Object value, Control.ValueOfBranch<?,? extends T>... typeOfBranches) -
valueOf
-
uncaughtExceptionHandler
-
report
-
forever
-
forever
public static void forever(@Nonnull Thread.UncaughtExceptionHandler exceptionHandler, @Nonnull ThrowingRunnable runnable) -
wait
-
sleep
-
parallel
public static void parallel(@Nonnull Executor executor, @Nonnull ThrowingRunnable... runnables) throws ParallelExecutionException- Throws:
ParallelExecutionException
-
parallel
@SafeVarargs public static <T> Seq<T> parallel(@Nonnull Executor executor, @Nonnull Callable<? extends T>... runnables) throws ParallelExecutionException- Throws:
ParallelExecutionException
-
parallel
@SafeVarargs public static <T> ThrowingConsumer<Callback<Seq<T>>> parallel(@Nonnull ThrowingConsumer<Callback<T>>... actions) -
async
-
async
@Nonnull public static <In, Out> Control.Async<In,Out> async(@Nonnull ThrowingBiConsumer<In,Callback<Out>> function) -
waterfall
@Nonnull public static <A> Control.Async<A,A> waterfall(@Nonnull List<ThrowingBiConsumer<A,Callback<A>>> fs) -
waterfall
@Nonnull public static <A> Control.Async<A,A> waterfall(@Nonnull Seq<ThrowingBiConsumer<A,Callback<A>>> fs) -
waterfall
@Nonnull @SafeVarargs public static <A> Control.Async<A,A> waterfall(@Nonnull ThrowingBiConsumer<A,Callback<A>>... fs) -
toThrowable
-
iterate
public static <A> A iterate(@Nonnull UnaryOperator<A> operation, @Nonnull BiPredicate<A,A> exitCriterion, A startValue)Iterates a function on a given value until a certain exit criterion is met.Example: Finding the square root of 10 until the error is less than 0.01.
final double squareRoot = Control.iterate( value -> value * (value * value > 10.0 ? 0.5 : 1.5), (prev, curr) -> Math.abs(10.0 - curr * curr) < 0.01, 10.0 );The exit criterion is a predicate that has access to the current value as well as the value of the previous iteration. This allows for exit criteria like "iterate until the value does not change much".
- Type Parameters:
A- The type of the iterated values.- Parameters:
operation- The function to iterate on the given value.exitCriterion- The predicate that decided when to stop iterating, with respect to the previous' iteration value and the current's iteration value.startValue- The value to start iterating with.- Returns:
- The resulting value.
-
exhaustively
Applies the given function on a value until that value does not change anymore (i.e. until it reaches a fixed point).- Type Parameters:
A- The type of the value to be iterated on.- Parameters:
operation- The function to be applied iteratively.startValue- The value to start with.- Returns:
- A fixed point of the given operation.
-
swap
-
swap
-
swap
-
swap
-
swap
-
swap
-
swap
-
swap
-
swap
-
memoizing
Turns a function into a memoizing function which caches result values for the given arguments.final Function<A, B> f = Control.memoizing(arg -> expensiveCalculation(arg)); final A one = ...; final A two = ...; f.apply(one); // actually invokes expensiveCalculation(one) f.apply(one); // returns the cached value for the key "one" f.apply(two); // actually invokes expensiveCalculation(two)
- Type Parameters:
A- The type of the argument to the function.R- The type of the results of the function.- Parameters:
function- A pure function (a proper function which returns the same value for a given argument every time)- Returns:
- The memoizing function.
-
memoizing
-
memoizing
-
memoizing
Turns a supplier into a memoizing supplier, which invokes the actual supplier only once, caches the result, and returns that henceforth.class Thing { static Thing expensiveFactory() { ... } } final Supplier<Thing> supplier = Control.memoizing(Thing::expensiveFactory); final Thing thing = supplier.get(); final Thing thing2 = supplier.get(); // returns the same thing- Type Parameters:
T- The type of the thing the supplier supplies.- Parameters:
supplier- The supplier to be turned into a memoizing supplier.- Returns:
- A memoizing supplier which calls the given supplier only ever once.
-
atomic
Turns a supplier into a thread safe supplier, that is the invocation of get will be guarded by synchronization.Useful to get a thread-safe version of a memoizing supplier:
final Supplier<Thing> thingSupplier = </Thing>Control.atomic(Control.memoizing(Thing::expensiveFactory));
-
times
-
findFirstNonNull
@Nonnull @SafeVarargs public static <K, T> Optional<T> findFirstNonNull(K k, Function<? super K,? extends T>... fs)Evaluates the given functions one by one, returning the result of the first function that does not yield null, without executing the remaining functions.If no function returned a non-null value, this function returns
Optional.empty().- Parameters:
k- The single parameter to apply to the functions when evaluating them.fs- The functions.- Returns:
- The first non-null result wrapped in an
Optional.
-