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 <code>true</code>; 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 <code>true</code>; 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.

<pre> Assert.isTrue(i &gt; 0); Assert.isTrue(myObject.isOk()); </pre>

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

Value Params
expression

the boolean expression to check

Throws
IllegalArgumentException

if expression is <code>false</code>

See also

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

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

Validate that the argument condition is <code>true</code>; 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 <code>true</code>; 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.

<pre> Assert.isTrue(i &gt;= min &amp;&amp; i &lt;= max, &quot;The value must be between %d and %d&quot;, min, max); Assert.isTrue(myObject.isOk(), &quot;The object is not okay&quot;); </pre>

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 <code>false</code>

See also

#isTrue(boolean)

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

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

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

<pre> Assert.noNullElements(myCollection, &quot;The collection contains null at position %d&quot;); </pre>

If the iterable is <code>null</code>, then the message in the exception is "The validated object is null".

If the iterable has a <code>null</code> element, then the iteration index of the invalid element is appended to the <code>values</code> 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 <code>null</code> method for chaining)

Throws
IllegalArgumentException

if an element is <code>null</code>

NullPointerException

if the array is <code>null</code>

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

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

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

<pre> Assert.notEmpty(myArray); </pre>

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

Value Params
<T>

the array type

Returns

the validated array (never <code>null</code> method for chaining)

Throws
IllegalArgumentException

if the array is empty

NullPointerException

if the array is <code>null</code>

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

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

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

<pre> Assert.notEmpty(myString); </pre>

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 <code>null</code> method for chaining)

Throws
IllegalArgumentException

if the character sequence is empty

NullPointerException

if the character sequence is <code>null</code>

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

Validate that the specified argument is not <code>null</code>; otherwise throwing an exception.

Validate that the specified argument is not <code>null</code>; otherwise throwing an exception.

<pre> Assert.notNull(myObject, &quot;The object must not be null&quot;); </pre>

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 <code>null</code> for method chaining)

Throws
NullPointerException

if the object is <code>null</code>

See also

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

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

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

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

<pre> Assert.notNull(myObject, &quot;The object must not be null&quot;); </pre>

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 <code>null</code> for method chaining)

Throws
NullPointerException

if the object is <code>null</code>

See also

#notNull(Object)