Package com.mastfrog.util.preconditions
Class Checks
java.lang.Object
com.mastfrog.util.preconditions.Checks
Code sanity checks, to simplify things like null checks. Use these where
applicable instead of directly throwing exceptions. That way, expensive tests
can be switched to use assertions for production.
- Author:
- Tim Boudreau
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidatLeastOneNotNull(String msg, Object... objects) static longcanCastToInt(String name, long value) static voidencodable(CharSequence seq, Charset charset) Test that a character sequence is encodable in the passe charset.static Pathexecutable(String paramName, Path file) static Pathexecutable(Path file) Test that file.static PathAsserts that a file must exist.static PathAsserts that a file must exist.static voidfileExists(File file) Test that file exists and is a file not a folder.static FilefileExists(String paramName, File file) Test that file exists and is a file not a folder.static PathfileExists(String paramName, Path file) Test that file exists and is a file not a folder.static PathfileExists(Path file) static voidfolderExists(File file) Test that a file exists and is a folder.static FilefolderExists(String paramName, File file) Test that a file exists and is a folder.static PathfolderExists(String paramName, Path file) Test that file exists and is a file not a folder.static intgreaterThanOne(String name, int val) Determine that the passed argument is greater than one.static longgreaterThanOne(String name, long val) Determine that the passed argument is greater than one.static <T extends Number>
TgreaterThanOne(String name, T val) Determine that the passed argument is greater than one.static intgreaterThanZero(String name, int val) Determine that the passed argument >=0.static longgreaterThanZero(String name, long val) Determine that the passed argument >=1.static <T extends Number>
TgreaterThanZero(String name, T val) Determine that the passed argument >=0.static StringDetermine if a string value contains only digits.static voidisInstance(String name, Class<?> type, Object value) Determine if an object is an instance of a given typestatic voidisOfLength(String name, int length, Object[] value) Check that the given argument is of the given length, throw an InvalidArgumentException if that is not the case.static voidmayNotContain(String name, CharSequence value, char... chars) Throws an exception if the passed string contains any of the passed charactersstatic voidmayNotStartWith(String name, CharSequence value, char c) Throws an exception if the passed string starts with the passed characterstatic voidmustContain(String name, CharSequence value, char c) Throws an exception if the passed value does not contain at least one of the passed characterstatic voidnoDuplicates(String name, Collection<?> collection) Throws an exception if the passed set contains duplicate elements.static intnonNegative(String name, int val) Determine that the passed argument is not a negative numberstatic longnonNegative(String name, long val) Determine that the passed argument is not a negative number.static <T extends Number>
TnonNegative(String name, T val) Determine that the passed argument is not a negative numberstatic <T> T[]noNullElements(String name, T... arr) Verify that an array does not contain null elements, throwing an exception if it does.static intThrows an exception if a parameter is equal to zero.static longThrows an exception if a parameter is equal to zero.static <T extends Number>
TThrows an exception if a parameter is equal to zero.static voidnotEmpty(String name, Collection<?> collection) static <T extends CharSequence>
TThrows an exception if the passed string is emptystatic <T> TnotEmptyOrNull(String name, T array) Determine that the passed array argument is an array is not null has a length > 0static <T> TDetermine that the passed parameter is not nullstatic <T extends CharSequence>
TnotNullOrEmpty(String name, T value) Throws an exception if the passed string is null or emptystatic voidstatic voidTest that a file exists and the current user has read permission.static FileTest that a file exists and the current user has read permission.static PathTest that a file exists and is readable.static PathTest that file.static FilereadableAndNonZeroLength(String paramName, File file) Test that a file exists and the current user has read permission.static PathregularFile(String paramName, Path file, LinkOption... opts) Assert that a file exists and is a regular file.static PathregularFile(Path file) Assert that a file exists and is a regular file.static PathregularFile(Path file, LinkOption... opts) Assert that a file exists and is a regular file.static Pathstatic PathTest that file.
-
Method Details
-
notSame
-
isDigits
Determine if a string value contains only digits.- Parameters:
name-value-- Returns:
-
canCastToInt
-
atLeastOneNotNull
-
notNull
Determine that the passed parameter is not null- Parameters:
name- The name of the parameterval- The value of the parameter- Throws:
NullPointerException- if the name is nullNullArgumentException- if the value is null
-
nonNegative
Determine that the passed argument is not a negative number- Parameters:
name- The name of the argumentval- The value- Throws:
InvalidArgumentException- if the number is negative
-
nonNegative
Determine that the passed argument is not a negative number- Parameters:
name- The name of the argumentval- The value- Throws:
InvalidArgumentException- if the number is negative
-
nonNegative
Determine that the passed argument is not a negative number.- Parameters:
name- The name of the argumentval- The value- Throws:
InvalidArgumentException- if the number is negative
-
greaterThanOne
Determine that the passed argument is greater than one.- Parameters:
name- The name of the argumentval- The value- Throws:
InvalidArgumentException- if the number is negative
-
greaterThanOne
Determine that the passed argument is greater than one.- Parameters:
name- The name of the argumentval- The value- Throws:
InvalidArgumentException- if the number is negative
-
greaterThanOne
Determine that the passed argument is greater than one.- Parameters:
name- The name of the argumentval- The value- Throws:
InvalidArgumentException- if the number is negative
-
greaterThanZero
Determine that the passed argument >=0.- Parameters:
name- The name of the argumentval- The value- Throws:
InvalidArgumentException- if the number is negative
-
greaterThanZero
Determine that the passed argument >=0.- Parameters:
name- The name of the argumentval- The value- Throws:
InvalidArgumentException- if the number is negative
-
greaterThanZero
Determine that the passed argument >=1.- Parameters:
name- The name of the argumentval- The value- Throws:
InvalidArgumentException- if the number is negative
-
notEmptyOrNull
Determine that the passed array argument- is an array
- is not null
- has a length > 0
- Parameters:
name- The parameter name for use in an exceptionarray- An array- Throws:
InvalidArgumentException- if array is not an arrayInvalidArgumentException
-
noNullElements
Verify that an array does not contain null elements, throwing an exception if it does. Do not use for primitive array types - they cannot contain nulls anyway- Parameters:
name- The name of the parameterarr- An array of some sort- Throws:
InvalidArgumentException- if the array has null elements, or if array is an array of primitive types (these cannot contain null values and do not need such checking)NullArgumentException- if the passed array is null
-
nonZero
Throws an exception if a parameter is equal to zero.- Parameters:
name- The name of the parameterval- A number- Throws:
InvalidArgumentException- if the value of the number equals 0
-
nonZero
Throws an exception if a parameter is equal to zero.- Parameters:
name- The name of the parameterval- A number- Throws:
InvalidArgumentException- if the value of the number equals 0
-
nonZero
Throws an exception if a parameter is equal to zero.- Parameters:
name- The name of the parameterval- A number- Throws:
InvalidArgumentException- if the value of the number equals 0
-
notEmpty
Throws an exception if the passed string is empty- Parameters:
name- The name of the string used in the exceptionvalue- A string value- Throws:
InvalidArgumentException- if the String is empty, but not if it is null
-
notEmpty
-
notNullOrEmpty
Throws an exception if the passed string is null or empty- Parameters:
name- The parameter name for use in the exception messagevalue- The value- Throws:
InvalidArgumentException- if the value is null or "".equals(value)NullArgumentException- if the passed value is null
-
mayNotContain
Throws an exception if the passed string contains any of the passed characters- Parameters:
name- The parameter name for use in the exception messagevalue- The value (may be null - use notNull(name, value) for null checkschars- An array of characters- Throws:
InvalidArgumentException- if the value contains any of the passed characters, or if the passed character array length is 0NullArgumentException- if the passed char array is null
-
mustContain
Throws an exception if the passed value does not contain at least one of the passed character- Parameters:
name- The parameter name for constructing the exception messagevalue- A valuec- A character which must be present
-
noDuplicates
Throws an exception if the passed set contains duplicate elements. Note: do not use this method on very large collections or lazily resolved collections, as it will trigger resolving and iterating the entire collection.- Parameters:
name- The parameter name for constructing the exception messagecollection- A collection of something
-
mayNotStartWith
Throws an exception if the passed string starts with the passed character- Parameters:
name- The parameter name for constructing the exception messagevalue- a string or equivalent. May be null, use notNull() for null checks.- Throws:
InvalidArgumentException- if the passed value starts with the passed character
-
isInstance
Determine if an object is an instance of a given type- Parameters:
name- The parameter name for constructing the exception messagetype- The type soughtvalue- The object- Throws:
ClassCastException- if the passed object is of the wrong type
-
isOfLength
Check that the given argument is of the given length, throw an InvalidArgumentException if that is not the case.- Parameters:
name- The parameter name for constructing the exception messagelength- The required length of the given valuevalue- The value to check the length of- Throws:
InvalidArgumentException- if the length of the passed in value is not equal to the specified required lengthNullPointerException- if the name is nullNullArgumentException- if the value is null
-
fileExists
Test that file exists and is a file not a folder.- Parameters:
file- The file
-
exists
Asserts that a file must exist.- Parameters:
file- The file
-
exists
Asserts that a file must exist.- Parameters:
paramName- the parameter namefile- The file
-
readable
Test that file.- Parameters:
file- The file- Returns:
- the file
-
readable
Test that a file exists and is readable.- Parameters:
paramName- The parameter namefile- The file- Returns:
- the path
-
regularFile
Assert that a file exists and is a regular file.- Parameters:
file- The path- Returns:
- The file
-
regularFile
Assert that a file exists and is a regular file.- Parameters:
file- The pathopts- link options for Files.isRegularFile()- Returns:
- The file
-
regularFile
Assert that a file exists and is a regular file.- Parameters:
paramName- the parameter namefile- The pathopts- link options for Files.isRegularFile()- Returns:
- The file
-
writable
Test that file.- Parameters:
file- The file
-
writable
-
executable
Test that file.- Parameters:
file- The file
-
executable
-
fileExists
-
fileExists
Test that file exists and is a file not a folder.- Parameters:
file- The file
-
folderExists
Test that file exists and is a file not a folder.- Parameters:
file- The file
-
fileExists
Test that file exists and is a file not a folder.- Parameters:
file- The fileparamName- The name of the method parameter
-
folderExists
Test that a file exists and is a folder.- Parameters:
file- The file
-
folderExists
Test that a file exists and is a folder.- Parameters:
file- The file
-
readable
Test that a file exists and the current user has read permission.- Parameters:
file- A file
-
readable
Test that a file exists and the current user has read permission.- Parameters:
file- A file
-
readableAndNonZeroLength
Test that a file exists and the current user has read permission.- Parameters:
file- A file
-
encodable
Test that a character sequence is encodable in the passe charset.- Parameters:
seq- A stringcharset- A character set
-