Skip to content

Commit 2c9a10d

Browse files
author
Gary Gregory
committed
Add IOBiConsumer.
1 parent 5b4b038 commit 2c9a10d

3 files changed

Lines changed: 114 additions & 0 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@ The <action> type attribute can be add,update,fix,remove.
284284
<action dev="ggregory" type="add" due-to="Gary Gregory">
285285
Add IOExceptionList.checkEmpty(List, Object).
286286
</action>
287+
<action dev="ggregory" type="add" due-to="Gary Gregory">
288+
Add IOBiConsumer.
289+
</action>
287290
<!-- UPDATE -->
288291
<action dev="ggregory" type="add" due-to="Gary Gregory">
289292
Update FileEntry to use FileTime instead of long for file time stamps.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
18+
package org.apache.commons.io.function;
19+
20+
import java.io.IOException;
21+
import java.util.Objects;
22+
import java.util.function.BiConsumer;
23+
24+
/**
25+
* Like {@link BiConsumer} but throws {@link IOException}.
26+
*
27+
* @param <T> the type of the first argument to the operation
28+
* @param <U> the type of the second argument to the operation
29+
*
30+
* @see BiConsumer
31+
* @since 1.8
32+
*/
33+
@FunctionalInterface
34+
public interface IOBiConsumer<T, U> {
35+
36+
/**
37+
* Performs this operation on the given arguments.
38+
*
39+
* @param t the first input argument
40+
* @param u the second input argument
41+
* @throws IOException if an I/O error occurs.
42+
*/
43+
void accept(T t, U u) throws IOException;
44+
45+
/**
46+
* Returns a composed {@code BiConsumer} that performs, in sequence, this operation followed by the {@code after}
47+
* operation. If performing either operation throws an exception, it is relayed to the caller of the composed operation.
48+
* If performing this operation throws an exception, the {@code after} operation will not be performed.
49+
*
50+
* @param after the operation to perform after this operation
51+
* @return a composed {@code BiConsumer} that performs in sequence this operation followed by the {@code after}
52+
* operation
53+
* @throws NullPointerException if {@code after} is null
54+
*/
55+
default IOBiConsumer<T, U> andThen(IOBiConsumer<? super T, ? super U> after) {
56+
Objects.requireNonNull(after);
57+
58+
return (l, r) -> {
59+
accept(l, r);
60+
after.accept(l, r);
61+
};
62+
}
63+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
18+
package org.apache.commons.io.function;
19+
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
22+
import java.io.IOException;
23+
import java.util.concurrent.atomic.AtomicReference;
24+
25+
import org.junit.jupiter.api.Test;
26+
27+
/**
28+
* Tests {@link IOBiConsumer}.
29+
*/
30+
public class IOBiConsumerTest {
31+
32+
@Test
33+
public void testAccept() throws IOException {
34+
final AtomicReference<String> ref = new AtomicReference<>();
35+
final IOBiConsumer<String, Integer> biConsumer = (s, i) -> ref.set(s + i);
36+
biConsumer.accept("A", 1);
37+
assertEquals("A1", ref.get());
38+
}
39+
40+
@Test
41+
public void testAndThen() throws IOException {
42+
final AtomicReference<String> ref = new AtomicReference<>();
43+
final IOBiConsumer<String, Integer> biConsumer1 = (s, i) -> ref.set(s + i);
44+
final IOBiConsumer<String, Integer> biConsumer2 = (s, i) -> ref.set(ref.get() + i + s);
45+
biConsumer1.andThen(biConsumer2).accept("B", 2);
46+
assertEquals("B22B", ref.get());
47+
}
48+
}

0 commit comments

Comments
 (0)