edu.mit.jverbnet.util
Class Checks

java.lang.Object
  extended by edu.mit.jverbnet.util.Checks

public class Checks
extends java.lang.Object

Utilities for checking variables.

Since:
JVerbnet 1.0.0
Version:
1.2.0
Author:
Mark A. Finlayson

Nested Class Summary
static class Checks.Check<S>
          Abstract implementation of a check.
static class Checks.Condition<S>
          Abstract implementation of a condition.
static interface Checks.ICheck<S>
          A check object provides the ability to throw an exception when a specified object satisfies the condition.
static interface Checks.ICondition<S>
          A condition object defines a boolean condition over a set of objects.
static interface Checks.IMask<S>
          A mask object provides the ability to substitute different values for a specified object if it matches the condition.
static interface Checks.IReallocationStrategy
          A reallocation strategy defines if and how a collection should be reallocated.
static class Checks.Mask<S>
          Abstract implementation of a mask.
 
Field Summary
static Checks.IReallocationStrategy DoNotReallocate
          A reallocation strategy that does nothing.
static java.lang.String genericVarName
          The default variable name to use in the exceptions thrown by this class, if the specific variable name is null, empty, or all whitespace.
static Checks.Mask<java.lang.String> IsBlank
          A mask/check with that deals with strings.
static Checks.Mask<java.lang.String> IsEmpty
          A mask/check with that deals with strings.
static Checks.Mask<java.util.Collection<?>> IsEmptyCol
          A mask/check object that deals with collections.
static Checks.Mask<java.lang.String> IsEmptyOrBlank
          A mask/check with that deals with strings.
static Checks.Mask<java.lang.Object> IsNull
          A mask/check with that deals with objects.
static Checks.Mask<java.lang.String> IsNullEmptyOrBlank
          A mask/check with that deals with strings.
static Checks.Mask<java.lang.String> IsNullOrBlank
          A mask/check with that deals with strings.
static Checks.Mask<java.lang.String> IsNullOrEmpty
          A mask/check with that deals with strings.
static Checks.Mask<java.util.Collection<?>> IsNullOrEmptyCol
          A mask/check object that deals with collections.
static Checks.IReallocationStrategy ModifiableLeaveNulls
          Reallocates collections as modifiable collections.
static Checks.IReallocationStrategy ModifiableMaskNullWithEmpty
          Reallocates collections as modifiable collections.
static Checks.Mask<java.lang.String> NotBlank
          A mask/check with that deals with strings.
static Checks.Mask<java.lang.String> NotEmpty
          A mask/check with that deals with strings.
static Checks.Mask<java.util.Collection<?>> NotEmptyCol
          A mask/check object that deals with collections.
static Checks.Mask<java.lang.String> NotEmptyOrBlank
          A mask/check with that deals with strings.
static Checks.Mask<java.lang.Object> NotNull
          A mask/check with that deals with objects.
static Checks.Mask<java.lang.String> NotNullEmptyOrBlank
          A mask/check with that deals with strings.
static Checks.Mask<java.lang.String> NotNullOrBlank
          A mask/check with that deals with strings.
static Checks.Mask<java.lang.String> NotNullOrEmpty
          A mask/check with that deals with strings.
static Checks.Mask<java.util.Collection<?>> NotNullOrEmptyCol
          A mask/check object that deals with collections.
static Checks.IReallocationStrategy UnmodifiableLeaveNulls
          Reallocates collections as unmodifiable collections.
static Checks.IReallocationStrategy UnmodifiableMaskNullWithEmpty
          Reallocates collections as unmodifiable collections.
 
Constructor Summary
Checks()
           
 
Method Summary
static
<S,E extends S>
java.util.List<E>
allElementsAre(Checks.ICheck<S> test, java.lang.String varName, java.util.List<E> list, Checks.IReallocationStrategy r)
          Runs the check object over all elements in the list.
static
<S,E extends S>
java.util.Set<E>
allElementsAre(Checks.ICheck<S> test, java.lang.String varName, java.util.Set<E> set, Checks.IReallocationStrategy r)
          Runs the check object over all elements in the set.
