Skip to content

Commit 7e5f3da

Browse files
committed
Add ByteConsumer
1 parent 2323400 commit 7e5f3da

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ The <action> type attribute can be add,update,fix,remove.
8484
<action type="add" dev="ggregory" due-to="Finger, Gary Gregory, Piotr P. Karwasz">Add org.apache.commons.lang3.time.DateUtils.toLocalDateTime(Date[, TimeZone]) #1385.</action>
8585
<action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.lang3.time.DateUtils.toOffsetDateTime(Date[, TimeZone]).</action>
8686
<action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.lang3.time.DateUtils.toZonedDateTime(Date[, TimeZone]).</action>
87+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add ByteConsumer.</action>
8788
<!-- UPDATE -->
8889
<action type="update" dev="ggregory" due-to="Gary Gregory">[test] Bump org.apache.commons:commons-text from 1.13.1 to 1.14.0.</action>
8990
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 85 to 87.</action>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
* https://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.lang3.function;
19+
20+
import java.util.Objects;
21+
import java.util.function.IntConsumer;
22+
23+
/**
24+
* A functional interface like {@link IntConsumer} but for {@code byte}.
25+
*
26+
* @see IntConsumer
27+
* @since 3.19.0
28+
*/
29+
@FunctionalInterface
30+
public interface ByteConsumer {
31+
32+
/** NOP singleton */
33+
ByteConsumer NOP = t -> {
34+
/* NOP */ };
35+
36+
/**
37+
* Gets the NOP singleton.
38+
*
39+
* @return The NOP singleton.
40+
*/
41+
static ByteConsumer nop() {
42+
return NOP;
43+
}
44+
45+
/**
46+
* Accepts the given arguments.
47+
*
48+
* @param value the input argument
49+
*/
50+
void accept(byte value);
51+
52+
/**
53+
* Returns a composed {@link ByteConsumer} that performs, in sequence, this operation followed by the {@code after} operation. If performing either
54+
* operation throws an exception, it is relayed to the caller of the composed operation. If performing this operation throws an exception, the {@code after}
55+
* operation will not be performed.
56+
*
57+
* @param after the operation to perform after this operation
58+
* @return a composed {@link ByteConsumer} that performs in sequence this operation followed by the {@code after} operation
59+
* @throws NullPointerException if {@code after} is null
60+
*/
61+
default ByteConsumer andThen(final ByteConsumer after) {
62+
Objects.requireNonNull(after);
63+
return (final byte t) -> {
64+
accept(t);
65+
after.accept(t);
66+
};
67+
}
68+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
* https://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.lang3.function;
19+
20+
import static org.apache.commons.lang3.LangAssertions.assertNullPointerException;
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
23+
import java.util.concurrent.atomic.AtomicInteger;
24+
25+
import org.apache.commons.lang3.AbstractLangTest;
26+
import org.junit.jupiter.api.Assertions;
27+
import org.junit.jupiter.api.Test;
28+
29+
/**
30+
* Tests {@link ByteConsumer}.
31+
*/
32+
class ByteConsumerTest extends AbstractLangTest {
33+
34+
private static final byte B0 = (byte) 0;
35+
private static final byte B1 = (byte) 1;
36+
37+
private ByteConsumer accept(final ByteConsumer consumer, final byte expected) {
38+
consumer.accept(expected);
39+
return consumer;
40+
}
41+
42+
@Test
43+
void testAccept() {
44+
final AtomicInteger ref = new AtomicInteger();
45+
accept(v -> ref.lazySet(v), B1);
46+
assertEquals(1, ref.get());
47+
accept(v -> ref.lazySet(v), B0);
48+
assertEquals(0, ref.get());
49+
}
50+
51+
@Test
52+
void testAndThen() throws Throwable {
53+
final ByteConsumer nop = ByteConsumer.nop();
54+
nop.andThen(nop);
55+
// Documented in Javadoc edge-case.
56+
assertNullPointerException(() -> nop.andThen(null));
57+
58+
final AtomicInteger ref1 = new AtomicInteger();
59+
final AtomicInteger ref2 = new AtomicInteger();
60+
61+
final ByteConsumer bc = ref1::lazySet;
62+
final ByteConsumer composite = bc.andThen(ref2::lazySet);
63+
64+
composite.accept(B1);
65+
assertEquals(1, ref1.get());
66+
assertEquals(1, ref2.get());
67+
68+
composite.accept(B0);
69+
assertEquals(0, ref1.get());
70+
assertEquals(0, ref2.get());
71+
72+
// Check order
73+
final ByteConsumer bad = value -> {
74+
throw new IllegalStateException();
75+
};
76+
final ByteConsumer badComposite = bad.andThen(ref2::lazySet);
77+
78+
Assertions.assertThrows(IllegalStateException.class, () -> badComposite.accept(B1));
79+
assertEquals(0, ref2.get(), "Second consumer should not be invoked");
80+
}
81+
82+
}

0 commit comments

Comments
 (0)