Package jme3utilities

Class Validate

java.lang.Object
jme3utilities.Validate

public final class Validate extends Object
Utility methods to throw exceptions for invalid method arguments.

These methods are intended for checking the arguments of public/protected methods in library classes. To check return values, or the arguments of private/package methods, use assertions.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Logger
    message logger for this class
    static boolean
    If true, throw NullPointerException for null arguments.
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    axisIndex(int iValue, String description)
    Validate an axis index as a method argument.
    static boolean
    finite(float fValue, String description)
    Validate a finite single-precision value as a method argument.
    static boolean
    finite(Vector3f vector, String description)
    Validate a finite Vector3f as a method argument.
    static boolean
    finite(Vec3d vector, String description)
    Validate a finite Vec3d as a method argument.
    static boolean
    fraction(double dValue, String description)
    Validate a non-negative proper fraction as a method argument.
    static boolean
    fraction(float fValue, String description)
    Validate a non-negative proper fraction as a method argument.
    static boolean
    inRange(double dValue, String description, double min, double max)
    Validate a limited double-precision value as a method argument.
    static boolean
    inRange(float fValue, String description, float min, float max)
    Validate a limited single-precision value as a method argument.
    static boolean
    inRange(int iValue, String description, int min, int max)
    Validate a limited integer as a method argument.
    static boolean
    nonEmpty(float[] array, String description)
    Validate a non-null, non-empty float array as a method argument.
    static boolean
    nonEmpty(Object[] array, String description)
    Validate a non-null, non-empty object array as a method argument.
    static boolean
    nonEmpty(String string, String description)
    Validate a non-null, non-empty string as a method argument.
    static boolean
    nonEmpty(Collection collection, String description)
    Validate a non-null, non-empty collection as a method argument.
    static boolean
    nonNegative(float fValue, String description)
    Validate a non-negative single-precision value as a method argument.
    static boolean
    nonNegative(int iValue, String description)
    Validate a non-negative integer as a method argument.
    static boolean
    nonNegative(Vector3f vector, String description)
    Validate a non-negative Vector3f as a method argument.
    static boolean
    nonNull(Object object, String description)
    Validate a non-null reference.
    static boolean
    nonZero(long lValue, String description)
    Validate a non-zero long value as a method argument.
    static boolean
    nonZero(Quaternion quaternion, String description)
    Validate a non-zero Quaternion as a method argument.
    static boolean
    nonZero(Vector3f vector, String description)
    Validate a non-zero Vector3f as a method argument.
    static boolean
    nonZero(Quatd quaternion, String description)
    Validate a non-zero Quatd as a method argument.
    static boolean
    positive(float fValue, String description)
    Validate a positive single-precision value as a method argument.
    static boolean
    positive(int iValue, String description)
    Validate a positive integer value as a method argument.
    static boolean
    positive(Vector3f vector, String description)
    Validate an all-positive Vector3f as a method argument.
    static boolean
    require(boolean value, String what)
    Validate an arbitrary boolean-valued expression involving method arguments.
    static boolean
    standardAngle(float fValue, String description)
    Validate a standardized angle as a method argument.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • logger

      public static final Logger logger
      message logger for this class
    • throwNpe

      public static boolean throwNpe
      If true, throw NullPointerException for null arguments. Otherwise, throw IllegalArgumentException.
  • Method Details

    • axisIndex

      public static boolean axisIndex(int iValue, String description)
      Validate an axis index as a method argument.
      Parameters:
      iValue - the value to validate
      description - a description of the argument
      Returns:
      true
      Throws:
      IllegalArgumentException - if the value is outside the range [0, 2]
    • finite

      public static boolean finite(float fValue, String description)
      Validate a finite single-precision value as a method argument.
      Parameters:
      fValue - the value to validate
      description - a description of the argument
      Returns:
      true
      Throws:
      IllegalArgumentException - if the value is NaN or infinite
    • finite

      public static boolean finite(Vector3f vector, String description)
      Validate a finite Vector3f as a method argument.
      Parameters:
      vector - the vector to validate (unaffected)
      description - a description of the argument
      Returns:
      true
      Throws:
      IllegalArgumentException - if the vector has a NaN or infinite component
      NullPointerException - or IllegalArgumentException if the vector is null
    • finite

      public static boolean finite(Vec3d vector, String description)
      Validate a finite Vec3d as a method argument.
      Parameters:
      vector - the vector to validate (unaffected)
      description - a description of the argument
      Returns:
      true
      Throws:
      IllegalArgumentException - if the vector has a NaN or infinite component
      NullPointerException - or IllegalArgumentException if the vector is null
    • fraction

      public static boolean fraction(float fValue, String description)
      Validate a non-negative proper fraction as a method argument.
      Parameters:
      fValue - the value to validate
      description - a description of the argument
      Returns:
      true
      Throws:
      IllegalArgumentException - if the value is outside the range [0, 1]
    • fraction

      public static boolean fraction(double dValue, String description)
      Validate a non-negative proper fraction as a method argument.
      Parameters:
      dValue - the value to validate
      description - a description of the argument
      Returns:
      true
      Throws:
      IllegalArgumentException - if the value is outside the range [0, 1]
    • inRange

      public static boolean inRange(int iValue, String description, int min, int max)
      Validate a limited integer as a method argument.
      Parameters:
      iValue - the value to validate
      description - a description of the argument
      min - the smallest valid value (≤max)
      max - the largest valid value (≥max)
      Returns:
      true
      Throws:
      IllegalArgumentException - if the value is outside the range [min, max]

      Compare with Objects.checkIndex() in Java 9.

    • inRange

      public static boolean inRange(float fValue, String description, float min, float max)
      Validate a limited single-precision value as a method argument.
      Parameters:
      fValue - the value to validate
      description - a description of the argument
      min - the smallest valid value (≤max)
      max - the largest valid value (≥max)
      Returns:
      true
      Throws:
      IllegalArgumentException - if the value is outside the range
    • inRange

      public static boolean inRange(double dValue, String description, double min, double max)
      Validate a limited double-precision value as a method argument.
      Parameters:
      dValue - the value to validate
      description - a description of the argument
      min - the smallest valid value (≤max)
      max - the largest valid value (≥max)
      Returns:
      true
      Throws:
      IllegalArgumentException - if the value is outside the range
    • nonEmpty

      public static boolean nonEmpty(Collection collection, String description)
      Validate a non-null, non-empty collection as a method argument.
      Parameters:
      collection - the collection to validate (unaffected)
      description - a description of the argument
      Returns:
      true
      Throws:
      NullPointerException - or IllegalArgumentException if the collection is null
      IllegalArgumentException - if the collection is empty
    • nonEmpty

      public static boolean nonEmpty(float[] array, String description)
      Validate a non-null, non-empty float array as a method argument.
      Parameters:
      array - the array to validate (unaffected)
      description - a description of the argument
      Returns:
      true
      Throws:
      NullPointerException - or IllegalArgumentException if the array is null
      IllegalArgumentException - if the array has zero length
    • nonEmpty

      public static boolean nonEmpty(Object[] array, String description)
      Validate a non-null, non-empty object array as a method argument.
      Parameters:
      array - the array to validate (unaffected)
      description - a description of the argument
      Returns:
      true
      Throws:
      NullPointerException - or IllegalArgumentException if the array is null
      IllegalArgumentException - if the array has zero length
    • nonEmpty

      public static boolean nonEmpty(String string, String description)
      Validate a non-null, non-empty string as a method argument.
      Parameters:
      string - the String to validate
      description - a description of the argument
      Returns:
      true
      Throws:
      NullPointerException - or IllegalArgumentException if the String is null
      IllegalArgumentException - if the String has zero length
    • nonNegative

      public static boolean nonNegative(int iValue, String description)
      Validate a non-negative integer as a method argument.
      Parameters:
      iValue - the value to validate
      description - a description of the argument
      Returns:
      true
      Throws:
      IllegalArgumentException - if the value is negative
    • nonNegative

      public static boolean nonNegative(float fValue, String description)
      Validate a non-negative single-precision value as a method argument.
      Parameters:
      fValue - the value to validate
      description - a description of the argument
      Returns:
      true
      Throws:
      IllegalArgumentException - if the value is negative or NaN
    • nonNegative

      public static boolean nonNegative(Vector3f vector, String description)
      Validate a non-negative Vector3f as a method argument.
      Parameters:
      vector - the vector to validate (unaffected)
      description - a description of the argument
      Returns:
      true
      Throws:
      IllegalArgumentException - if the vector has a negative or NaN component
      NullPointerException - or IllegalArgumentException if the vector is null
    • nonNull

      public static boolean nonNull(Object object, String description)
      Validate a non-null reference. In many methods, validation can be omitted because the object in question is about to be dereferenced.

      While it might seem more logical to throw an IllegalArgumentException in the case of a method argument, the javadoc for NullPointerException says, "Applications should throw instances of this class to indicate other illegal uses of the null object." To throw an IllegalArgumentException instead, set throwNpe to false.

      Compare with java.util.Objects.requireNonNull().

      Parameters:
      object - the reference to validate
      description - a description of the argument
      Returns:
      true
      Throws:
      NullPointerException - or IllegalArgumentException if the reference is null
    • nonZero

      public static boolean nonZero(long lValue, String description)
      Validate a non-zero long value as a method argument.
      Parameters:
      lValue - the value to validate
      description - a description of the argument
      Returns:
      true
      Throws:
      IllegalArgumentException - if the value is zero
    • nonZero

      public static boolean nonZero(Quaternion quaternion, String description)
      Validate a non-zero Quaternion as a method argument.
      Parameters:
      quaternion - the Quaternion to validate (unaffected)
      description - a description of the argument
      Returns:
      true
      Throws:
      IllegalArgumentException - if the Quaternion equals (0,0,0,0)
      NullPointerException - or IllegalArgumentException if the Quaternion is null
    • nonZero

      public static boolean nonZero(Quatd quaternion, String description)
      Validate a non-zero Quatd as a method argument.
      Parameters:
      quaternion - the Quatd to validate (unaffected)
      description - a description of the argument
      Returns:
      true
      Throws:
      IllegalArgumentException - if the Quatd equals (0,0,0,0)
      NullPointerException - or IllegalArgumentException if the Quatd is null
    • nonZero

      public static boolean nonZero(Vector3f vector, String description)
      Validate a non-zero Vector3f as a method argument.
      Parameters:
      vector - the vector to validate (unaffected)
      description - a description of the argument
      Returns:
      true
      Throws:
      IllegalArgumentException - if the vector equals (0,0,0)
      NullPointerException - or IllegalArgumentException if the vector is null
    • positive

      public static boolean positive(int iValue, String description)
      Validate a positive integer value as a method argument.
      Parameters:
      iValue - the value to validate
      description - a description of the argument
      Returns:
      true
      Throws:
      IllegalArgumentException - if the value is negative or zero
    • positive

      public static boolean positive(float fValue, String description)
      Validate a positive single-precision value as a method argument.
      Parameters:
      fValue - the value to validate
      description - a description of the argument
      Returns:
      true
      Throws:
      IllegalArgumentException - if the value is not positive
    • positive

      public static boolean positive(Vector3f vector, String description)
      Validate an all-positive Vector3f as a method argument.
      Parameters:
      vector - the vector to validate (unaffected)
      description - a description of the argument
      Returns:
      true
      Throws:
      IllegalArgumentException - if any component is not positive
      NullPointerException - or IllegalArgumentException if the vector is null
    • require

      public static boolean require(boolean value, String what)
      Validate an arbitrary boolean-valued expression involving method arguments.
      Parameters:
      value - the value of the expression (required to be true)
      what - a description of the requirement
      Returns:
      true
      Throws:
      IllegalArgumentException - if the value is false
    • standardAngle

      public static boolean standardAngle(float fValue, String description)
      Validate a standardized angle as a method argument.
      Parameters:
      fValue - the value to validate
      description - a description of the argument
      Returns:
      true
      Throws:
      IllegalArgumentException - if the value is outside the range [-PI, PI]