static
<S,E extends S>
java.util.SortedSet<E>
allElementsAre(Checks.ICheck<S> test, java.lang.String varName, java.util.SortedSet<E> set, Checks.IReallocationStrategy r)
          Runs the check object over all elements in the set.
static
<S,K extends S,V extends S>
java.util.Map<K,V>
allKeysAndValuesAre(Checks.ICheck<S> test, java.lang.String varName, java.util.Map<K,V> map, Checks.IReallocationStrategy r)
          Runs the check object over all keys and values in the map.
static
<S,K extends S,V extends S>
java.util.SortedMap<K,V>
allKeysAndValuesAre(Checks.ICheck<S> test, java.lang.String varName, java.util.SortedMap<K,V> map, Checks.IReallocationStrategy r)
          Runs the check object over all keys and values in the map.
static
<S,K extends S,V>
java.util.Map<K,V>
allKeysAre(Checks.ICheck<S> test, java.lang.String varName, java.util.Map<K,V> map, Checks.IReallocationStrategy r)
          Runs the check object over all keys in the map.
static
<S,K extends S,V>
java.util.SortedMap<K,V>
allKeysAre(Checks.ICheck<S> test, java.lang.String varName, java.util.SortedMap<K,V> map, Checks.IReallocationStrategy r)
          Runs the check object over all keys in the map.
static
<S,K,V extends S>
java.util.Map<K,V>
allValuesAre(Checks.ICheck<S> test, java.lang.String varName, java.util.Map<K,V> map, Checks.IReallocationStrategy r)
          Runs the check object over all values in the map.
static
<S,K,V extends S>
java.util.SortedMap<K,V>
allValuesAre(Checks.ICheck<V> test, java.lang.String varName, java.util.SortedMap<K,V> map, Checks.IReallocationStrategy r)
          Runs the check object over all values in the map.
static
<T> Checks.Mask<T>
isNull()
          Returns an IsNull mask object cast to the appropriate type.
static Checks.Mask<java.util.Collection<?>> maxSizeAndNotNull(int size)
          Returns a mask object whose condition is that the collection be non- null and have a size less than or equal to the specified size.
static Checks.Mask<java.util.Collection<?>> minSizeAndNotNull(int size)
          Returns a mask object whose condition is that the collection be non- null and have a size equal to or greater than the specified size.
static java.lang.String normalizeVarName(java.lang.String varName)
          Normalizes a variable name to either a trimmed version of itself, or returns the generic variable name.
static
<T> Checks.Mask<T>
notNull()
          Returns a NotNull mask object cast to the appropriate type.
static
<T> T
thisLineShouldNeverBeCalled()
          Throws an exception if called.
static
<T> T
thisMethodShouldNeverBeCalled()
          Throws an exception if called.
static java.lang.String trim(Checks.ICondition<java.lang.Object> test, java.lang.String value)
          Trims the string if it matches the condition.
static java.lang.String trimAndMask(Checks.ICondition<java.lang.Object> test, java.lang.String value, Checks.IMask<java.lang.String> mask, java.lang.String maskValue)
          First trims the string if it matches the condition, then applies the mask to the resulting value.
static java.lang.String trimAndMaskNull(java.lang.String value, java.lang.String maskValue)
          Convenience implementation of the most common use case of the trimAndMask(ICondition, String, IMask, String) method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

genericVarName

public static final java.lang.String genericVarName
The default variable name to use in the exceptions thrown by this class, if the specific variable name is null, empty, or all whitespace.

Since:
JVerbnet 1.0.0
See Also:
Constant Field Values

NotNull

public static final Checks.Mask<java.lang.Object> NotNull
A mask/check with that deals with objects. The condition is "not null".

Since:
JVerbnet 1.0.0

IsNull

public static final Checks.Mask<java.lang.Object> IsNull
A mask/check with that deals with objects. The condition is "is null".

Since:
JVerbnet 1.0.0

IsEmpty

