Add IOUnaryOperator
MODIFY
src/changes/changes.xml
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(*).

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

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

MODIFY
src/test/java/org/apache/commons/io/function/TestConstants.java
src/test/java/org/apache/commons/io/function/TestConstants.java
throw new IOException("Failure");
};
+ static IOUnaryOperator<?> THROWING_IO_UNARY_OPERATOR = t -> throwIOException();
+
static Predicate<Object> THROWING_PREDICATE = t -> {
throw new UncheckedIOException(new IOException("Failure"));
};

MODIFY
src/test/java/org/apache/commons/io/function/TestUtils.java
src/test/java/org/apache/commons/io/function/TestUtils.java
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");
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;
}
}

Add IOUnaryOperator