diff --git a/common/common-lang/src/main/java/com/twelvemonkeys/lang/Validate.java b/common/common-lang/src/main/java/com/twelvemonkeys/lang/Validate.java
index c6f308ea..16897f31 100755
--- a/common/common-lang/src/main/java/com/twelvemonkeys/lang/Validate.java
+++ b/common/common-lang/src/main/java/com/twelvemonkeys/lang/Validate.java
@@ -9,7 +9,7 @@ import java.util.Map;
*
* Uses type parameterized return values, thus making it possible to check
* constructor arguments before
- * they are passed on to {@code super} or {@code this} type constructors.
+ * they are passed on to {@code super} or {@code this} type constructors.
*
* @author Harald Kuhr
* @author last modified by $Author: haku $
@@ -32,23 +32,34 @@ public final class Validate {
if (pParameter == null) {
throw new IllegalArgumentException(String.format("%s may not be null", pParamName == null ? UNSPECIFIED_PARAM_NAME : pParamName));
}
+
return pParameter;
}
- // Not empty...
+ // Not empty
public static T notEmpty(final T pParameter) {
return notEmpty(pParameter, null);
}
public static T notEmpty(final T pParameter, final String pParamName) {
- if (pParameter == null || pParameter.length() == 0) {
- throw new IllegalArgumentException(String.format("%s may not be empty", pParamName == null ? UNSPECIFIED_PARAM_NAME : pParamName));
+ if (pParameter == null || pParameter.length() == 0 || isOnlyWhiteSpace(pParameter)) {
+ throw new IllegalArgumentException(String.format("%s may not be blank", pParamName == null ? UNSPECIFIED_PARAM_NAME : pParamName));
}
return pParameter;
}
+ private static 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[] notEmpty(final T[] pParameter) {
return notEmpty(pParameter, null);
}
@@ -92,7 +103,7 @@ public final class Validate {
}
public static T[] noNullElements(final T[] pParameter, final String pParamName) {
- noNullElements(Arrays.asList(pParameter), pParamName);
+ noNullElements(pParameter == null ? null : Arrays.asList(pParameter), pParamName);
return pParameter;
}
@@ -101,6 +112,8 @@ public final class Validate {
}
public static Collection noNullElements(final Collection pParameter, final String pParamName) {
+ notNull(pParameter, pParamName);
+
for (T element : pParameter) {
if (element == null) {
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;
}
- public static Map noNullElements(final Map pParameter) {
- return noNullElements(pParameter, null);
+ public static Map noNullValues(final Map pParameter) {
+ return noNullValues(pParameter, null);
}
- public static Map noNullElements(final Map pParameter, final String pParamName) {
- for (V element : pParameter.values()) {
- if (element == null) {
- throw new IllegalArgumentException(String.format("%s may not contain null elements", pParamName == null ? UNSPECIFIED_PARAM_NAME : pParamName));
+ public static Map noNullValues(final Map pParameter, final String pParamName) {
+ notNull(pParameter, 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;
}
+ public static Map noNullKeys(final Map pParameter) {
+ return noNullKeys(pParameter, null);
+ }
+
+ public static Map noNullKeys(final Map 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) {
return isTrue(pExpression, pExpression, pMessage);
}
public static T isTrue(final boolean condition, final T value, final String message) {
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;
diff --git a/common/common-lang/src/test/java/com/twelvemonkeys/lang/ValidateTest.java b/common/common-lang/src/test/java/com/twelvemonkeys/lang/ValidateTest.java
new file mode 100644
index 00000000..760d747b
--- /dev/null
+++ b/common/common-lang/src/test/java/com/twelvemonkeys/lang/ValidateTest.java
@@ -0,0 +1,1013 @@
+/*
+ * Copyright (c) 2012, Harald Kuhr
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name "TwelveMonkeys" nor the
+ * names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package com.twelvemonkeys.lang;
+
+import org.junit.Test;
+
+import java.util.*;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import static org.junit.Assert.*;
+
+/**
+ * ValidateTest
+ *
+ * @author Harald Kuhr
+ * @author last modified by $Author: haraldk$
+ * @version $Id: ValidateTest.java,v 1.0 11.04.12 09:06 haraldk Exp$
+ */
+public class ValidateTest {
+ // Not null
+
+ @Test
+ public void testNotNull() {
+ assertEquals("foo", Validate.notNull("foo"));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNotNullNull() {
+ Validate.notNull(null);
+ }
+
+ @Test
+ public void testNotNullWithParameter() {
+ assertEquals("foo", Validate.notNull("foo", "bar"));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNotNullWithParameterNull() {
+ try {
+ Validate.notNull(null, "xyzzy");
+ }
+ catch (IllegalArgumentException e) {
+ assertTrue(e.getMessage().contains("xyzzy"));
+ throw e;
+ }
+ }
+
+ @Test
+ public void testNotNullWithNullParameter() {
+ Validate.notNull("foo", null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNotNullWithNullParameterNull() {
+ Validate.notNull(null, null);
+ }
+
+ // Not empty (CharSequence)
+
+ @Test
+ public void testNotEmptyCharSequence() {
+ assertEquals("foo", Validate.notEmpty("foo"));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNotEmptyCharSequenceNull() {
+ Validate.notEmpty((CharSequence) null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNotEmptyCharSequenceEmpty() {
+ Validate.notEmpty("");
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNotEmptyCharSequenceOnlyWS() {
+ Validate.notEmpty(" \t\r");
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNotEmptyCharSequenceNullWithParameter() {
+ try {
+ Validate.notEmpty((CharSequence) null, "xyzzy");
+ }
+ catch (IllegalArgumentException e) {
+ assertTrue(e.getMessage().contains("xyzzy"));
+ throw e;
+ }
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNotEmptyCharSequenceEmptyWithParameter() {
+ try {
+ Validate.notEmpty("", "xyzzy");
+ }
+ catch (IllegalArgumentException e) {
+ assertTrue(e.getMessage().contains("xyzzy"));
+ throw e;
+ }
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNotEmptyCharSequenceOnlyWSWithParameter() {
+ try {
+ Validate.notEmpty(" \t", "xyzzy");
+ }
+ catch (IllegalArgumentException e) {
+ assertTrue(e.getMessage().contains("xyzzy"));
+ throw e;
+ }
+ }
+
+ @Test
+ public void testNotEmptyCharSequenceWithParameter() {
+ assertEquals("foo", Validate.notEmpty("foo", "bar"));
+ }
+
+ @Test
+ public void testNotEmptyCharSequenceWithParameterNull() {
+ assertEquals("foo", Validate.notEmpty("foo", null));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNotEmptyCharSequenceNullWithParameterNull() {
+ try {
+ Validate.notEmpty((CharSequence) null, null);
+ }
+ catch (IllegalArgumentException e) {
+ assertTrue(e.getMessage().contains("parameter"));
+ throw e;
+ }
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNotEmptyCharSequenceEmptyWithParameterNull() {
+ try {
+ Validate.notEmpty("", null);
+ }
+ catch (IllegalArgumentException e) {
+ assertTrue(e.getMessage().contains("parameter"));
+ throw e;
+ }
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNotEmptyCharSequenceOnlyWSWithParameterNull() {
+ try {
+ Validate.notEmpty(" \t\t \n", null);
+ }
+ catch (IllegalArgumentException e) {
+ assertTrue(e.getMessage().contains("parameter"));
+ throw e;
+ }
+ }
+
+ // Not empty (array)
+
+ @Test
+ public void testNotEmptyArray() {
+ Integer[] array = new Integer[2];
+ assertSame(array, Validate.notEmpty(array));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNotEmptyArrayNull() {
+ Validate.notEmpty((Object[]) null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNotEmptyArrayEmpty() {
+ Validate.notEmpty(new String[0]);
+ }
+
+ @Test
+ public void testNotEmptyArrayParameter() {
+ Integer[] array = new Integer[2];
+ assertSame(array, Validate.notEmpty(array, "bar"));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNotEmptyArrayNullParameter() {
+ try {
+ Validate.notEmpty((Object[]) null, "xyzzy");
+ }
+ catch (IllegalArgumentException e) {
+ assertTrue(e.getMessage().contains("xyzzy"));
+ throw e;
+ }
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNotEmptyArrayEmptyParameter() {
+ try {
+ Validate.notEmpty(new Float[0], "xyzzy");
+ }
+ catch (IllegalArgumentException e) {
+ assertTrue(e.getMessage().contains("xyzzy"));
+ throw e;
+ }
+ }
+
+ @Test
+ public void testNotEmptyArrayWithParameterNull() {
+ Byte[] array = new Byte[1];
+ assertSame(array, Validate.notEmpty(array, null));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNotEmptyArrayNullWithParameterNull() {
+ try {
+ Validate.notEmpty((Object[]) null, null);
+ }
+ catch (IllegalArgumentException e) {
+ assertTrue(e.getMessage().contains("parameter"));
+ throw e;
+ }
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNotEmptyArrayEmptyWithParameterNull() {
+ try {
+ Validate.notEmpty(new Object[0], null);
+ }
+ catch (IllegalArgumentException e) {
+ assertTrue(e.getMessage().contains("parameter"));
+ throw e;
+ }
+ }
+
+ // Not empty (Collection)
+
+ @Test
+ public void testNotEmptyCollection() {
+ Collection collection = Arrays.asList(new Integer[2]);
+ assertSame(collection, Validate.notEmpty(collection));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNotEmptyCollectionNull() {
+ Validate.notEmpty((Collection>) null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNotEmptyCollectionEmpty() {
+ Validate.notEmpty(Collections.emptySet());
+ }
+
+ @Test
+ public void testNotEmptyCollectionParameter() {
+ List collection = Collections.singletonList(1);
+ assertSame(collection, Validate.notEmpty(collection, "bar"));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNotEmptyCollectionNullParameter() {
+ try {
+ Validate.notEmpty((Collection>) null, "xyzzy");
+ }
+ catch (IllegalArgumentException e) {
+ assertTrue(e.getMessage().contains("xyzzy"));
+ throw e;
+ }
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNotEmptyCollectionEmptyParameter() {
+ try {
+ Validate.notEmpty(new ArrayList