public static final Checks.Mask<java.lang.String> IsEmpty
A mask/check with that deals with strings. The condition is "is empty", which means a string that is non-null and of length zero.

Since:
JVerbnet 1.0.0

IsBlank

public static final Checks.Mask<java.lang.String> IsBlank
A mask/check with that deals with strings. The condition is "is blank", which means a string that is non-null, greater than length zero, and all whitespace. If the Checks.ICheck.check(String, Object) method returns, it returns the original string.

Since:
JVerbnet 1.0.0

IsNullOrEmpty

public static final Checks.Mask<java.lang.String> IsNullOrEmpty
A mask/check with that deals with strings. The condition is "is null or empty", which means a string that is null or of length zero.

Since:
JVerbnet 1.0.0

IsNullOrBlank

public static final Checks.Mask<java.lang.String> IsNullOrBlank
A mask/check with that deals with strings. The condition is "is null or blank", which means a string that is null or greater than length zero and contains only whitespace.

Since:
JVerbnet 1.0.0

IsEmptyOrBlank

public static final Checks.Mask<java.lang.String> IsEmptyOrBlank
A mask/check with that deals with strings. The condition is "is empty or blank", which means a string that is of length zero or of greater than length zero and contains only whitespace.

Since:
JVerbnet 1.0.0

IsNullEmptyOrBlank

public static final Checks.Mask<java.lang.String> IsNullEmptyOrBlank
A mask/check with that deals with strings. The condition is "is null, empty or blank", which means a string that is null; is of length zero; or of greater than length zero and contains only whitespace.

Since:
JVerbnet 1.0.0

NotEmpty

public static final Checks.Mask<java.lang.String> NotEmpty
A mask/check with that deals with strings. The condition is "not empty", which means a string that is not of length zero. If the Checks.ICheck.check(String, Object) method returns, it returns a trimmed version of the original string, or null if the original value was null.

Since:
JVerbnet 1.0.0

NotBlank

public static final Checks.Mask<java.lang.String> NotBlank
A mask/check with that deals with strings. The condition is "not blank", which means a string that is not of length greater than zero and contains only whitespace. If the Checks.ICheck.check(String, Object) method returns, it returns a trimmed version of the original string, or null if the original value was null.

Since:
JVerbnet 1.0.0

NotNullOrEmpty

public static final Checks.Mask<java.lang.String> NotNullOrEmpty
A mask/check with that deals with strings. The condition is "not blank", which means a string that is not of length greater than zero and contains only whitespace. If the Checks.ICheck.check(String, Object) method returns, it returns a trimmed version of the original string.

Since:
JVerbnet 1.0.0

NotNullOrBlank

public static final Checks.Mask<java.lang.String> NotNullOrBlank
A mask/check with that deals with strings. The condition is "not blank", which means a string that is not of length greater than zero and contains only whitespace. If the Checks.ICheck.check(String, Object) method returns, it returns a trimmed version of the original string.

Since:
JVerbnet 1.0.0

NotEmptyOrBlank

public static final Checks.Mask<java.lang.String> NotEmptyOrBlank
A mask/check with that deals with strings. The condition is "not blank", which means a string that is not of length greater than zero and contains only whitespace. If the Checks.ICheck.check(String, Object) method returns, it returns a trimmed version of the original string, or null if the original value was null.

Since:
JVerbnet 1.0.0

NotNullEmptyOrBlank

public static final Checks.Mask<java.lang.String> NotNullEmptyOrBlank
A mask/check with that deals with strings. The condition is "not blank", which means a string that is not of length greater than zero and contains only whitespace. If the Checks.ICheck.check(String, Object) method returns, it returns a trimmed version of the original string, or null if the original value was null.

Since:
JVerbnet 1.0.0

IsEmptyCol

public static final Checks.Mask<java.util.Collection<?>> IsEmptyCol
A mask/check object that deals with collections. The condition is "empty", meaning non-null and of size zero.

Since:
JVerbnet 1.0.0

IsNullOrEmptyCol

public static final Checks.Mask<java.util.Collection<?>> IsNullOrEmptyCol
A mask/check object that deals with collections. The condition is "null or empty", meaning the collection must be null or of size zero.

