mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-08-03 11:35:29 -04:00
Added test case.
Clean up + new methods.
This commit is contained in:
parent
b0c2b4886f
commit
80d2f4ad89
@ -32,23 +32,34 @@ public final class Validate {
|
|||||||
if (pParameter == null) {
|
if (pParameter == null) {
|
||||||
throw new IllegalArgumentException(String.format("%s may not be null", pParamName == null ? UNSPECIFIED_PARAM_NAME : pParamName));
|
throw new IllegalArgumentException(String.format("%s may not be null", pParamName == null ? UNSPECIFIED_PARAM_NAME : pParamName));
|
||||||
}
|
}
|
||||||
|
|
||||||
return pParameter;
|
return pParameter;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Not empty...
|
// Not empty
|
||||||
|
|
||||||
public static <T extends CharSequence> T notEmpty(final T pParameter) {
|
public static <T extends CharSequence> T notEmpty(final T pParameter) {
|
||||||
return notEmpty(pParameter, null);
|
return notEmpty(pParameter, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T extends CharSequence> T notEmpty(final T pParameter, final String pParamName) {
|
public static <T extends CharSequence> T notEmpty(final T pParameter, final String pParamName) {
|
||||||
if (pParameter == null || pParameter.length() == 0) {
|
if (pParameter == null || pParameter.length() == 0 || isOnlyWhiteSpace(pParameter)) {
|
||||||
throw new IllegalArgumentException(String.format("%s may not be empty", pParamName == null ? UNSPECIFIED_PARAM_NAME : pParamName));
|
throw new IllegalArgumentException(String.format("%s may not be blank", pParamName == null ? UNSPECIFIED_PARAM_NAME : pParamName));
|
||||||
}
|
}
|
||||||
|
|
||||||
return pParameter;
|
return pParameter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static <T extends CharSequence> boolean isOnlyWhiteSpace(T pParameter) {
|
||||||
|
for (int i = 0; i < pParameter.length(); i++) {
|
||||||
|
if (!Character.isWhitespace(pParameter.charAt(i))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public static <T> T[] notEmpty(final T[] pParameter) {
|
public static <T> T[] notEmpty(final T[] pParameter) {
|
||||||
return notEmpty(pParameter, null);
|
return notEmpty(pParameter, null);
|
||||||
}
|
}
|
||||||
@ -92,7 +103,7 @@ public final class Validate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static <T> T[] noNullElements(final T[] pParameter, final String pParamName) {
|
public static <T> T[] noNullElements(final T[] pParameter, final String pParamName) {
|
||||||
noNullElements(Arrays.asList(pParameter), pParamName);
|
noNullElements(pParameter == null ? null : Arrays.asList(pParameter), pParamName);
|
||||||
return pParameter;
|
return pParameter;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,6 +112,8 @@ public final class Validate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static <T> Collection<T> noNullElements(final Collection<T> pParameter, final String pParamName) {
|
public static <T> Collection<T> noNullElements(final Collection<T> pParameter, final String pParamName) {
|
||||||
|
notNull(pParameter, pParamName);
|
||||||
|
|
||||||
for (T element : pParameter) {
|
for (T element : pParameter) {
|
||||||
if (element == null) {
|
if (element == null) {
|
||||||
throw new IllegalArgumentException(String.format("%s may not contain null elements", pParamName == null ? UNSPECIFIED_PARAM_NAME : pParamName));
|
throw new IllegalArgumentException(String.format("%s may not contain null elements", pParamName == null ? UNSPECIFIED_PARAM_NAME : pParamName));
|
||||||
@ -110,27 +123,47 @@ public final class Validate {
|
|||||||
return pParameter;
|
return pParameter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <K, V> Map<K, V> noNullElements(final Map<K, V> pParameter) {
|
public static <K, V> Map<K, V> noNullValues(final Map<K, V> pParameter) {
|
||||||
return noNullElements(pParameter, null);
|
return noNullValues(pParameter, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <K, V> Map<K, V> noNullElements(final Map<K, V> pParameter, final String pParamName) {
|
public static <K, V> Map<K, V> noNullValues(final Map<K, V> pParameter, final String pParamName) {
|
||||||
for (V element : pParameter.values()) {
|
notNull(pParameter, pParamName);
|
||||||
if (element == null) {
|
|
||||||
throw new IllegalArgumentException(String.format("%s may not contain null elements", pParamName == null ? UNSPECIFIED_PARAM_NAME : pParamName));
|
for (V value : pParameter.values()) {
|
||||||
|
if (value == null) {
|
||||||
|
throw new IllegalArgumentException(String.format("%s may not contain null values", pParamName == null ? UNSPECIFIED_PARAM_NAME : pParamName));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return pParameter;
|
return pParameter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <K, V> Map<K, V> noNullKeys(final Map<K, V> pParameter) {
|
||||||
|
return noNullKeys(pParameter, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <K, V> Map<K, V> noNullKeys(final Map<K, V> pParameter, final String pParamName) {
|
||||||
|
notNull(pParameter, pParamName);
|
||||||
|
|
||||||
|
for (K key : pParameter.keySet()) {
|
||||||
|
if (key == null) {
|
||||||
|
throw new IllegalArgumentException(String.format("%s may not contain null keys", pParamName == null ? UNSPECIFIED_PARAM_NAME : pParamName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return pParameter;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Is true
|
||||||
|
|
||||||
public static boolean isTrue(final boolean pExpression, final String pMessage) {
|
public static boolean isTrue(final boolean pExpression, final String pMessage) {
|
||||||
return isTrue(pExpression, pExpression, pMessage);
|
return isTrue(pExpression, pExpression, pMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> T isTrue(final boolean condition, final T value, final String message) {
|
public static <T> T isTrue(final boolean condition, final T value, final String message) {
|
||||||
if (!condition) {
|
if (!condition) {
|
||||||
throw new IllegalArgumentException(String.format(message, value));
|
throw new IllegalArgumentException(String.format(message == null ? "expression may not be %s" : message, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user