Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id E1578200CEF for ; Mon, 4 Sep 2017 17:12:06 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id DFDE016520F; Mon, 4 Sep 2017 15:12:06 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 31E7F16520E for ; Mon, 4 Sep 2017 17:12:06 +0200 (CEST) Received: (qmail 22068 invoked by uid 500); 4 Sep 2017 15:12:05 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 22058 invoked by uid 99); 4 Sep 2017 15:12:05 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 04 Sep 2017 15:12:05 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 442B0F32D2; Mon, 4 Sep 2017 15:12:05 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: lburgazzoli@apache.org To: commits@camel.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: camel git commit: Add some functional bits Date: Mon, 4 Sep 2017 15:12:05 +0000 (UTC) archived-at: Mon, 04 Sep 2017 15:12:07 -0000 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 Authored: Mon Sep 4 17:11:35 2017 +0200 Committer: lburgazzoli 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 Predicate of(Predicate predicate) { + ObjectHelper.notNull(predicate, "Predicate"); + + return predicate; + } + + + /** + * Negates a predicate, useful for method references. + * + *
+     *     Stream.of("A", "", "B")
+     *         .filter(Predicates.negate(String::isEmpty))
+     *         .count();
+     * 
+ */ + public static Predicate negate(Predicate 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("")); + } +}