Since:
JVerbnet 1.0.0

NotEmptyCol

public static final Checks.Mask<java.util.Collection<?>> NotEmptyCol
A mask/check object that deals with collections. The condition is "not empty", meaning of not of size zero. A null collection is not considered of size zero.

Since:
JVerbnet 1.0.0

NotNullOrEmptyCol

public static final Checks.Mask<java.util.Collection<?>> NotNullOrEmptyCol
A mask/check object that deals with collections. The condition is "not null or empty", meaning the collection must be not null and have at least one element.

Since:
JVerbnet 1.0.0

DoNotReallocate

public static final Checks.IReallocationStrategy DoNotReallocate
A reallocation strategy that does nothing.

Since:
JVerbnet 1.0.0

ModifiableLeaveNulls

public static final Checks.IReallocationStrategy ModifiableLeaveNulls
Reallocates collections as modifiable collections. null collections are passed back as nulls.

Since:
JVerbnet 1.0.0

UnmodifiableLeaveNulls

public static final Checks.IReallocationStrategy UnmodifiableLeaveNulls
Reallocates collections as unmodifiable collections. null collections are passed back as nulls.

Since:
JVerbnet 1.0.0

ModifiableMaskNullWithEmpty

public static final Checks.IReallocationStrategy ModifiableMaskNullWithEmpty
Reallocates collections as modifiable collections. null collections are passed back as modifiable empty collections.

Since:
JVerbnet 1.0.0

UnmodifiableMaskNullWithEmpty

public static final Checks.IReallocationStrategy UnmodifiableMaskNullWithEmpty
Reallocates collections as unmodifiable collections. null collections are passed back as unmodifiable empty collections.

Since:
JVerbnet 1.0.0
Constructor Detail

Checks

public Checks()
Method Detail

normalizeVarName

public static java.lang.String normalizeVarName(java.lang.String varName)
Normalizes a variable name to either a trimmed version of itself, or returns the generic variable name.

Parameters:
varName - the variable name to normalized
Returns:
the normalized name
Since:
JVerbnet 1.0.0

notNull

public static <T> Checks.Mask<T> notNull()
Returns a NotNull mask object cast to the appropriate type.

Returns:
a NotNull mask object cast to the appropriate type
Since:
JVerbnet 1.0.0

isNull

public static <T> Checks.Mask<T> isNull()
Returns an IsNull mask object cast to the appropriate type.

Returns:
an IsNull mask object cast to the appropriate type
Since:
JVerbnet 1.0.0

trim

public static java.lang.String trim(Checks.ICondition<java.lang.Object> test,
                                    java.lang.String value)
Trims the string if it matches the condition.

Parameters:
test - the condition to test; if the condition object is null, the NotNull object is used.
value - the string to test
Returns:
a trimmed version of the string if the condition is met (or null, if the original string was null); otherwise the original object
Since:
JVerbnet 1.0.0

trimAndMask

public static java.lang.String trimAndMask(Checks.ICondition<java.lang.Object> test,
                                           java.lang.String value,
                                           Checks.IMask<java.lang.String> mask,
                                           java.lang.String maskValue)
First trims the string if it matches the condition, then applies the mask to the resulting value.

Parameters:
test - the condition to test; if the condition object is null, the NotNull object is used.
value - the string to test
mask - the mask to apply to the trimmed string
maskValue - the mask value to substitute if necessary
Returns:
a trimmed and masked version of the original string
Since:
JVerbnet 1.0.0

trimAndMaskNull

public static java.lang.String trimAndMaskNull(java.lang.String value,
                                               java.lang.String maskValue)
Convenience implementation of the most common use case of the trimAndMask(ICondition, String, IMask, String) method.

Parameters:
value - the value to be trimmed then masked
maskValue - the mask value
Returns:
a trimmed version of the string, or the empty string if the string was null
Since:
JVerbnet 1.0.0

minSizeAndNotNull

