/** * 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); }
/** * Ensures that the given {@code expected} and {@code actual} values are equal. Otherwise, an * exception specified by the given {@code exceptionMaker} is thrown. * * @param <X> Exception type. * @param expected The expected value. * @param actual The actual value. * @param exceptionMaker A way of creating the exception for non-equal objects. * @throws X If the two objects are not equal. */ public static <X extends Throwable> void mustBeEqual(Object expected, Object actual, Supplier<? extends X> exceptionMaker) throws X { mustBeTrue(Objects.equals(expected, actual), exceptionMaker); }
private static void ensureNotEmpty(Collection<?> array) { mustBeTrue(! array.isEmpty(), withMessage("No elements to search", IllegalArgumentException::new)); }
private void checkAllowNonLinearTasks() { mustBeTrue(allowNonLinearTasks, withMessage("Non-linear tasks are not allowed", RejectedExecutionException::new)); }
/** * Marks a given task as completed. <p> * * Because this method may be called from within the actor system (just after executing the task), * it <em>must not block</em>. (It may also be called by application threads to cancel tasks.) * * @param task The task to complete. */ void markCompleted(LinearFutureTask<?> task) { final boolean removed = pendingTasks.remove(task); mustBeTrue(removed, IllegalStateException::new); final boolean terminated; synchronized (stateLock) { final int currentPendingTaskCount = pendingTaskCount; pendingTaskCount = currentPendingTaskCount - 1; terminated = shuttingDown && currentPendingTaskCount == 1; } if (terminated) { dispose(); } }
/** * Locates an enum among the given array of enums, where it is assumed that the enum must be * present. Returns the index of the enum, or throws an {@link IllegalArgumentException} if it * couldn't be found. * * @param <E> Enum type. * @param en The enum to find. * @param enums The enums to search in. * @return The index. * @throws IllegalArgumentException If the enum could not be found. */ public static <E extends Enum<E>> int indexOfMandatory(E en, E[] enums) { final int index = indexOf(en, enums); mustBeTrue(index != -1, withMessage(() -> en + " is not among " + Arrays.toString(enums), IllegalArgumentException::new)); return index; } }
/** * Attempts the transition {@code from} -> {@code to}, returning the {@code to} state * if allowed. * * @param from The <i>from</i> state. * @param to The <i>to</i> state. * @return The <i>to</i> state. * @throws IllegalTransitionException If the transition is not permitted. */ public S guard(S from, S to) throws IllegalTransitionException { mustBeTrue(isAllowed(from, to), withMessage("Cannot transition " + from + " -> " + to, IllegalTransitionException::new)); return to; }
mustBeTrue(rangeFromIncl >= 1024, withMessage(() -> "Lower port range must include 1024 or higher", IllegalArgumentException::new)); mustBeTrue(rangeFromIncl < rangeToExcl, withMessage(() -> "Port range cannot overlap", IllegalArgumentException::new)); mustBeTrue(rangeToExcl <= 65536, withMessage(() -> "Upper port range must exclude 65536 or lower", IllegalArgumentException::new)); final int randomPort = randomInRange(rangeFromIncl, rangeToExcl);