package com.alibaba.excel.util; import java.util.Objects; /** * Validate * * @author Jiaju Zhuang */ public class Validate { private static final String DEFAULT_IS_TRUE_EX_MESSAGE = "The validated expression is false"; private static final String DEFAULT_IS_NULL_EX_MESSAGE = "The validated object is null"; /** *

Validate that the argument condition is {@code true}; otherwise * throwing an exception with the specified message. This method is useful when * validating according to an arbitrary boolean expression, such as validating a * primitive number or using your own custom validation expression.

* *
Validate.isTrue(i > 0.0, "The value must be greater than zero: %d", i);
* *

For performance reasons, the long value is passed as a separate parameter and * appended to the exception message only in the case of an error.

* * @param expression the boolean expression to check * @param message the {@link String#format(String, Object...)} exception message if invalid, not null * @param value the value to append to the message when invalid * @throws IllegalArgumentException if expression is {@code false} * @see #isTrue(boolean) * @see #isTrue(boolean, String, double) * @see #isTrue(boolean, String, Object...) */ public static void isTrue(final boolean expression, final String message, final long value) { if (!expression) { throw new IllegalArgumentException(String.format(message, Long.valueOf(value))); } } /** *

Validate that the argument condition is {@code true}; otherwise * throwing an exception with the specified message. This method is useful when * validating according to an arbitrary boolean expression, such as validating a * primitive number or using your own custom validation expression.

* *
Validate.isTrue(d > 0.0, "The value must be greater than zero: %s", d);
* *

For performance reasons, the double value is passed as a separate parameter and * appended to the exception message only in the case of an error.

* * @param expression the boolean expression to check * @param message the {@link String#format(String, Object...)} exception message if invalid, not null * @param value the value to append to the message when invalid * @throws IllegalArgumentException if expression is {@code false} * @see #isTrue(boolean) * @see #isTrue(boolean, String, long) * @see #isTrue(boolean, String, Object...) */ public static void isTrue(final boolean expression, final String message, final double value) { if (!expression) { throw new IllegalArgumentException(String.format(message, Double.valueOf(value))); } } /** *

Validate that the argument condition is {@code true}; otherwise * throwing an exception with the specified message. This method is useful when * validating according to an arbitrary boolean expression, such as validating a * primitive number or using your own custom validation expression.

* *
     * Validate.isTrue(i >= min && i <= max, "The value must be between %d and %d", min, max);
     * Validate.isTrue(myObject.isOk(), "The object is not okay");
* * @param expression the boolean expression to check * @param message the {@link String#format(String, Object...)} exception message if invalid, not null * @param values the optional values for the formatted exception message, null array not recommended * @throws IllegalArgumentException if expression is {@code false} * @see #isTrue(boolean) * @see #isTrue(boolean, String, long) * @see #isTrue(boolean, String, double) */ public static void isTrue(final boolean expression, final String message, final Object... values) { if (!expression) { throw new IllegalArgumentException(String.format(message, values)); } } /** *

Validate that the argument condition is {@code true}; otherwise * throwing an exception. This method is useful when validating according * to an arbitrary boolean expression, such as validating a * primitive number or using your own custom validation expression.

* *
     * Validate.isTrue(i > 0);
     * Validate.isTrue(myObject.isOk());
* *

The message of the exception is "The validated expression is * false".

* * @param expression the boolean expression to check * @throws IllegalArgumentException if expression is {@code false} * @see #isTrue(boolean, String, long) * @see #isTrue(boolean, String, double) * @see #isTrue(boolean, String, Object...) */ public static void isTrue(final boolean expression) { if (!expression) { throw new IllegalArgumentException(DEFAULT_IS_TRUE_EX_MESSAGE); } } /** *

Validate that the specified argument is not {@code null}; * otherwise throwing an exception. * *

Validate.notNull(myObject, "The object must not be null");
* *

The message of the exception is "The validated object is * null".

* * @param the object type * @param object the object to check * @return the validated object (never {@code null} for method chaining) * @throws NullPointerException if the object is {@code null} * @see #notNull(Object, String, Object...) */ public static T notNull(final T object) { return notNull(object, DEFAULT_IS_NULL_EX_MESSAGE); } /** *

Validate that the specified argument is not {@code null}; * otherwise throwing an exception with the specified message. * *

Validate.notNull(myObject, "The object must not be null");
* * @param the object type * @param object the object to check * @param message the {@link String#format(String, Object...)} exception message if invalid, not null * @param values the optional values for the formatted exception message * @return the validated object (never {@code null} for method chaining) * @throws NullPointerException if the object is {@code null} * @see #notNull(Object) */ public static T notNull(final T object, final String message, final Object... values) { return Objects.requireNonNull(object, () -> String.format(message, values)); } }