Commit b53fdf54 authored by Rajiv Prabhakar's avatar Rajiv Prabhakar
Browse files

v1.9.0 release: Validatec - varargs Strings-notEmpty added

parent 38ed6bab
Pipeline #9908657 passed with stage
in 1 minute and 19 seconds
......@@ -6,7 +6,7 @@
<groupId>com.rajivprab</groupId>
<artifactId>cava</artifactId>
<version>1.8.1</version>
<version>1.9.0</version>
<name>Cava: Clean Java</name>
<description>A library that enables users to write minimal, clean and simple Java code</description>
......
......@@ -500,7 +500,7 @@ public class Validatec {
return "Found: " + value + ", in collection: " + collection;
}
// ---------- Not Empty
// ---------- Not Empty - Collection
public static <T> void notEmpty(Collection<T> collection) {
notEmpty(collection, getCollectionNotEmptyMessage(collection));
......@@ -510,10 +510,10 @@ public class Validatec {
notEmpty(collection, message, DEFAULT_EXCEPTION);
}
public static <T> void notEmpty(Collection<T> collection, String message, Class<? extends RuntimeException> exceptionType) {
public static <T> void notEmpty(Collection<T> collection, String msg, Class<? extends RuntimeException> exceptionType) {
if (collection == null || collection.isEmpty()) {
log.error(getCollectionNotEmptyMessage(collection));
throwException(message, exceptionType);
throwException(msg, exceptionType);
}
}
......@@ -521,6 +521,33 @@ public class Validatec {
return "Collection is empty: " + collection;
}
// ---------- Not Empty - String
public static void notEmpty(String... strings) {
notEmpty(DEFAULT_EXCEPTION, strings);
}
public static void notEmpty(Class<? extends RuntimeException> exceptionType, String... strings) {
notEmpty(getStringNotEmptyMessage(strings), exceptionType, strings);
}
public static void notEmpty(String message, Class<? extends RuntimeException> exceptionType, String... strings) {
if (strings == null) {
log.error(getStringNotEmptyMessage(strings));
throwException(message, exceptionType);
}
for (String string : strings) {
if (Strings.isNullOrEmpty(string)) {
log.error(getStringNotEmptyMessage(strings));
throwException(message, exceptionType);
}
}
}
private static String getStringNotEmptyMessage(String... strings) {
return "One of the strings is null or empty: " + Arrays.toString(strings);
}
// ---------- Map contains key, value
public static <K, V> void contains(Map<K, V> map, K key, V value) {
......
......@@ -431,6 +431,33 @@ public class ValidatecTest extends TestBase {
}
}
// -------- Strings notEmpty
@Test
public void stringsNotEmpty_allGood() {
Validatec.notEmpty(null, ArithmeticException.class, "a");
}
@Test
public void stringsNotEmpty_fail_constructDefaultException() {
try {
Validatec.notEmpty("123", "");
Assert.fail();
} catch (IllegalArgumentException expected) {
Truth.assertThat(expected).hasMessageThat().isEqualTo("One of the strings is null or empty: [123, ]");
}
}
@Test
public void stringsNotEmpty_fail_constructGivenException() {
try {
Validatec.notEmpty("my custom message", ArithmeticException.class, null, "123");
Assert.fail();
} catch (ArithmeticException expected) {
Truth.assertThat(expected).hasMessageThat().contains("my custom message");
}
}
// -------- Map contains
@Test
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment