Skip to content

Commit ab687c9

Browse files
author
Gary Gregory
committed
Change parameter order of new methods, use var-args, improve tests
1 parent 7d198e4 commit ab687c9

15 files changed

Lines changed: 265 additions & 31 deletions

src/main/java/org/apache/commons/io/FileUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ public static long checksumCRC32(final File file) throws IOException {
345345
* @see #forceDelete(File)
346346
*/
347347
public static void cleanDirectory(final File directory) throws IOException {
348-
IOConsumer.forAll(listFiles(directory, null), FileUtils::forceDelete);
348+
IOConsumer.forAll(FileUtils::forceDelete, listFiles(directory, null));
349349
}
350350

351351
/**
@@ -358,7 +358,7 @@ public static void cleanDirectory(final File directory) throws IOException {
358358
* @see #forceDeleteOnExit(File)
359359
*/
360360
private static void cleanDirectoryOnExit(final File directory) throws IOException {
361-
IOConsumer.forAll(listFiles(directory, null), FileUtils::forceDeleteOnExit);
361+
IOConsumer.forAll(FileUtils::forceDeleteOnExit, listFiles(directory, null));
362362
}
363363

364364
/**

src/main/java/org/apache/commons/io/IOUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ public static void close(final Closeable closeable) throws IOException {
396396
* @since 2.8.0
397397
*/
398398
public static void close(final Closeable... closeables) throws IOExceptionList {
399-
IOConsumer.forAll(closeables, IOUtils::close);
399+
IOConsumer.forAll(IOUtils::close, closeables);
400400
}
401401

402402
/**
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.io.function;
18+
19+
import java.io.IOException;
20+
21+
/**
22+
* Erases {@link IOException} for the compiler but still throws that exception at runtime.
23+
*/
24+
class Erase {
25+
26+
/**
27+
* Delegates to the given {@link IOBiConsumer} but erases its {@link IOException} for the compiler, while still throwing
28+
* the exception at runtime.
29+
*
30+
* @param <T> See delegate.
31+
* @param <U> See delegate.
32+
* @param consumer See delegate.
33+
* @param t See delegate.
34+
* @param u See delegate.
35+
* @see IOBiConsumer
36+
*/
37+
static <T, U> void accept(final IOBiConsumer<T, U> consumer, final T t, final U u) {
38+
try {
39+
consumer.accept(t, u);
40+
} catch (final IOException ex) {
41+
rethrow(ex); // throws IOException
42+
}
43+
}
44+
45+
/**
46+
* Delegates to the given {@link IOConsumer} but erases its {@link IOException} for the compiler, while still throwing
47+
* the exception at runtime.
48+
*
49+
* @param <T> See delegate.
50+
* @param consumer See delegate.
51+
* @param t See delegate.
52+
* @see IOConsumer
53+
*/
54+
static <T> void accept(final IOConsumer<T> consumer, final T t) {
55+
try {
56+
consumer.accept(t);
57+
} catch (final IOException ex) {
58+
rethrow(ex); // throws IOException
59+
}
60+
}
61+
62+
/**
63+
* Delegates to the given {@link IOBiFunction} but erases its {@link IOException} for the compiler, while still throwing
64+
* the exception at runtime.
65+
*
66+
* @param <T> See delegate.
67+
* @param <U> See delegate.
68+
* @param <R> See delegate.
69+
* @param mapper See delegate.
70+
* @param t See delegate.
71+
* @param u See delegate.
72+
* @return See delegate.
73+
* @see IOBiFunction
74+
*/
75+
static <T, U, R> R apply(final IOBiFunction<? super T, ? super U, ? extends R> mapper, final T t, final U u) {
76+
try {
77+
return mapper.apply(t, u);
78+
} catch (final IOException e) {
79+
throw rethrow(e); // throws IOException
80+
}
81+
}
82+
83+
/**
84+
* Delegates to the given {@link IOFunction} but erases its {@link IOException} for the compiler, while still throwing
85+
* the exception at runtime.
86+
*
87+
* @param <T> See delegate.
88+
* @param <R> See delegate.
89+
* @param mapper See delegate.
90+
* @param t See delegate.
91+
* @return See delegate.
92+
* @see IOFunction
93+
*/
94+
static <T, R> R apply(final IOFunction<? super T, ? extends R> mapper, final T t) {
95+
try {
96+
return mapper.apply(t);
97+
} catch (final IOException e) {
98+
throw rethrow(e); // throws IOException
99+
}
100+
}
101+
102+
/**
103+
* Delegates to the given {@link IOComparator} but erases its {@link IOException} for the compiler, while still throwing
104+
* the exception at runtime.
105+
*
106+
* @param <T> See delegate.
107+
* @param comparator See delegate.
108+
* @param t See delegate.
109+
* @param u See delegate.
110+
* @return See delegate.
111+
* @see IOComparator
112+
*/
113+
static <T> int compare(final IOComparator<? super T> comparator, final T t, final T u) {
114+
try {
115+
return comparator.compare(t, u);
116+
} catch (final IOException e) {
117+
throw rethrow(e); // throws IOException
118+
}
119+
}
120+
121+
/**
122+
* Delegates to the given {@link IOSupplier} but erases its {@link IOException} for the compiler, while still throwing
123+
* the exception at runtime.
124+
*
125+
* @param <T> See delegate.
126+
* @param supplier See delegate.
127+
* @return See delegate.
128+
* @see IOSupplier
129+
*/
130+
static <T> T get(final IOSupplier<T> supplier) {
131+
try {
132+
return supplier.get();
133+
} catch (final IOException e) {
134+
throw rethrow(e); // throws IOException
135+
}
136+
}
137+
138+
/**
139+
* Throws the given throwable.
140+
*
141+
* @param <T> The throwable cast type.
142+
* @param throwable The throwable to rethrow.
143+
* @return nothing because we throw.
144+
* @throws T Always thrown.
145+
*/
146+
@SuppressWarnings("unchecked")
147+
static <T extends Throwable> RuntimeException rethrow(final Throwable throwable) throws T {
148+
throw (T) throwable;
149+
}
150+
151+
/**
152+
* Delegates to the given {@link IORunnable} but erases its {@link IOException} for the compiler, while still throwing
153+
* the exception at runtime.
154+
*
155+
* @param runnable See delegate.
156+
* @see IORunnable
157+
*/
158+
static void run(final IORunnable runnable) {
159+
try {
160+
runnable.run();
161+
} catch (final IOException e) {
162+
throw rethrow(e); // throws IOException
163+
}
164+
}
165+
166+
/**
167+
* Delegates to the given {@link IOPredicate} but erases its {@link IOException} for the compiler, while still throwing
168+
* the exception at runtime.
169+
*
170+
* @param <T> See delegate.
171+
* @param predicate See delegate.
172+
* @param t See delegate.
173+
* @return See delegate.
174+
* @see IOPredicate
175+
*/
176+
static <T> boolean test(final IOPredicate<? super T> predicate, final T t) {
177+
try {
178+
return predicate.test(t);
179+
} catch (final IOException e) {
180+
throw rethrow(e); // throws IOException
181+
}
182+
}
183+
184+
/** No instances. */
185+
private Erase() {
186+
// No instances.
187+
}
188+
189+
}

src/main/java/org/apache/commons/io/function/IOConsumer.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,40 +42,41 @@ public interface IOConsumer<T> {
4242

4343
/**
4444
* Performs an action for each element of the collection gathering any exceptions.
45+
* @param action The action to apply to each input element.
46+
* @param collection The input to stream.
4547
*
4648
* @param <T> The element type.
47-
* @param collection The input to stream.
48-
* @param action The action to apply to each input element.
4949
* @throws IOExceptionList if any I/O errors occur.
5050
* @since 2.12.0
5151
*/
52-
static <T> void forAll(final Iterable<T> collection, final IOConsumer<T> action) throws IOExceptionList {
52+
static <T> void forAll(final IOConsumer<T> action, final Iterable<T> collection) throws IOExceptionList {
5353
IOStreams.forAll(IOStreams.of(collection), action);
5454
}
5555

5656
/**
5757
* Performs an action for each element of the collection gathering any exceptions.
58+
* @param action The action to apply to each input element.
59+
* @param stream The input to stream.
5860
*
5961
* @param <T> The element type.
60-
* @param stream The input to stream.
61-
* @param action The action to apply to each input element.
6262
* @throws IOExceptionList if any I/O errors occur.
6363
* @since 2.12.0
6464
*/
65-
static <T> void forAll(final Stream<T> stream, final IOConsumer<T> action) throws IOExceptionList {
65+
static <T> void forAll(final IOConsumer<T> action, final Stream<T> stream) throws IOExceptionList {
6666
IOStreams.forAll(stream, action, IOIndexedException::new);
6767
}
6868

6969
/**
7070
* Performs an action for each element of the array gathering any exceptions.
71+
* @param action The action to apply to each input element.
72+
* @param array The input to stream.
7173
*
7274
* @param <T> The element type.
73-
* @param array The input to stream.
74-
* @param action The action to apply to each input element.
7575
* @throws IOExceptionList if any I/O errors occur.
7676
* @since 2.12.0
7777
*/
78-
static <T> void forAll(final T[] array, final IOConsumer<T> action) throws IOExceptionList {
78+
@SafeVarargs
79+
static <T> void forAll(final IOConsumer<T> action, final T... array) throws IOExceptionList {
7980
IOStreams.forAll(IOStreams.of(array), action);
8081
}
8182

src/main/java/org/apache/commons/io/input/ObservableInputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public void consume() throws IOException {
175175
}
176176

177177
private void forEachObserver(final IOConsumer<Observer> action) throws IOException {
178-
IOConsumer.forAll(observers, Objects.requireNonNull(action));
178+
IOConsumer.forAll(Objects.requireNonNull(action), observers);
179179
}
180180

181181
/**

src/main/java/org/apache/commons/io/output/FilterCollectionWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void flush() throws IOException {
105105
}
106106

107107
private FilterCollectionWriter forAllWriters(final IOConsumer<Writer> action) throws IOExceptionList {
108-
IOConsumer.forAll(writers(), action);
108+
IOConsumer.forAll(action, writers());
109109
return this;
110110
}
111111

src/test/java/org/apache/commons/io/function/IOBiConsumerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void testAndThen() throws IOException {
5151
}
5252

5353
@Test
54-
public void testAsBiConsumer() throws IOException {
54+
public void testAsBiConsumer() {
5555
final Map<String, Integer> map = new HashMap<>();
5656
map.put("a", 1);
5757
assertThrows(UncheckedIOException.class, () -> map.forEach(TestConstants.THROWING_IO_BI_CONSUMER.asBiConsumer()));

src/test/java/org/apache/commons/io/function/IOBiFunctionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void testApplyThrowsException() {
7979
}
8080

8181
@Test
82-
public void testAsBiFunction() throws IOException {
82+
public void testAsBiFunction() {
8383
final Map<String, Long> map = new HashMap<>();
8484
map.put("1", 0L);
8585
final IOBiFunction<String, Long, Long> f = (t, u) -> Files.size(PathUtils.current());

src/test/java/org/apache/commons/io/function/IOBinaryOperatorStreamTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ public void testAsBinaryOperator() {
5252
assertEquals(TestConstants.ABS_PATH_A, Stream.of(TestConstants.ABS_PATH_A, TestConstants.ABS_PATH_A).reduce(MIN_BY_BO).get());
5353
}
5454

55+
/**
56+
*/
5557
@Test
56-
public void testMaxBy() throws IOException {
58+
public void testMaxBy() {
5759
assertEquals(TestConstants.ABS_PATH_A, Stream.of(TestConstants.ABS_PATH_A, TestConstants.ABS_PATH_A).reduce(MAX_BY_BO).get());
5860
// in-line lambda ok:
5961
final IOBinaryOperator<Path> binIoOp = IOBinaryOperator.maxBy((t, u) -> t.toRealPath().compareTo(u));
@@ -68,7 +70,7 @@ public void testMaxBy() throws IOException {
6870
}
6971

7072
@Test
71-
public void testMinBy() throws IOException {
73+
public void testMinBy() {
7274
assertEquals(TestConstants.ABS_PATH_A, Stream.of(TestConstants.ABS_PATH_A, TestConstants.ABS_PATH_A).reduce(MIN_BY_BO).get());
7375
// in-line lambda ok:
7476
final IOBinaryOperator<Path> binIoOp = IOBinaryOperator.minBy((t, u) -> t.toRealPath().compareTo(u));

src/test/java/org/apache/commons/io/function/IOComparatorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class IOComparatorTest {
3939
static final IOComparator<Path> REAL_PATH_COMP = (final Path t, final Path u) -> t.toRealPath().compareTo(u);
4040

4141
@Test
42-
public void testAsComparator() throws IOException {
42+
public void testAsComparator() {
4343
assertEquals(0, REAL_PATH_COMP.asComparator().compare(TestConstants.ABS_PATH_A, TestConstants.ABS_PATH_A));
4444
assertThrows(UncheckedIOException.class,
4545
() -> TestConstants.THROWING_IO_COMPARATOR.asComparator().compare(TestConstants.ABS_PATH_A, TestConstants.ABS_PATH_B));
@@ -56,7 +56,7 @@ public void testComparePath() throws IOException {
5656
}
5757

5858
@Test
59-
public void testThrowing() throws IOException {
59+
public void testThrowing() {
6060
assertThrows(IOException.class, () -> TestConstants.THROWING_IO_COMPARATOR.compare(TestConstants.ABS_PATH_A, TestConstants.ABS_PATH_B));
6161
}
6262

0 commit comments

Comments
 (0)