This is an automated email from the ASF dual-hosted git repository. orpiske pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new bea7394ecd9 (chores) camel-util: resolves emptyness check in compile time bea7394ecd9 is described below commit bea7394ecd9ec32a6f86c82903f3b077ef77665e Author: Otavio Rodolfo Piske <angusyo...@gmail.com> AuthorDate: Tue May 31 13:28:32 2022 +0200 (chores) camel-util: resolves emptyness check in compile time --- .../org/apache/camel/util/ObjectHelperTest.java | 4 +- .../java/org/apache/camel/util/ObjectHelper.java | 70 ++++++++++++++++++++-- 2 files changed, 67 insertions(+), 7 deletions(-) diff --git a/core/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java b/core/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java index 54b2089c8b6..382db0374ab 100644 --- a/core/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java @@ -564,7 +564,7 @@ public class ObjectHelperTest { @Test public void testIsEmpty() { - assertTrue(org.apache.camel.util.ObjectHelper.isEmpty(null)); + assertTrue(org.apache.camel.util.ObjectHelper.isEmpty((Object) null)); assertTrue(org.apache.camel.util.ObjectHelper.isEmpty("")); assertTrue(org.apache.camel.util.ObjectHelper.isEmpty(" ")); assertFalse(org.apache.camel.util.ObjectHelper.isEmpty("A")); @@ -575,7 +575,7 @@ public class ObjectHelperTest { @Test public void testIsNotEmpty() { - assertFalse(org.apache.camel.util.ObjectHelper.isNotEmpty(null)); + assertFalse(org.apache.camel.util.ObjectHelper.isNotEmpty((Object) null)); assertFalse(org.apache.camel.util.ObjectHelper.isNotEmpty("")); assertFalse(org.apache.camel.util.ObjectHelper.isNotEmpty(" ")); assertTrue(org.apache.camel.util.ObjectHelper.isNotEmpty("A")); diff --git a/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java b/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java index 10f7feb6203..261addc0bc2 100644 --- a/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java +++ b/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java @@ -185,15 +185,45 @@ public final class ObjectHelper { * @param value the value, if its a String it will be tested for text length as well * @return true if empty */ - public static boolean isEmpty(Object value) { + public static boolean isEmpty(String value) { + return value == null || value.trim().isEmpty(); + } + + /** + * Tests whether the value is <tt>null</tt> or an an empty collection + * + * @param value the value to test + * @return true if empty + */ + public static boolean isEmpty(Collection<?> value) { + return value == null || value.isEmpty(); + } + + /** + * Tests whether the value is <tt>null</tt> or an an empty map + * + * @param value the value to test + * @return true if empty + */ + public static boolean isEmpty(Map<?, ?> value) { + return value == null || value.isEmpty(); + } + + /** + * Tests whether the value is <tt>null</tt>, an empty string or an empty collection/map. + * + * @param value the value, if its a String it will be tested for text length as well + * @return true if empty + */ + public static <T> boolean isEmpty(T value) { if (value == null) { return true; } else if (value instanceof String) { - return ((String) value).trim().isEmpty(); + return isEmpty((String) value); } else if (value instanceof Collection) { - return ((Collection<?>) value).isEmpty(); + return isEmpty((Collection<?>) value); } else if (value instanceof Map) { - return ((Map<?, ?>) value).isEmpty(); + return isEmpty((Map<?, ?>) value); } else { return false; } @@ -205,7 +235,37 @@ public final class ObjectHelper { * @param value the value, if its a String it will be tested for text length as well * @return true if <b>not</b> empty */ - public static boolean isNotEmpty(Object value) { + public static <T> boolean isNotEmpty(T value) { + return !isEmpty(value); + } + + /** + * Tests whether the value is <b>not</b> <tt>null</tt> or an empty string + * + * @param value the value, if its a String it will be tested for text length as well + * @return true if <b>not</b> empty + */ + public static boolean isNotEmpty(String value) { + return !isEmpty(value); + } + + /** + * Tests whether the value is <tt>null</tt> or an an empty collection + * + * @param value the value to test + * @return true if empty + */ + public static boolean isNotEmpty(Collection<?> value) { + return !isEmpty(value); + } + + /** + * Tests whether the value is <tt>null</tt> or an an empty map + * + * @param value the value to test + * @return true if empty + */ + public static boolean isNotEmpty(Map<?, ?> value) { return !isEmpty(value); }