Assert

object Assert

Assertion tool class

Since

3.0.0

class Object
trait Matchable
class Any

Value members

Concrete methods

def isTrue(expression: Boolean): Unit

Validate that the argument condition is 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 that the argument condition is 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.

Assert.isTrue(i > 0);
Assert.isTrue(myObject.isOk());

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

Value Params
expression

the boolean expression to check

Throws
IllegalArgumentException

if expression is false

See also

#isTrue(boolean, String, Object...)

def isTrue(expression: Boolean, message: String, values: AnyRef*): Unit

Validate that the argument condition is 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 that the argument condition is 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.

Assert.isTrue(i >= min && i <= max, "The value must be between %d and %d", min, max);
Assert.isTrue(myObject.isOk(), "The object is not okay");
Value Params
expression

the boolean expression to check

message

the [[String#format(String, Object...)]] exception message if invalid, not null

values

the optional values for the formatted exception message, null array not recommended

Throws
IllegalArgumentException

if expression is false

See also

#isTrue(boolean)

def noNullElements[T <: Iterable[_]](iterable: T, message: String, values: AnyRef*): T

Validate that the specified argument iterable is neither null nor contains any elements that are null; otherwise throwing an exception with the specified message.

Validate that the specified argument iterable is neither null nor contains any elements that are null; otherwise throwing an exception with the specified message.

Assert.noNullElements(myCollection, "The collection contains null at position %d");

If the iterable is null, then the message in the exception is "The validated object is null".

If the iterable has a null element, then the iteration index of the invalid element is appended to the values argument.

Value Params
<T>

the iterable type

iterable

the iterable to check, validated not null by this method

message

the [[String#format(String, Object...)]] exception message if invalid, not null

values

the optional values for the formatted exception message, null array not recommended

Returns

the validated iterable (never null method for chaining)

Throws
IllegalArgumentException

if an element is null

NullPointerException

if the array is null

def notEmpty[T <: CharSequence](chars: T): T

Validate that the specified argument array is neither null nor a length of zero (no elements); otherwise throwing an exception.

Validate that the specified argument array is neither null nor a length of zero (no elements); otherwise throwing an exception.

Assert.notEmpty(myArray);

The message in the exception is "The validated array is empty".

Value Params
<T>

the array type

Returns

the validated array (never null method for chaining)

Throws
IllegalArgumentException

if the array is empty

NullPointerException

if the array is null

def notEmpty[T <: CharSequence](chars: T, message: String, values: AnyRef*): T

Validate that the specified argument character sequence is neither null nor a length of zero (no characters); otherwise throwing an exception with the specified message.

Validate that the specified argument character sequence is neither null nor a length of zero (no characters); otherwise throwing an exception with the specified message.

Assert.notEmpty(myString);

The message in the exception is "The validated character sequence is empty".

Value Params
<T>

the character sequence type

chars

the character sequence to check, validated not null by this method

Returns

the validated character sequence (never null method for chaining)

Throws
IllegalArgumentException

if the character sequence is empty

NullPointerException

if the character sequence is null

def notNull[T](value: T): T

Validate that the specified argument is not null; otherwise throwing an exception.

Validate that the specified argument is not null; otherwise throwing an exception.

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

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

Value Params
<T>

the object type

value

the object to check

Returns

the validated object (never null for method chaining)

Throws
NullPointerException

if the object is null

See also

#notNull(Object, String, Object...)

def notNull[T](value: T, message: String, values: AnyRef*): T

Validate that the specified argument is not null; otherwise throwing an exception with the specified message.

Validate that the specified argument is not null; otherwise throwing an exception with the specified message.

Assert.notNull(myObject, "The object must not be null");
Value Params
<T>

the object type

message

the [[String#format(String, Object...)]] exception message if invalid, not null

object

the object to check

values

the optional values for the formatted exception message

Returns

the validated object (never null for method chaining)

Throws
NullPointerException

if the object is null

See also

#notNull(Object)