Class ArgChecks
- java.lang.Object
-
- org.hansken.plugin.extraction.util.ArgChecks
-
public final class ArgChecks extends Object
A collection of methods for defensively checking arguments passed to methods.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Object[]
argNotAllNull(String name, Object... values)
Check that not all values with given name arenull
, otherwise throw an exception.static String
argNotEmpty(String name, String value)
Check that the string with given name is notnull
or empty, otherwise throw an exception.static <T> Collection<T>
argNotEmpty(String name, Collection<T> value)
Check that the collection value with given name is not empty, otherwise throw an exception.static <T> T[]
argNotEmpty(String name, T[] value)
Check that the array value with given name is not empty, otherwise throw an exception.static float
argNotNegative(String name, float value)
Check that the float with given name is not negative, otherwise throw an exception.static int
argNotNegative(String name, int value)
Check that the int with given name is not negative, otherwise throw an exception.static long
argNotNegative(String name, long value)
Check that the long with given name is not negative, otherwise throw an exception.static <T> T
argNotNull(String name, T value)
Check that the value with given name is notnull
, otherwise throw an exception.static <T> List<T>
argsIsType(String name, List<T> value, Class<?> type)
Check that the value with given name is of typetype
, otherwise throw an exception.
-
-
-
Method Detail
-
argNotNull
public static <T> T argNotNull(String name, T value)
Check that the value with given name is notnull
, otherwise throw an exception.Example usage:
private final Author author; public Book(final Author author) { // this throws a NullPointerException when the author is null this.author = argNotNull("author", author); }
- Type Parameters:
T
- the type of the value- Parameters:
name
- a descriptive name for the value (most likely the argument name)value
- the value itself- Returns:
- the value, so it can be used to inline in an assignment
- Throws:
NullPointerException
- ifvalue == null
-
argsIsType
public static <T> List<T> argsIsType(String name, List<T> value, Class<?> type)
Check that the value with given name is of typetype
, otherwise throw an exception.Example usage:
public Book(final List<Object> authors) { // this throws a ClassCastException when authors contains a type other than Author.class argsIsType("author", authors, Author.class); }
- Type Parameters:
T
- the type of the value- Parameters:
name
- a descriptive name for the value (most likely the argument name)value
- the value itselftype
- the class type we want to check againstvalue
- Returns:
- the value, so it can be used to inline in an assignment
- Throws:
ClassCastException
- ifvalue.allMatch(type::isInstance)
is false
-
argNotAllNull
public static Object[] argNotAllNull(String name, Object... values)
Check that not all values with given name arenull
, otherwise throw an exception.Example usage:
private final Author author; public Book(final Author... authors) { // this throws a NullPointerException when the author is null this.author = argNotAllNull("authors", authors); }
- Parameters:
name
- a descriptive name for the values (most likely the argument name)values
- the values, which can be different types- Returns:
- the values, so it can be used to inline in an assignment
- Throws:
NullPointerException
- ifvalues.allMatch(Objects::isNull)
-
argNotEmpty
public static <T> T[] argNotEmpty(String name, T[] value)
Check that the array value with given name is not empty, otherwise throw an exception.Example usage:
private final Author[] author; public Book(final Author[] author) { // this throws a NullPointerException when the author array is empty this.author = argNotEmpty("author", author); }
- Type Parameters:
T
- the type of the value- Parameters:
name
- a descriptive name for the value (most likely the argument name)value
- the value itself- Returns:
- the value, so it can be used to inline in an assignment
- Throws:
IllegalArgumentException
- ifvalue == null || value.length == 0
-
argNotEmpty
public static <T> Collection<T> argNotEmpty(String name, Collection<T> value)
Check that the collection value with given name is not empty, otherwise throw an exception.Example usage:
private final Collection<Author> author; public Book(final Collection<Author> author) { // this throws a NullPointerException when the author collection is empty this.author = argNotEmpty("author", author); }
- Type Parameters:
T
- the type of the value- Parameters:
name
- a descriptive name for the value (most likely the argument name)value
- the value itself- Returns:
- the value, so it can be used to inline in an assignment
- Throws:
IllegalArgumentException
- ifvalue == null || value.isEmpty()
-
argNotEmpty
public static String argNotEmpty(String name, String value)
Check that the string with given name is notnull
or empty, otherwise throw an exception.Example usage:
private final String title; public Book(final String title) { // this throws a NullPointerException when the author is null // or an IllegalArgumentException when author is an empty String this.title = argNotEmpty("title", title); }
- Parameters:
name
- a descriptive name for the string (most likely the argument name)value
- the string itself- Returns:
- the string, so it can be used to inline in an assignment
- Throws:
NullPointerException
- ifvalue == null
IllegalArgumentException
- ifvalue.isEmpty()
-
argNotNegative
public static int argNotNegative(String name, int value)
Check that the int with given name is not negative, otherwise throw an exception. Example usage:private final int pageCount; public Book(final int pageCount) { // this throws an IllegalArgumentException when pageCount is negative this.pageCount = argNotNegative("pageCount", pageCount); }
- Parameters:
name
- a descriptive name for the int (most likely the argument name)value
- the int itself- Returns:
- the int, so it can be used in an inline assignment
- Throws:
IllegalArgumentException
- ifvalue < 0
-
argNotNegative
public static long argNotNegative(String name, long value)
Check that the long with given name is not negative, otherwise throw an exception. Example usage:private final long pageCount; public Book(final long pageCount) { // this throws an IllegalArgumentException when pageCount is negative this.pageCount = argNotNegative("pageCount", pageCount); }
- Parameters:
name
- a descriptive name for the long (most likely the argument name)value
- the long itself- Returns:
- the long, so it can be used in an inline assignment
- Throws:
IllegalArgumentException
- ifvalue < 0
-
argNotNegative
public static float argNotNegative(String name, float value)
Check that the float with given name is not negative, otherwise throw an exception. Example usage:private final float pageCount; public Book(final float pageCount) { // this throws an IllegalArgumentException when pageCount is negative this.pageCount = argNotNegative("pageCount", pageCount); }
- Parameters:
name
- a descriptive name for the float (most likely the argument name)value
- the float itself- Returns:
- the float, so it can be used in an inline assignment
- Throws:
IllegalArgumentException
- ifvalue < 0
-
-