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 2021/05/16 13:54:43 UTC

[commons-io] 03/07: Add IOConsumer.noop().

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-io.git

commit 9bb9b04e6155ab292b8899b86d75ea486edb047b
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun May 16 08:09:31 2021 -0400

    Add IOConsumer.noop().
    
    Fix local var name typos.
---
 src/changes/changes.xml                            |  3 ++
 .../org/apache/commons/io/function/IOConsumer.java | 17 +++++++++
 .../org/apache/commons/io/IOUtilsTestCase.java     | 20 +++++-----
 .../apache/commons/io/function/IOConsumerTest.java | 44 ++++++++++++++++++++++
 4 files changed, 74 insertions(+), 10 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 65e2b8f..1ae6fdd 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -207,6 +207,9 @@ The <action> type attribute can be add,update,fix,remove.
         Add CharacterFilterReader(Reader, IntPredicate), #227.
         Add CharacterFilterReaderIntPredicateTest, #227.
       </action>
+      <action dev="ggregory" type="add" due-to="Gary Gregory">
+        Add IOConsumer.noop().
+      </action>
       <!-- UPDATES -->
       <action dev="ggregory" type="update" due-to="Dependabot">
         Update junit-jupiter from 5.6.2 to 5.7.0 #153.
diff --git a/src/main/java/org/apache/commons/io/function/IOConsumer.java b/src/main/java/org/apache/commons/io/function/IOConsumer.java
index c730f9f..b083f58 100644
--- a/src/main/java/org/apache/commons/io/function/IOConsumer.java
+++ b/src/main/java/org/apache/commons/io/function/IOConsumer.java
@@ -31,6 +31,23 @@ import java.util.function.Consumer;
 public interface IOConsumer<T> {
 
     /**
+     * Package private constant; consider private.
+     */
+    static final IOConsumer<?> NOOP_IO_CONSUMER = t -> {/* noop */};
+
+    /**
+     * Returns a constant NOOP consumer.
+     * 
+     * @param <T> Type consumer type.
+     * @return a constant NOOP consumer.
+     * @since 2.9.0
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> IOConsumer<T> noop() {
+        return (IOConsumer<T>) NOOP_IO_CONSUMER;
+    }
+
+    /**
      * Performs this operation on the given argument.
      *
      * @param t the input argument
diff --git a/src/test/java/org/apache/commons/io/IOUtilsTestCase.java b/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
index 9b4d41e..5e89881 100644
--- a/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
+++ b/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
@@ -337,25 +337,25 @@ public class IOUtilsTestCase {
 
     @Test
     public void testCloseConsumer() {
-        final Closeable nulCloseable = null;
-        assertDoesNotThrow(() -> IOUtils.close(nulCloseable, null)); // null consumer
+        final Closeable nullCloseable = null;
+        assertDoesNotThrow(() -> IOUtils.close(nullCloseable, null)); // null consumer
         assertDoesNotThrow(() -> IOUtils.close(new StringReader("s"), null)); // null consumer
         assertDoesNotThrow(() -> IOUtils.close(new ThrowOnCloseReader(new StringReader("s")), null)); // null consumer
 
         final IOConsumer<IOException> nullConsumer = null; // null consumer doesn't throw
-        assertDoesNotThrow(() -> IOUtils.close(nulCloseable, nullConsumer));
+        assertDoesNotThrow(() -> IOUtils.close(nullCloseable, nullConsumer));
         assertDoesNotThrow(() -> IOUtils.close(new StringReader("s"), nullConsumer));
         assertDoesNotThrow(() -> IOUtils.close(new ThrowOnCloseReader(new StringReader("s")), nullConsumer));
 
-        final IOConsumer<IOException> silentConsumer = i -> {}; // silent consumer doesn't throw
-        assertDoesNotThrow(() -> IOUtils.close(nulCloseable, silentConsumer));
+        final IOConsumer<IOException> silentConsumer = IOConsumer.noop(); // noop consumer doesn't throw
+        assertDoesNotThrow(() -> IOUtils.close(nullCloseable, silentConsumer));
         assertDoesNotThrow(() -> IOUtils.close(new StringReader("s"), silentConsumer));
         assertDoesNotThrow(() -> IOUtils.close(new ThrowOnCloseReader(new StringReader("s")), silentConsumer));
 
         final IOConsumer<IOException> noisyConsumer = i -> {
             throw i;
         }; // consumer passes on the throw
-        assertDoesNotThrow(() -> IOUtils.close(nulCloseable, noisyConsumer)); // no throw
+        assertDoesNotThrow(() -> IOUtils.close(nullCloseable, noisyConsumer)); // no throw
         assertDoesNotThrow(() -> IOUtils.close(new StringReader("s"), noisyConsumer)); // no throw
         assertThrows(IOException.class,
             () -> IOUtils.close(new ThrowOnCloseReader(new StringReader("s")), noisyConsumer)); // closeable throws
@@ -363,14 +363,14 @@ public class IOUtilsTestCase {
 
     @Test
     public void testCloseMulti() {
-        final Closeable nulCloseable = null;
+        final Closeable nullCloseable = null;
         final Closeable[] closeables = {null, null};
-        assertDoesNotThrow(() -> IOUtils.close(nulCloseable, nulCloseable));
+        assertDoesNotThrow(() -> IOUtils.close(nullCloseable, nullCloseable));
         assertDoesNotThrow(() -> IOUtils.close(closeables));
         assertDoesNotThrow(() -> IOUtils.close((Closeable[]) null));
-        assertDoesNotThrow(() -> IOUtils.close(new StringReader("s"), nulCloseable));
+        assertDoesNotThrow(() -> IOUtils.close(new StringReader("s"), nullCloseable));
         assertThrows(IOException.class,
-            () -> IOUtils.close(nulCloseable, new ThrowOnCloseReader(new StringReader("s"))));
+            () -> IOUtils.close(nullCloseable, new ThrowOnCloseReader(new StringReader("s"))));
     }
 
     @Test
diff --git a/src/test/java/org/apache/commons/io/function/IOConsumerTest.java b/src/test/java/org/apache/commons/io/function/IOConsumerTest.java
new file mode 100644
index 0000000..869700e
--- /dev/null
+++ b/src/test/java/org/apache/commons/io/function/IOConsumerTest.java
@@ -0,0 +1,44 @@
+/*
+ * 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.io.function;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.StringReader;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.test.ThrowOnCloseReader;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@linnk IOConsumer}.
+ */
+public class IOConsumerTest {
+
+    @Test
+    public void testNoopIOConsumer() {
+        final Closeable nullCloseable = null;
+        final IOConsumer<IOException> noopConsumer = IOConsumer.noop(); // noop consumer doesn't throw
+        assertDoesNotThrow(() -> IOUtils.close(nullCloseable, noopConsumer));
+        assertDoesNotThrow(() -> IOUtils.close(new StringReader("s"), noopConsumer));
+        assertDoesNotThrow(() -> IOUtils.close(new ThrowOnCloseReader(new StringReader("s")), noopConsumer));
+
+    }
+}