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 596902ccaed90e0f1d72945e7eeb5c928dabfec1 Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Sun Aug 7 23:02:08 2022 -0400 Add IOUnaryOperator --- src/changes/changes.xml | 2 +- .../commons/io/function/IOUnaryOperator.java | 56 ++++++++++++++++++++++ .../commons/io/function/IOUnaryOperatorTest.java | 56 ++++++++++++++++++++++ .../apache/commons/io/function/TestConstants.java | 2 + .../org/apache/commons/io/function/TestUtils.java | 11 +++-- 5 files changed, 123 insertions(+), 4 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index f6c240ae..d1a63601 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -360,7 +360,7 @@ The <action> type attribute can be add,update,fix,remove. Add IOExceptionList.checkEmpty(List, Object). </action> <action dev="ggregory" type="add" due-to="Gary Gregory"> - Add IOBiConsumer, IOTriConsumer, IOComparator. + Add IOBiConsumer, IOTriConsumer, IOComparator, IOUnaryOperator. </action> <action dev="ggregory" type="add" due-to="Gary Gregory"> Add and reuse IOConsumer forAll(*), forEach(*), and forEachIndexed(*). diff --git a/src/main/java/org/apache/commons/io/function/IOUnaryOperator.java b/src/main/java/org/apache/commons/io/function/IOUnaryOperator.java new file mode 100644 index 00000000..cf6445d5 --- /dev/null +++ b/src/main/java/org/apache/commons/io/function/IOUnaryOperator.java @@ -0,0 +1,56 @@ +/* + * 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 java.io.IOException; +import java.io.UncheckedIOException; +import java.util.function.UnaryOperator; + +/** + * Like {@link UnaryOperator} but throws {@link IOException}. + * + * @param <T> the type of the operand and result of the operator. + * + * @see UnaryOperator + * @see IOFunction + * @since 2.12.0 + */ +@FunctionalInterface +public interface IOUnaryOperator<T> extends IOFunction<T, T> { + + /** + * Creates a unary operator that always returns its input argument. + * + * @param <T> the type of the input and output of the operator. + * @return a unary operator that always returns its input argument. + */ + static <T> IOUnaryOperator<T> identity() { + return t -> t; + } + + /** + * Creates a {@link UnaryOperator} for this instance that throws {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @return an unchecked BiFunction. + */ + default UnaryOperator<T> asUnaryOperator() { + return t -> Uncheck.apply(this, t); + } + +} diff --git a/src/test/java/org/apache/commons/io/function/IOUnaryOperatorTest.java b/src/test/java/org/apache/commons/io/function/IOUnaryOperatorTest.java new file mode 100644 index 00000000..11dbae0f --- /dev/null +++ b/src/test/java/org/apache/commons/io/function/IOUnaryOperatorTest.java @@ -0,0 +1,56 @@ +/* + * 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.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +import org.junit.jupiter.api.Test; + +/** + * Tests {@link IOUnaryOperator}. + */ +public class IOUnaryOperatorTest { + + @Test + public void testAsUnaryOperator() { + final List<Path> list = Arrays.asList(TestConstants.ABS_PATH_A, TestConstants.ABS_PATH_A); + final IOUnaryOperator<Path> throwingIOUnaryOperator = TestUtils.throwingIOUnaryOperator(); + assertThrows(UncheckedIOException.class, () -> list.replaceAll(throwingIOUnaryOperator.asUnaryOperator())); + assertEquals("a", Optional.of("a").map(IOUnaryOperator.identity().asUnaryOperator()).get()); + assertEquals("a", Optional.of("a").map(IOUnaryOperator.identity().asFunction()).get()); + } + + @Test + public void testIdentity() throws IOException { + assertEquals(IOUnaryOperator.identity(), IOUnaryOperator.identity()); + final IOUnaryOperator<byte[]> identityFunction = IOUnaryOperator.identity(); + final byte[] buf = {(byte) 0xa, (byte) 0xb, (byte) 0xc}; + assertEquals(buf, identityFunction.apply(buf)); + assertArrayEquals(buf, identityFunction.apply(buf)); + } + +} diff --git a/src/test/java/org/apache/commons/io/function/TestConstants.java b/src/test/java/org/apache/commons/io/function/TestConstants.java index c361e878..482d8389 100644 --- a/src/test/java/org/apache/commons/io/function/TestConstants.java +++ b/src/test/java/org/apache/commons/io/function/TestConstants.java @@ -74,6 +74,8 @@ class TestConstants { throw new IOException("Failure"); }; + static IOUnaryOperator<?> THROWING_IO_UNARY_OPERATOR = t -> throwIOException(); + static Predicate<Object> THROWING_PREDICATE = t -> { throw new UncheckedIOException(new IOException("Failure")); }; diff --git a/src/test/java/org/apache/commons/io/function/TestUtils.java b/src/test/java/org/apache/commons/io/function/TestUtils.java index 47889bee..1338ee79 100644 --- a/src/test/java/org/apache/commons/io/function/TestUtils.java +++ b/src/test/java/org/apache/commons/io/function/TestUtils.java @@ -22,6 +22,10 @@ import java.util.concurrent.atomic.AtomicReference; class TestUtils { + static <T> T compareAndSetThrows(final AtomicReference<T> ref, final T update) throws IOException { + return compareAndSetThrows(ref, null, update); + } + static <T> T compareAndSetThrows(final AtomicReference<T> ref, final T expected, final T update) throws IOException { if (!ref.compareAndSet(expected, update)) { throw new IOException("Unexpected"); @@ -29,8 +33,9 @@ class TestUtils { return ref.get(); // same as update } - static <T> T compareAndSetThrows(final AtomicReference<T> ref, final T update) throws IOException { - return compareAndSetThrows(ref, null, update); - } + @SuppressWarnings("unchecked") + static <T> IOUnaryOperator<T> throwingIOUnaryOperator() { + return (IOUnaryOperator<T>) TestConstants.THROWING_IO_UNARY_OPERATOR; + } }