You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/03/21 15:38:27 UTC

[commons-lang] branch master updated: Add Consumers.

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new 8218767  Add Consumers.
8218767 is described below

commit 82187678948b16b827454b5007bb9073d0666c81
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Mar 21 11:38:20 2022 -0400

    Add Consumers.
---
 src/changes/changes.xml                            |  1 +
 .../apache/commons/lang3/function/Consumers.java   | 49 ++++++++++++++++++++++
 .../commons/lang3/function/ConsumersTest.java      | 48 +++++++++++++++++++++
 3 files changed, 98 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 45e5cdc..5d61e31 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -124,6 +124,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add TimeZones.toTimeZone(TimeZone).</action>
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add FutureTasks.</action>
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add Memoizer(Function) and Memoizer(Function, boolean).</action>
+    <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add Consumers.</action>
     <!-- UPDATE -->
     <action                   type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump spotbugs-maven-plugin from 4.2.0 to 4.5.3.0 #735, #808, #822, #834.</action>
     <action                   type="update" dev="ggregory" due-to="Dependabot, XenoAmess">Bump actions/cache from v2.1.4 to v2.1.7 #742, #752, #764, #833.</action>
diff --git a/src/main/java/org/apache/commons/lang3/function/Consumers.java b/src/main/java/org/apache/commons/lang3/function/Consumers.java
new file mode 100644
index 0000000..19b5efd
--- /dev/null
+++ b/src/main/java/org/apache/commons/lang3/function/Consumers.java
@@ -0,0 +1,49 @@
+/*
+ * 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.commons.lang3.function;
+
+import java.util.function.Consumer;
+import java.util.function.Function;
+
+/**
+ * Provides {@link Consumer} instances.
+ *
+ * @since 3.13.0
+ */
+public class Consumers {
+
+    /** NOP singleton. */
+    @SuppressWarnings("rawtypes")
+    private static final Consumer NOP = Function.identity()::apply;
+
+    private Consumers() {
+        // No instances.
+    }
+
+    /**
+     * Gets the NOP Consumer singleton.
+     *
+     * @param <T> type type to consume.
+     * @return the NOP Consumer singleton..
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> Consumer<T> nop() {
+        return NOP;
+    }
+
+}
diff --git a/src/test/java/org/apache/commons/lang3/function/ConsumersTest.java b/src/test/java/org/apache/commons/lang3/function/ConsumersTest.java
new file mode 100644
index 0000000..f7b124f
--- /dev/null
+++ b/src/test/java/org/apache/commons/lang3/function/ConsumersTest.java
@@ -0,0 +1,48 @@
+/*
+ * 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.commons.lang3.function;
+
+import java.util.function.Consumer;
+import java.util.stream.Stream;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link Consumers}.
+ */
+public class ConsumersTest {
+
+    /**
+     * Tests {@link Consumers#nop()}.
+     */
+    @Test
+    public void testNop() {
+        Stream.of("").forEach(Consumers.nop());
+        //
+        Consumer<?> c1 = Consumers.nop();
+        c1.accept(null);
+        Consumer<Object> c2 = Consumers.nop();
+        c2.accept(null);
+        Consumer<String> c3 = Consumers.nop();
+        c3.accept(null);
+        //
+        Consumers.nop().accept(null);
+        Consumers.nop().accept("");
+    }
+
+}