Add IOConsumer.noop().
Fix local var name typos.
MODIFY
src/changes/changes.xml
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.

MODIFY
src/main/java/org/apache/commons/io/function/IOConsumer.java
src/main/java/org/apache/commons/io/function/IOConsumer.java
@FunctionalInterface
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.
*

MODIFY
src/test/java/org/apache/commons/io/IOUtilsTestCase.java
src/test/java/org/apache/commons/io/IOUtilsTestCase.java
@@ -337,25 +337,25 @@ public void testClose() {
@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 void testCloseConsumer() {
@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

ADD
src/test/java/org/apache/commons/io/function/IOConsumerTest.java
+/*
+ * 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));
+
+ }
+}

Add IOConsumer.noop().

Fix local var name typos.