private static void doWithExceptionHandler(CheckedRunnable<?> r, ExceptionHandler errorHandler, String message) { try { r.run(); } catch (Throwable e) { errorHandler.onException(message, e); } }
/** * Ensures that the given value is {@code null}. Otherwise, an * exception specified by the given {@code exceptionMaker} is thrown. * * @param <X> Exception type. * @param value The value to test. * @param exceptionMaker A way of creating the exception for a non-{@code null} value. * @throws X If the value is not {@code null}. */ public static <X extends Throwable> void mustBeNull(Object value, Supplier<? extends X> exceptionMaker) throws X { mustBeTrue(value == null, exceptionMaker); }
/** * Obtains the element at the given index. * * @param <T> Element type. * @param elementIndex The element index. * @return The element, cast to {@code T}. */ protected final <T> T get(int elementIndex) { return Classes.cast(elements[elementIndex]); }
/** * Creates a new array initialised with default values to a desired length. * * @param <T> Array type. * @param array The original array to mimic. * @param newLength The length of the new array. * @return The new array instance. */ public static <T> T clear(T array, int newLength) { return allocate(Classes.<Class<T>>cast(array.getClass()), newLength); }
/** * A convenient variant of {@link #mustExist(Object, Supplier)} that throws a * {@link NullArgumentException} with the supplied {@code message}. * * @param <T> Value type. * @param value The value to check. * @param message The message to the {@link NullArgumentException}. * @return The verified value. * @throws NullArgumentException If the given value is {@code null}. */ public static <T> T mustExist(T value, String message) throws NullArgumentException { return mustExist(value, withMessage(message, NullArgumentException::new)); }
private static <X extends Throwable> CheckedSupplier<Void, X> toVoidSupplier(CheckedRunnable<X> r) { return () -> { r.run(); return null; }; }
/** * A convenient variant of {@link #mustExist(Object, Supplier)} that throws a * {@link NullArgumentException} with no message. * * @param <T> Value type. * @param value The value to check. * @return The verified value. * @throws NullArgumentException If the given value is {@code null}. */ public static <T> T mustExist(T value) throws NullArgumentException { return mustExist(value, NullArgumentException::new); }
/** * Curries a {@link ExceptionHandler} with a specific error {@code summary}, so that it can be used * as a simpler {@link Throwable} {@link Consumer}. * * @param summary The error summary to feed to the {@code exceptionHandler}. * @param exceptionHandler Handles errors. * @return A curried {@link Throwable} {@link Consumer}. */ public static Consumer<Throwable> withSummary(String summary, ExceptionHandler exceptionHandler) { return cause -> exceptionHandler.onException(summary, cause); }
/** * Chains the output of a function into the input of a consumer, thereby creating a new * consumer. * * @param <U> Input type of the {@code before} function. * @param <V> Output type of the {@code before} function and input type of the {@code after} consumer. * @param <X> Exception type. * @param before The initial function to apply. * @param after The consumer to chain to. * @return A composite {@link CheckedConsumer}. * @throws X If an error occurs. */ public static <U, V, X extends Throwable> CheckedConsumer<U, X> chain(CheckedFunction<U, V, X> before, CheckedConsumer<V, X> after) throws X { return u -> after.accept(before.apply(u)); }
/** * Applies a function to the result of a supplier, thereby creating a new supplier. * * @param <U> Original type supplied by {@code before} and input to the {@code after} function. * @param <V> Output type of the {@code after} function. * @param <X> Exception type. * @param before The original value supplier. * @param after The transformation to apply to the supplied value. * @return A composite {@link CheckedSupplier}. * @throws X If an error occurs. */ public static <U, V, X extends Throwable> CheckedSupplier<V, X> chain(CheckedSupplier<U, ? extends X> before, CheckedFunction<U, V, ? extends X> after) throws X { return () -> after.apply(before.get()); }
/** * Closes a given {@link Closeable} instance, wrapping any potential {@link IOException} * in an unchecked {@link RuntimeIOException}. * * @param closeable The {@link Closeable} to close. */ public static void closeUnchecked(Closeable closeable) { Exceptions.wrapStrict(closeable::close, RuntimeIOException::new); } }
public static <T, X extends Throwable> LazyReference<T, X> from(CheckedSupplier<T, X> supplier) { return new LazyReference<>(supplier); } }
public <T> T getObjective() { return Classes.cast(objective); }
LinearFutureTask(ActorExecutor executor, FutureTask<V> delegateTask, String key) { mustExist(key, "Key cannot be null"); this.executor = executor; this.delegateTask = delegateTask; this.key = key; }
public <T> T getResult() { return Classes.cast(result); }
private O self() { return Classes.cast(this); }
public <T> T getMetadata() { return Classes.cast(metadata); }