The old implementation searched for all possible values and then executed each query to see what matches. The new implementation uses several indices to find only the matching values.
102 lines
3.2 KiB
Java
102 lines
3.2 KiB
Java
package org.lucares.utils;
|
|
|
|
import java.text.MessageFormat;
|
|
import java.util.Objects;
|
|
|
|
public class Preconditions {
|
|
public static void checkEven(final long value, final String message) {
|
|
if (value % 2 != 0) {
|
|
throw new IllegalStateException(message + ". Was: " + value);
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param a
|
|
* @param b
|
|
* @param message formatted with {@link MessageFormat}
|
|
* @param args
|
|
*/
|
|
public static void checkGreater(final long a, final long b, final String message, final Object... args) {
|
|
|
|
if (a <= b) {
|
|
throw new IllegalStateException(MessageFormat.format(message, args) + " Expected: " + a + " > " + b);
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param a
|
|
* @param b
|
|
* @param message formatted with {@link MessageFormat}
|
|
* @param args
|
|
* @throws IllegalStateException if {@code a} is not greater or equal to
|
|
* {@code b}
|
|
*/
|
|
public static void checkGreaterOrEqual(final long a, final long b, final String message, final Object... args) {
|
|
if (a < b) {
|
|
throw new IllegalStateException(MessageFormat.format(message, args) + " Expected: " + a + " >= " + b);
|
|
}
|
|
}
|
|
|
|
public static void checkSmaller(final long a, final long b, final String message, final Object... args) {
|
|
if (a >= b) {
|
|
throw new IllegalStateException(MessageFormat.format(message, args) + " Expected: " + a + " < " + b);
|
|
}
|
|
}
|
|
|
|
public static void checkEqual(final Object actual, final Object expected) {
|
|
checkEqual(actual, expected, "expected {0} is equal to {1}", actual, expected);
|
|
}
|
|
|
|
/**
|
|
* Check that the given values are equal. The check is done with
|
|
* {@link Objects#equals(Object, Object)}
|
|
*
|
|
* @param actual the actual value
|
|
* @param expected the expected value
|
|
* @param message formatted with {@link MessageFormat}
|
|
* @param args arguments for the message
|
|
* @throws IllegalStateException if {@code actual} is not equal to
|
|
* {@code expected}
|
|
*/
|
|
public static void checkEqual(final Object actual, final Object expected, final String message,
|
|
final Object... args) {
|
|
if (!Objects.equals(actual, expected)) {
|
|
throw new IllegalStateException(
|
|
MessageFormat.format(message, args) + " Expected: " + actual + " equals " + expected);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check that the given value is true.
|
|
*
|
|
* @param actual must be true
|
|
* @param message formatted with {@link MessageFormat}
|
|
* @param args arguments for the message
|
|
* @throws IllegalStateException if {@code actual} is not true
|
|
*/
|
|
public static void checkTrue(final boolean actual, final String message, final Object... args) {
|
|
checkEqual(actual, true, message, args);
|
|
}
|
|
|
|
/**
|
|
* Check that the given value is false.
|
|
*
|
|
* @param actual must be false
|
|
* @param message formatted with {@link MessageFormat}
|
|
* @param args arguments for the message
|
|
* @throws IllegalStateException if {@code actual} is not false
|
|
*/
|
|
public static void checkFalse(final boolean actual, final String message, final Object... args) {
|
|
checkEqual(actual, false, message, args);
|
|
}
|
|
|
|
public static void checkNull(final Object actual, final String message, final Object... args) {
|
|
if (actual != null) {
|
|
throw new IllegalStateException(MessageFormat.format(message, args));
|
|
}
|
|
}
|
|
|
|
}
|