Repository: camel Updated Branches: refs/heads/master 5db786a28 -> 70c18da39
Add some functional bits Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/70c18da3 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/70c18da3 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/70c18da3 Branch: refs/heads/master Commit: 70c18da3980e4d39b12bd7c62d6f9cfb3f2dd867 Parents: 5db786a Author: lburgazzoli <[email protected]> Authored: Mon Sep 4 17:11:35 2017 +0200 Committer: lburgazzoli <[email protected]> Committed: Mon Sep 4 17:11:43 2017 +0200 ---------------------------------------------------------------------- .../apache/camel/util/function/Predicates.java | 55 ++++++++++++++++++++ .../camel/util/function/PredicatesTest.java | 32 ++++++++++++ 2 files changed, 87 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/70c18da3/camel-core/src/main/java/org/apache/camel/util/function/Predicates.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/util/function/Predicates.java b/camel-core/src/main/java/org/apache/camel/util/function/Predicates.java new file mode 100644 index 0000000..d8c8780 --- /dev/null +++ b/camel-core/src/main/java/org/apache/camel/util/function/Predicates.java @@ -0,0 +1,55 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.util.function; + +import java.util.function.Predicate; + +import org.apache.camel.util.ObjectHelper; + +/** + * Predicate helpers, inspired by http://minborgsjavapot.blogspot.it/2016/03/put-your-java-8-method-references-to.html + * + */ +public final class Predicates { + private Predicates() { + } + + /** + * Wrap a predicate, useful for method references. + */ + public static <T> Predicate<T> of(Predicate<T> predicate) { + ObjectHelper.notNull(predicate, "Predicate"); + + return predicate; + } + + + /** + * Negates a predicate, useful for method references. + * + * <pre> + * Stream.of("A", "", "B") + * .filter(Predicates.negate(String::isEmpty)) + * .count(); + * </pre> + */ + public static <T> Predicate<T> negate(Predicate<T> predicate) { + ObjectHelper.notNull(predicate, "Predicate"); + + return predicate.negate(); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/70c18da3/camel-core/src/test/java/org/apache/camel/util/function/PredicatesTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/util/function/PredicatesTest.java b/camel-core/src/test/java/org/apache/camel/util/function/PredicatesTest.java new file mode 100644 index 0000000..559d223 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/util/function/PredicatesTest.java @@ -0,0 +1,32 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.util.function; + +import org.junit.Assert; +import org.junit.Test; + +public class PredicatesTest { + + @Test + public void testNegate() { + Assert.assertFalse(Predicates.of(String::isEmpty).test("something")); + Assert.assertTrue(Predicates.of(String::isEmpty).negate().test("something")); + Assert.assertTrue(Predicates.negate(String::isEmpty).test("something")); + Assert.assertFalse(Predicates.negate(String::isEmpty).test("")); + Assert.assertFalse(Predicates.of(String::isEmpty).negate().test("")); + } +}
