Repository: camel Updated Branches: refs/heads/master fcdf9f927 -> 36c9839e1
CAMEL-10390: ObjectHelper's after/before/between enhancements Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/36c9839e Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/36c9839e Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/36c9839e Branch: refs/heads/master Commit: 36c9839e1f564f9c6b9421b7fa1a85ea149d070f Parents: fcdf9f9 Author: lburgazzoli <lburgazz...@gmail.com> Authored: Thu Oct 20 01:38:10 2016 +0200 Committer: lburgazzoli <lburgazz...@gmail.com> Committed: Thu Oct 20 11:15:55 2016 +0200 ---------------------------------------------------------------------- .../org/apache/camel/util/ObjectHelper.java | 73 ++++++++++++++++++++ .../org/apache/camel/util/ObjectHelperTest.java | 13 ++++ 2 files changed, 86 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/36c9839e/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java b/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java index 0f10eaf..5f852c7 100644 --- a/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java +++ b/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java @@ -42,9 +42,11 @@ import java.util.Locale; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.Properties; import java.util.Scanner; import java.util.concurrent.Callable; +import java.util.function.Function; import org.w3c.dom.Node; import org.w3c.dom.NodeList; @@ -456,6 +458,23 @@ public final class ObjectHelper { } /** + * Returns an object after the given token + * + * @param text the text + * @param after the token + * @param mapper a mapping function to convert the string after the token to type T + * @return an Optional describing the result of applying a mapping function to the text after the token. + */ + public static <T> Optional<T> after(String text, String after, Function<String, T> mapper) { + String result = after(text, after); + if (result == null) { + return Optional.empty(); + } else { + return Optional.ofNullable(mapper.apply(result)); + } + } + + /** * Returns the string before the given token * * @param text the text @@ -470,6 +489,24 @@ public final class ObjectHelper { } /** + * Returns an object before the given token + * + * @param text the text + * @param before the token + * @param mapper a mapping function to convert the string before the token to type T + * @return an Optional describing the result of applying a mapping function to the text before the token. + */ + public static <T> Optional<T> before(String text, String before, Function<String, T> mapper) { + String result = before(text, before); + if (result == null) { + return Optional.empty(); + } else { + return Optional.ofNullable(mapper.apply(result)); + } + } + + + /** * Returns the string between the given tokens * * @param text the text @@ -486,6 +523,24 @@ public final class ObjectHelper { } /** + * Returns an object between the given token + * + * @param text the text + * @param after the before token + * @param before the after token + * @param mapper a mapping function to convert the string between the token to type T + * @return an Optional describing the result of applying a mapping function to the text between the token. + */ + public static <T> Optional<T> between(String text, String after, String before, Function<String, T> mapper) { + String result = between(text, after, before); + if (result == null) { + return Optional.empty(); + } else { + return Optional.ofNullable(mapper.apply(result)); + } + } + + /** * Returns the string between the most outer pair of tokens * <p/> * The number of token pairs must be evenly, eg there must be same number of before and after tokens, otherwise <tt>null</tt> is returned @@ -549,6 +604,24 @@ public final class ObjectHelper { } /** + * Returns an object between the most outer pair of tokens + * + * @param text the text + * @param after the before token + * @param before the after token + * @param mapper a mapping function to convert the string between the most outer pair of tokens to type T + * @return an Optional describing the result of applying a mapping function to the text between the most outer pair of tokens. + */ + public static <T> Optional<T> betweenOuterPair(String text, char before, char after, Function<String, T> mapper) { + String result = betweenOuterPair(text, before, after); + if (result == null) { + return Optional.empty(); + } else { + return Optional.ofNullable(mapper.apply(result)); + } + } + + /** * Returns true if the collection contains the specified value */ public static boolean contains(Object collectionOrArray, Object value) { http://git-wip-us.apache.org/repos/asf/camel/blob/36c9839e/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java b/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java index 3412fe0..dcb7432 100644 --- a/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java +++ b/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java @@ -28,6 +28,7 @@ import java.util.List; import java.util.Map; import java.util.NoSuchElementException; import java.util.Properties; +import java.util.function.Function; import org.w3c.dom.Node; import org.w3c.dom.NodeList; @@ -681,18 +682,27 @@ public class ObjectHelperTest extends TestCase { assertEquals("Hello ", ObjectHelper.before("Hello World", "World")); assertEquals("Hello ", ObjectHelper.before("Hello World Again", "World")); assertEquals(null, ObjectHelper.before("Hello Again", "Foo")); + + assertTrue(ObjectHelper.before("mykey:ignore", ":", "mykey"::equals).orElse(false)); + assertFalse(ObjectHelper.before("ignore:ignore", ":", "mykey"::equals).orElse(false)); } public void testAfter() { assertEquals(" World", ObjectHelper.after("Hello World", "Hello")); assertEquals(" World Again", ObjectHelper.after("Hello World Again", "Hello")); assertEquals(null, ObjectHelper.after("Hello Again", "Foo")); + + assertTrue(ObjectHelper.after("ignore:mykey", ":", "mykey"::equals).orElse(false)); + assertFalse(ObjectHelper.after("ignore:ignore", ":", "mykey"::equals).orElse(false)); } public void testBetween() { assertEquals("foo bar", ObjectHelper.between("Hello 'foo bar' how are you", "'", "'")); assertEquals("foo bar", ObjectHelper.between("Hello ${foo bar} how are you", "${", "}")); assertEquals(null, ObjectHelper.between("Hello ${foo bar} how are you", "'", "'")); + + assertTrue(ObjectHelper.between("begin:mykey:end", "begin:", ":end", "mykey"::equals).orElse(false)); + assertFalse(ObjectHelper.between("begin:ignore:end", "begin:", ":end", "mykey"::equals).orElse(false)); } public void testBetweenOuterPair() { @@ -702,6 +712,9 @@ public class ObjectHelperTest extends TestCase { assertEquals(null, ObjectHelper.betweenOuterPair("foo)bar)baz123", '(', ')')); assertEquals("bar", ObjectHelper.betweenOuterPair("foo(bar)baz123", '(', ')')); assertEquals("'bar', 'baz()123', 123", ObjectHelper.betweenOuterPair("foo('bar', 'baz()123', 123)", '(', ')')); + + assertTrue(ObjectHelper.betweenOuterPair("foo(bar)baz123", '(', ')', "bar"::equals).orElse(false)); + assertFalse(ObjectHelper.betweenOuterPair("foo[bar)baz123", '(', ')', "bar"::equals).orElse(false)); } public void testIsJavaIdentifier() {