| | |
| | | |
| | | /** |
| | | * This method validates that the specified parameter is not null. It |
| | | * throws a RuntimeException or AssertionError if it is null after logging |
| | | * this error. |
| | | * throws an AssertionError if it is null after logging this error. |
| | | * <p> |
| | | * This should be used like an assert, except it is not turned |
| | | * off at runtime. That is, it should only be used in situations where |
| | |
| | | * overhead from the calling code. This idiom should only be used when |
| | | * performance testing proves that it is necessary. |
| | | * |
| | | * @throws NullPointerException if and only if param is null |
| | | * @throws AssertionError this is thrown instead of NullPointerException |
| | | * @throws AssertionError if and only if param is null |
| | | * if assertions are enabled |
| | | */ |
| | | public static boolean ensureNotNull(Object param) |
| | | throws NullPointerException, AssertionError { |
| | | throws AssertionError { |
| | | if (ENABLE_CHECKS) { |
| | | if (param == null) throwNull(""); |
| | | } |
| | |
| | | |
| | | /** |
| | | * This method validates that the specified parameters are not null. It |
| | | * throws a RuntimeException or AssertionError if one of them are null |
| | | * after logging this error. It's similar to the ensureNotNull(Object) |
| | | * call except it provides the convenience of checking two parameters |
| | | * at once. |
| | | * throws an AssertionError if one of them are null after logging this error. |
| | | * It's similar to the ensureNotNull(Object) call except it provides the |
| | | * convenience of checking two parameters at once. |
| | | * <p> |
| | | * This should be used like an assert, except it is not turned |
| | | * off at runtime. That is, it should only be used in situations where |
| | |
| | | * overhead from the calling code. This idiom should only be used when |
| | | * performance testing proves that it is necessary. |
| | | * |
| | | * @throws NullPointerException if and only if one of the parameters is null |
| | | * @throws AssertionError this is thrown instead of NullPointerException |
| | | * if assertions are enabled |
| | | * @throws AssertionError if and only if any of the parameters is null |
| | | */ |
| | | public static boolean ensureNotNull(Object param1, Object param2) |
| | | throws NullPointerException, AssertionError { |
| | | throws AssertionError { |
| | | if (ENABLE_CHECKS) { |
| | | if (param1 == null) throwNull(PARAM_DESCRIPTIONS[1]); |
| | | if (param2 == null) throwNull(PARAM_DESCRIPTIONS[2]); |
| | |
| | | |
| | | /** |
| | | * This method validates that the specified parameters are not null. It |
| | | * throws a RuntimeException or AssertionError if one of them are null |
| | | * after logging this error. It's similar to the ensureNotNull(Object) |
| | | * call except it provides the convenience of checking three parameters |
| | | * at once. |
| | | * throws an AssertionError if one of them are null after logging this error. |
| | | * It's similar to the ensureNotNull(Object) call except it provides the |
| | | * convenience of checking three parameters at once. |
| | | * <p> |
| | | * This should be used like an assert, except it is not turned |
| | | * off at runtime. That is, it should only be used in situations where |
| | |
| | | * overhead from the calling code. This idiom should only be used when |
| | | * performance testing proves that it is necessary. |
| | | * |
| | | * @throws NullPointerException if and only if one of the parameters is null |
| | | * @throws AssertionError this is thrown instead of NullPointerException |
| | | * if assertions are enabled |
| | | * @throws AssertionError if and only if one of the parameters is null |
| | | */ |
| | | public static boolean ensureNotNull(Object param1, Object param2, |
| | | Object param3) |
| | | throws NullPointerException, AssertionError { |
| | | throws AssertionError { |
| | | if (ENABLE_CHECKS) { |
| | | if (param1 == null) throwNull(PARAM_DESCRIPTIONS[1]); |
| | | if (param2 == null) throwNull(PARAM_DESCRIPTIONS[2]); |
| | |
| | | |
| | | /** |
| | | * This method validates that the specified parameters are not null. It |
| | | * throws a RuntimeException or AssertionError if one of them are null |
| | | * after logging this error. It's similar to the ensureNotNull(Object) |
| | | * call except it provides the convenience of checking three parameters |
| | | * at once. |
| | | * throws an AssertionError if one of them are null after logging this error. |
| | | * It's similar to the ensureNotNull(Object) call except it provides the |
| | | * convenience of checking four parameters at once. |
| | | * <p> |
| | | * This should be used like an assert, except it is not turned |
| | | * off at runtime. That is, it should only be used in situations where |
| | |
| | | * overhead from the calling code. This idiom should only be used when |
| | | * performance testing proves that it is necessary. |
| | | * |
| | | * @throws NullPointerException if and only if one of the parameters is null |
| | | * @throws AssertionError this is thrown instead of NullPointerException |
| | | * if assertions are enabled |
| | | * @throws AssertionError if and only if one of the parameters is null |
| | | */ |
| | | public static boolean ensureNotNull(Object param1, Object param2, |
| | | Object param3, Object param4) |
| | | throws NullPointerException, AssertionError { |
| | | throws AssertionError { |
| | | if (ENABLE_CHECKS) { |
| | | if (param1 == null) throwNull(PARAM_DESCRIPTIONS[1]); |
| | | if (param2 == null) throwNull(PARAM_DESCRIPTIONS[2]); |
| | |
| | | |
| | | /** |
| | | * This method validates that the specified parameter is true. It |
| | | * throws a RuntimeException or AssertionError if it is not true. |
| | | * throws an AssertionError if it is not true. |
| | | * <p> |
| | | * This should be used like an assert, except it is not turned |
| | | * off at runtime. That is, it should only be used in situations where |
| | |
| | | * overhead from the calling code. This idiom should only be used when |
| | | * performance testing proves that it is necessary. |
| | | * |
| | | * @throws AssertionError if condition is false and assertions are enabled |
| | | * @throws IllegalArgumentException if condition is false and assertions are |
| | | * not enabled |
| | | * @throws AssertionError if condition is false |
| | | */ |
| | | public static boolean ensureTrue(boolean condition) |
| | | throws IllegalArgumentException, AssertionError { |
| | | throws AssertionError { |
| | | if (ENABLE_CHECKS) { |
| | | if (!condition) { |
| | | ensureTrue(condition, ""); |
| | |
| | | |
| | | /** |
| | | * This method validates that the specified parameter is true. It |
| | | * throws a RuntimeException or AssertionError if it is not true. |
| | | * The supplied message is included in the error message. |
| | | * throws an AssertionError if it is not true. The supplied message is |
| | | * included in the error message. |
| | | * <p> |
| | | * This should be used like an assert, except it is not turned |
| | | * off at runtime. That is, it should only be used in situations where |
| | |
| | | * overhead from the calling code. This idiom should only be used when |
| | | * performance testing proves that it is necessary. |
| | | * |
| | | * @throws AssertionError if condition is false and assertions are enabled |
| | | * @throws IllegalArgumentException if condition is false and assertions are |
| | | * not enabled |
| | | * @throws AssertionError if condition is false |
| | | */ |
| | | public static boolean ensureTrue(boolean condition, String message) |
| | | throws IllegalArgumentException, AssertionError { |
| | | throws AssertionError { |
| | | if (ENABLE_CHECKS) { |
| | | if (!condition) { |
| | | String fullMessage = generateLineSpecificErrorMessage( |
| | |
| | | |
| | | logError(fullMessage); |
| | | |
| | | // Prefer throwing an assertion if they are enabled. |
| | | assert condition : fullMessage; |
| | | |
| | | throw new IllegalArgumentException(fullMessage); |
| | | throw new AssertionError(fullMessage); |
| | | } |
| | | } |
| | | return true; |
| | |
| | | |
| | | |
| | | private static void throwNull(String message) |
| | | throws NullPointerException, AssertionError { |
| | | throws AssertionError { |
| | | String fullMessage = generateLineSpecificErrorMessage( |
| | | "The specified parameter must not be null. " + message); |
| | | |
| | | logError(fullMessage); |
| | | |
| | | // Prefer throwing an assertion if they are enabled. |
| | | assert false : fullMessage; |
| | | |
| | | throw new NullPointerException(fullMessage); |
| | | throw new AssertionError(fullMessage); |
| | | } |
| | | |
| | | |