You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by zr...@apache.org on 2017/03/29 11:15:34 UTC

[2/2] camel git commit: CAMEL-11090 Add trimToNull method to StringHelper

CAMEL-11090 Add trimToNull method to StringHelper


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/112ef785
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/112ef785
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/112ef785

Branch: refs/heads/master
Commit: 112ef7851ab85653c86be7d4359b1a0f35b35902
Parents: 39ae85b
Author: Zoran Regvart <zr...@apache.org>
Authored: Wed Mar 29 13:14:29 2017 +0200
Committer: Zoran Regvart <zr...@apache.org>
Committed: Wed Mar 29 13:14:29 2017 +0200

----------------------------------------------------------------------
 .../org/apache/camel/util/StringHelper.java     | 26 ++++++++++++++++++++
 .../org/apache/camel/util/StringHelperTest.java |  9 +++++++
 2 files changed, 35 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/112ef785/camel-core/src/main/java/org/apache/camel/util/StringHelper.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/StringHelper.java b/camel-core/src/main/java/org/apache/camel/util/StringHelper.java
index 7322be0..c3f987f 100644
--- a/camel-core/src/main/java/org/apache/camel/util/StringHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/util/StringHelper.java
@@ -647,5 +647,31 @@ public final class StringHelper {
         return changed;
     }
 
+    /**
+     * Removes the leading and trailing whitespace and if the resulting
+     * string is empty returns {@code null}. Examples:
+     * <p>
+     * Examples:
+     * <blockquote><pre>
+     * trimToNull("abc") -> "abc"
+     * trimToNull(" abc") -> "abc"
+     * trimToNull(" abc ") -> "abc"
+     * trimToNull(" ") -> null
+     * trimToNull("") -> null
+     * </pre></blockquote>
+     */
+    public static String trimToNull(final String given) {
+        if (given == null) {
+            return null;
+        }
+
+        final String trimmed = given.trim();
+
+        if (trimmed.isEmpty()) {
+            return null;
+        }
+
+        return trimmed;
+    }
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/112ef785/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java b/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java
index 760971b..ca987d8 100644
--- a/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java
+++ b/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java
@@ -239,4 +239,13 @@ public class StringHelperTest extends TestCase {
         assertEquals(3, changed.get(1).intValue());
     }
 
+    public void testTrimToNull() {
+        assertEquals(StringHelper.trimToNull("abc"),"abc");
+        assertEquals(StringHelper.trimToNull(" abc") , "abc");
+        assertEquals(StringHelper.trimToNull(" abc "),"abc");
+        assertNull(StringHelper.trimToNull(" "));
+        assertNull(StringHelper.trimToNull("\t"));
+        assertNull(StringHelper.trimToNull(" \t "));
+        assertNull(StringHelper.trimToNull(""));
+    }
 }