Overview

From org.apache.commons.lang3.Validate javadoc:

This class assists in validating arguments. The validation methods are based along the following principles:

  • An invalid null argument causes a java.lang.NullPointerException.
  • A non-null argument causes an java.lang.IllegalArgumentException.
  • An invalid index into an array/collection/map/string causes an java.lang.IndexOutOfBoundsException.

Since Java 7 the java.util.Objects provides some validation methods which checks for null. The org.apache.commons.lang3.Validate provides much more validation methods and the ability to construct (validation error) messages with parameters.

Some Methods

Validate not null

Some(final Object argument) {
  Validate.notNull(argument, "Argument must not be null!");
  this.prop = argument;
  ...
java.lang.NullPointerException: Argument must not be null!
	at org.apache.commons.lang3.Validate.notNull(Validate.java:222)
    ...

java.util.Objects alternative

Some(final Object argument) {
  this.prop = Objects.requireNonNull(argument,"Argument must not be null!");
  ...
java.lang.NullPointerException: Argument must not be null!
	at java.util.Objects.requireNonNull(Objects.java:228)
    ...

Validate not empty

With the various notEmpty methods one can check

  • array
  • java.util.Collection
  • java.util.Map
  • java.lang.CharSequence

for emptiness. If the argument is null, a java.lang.NullPointerException is thrown. In the case of an empty element which means the collection etc. contains no elements, a java.lang.IllegalArgumentException is thrown (see above).

References