-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement concatenation * Fix doc error * Renamed `thenConcat` to `concat`
- Loading branch information
Showing
4 changed files
with
213 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/java/com/ginsberg/gatherers4j/ConcatenationGatherer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2024 Todd Ginsberg | ||
* | ||
* Licensed 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 com.ginsberg.gatherers4j; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Gatherer; | ||
import java.util.stream.Stream; | ||
|
||
import static com.ginsberg.gatherers4j.GathererUtils.mustNotBeNull; | ||
|
||
public class ConcatenationGatherer<INPUT> implements Gatherer<INPUT, ConcatenationGatherer.State<INPUT>, INPUT> { | ||
|
||
private final List<Stream<INPUT>> concatThese = new ArrayList<>(); | ||
|
||
ConcatenationGatherer(final Stream<INPUT> concatThis) { | ||
concat(concatThis); | ||
} | ||
|
||
public final ConcatenationGatherer<INPUT> concat(final Stream<INPUT> concatThis) { | ||
mustNotBeNull(concatThis, "Concatenated stream must not be null"); | ||
this.concatThese.add(concatThis); | ||
return this; | ||
} | ||
|
||
@Override | ||
public Supplier<State<INPUT>> initializer() { | ||
return () -> new State<>(concatThese); | ||
} | ||
|
||
@Override | ||
public Integrator<State<INPUT>, INPUT, INPUT> integrator() { | ||
return (_, element, downstream) -> downstream.push(element); | ||
} | ||
|
||
@Override | ||
public BiConsumer<State<INPUT>, Downstream<? super INPUT>> finisher() { | ||
return (state, downstream) -> { | ||
for (Stream<INPUT> concatThis : state.concatThese) { | ||
concatThis.forEach(downstream::push); | ||
} | ||
}; | ||
} | ||
|
||
public static class State<INPUT> { | ||
final List<Stream<INPUT>> concatThese; | ||
|
||
public State(List<Stream<INPUT>> concatThese) { | ||
this.concatThese = concatThese; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/test/java/com/ginsberg/gatherers4j/ConcatenationGathererTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright 2024 Todd Ginsberg | ||
* | ||
* Licensed 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 com.ginsberg.gatherers4j; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
class ConcatenationGathererTest { | ||
|
||
@Test | ||
void additionalStreamCannotBeNull() { | ||
assertThatThrownBy(() -> | ||
Stream.of("A").gather(Gatherers4j.concat(Stream.of("1")).concat(null)) | ||
).isExactlyInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
@Test | ||
void allowsEmptyConcat() { | ||
// Arrange | ||
final Stream<String> input1 = Stream.of("A", "B", "C"); | ||
final Stream<String> input2 = Stream.empty(); | ||
|
||
// Act | ||
final List<String> output = input1.gather(Gatherers4j.concat(input2)).toList(); | ||
|
||
// Assert | ||
assertThat(output).containsExactly("A", "B", "C"); | ||
} | ||
|
||
@Test | ||
void allowsEmptySource() { | ||
// Arrange | ||
final Stream<String> input1 = Stream.empty(); | ||
final Stream<String> input2 = Stream.of("D", "E", "F"); | ||
|
||
// Act | ||
final List<String> output = input1.gather(Gatherers4j.concat(input2)).toList(); | ||
|
||
// Assert | ||
assertThat(output).containsExactly("D", "E", "F"); | ||
} | ||
|
||
@Test | ||
void initialStreamCannotBeNull() { | ||
assertThatThrownBy(() -> | ||
Stream.of("A").gather(Gatherers4j.concat(null)) | ||
).isExactlyInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
@Test | ||
void multipleConcatenations() { | ||
// Arrange | ||
final Stream<String> input1 = Stream.of("A", "B", "C"); | ||
final Stream<String> input2 = Stream.of("D", "E", "F"); | ||
final Stream<String> input3 = Stream.of("G", "H", "I"); | ||
|
||
// Act | ||
final List<String> output = input1.gather(Gatherers4j.concat(input2).concat(input3)).toList(); | ||
|
||
// Assert | ||
assertThat(output).containsExactly("A", "B", "C", "D", "E", "F", "G", "H", "I"); | ||
} | ||
|
||
@Test | ||
void simpleConcatenation() { | ||
// Arrange | ||
final Stream<String> input1 = Stream.of("A", "B", "C"); | ||
final Stream<String> input2 = Stream.of("D", "E", "F"); | ||
|
||
// Act | ||
final List<String> output = input1.gather(Gatherers4j.concat(input2)).toList(); | ||
|
||
// Assert | ||
assertThat(output).containsExactly("A", "B", "C", "D", "E", "F"); | ||
} | ||
|
||
} |