public static final Checks.Mask<java.util.Collection<?>> minSizeAndNotNull(int size)
Returns a mask object whose condition is that the collection be non- null and have a size equal to or greater than the specified size.

Parameters:
size - the minimum size of the collection
Returns:
a mask object whose condition is that the collection be non- null and have a size equal to or greater than the specified size
Since:
JVerbnet 1.0.0

maxSizeAndNotNull

public static final Checks.Mask<java.util.Collection<?>> maxSizeAndNotNull(int size)
Returns a mask object whose condition is that the collection be non- null and have a size less than or equal to the specified size.

Parameters:
size - the maximum size of the collection
Returns:
a mask object whose condition is that the collection be non- null and have a size less than or equal to the specified size
Since:
JVerbnet 1.0.0

allElementsAre

public static <S,E extends S> java.util.List<E> allElementsAre(Checks.ICheck<S> test,
                                                               java.lang.String varName,
                                                               java.util.List<E> list,
                                                               Checks.IReallocationStrategy r)
Runs the check object over all elements in the list. Returns a list reallocated according to the specified reallocation strategy. The strategy may be null, meaning the DoNotReallocate strategy is used.

Parameters:
test - the test to be applied
varName - the variable name to be used in the exception message; if the name is null, empty, or all whitespace, the generic variable name is used.
list - the list to check
r - the reallocation strategy to use; if null, the DoNotReallocate strategy is used
Returns:
the reallocated list
Since:
JVerbnet 1.0.0

allElementsAre

public static <S,E extends S> java.util.Set<E> allElementsAre(Checks.ICheck<S> test,
                                                              java.lang.String varName,
                                                              java.util.Set<E> set,
                                                              Checks.IReallocationStrategy r)
Runs the check object over all elements in the set. Returns a set reallocated according to the specified reallocation strategy. The strategy may be null, meaning the DoNotReallocate strategy is used.

Parameters:
test - the test to be applied
varName - the variable name to be used in the exception message; if the name is null, empty, or all whitespace, the generic variable name is used.
set - the set to check
r - the reallocation strategy to use; if null, the DoNotReallocate strategy is used
Returns:
the reallocated set
Since:
JVerbnet 1.0.0

allElementsAre

public static <S,E extends S> java.util.SortedSet<E> allElementsAre(Checks.ICheck<S> test,
                                                                    java.lang.String varName,
                                                                    java.util.SortedSet<E> set,
                                                                    Checks.IReallocationStrategy r)
Runs the check object over all elements in the set. Returns a set reallocated according to the specified reallocation strategy. The strategy may be null, meaning the DoNotReallocate strategy is used.

Parameters:
test - the test to be applied
varName - the variable name to be used in the exception message; if the name is null, empty, or all whitespace, the generic variable name is used.
set - the set to check
r - the reallocation strategy to use; if null, the DoNotReallocate strategy is used
Returns:
the reallocated set
Since:
JVerbnet 1.0.0

allKeysAre

public static <S,K extends S,V> java.util.Map<K,V> allKeysAre(Checks.ICheck<S> test,
                                                              java.lang.String varName,
                                                              java.util.Map<K,V> map,
                                                              Checks.IReallocationStrategy r)
Runs the check object over all keys in the map. Returns a map reallocated according to the specified reallocation strategy. The strategy may be null, meaning the DoNotReallocate strategy is used.

Parameters:
test - the test to be applied
varName - the variable name to be used in the exception message; if the name is null, empty, or all whitespace, the generic variable name is used.
map - the map to check
r - the reallocation strategy to use; if null, the DoNotReallocate strategy is used
Returns:
the reallocated set
Since:
JVerbnet 1.0.0

allKeysAre

public static <S,K extends S,V> java.util.SortedMap<K,V> allKeysAre(Checks.ICheck<S> test,
                                                                    java.lang.String varName,
                                                                    java.util.SortedMap<K,V> map,
                                                                    Checks.IReallocationStrategy r)
Runs the check object over all keys in the map. Returns a map reallocated according to the specified reallocation strategy. The strategy may be null, meaning the DoNotReallocate strategy is used.

