LinearFutureTask(ActorExecutor executor, FutureTask<V> delegateTask, String key) { mustExist(key, "Key cannot be null"); this.executor = executor; this.delegateTask = delegateTask; this.key = key; }
/** * 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); }
/** * A helper for decorating an existing {@link Callable} instance * with a {@link LinearTask}, on the basis of the provided key. * * @param <V> Return type. * @param callable The callable to delegate to. * @param key The key used for ordering. * @return A {@link LinearCallable} instance. */ public static <V> LinearCallable<V> decorate(Callable<V> callable, String key) { mustExist(callable, "Callable cannot be null"); mustExist(key, "Key cannot be null"); return new LinearCallable<V>() { @Override public V call() throws Exception { return callable.call(); } @Override public String getKey() { return key; } }; } }
/** * A helper for decorating an existing {@link Runnable} instance * with a {@link LinearTask}, on the basis of the provided key. * * @param runnable The runnable to delegate to. * @param key The key used for ordering. * @return A {@link LinearRunnable} instance. */ public static LinearRunnable decorate(Runnable runnable, String key) { mustExist(runnable, "Runnable cannot be null"); mustExist(key, "Key cannot be null"); return new LinearRunnable() { @Override public void run() { runnable.run(); } @Override public String getKey() { return key; } }; } }
/** * Ensures that {@code value} is less than {@code comparand}, returning the verified value if * true or throwing an exception otherwise. * * @param <T> Value type. * @param <X> Exception type. * @param value The value to compare. * @param comparand The comparand. * @param exceptionMaker A way of creating the exception for a failed comparison. * @return The verified value, as supplied to this method. * @throws X If the comparison failed. */ public static <T extends Comparable<T>, X extends Throwable> T mustBeLess(T value, T comparand, Supplier<? extends X> exceptionMaker) throws X { mustExist(value); mustExist(comparand); if (value.compareTo(comparand) < 0) return value; else throw exceptionMaker.get(); }
/** * Ensures that {@code value} is greater than or equal to {@code comparand}, returning the verified value if * true or throwing an exception otherwise. * * @param <T> Value type. * @param <X> Exception type. * @param value The value to compare. * @param comparand The comparand. * @param exceptionMaker A way of creating the exception for a failed comparison. * @return The verified value, as supplied to this method. * @throws X If the comparison failed. */ public static <T extends Comparable<T>, X extends Throwable> T mustBeGreaterOrEqual(T value, T comparand, Supplier<? extends X> exceptionMaker) throws X { mustExist(value); mustExist(comparand); if (value.compareTo(comparand) >= 0) return value; else throw exceptionMaker.get(); }
/** * Ensures that {@code value} is greater than {@code comparand}, returning the verified value if * true or throwing an exception otherwise. * * @param <T> Value type. * @param <X> Exception type. * @param value The value to compare. * @param comparand The comparand. * @param exceptionMaker A way of creating the exception for a failed comparison. * @return The verified value, as supplied to this method. * @throws X If the comparison failed. */ public static <T extends Comparable<T>, X extends Throwable> T mustBeGreater(T value, T comparand, Supplier<? extends X> exceptionMaker) throws X { mustExist(value); mustExist(comparand); if (value.compareTo(comparand) > 0) return value; else throw exceptionMaker.get(); }
/** * Ensures that {@code value} is less than or equal to {@code comparand}, returning the verified value if * true or throwing an exception otherwise. * * @param <T> Value type. * @param <X> Exception type. * @param value The value to compare. * @param comparand The comparand. * @param exceptionMaker A way of creating the exception for a failed comparison. * @return The verified value, as supplied to this method. * @throws X If the comparison failed. */ public static <T extends Comparable<T>, X extends Throwable> T mustBeLessOrEqual(T value, T comparand, Supplier<? extends X> exceptionMaker) throws X { mustExist(value); mustExist(comparand); if (value.compareTo(comparand) <= 0) return value; else throw exceptionMaker.get(); }
/** * 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)); }
static <T> FieldPatch<T> of(T value) { return new StandardFieldPatch<>(mustExist(value, "Value cannot be null")); }
TaskScheduler getTimeoutScheduler() { return mustExist(timeoutScheduler, withMessage("Timeout scheduler is not in use", IllegalStateException::new)); }
TaskScheduler getBackgroundScheduler() { return mustExist(backgroundScheduler, withMessage("Background scheduler is not in use", IllegalStateException::new)); }
/** * Ensures that the given {@code map} contains the specified {@code key} and returns the * mapped value. Otherwise, an exception specified by the given {@code exceptionMaker} is * thrown. * * @param <K> Key type. * @param <V> Value type. * @param <X> Exception type. * @param map The map to query. * @param key The key that must be present. * @param errorTemplate The template for forming the error, where the single format specifier * '%s' is substituted for the missing key. * @param exceptionMaker A way of creating the exception for a missing value. * @return The value. * @throws X If the mapping wasn't present. */ public static <K, V, X extends Throwable> V mustExist(Map<K, V> map, K key, String errorTemplate, Function<String, ? extends X> exceptionMaker) throws X { return mustExist(map.get(key), withMessage(() -> String.format(errorTemplate, key), exceptionMaker)); }
default <V> CheckedFunction<V, R, X> compose(CheckedFunction<? super V, ? extends T, ? extends X> before) { Functions.mustExist(before); return v -> apply(before.apply(v)); }
default <V> CheckedFunction<T, V, X> andThen(CheckedFunction<? super R, ? extends V, ? extends X> after) { Functions.mustExist(after); return t -> after.apply(apply(t)); }
public ConsumerPipe(ConsumerPipeConfig config, RecordHandler<K, V> handler, String threadName) { this.handler = handler; if (config.isAsync()) { mustExist(threadName, "Thread name cannot be null"); queue = new LinkedBlockingQueue<>(config.getBacklogBatches()); thread = WorkerThread.builder() .withOptions(new WorkerOptions().daemon().withName(threadName)) .onCycle(this::cycle) .buildAndStart(); } else { queue = null; thread = null; } }