org.geotoolkit.util
Class ArgumentChecks

Object
  extended by Static
      extended by ArgumentChecks

public final class ArgumentChecks
extends Static

Provides static methods for performing argument checks. The methods in this class throw one of the following exceptions (or a subclass of them) widely used in standard Java API:

Thrown by
Exception
NullPointerException ensureNonNull
IndexOutOfBoundsException ensureValidIndex
IllegalArgumentException ensurePositive, ensureStrictlyPositive, ensureBetween


Method Arguments
By convention, the value to check is always the last parameter given to every methods in this class. The other parameters may include the programmatic name of the argument being checked. This programmatic name is used for building an error message localized in the default locale if the check failed.

Since:
3.17
Version:
3.20
Author:
Martin Desruisseaux (Geomatys)
Module:
utility/geotk-utility (download)    View source code for this class

Method Summary
static void ensureBetween(String name, double min, double max, double value)
          Ensures that the given floating point value is between the given bounds, inclusive.
static void ensureBetween(String name, float min, float max, float value)
          Ensures that the given floating point value is between the given bounds, inclusive.
static void ensureBetween(String name, int min, int max, int value)
          Ensures that the given integer value is between the given bounds, inclusive.
static void ensureBetween(String name, long min, long max, long value)
          Ensures that the given long value is between the given bounds, inclusive.
static
<T> void
ensureCanCast(String name, Class<? extends T> expectedType, T value)
          Ensures that the specified value is null or an instance assignable to the given type.
static void ensureDimensionMatches(String name, DirectPosition position, int expected)
          Ensures that the given direct position has the expected number of dimensions.
static void ensureNonNull(String name, int index, Object[] array)
          Makes sure that an array element is non-null.
static void ensureNonNull(String name, Object object)
          Makes sure that an argument is non-null.
static void ensurePositive(String name, double value)
          Ensures that the given floating point value is greater than or equals to zero.
static void ensurePositive(String name, float value)
          Ensures that the given floating point value is greater than or equals to zero.
static void ensurePositive(String name, int value)
          Ensures that the given integer value is greater than or equals to zero.
static void ensurePositive(String name, long value)
          Ensures that the given long value is greater than or equals to zero.
static void ensureStrictlyPositive(String name, double value)
          Ensures that the given floating point value is greater than zero.
static void ensureStrictlyPositive(String name, float value)
          Ensures that the given floating point value is greater than zero.
static void ensureStrictlyPositive(String name, int value)
          Ensures that the given integer value is greater than zero.
static void ensureStrictlyPositive(String name, long value)
          Ensures that the given long value is greater than zero.
static void ensureValidIndex(int upper, int index)
          Ensures that the given index is equals or greater than zero and lower than the given upper value.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

ensureNonNull

public static void ensureNonNull(String name,
                                 Object object)
                          throws NullArgumentException
Makes sure that an argument is non-null. If the given object is null, then a NullArgumentException is thrown with a localized message containing the given name.

Parameters:
name - The name of the argument to be checked. Used only in case an exception is thrown.
object - The user argument to check against null value.
Throws:
NullArgumentException - if object is null.

ensureNonNull

public static void ensureNonNull(String name,
                                 int index,
                                 Object[] array)
                          throws NullArgumentException
Makes sure that an array element is non-null. If array[index] is null, then a NullArgumentException is thrown with a localized message containing the given name.

Parameters:
name - The name of the argument to be checked. Used only in case an exception is thrown.
index - Index of the element to check.
array - The user argument to check against null element.
Throws:
NullArgumentException - if array or array[index] is null.

ensureCanCast

public static <T> void ensureCanCast(String name,
                                     Class<? extends T> expectedType,
                                     T value)
                          throws IllegalArgumentException
Ensures that the specified value is null or an instance assignable to the given type. If this method does not thrown an exception, then the value can be casted to the class represented by expectedType without throwing a ClassCastException.

Type Parameters:
T - The compile-time type of the value.
Parameters:
name - The name of the argument to be checked, used only if an exception is thrown. Can be null if the name is unknown.
expectedType - the expected type (class or interface).
value - The value to check, or null.
Throws:
IllegalArgumentException - if value is non-null and is not assignable to the given type.
Since:
3.18

ensureValidIndex

public static void ensureValidIndex(int upper,
                                    int index)
                             throws IndexOutOfBoundsException
Ensures that the given index is equals or greater than zero and lower than the given upper value. This method is primarily designed for methods that expect only an index argument. For this reason, this method does not take the argument name.

Parameters:
upper - The maximal index value, exclusive.
index - The index to check.
Throws:
IndexOutOfBoundsException - If the given index is negative or not lower than the given upper value.

ensurePositive

public static void ensurePositive(String name,
                                  int value)
                           throws IllegalArgumentException
Ensures that the given integer value is greater than or equals to zero.

Parameters:
name - The name of the argument to be checked, used only if an exception is thrown.
value - The user argument to check.
Throws:
IllegalArgumentException - if the given value is negative.