Parameters:
test - the test to be applied
varName - the variable name to be used in the exception message; if the name is null, empty, or all whitespace, the generic variable name is used.
map - the map to check
r - the reallocation strategy to use; if null, the DoNotReallocate strategy is used
Returns:
the reallocated set
Since:
JVerbnet 1.0.0

allValuesAre

public static <S,K,V extends S> java.util.Map<K,V> allValuesAre(Checks.ICheck<S> test,
                                                                java.lang.String varName,
                                                                java.util.Map<K,V> map,
                                                                Checks.IReallocationStrategy r)
Runs the check object over all values in the map. Returns a map reallocated according to the specified reallocation strategy. The strategy may be null, meaning the DoNotReallocate strategy is used.

Parameters:
test - the test to be applied
varName - the variable name to be used in the exception message; if the name is null, empty, or all whitespace, the generic variable name is used.
map - the map to check
r - the reallocation strategy to use; if null, the DoNotReallocate strategy is used
Returns:
the reallocated set
Since:
JVerbnet 1.0.0

allValuesAre

public static <S,K,V extends S> java.util.SortedMap<K,V> allValuesAre(Checks.ICheck<V> test,
                                                                      java.lang.String varName,
                                                                      java.util.SortedMap<K,V> map,
                                                                      Checks.IReallocationStrategy r)
Runs the check object over all values in the map. Returns a map reallocated according to the specified reallocation strategy. The strategy may be null, meaning the DoNotReallocate strategy is used.

Parameters:
test - the test to be applied
varName - the variable name to be used in the exception message; if the name is null, empty, or all whitespace, the generic variable name is used.
map - the map to check
r - the reallocation strategy to use; if null, the DoNotReallocate strategy is used
Returns:
the reallocated set
Since:
JVerbnet 1.0.0

allKeysAndValuesAre

public static <S,K extends S,V extends S> java.util.Map<K,V> allKeysAndValuesAre(Checks.ICheck<S> test,
                                                                                 java.lang.String varName,
                                                                                 java.util.Map<K,V> map,
                                                                                 Checks.IReallocationStrategy r)
Runs the check object over all keys and values in the map. Returns a map reallocated according to the specified reallocation strategy. The strategy may be null, meaning the DoNotReallocate strategy is used.

Parameters:
test - the test to be applied
varName - the variable name to be used in the exception message; if the name is null, empty, or all whitespace, the generic variable name is used.
map - the map to check
r - the reallocation strategy to use; if null, the DoNotReallocate strategy is used
Returns:
the reallocated set
Since:
JVerbnet 1.0.0

allKeysAndValuesAre

public static <S,K extends S,V extends S> java.util.SortedMap<K,V> allKeysAndValuesAre(Checks.ICheck<S> test,
                                                                                       java.lang.String varName,
                                                                                       java.util.SortedMap<K,V> map,
                                                                                       Checks.IReallocationStrategy r)
Runs the check object over all keys and values in the map. Returns a map reallocated according to the specified reallocation strategy. The strategy may be null, meaning the DoNotReallocate strategy is used.

Parameters:
test - the test to be applied
varName - the variable name to be used in the exception message; if the name is null, empty, or all whitespace, the generic variable name is used.
map - the map to check
r - the reallocation strategy to use; if null, the DoNotReallocate strategy is used
Returns:
the reallocated set
Since:
JVerbnet 1.0.0

thisMethodShouldNeverBeCalled

public static <T> T thisMethodShouldNeverBeCalled()
Throws an exception if called. The exception message includes a pointer to the source code where the method was invoked.

Throws:
java.lang.IllegalStateException - if this method is called
Since:
JVerbnet 1.0.0

thisLineShouldNeverBeCalled

public static <T> T thisLineShouldNeverBeCalled()
                                     throws java.lang.IllegalStateException
Throws an exception if called. The exception message includes a pointer to the source code where the method was invoked.

Throws:
java.lang.IllegalStateException - if this method is called
Since:
JVerbnet 1.0.0


Copyright © 2012 ${project.organization.name}. All Rights Reserved.