Class ObjectUtils

java.lang.Object
net.solarnetwork.util.ObjectUtils

public final class ObjectUtils extends Object
Utilities for dealing with objects.
Version:
1.1
Author:
matt
  • Constructor Details

    • ObjectUtils

      public ObjectUtils()
  • Method Details

    • requireNonNullArgument

      public static <T> T requireNonNullArgument(T arg, String argumentName)
      Require a non-null method argument.

      This is similar to Objects.requireNonNull(Object, String) except argumentName is just the name of the required argument and an IllegalArgumentException is thrown instead of a NullPointerException. Example use:

       public Foo(Bar bar, Baz baz) {
              this.bar = ObjectUtils.requireNonNullArgument(bar, "bar");
              this.baz = ObjectUtils.requireNonNullArgument(baz, "baz");
       }
       
      Type Parameters:
      T - the argument type
      Parameters:
      arg - the argument to require to be non-null
      argumentName - the name of arg to report in the IllegalArgumentException if arg is null
      Returns:
      arg
      Throws:
      IllegalArgumentException - if arg is null
    • requireNonEmptyArgument

      public static <T> T[] requireNonEmptyArgument(T[] arg, String argumentName)
      Require a non-empty array method argument.

      This is similar to Objects.requireNonNull(Object, String) except argumentName is just the name of the required argument and an IllegalArgumentException is thrown instead of a NullPointerException. Example use:

       public Foo(Bar[] bar) {
              this.bar = ObjectUtils.requireNonNullArgument(bar, "bar");
       }
       
      Type Parameters:
      T - the argument type
      Parameters:
      arg - the argument to require to be non-empty
      argumentName - the name of arg to report in the IllegalArgumentException if arg is null
      Returns:
      arg
      Throws:
      IllegalArgumentException - if arg is null or empty
    • requireNonEmptyArgument

      public static <T extends Collection<?>> T requireNonEmptyArgument(T arg, String argumentName)
      Require a non-empty array method argument.

      This is similar to Objects.requireNonNull(Object, String) except argumentName is just the name of the required argument and an IllegalArgumentException is thrown instead of a NullPointerException. Example use:

       public Foo(List<Bar> bar) {
              this.bar = ObjectUtils.requireNonNullArgument(bar, "bar");
       }
       
      Type Parameters:
      T - the argument type
      Parameters:
      arg - the argument to require to be non-empty
      argumentName - the name of arg to report in the IllegalArgumentException if arg is null
      Returns:
      arg
      Throws:
      IllegalArgumentException - if arg is null or empty