ensurePositive

public static void ensurePositive(String name,
                                  long value)
                           throws IllegalArgumentException
Ensures that the given long value is greater than or equals to zero.

Parameters:
name - The name of the argument to be checked, used only if an exception is thrown.
value - The user argument to check.
Throws:
IllegalArgumentException - if the given value is negative.

ensurePositive

public static void ensurePositive(String name,
                                  float value)
                           throws IllegalArgumentException
Ensures that the given floating point value is greater than or equals to zero.

Parameters:
name - The name of the argument to be checked, used only if an exception is thrown.
value - The user argument to check.
Throws:
IllegalArgumentException - if the given value is NaN or negative.

ensurePositive

public static void ensurePositive(String name,
                                  double value)
                           throws IllegalArgumentException
Ensures that the given floating point value is greater than or equals to zero.

Parameters:
name - The name of the argument to be checked, used only if an exception is thrown.
value - The user argument to check.
Throws:
IllegalArgumentException - if the given value is NaN or negative.

ensureStrictlyPositive

public static void ensureStrictlyPositive(String name,
                                          int value)
                                   throws IllegalArgumentException
Ensures that the given integer value is greater than zero.

Parameters:
name - The name of the argument to be checked, used only if an exception is thrown.
value - The user argument to check.
Throws:
IllegalArgumentException - if the given value is negative or equals to zero.

ensureStrictlyPositive

public static void ensureStrictlyPositive(String name,
                                          long value)
                                   throws IllegalArgumentException
Ensures that the given long value is greater than zero.

Parameters:
name - The name of the argument to be checked, used only if an exception is thrown.
value - The user argument to check.
Throws:
IllegalArgumentException - if the given value is negative or equals to zero.

ensureStrictlyPositive

public static void ensureStrictlyPositive(String name,
                                          float value)
                                   throws IllegalArgumentException
Ensures that the given floating point value is greater than zero.

Parameters:
name - The name of the argument to be checked, used only if an exception is thrown.
value - The user argument to check.
Throws:
IllegalArgumentException - if the given value is NaN, zero or negative.

ensureStrictlyPositive

public static void ensureStrictlyPositive(String name,
                                          double value)
                                   throws IllegalArgumentException
Ensures that the given floating point value is greater than zero.

Parameters:
name - The name of the argument to be checked, used only if an exception is thrown.
value - The user argument to check.
Throws:
IllegalArgumentException - if the given value is NaN, zero or negative.

ensureBetween

public static void ensureBetween(String name,
                                 int min,
                                 int max,
                                 int value)
                          throws IllegalArgumentException
Ensures that the given integer value is between the given bounds, inclusive. This method is used for checking values that are not index.

Parameters:
name - The name of the argument to be checked. Used only in case an exception is thrown.
min - The minimal value, inclusive.
max - The maximal value, inclusive.
value - The value to be tested.
Throws:
IllegalArgumentException - if the given value is not in the given range.
See Also:
ensureValidIndex(int, int)

ensureBetween

public static void ensureBetween(String name,
                                 long min,
                                 long max,
                                 long value)
                          throws IllegalArgumentException
Ensures that the given long value is between the given bounds, inclusive.

Parameters:
name - The name of the argument to be checked. Used only in case an exception is thrown.
min - The minimal value, inclusive.
max - The maximal value, inclusive.
value - The value to be tested.
Throws:
IllegalArgumentException - if the given value is not in the given range.

ensureBetween

public static void ensureBetween(String name,
                                 float min,
                                 float max,
                                 float value)
                          throws IllegalArgumentException
Ensures that the given floating point value is between the given bounds, inclusive.

Parameters:
name - The name of the argument to be checked. Used only in case an exception is thrown.
min - The minimal value, inclusive.
max - The maximal value, inclusive.
value - The value to be tested.
Throws:
IllegalArgumentException - if the given value is NaN or not in the given range.

ensureBetween

public static void ensureBetween(String name,
                                 double min,
                                 double max,
                                 double value)
                          throws IllegalArgumentException
Ensures that the given floating point value is between the given bounds, inclusive.

Parameters:
name - The name of the argument to be checked. Used only in case an exception is thrown.
min - The minimal value, inclusive.
max - The maximal value, inclusive.
value - The value to be tested.
Throws:
IllegalArgumentException - if the given value is NaN or not in the given range.

ensureDimensionMatches

public static void ensureDimensionMatches(String name,
                                          DirectPosition position,
                                          int expected)
                                   throws MismatchedDimensionException
Ensures that the given direct position has the expected number of dimensions. This method does nothing if the direct position is null.

Parameters:
name - The name of the argument to be checked. Used only in case an exception is thrown.
position - The direct position to check for its dimension.
expected - The expected number of dimensions.
Throws:
MismatchedDimensionException - If the given direct position is non-null and does not have the expected number of dimensions.
Since:
3.20


Copyright © 2009-2012 Geotoolkit.org. All Rights Reserved.