-
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.
+ Going with this implementation for now, not sure if there is much call for a predicate based version of this
- Loading branch information
Showing
5 changed files
with
160 additions
and
3 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
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
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
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,70 @@ | ||
/* | ||
* 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; | ||
|
||
public class SizeGatherer<INPUT> implements Gatherer<INPUT, SizeGatherer.State<INPUT>, INPUT> { | ||
|
||
private final long targetSize; | ||
|
||
SizeGatherer(long targetSize) { | ||
if (targetSize < 0) { | ||
throw new IllegalArgumentException("Target size cannot be negative"); | ||
} | ||
this.targetSize = targetSize; | ||
} | ||
|
||
@Override | ||
public Supplier<State<INPUT>> initializer() { | ||
return State::new; | ||
} | ||
|
||
@Override | ||
public Integrator<State<INPUT>, INPUT, INPUT> integrator() { | ||
return (state, element, downstream) -> { | ||
state.elements.add(element); | ||
if (state.elements.size() > targetSize) { | ||
fail(); | ||
} | ||
return !downstream.isRejecting(); | ||
}; | ||
} | ||
|
||
@Override | ||
public BiConsumer<State<INPUT>, Downstream<? super INPUT>> finisher() { | ||
return (state, downstream) -> { | ||
if (state.elements.size() == targetSize) { | ||
state.elements.forEach(downstream::push); | ||
} else { | ||
fail(); | ||
} | ||
}; | ||
} | ||
|
||
private void fail() { | ||
throw new IllegalStateException("Size must be exactly " + targetSize); | ||
} | ||
|
||
public static class State<INPUT> { | ||
final List<INPUT> elements = new ArrayList<>(); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/test/java/com/ginsberg/gatherers4j/SizeGathererTest.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,62 @@ | ||
/* | ||
* 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 SizeGathererTest { | ||
|
||
@Test | ||
void sizeMustNotBeNegative() { | ||
assertThatThrownBy(() -> | ||
Stream.empty().gather(Gatherers4j.exactSize(-1)).toList() | ||
).isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
@Test | ||
void doesNotEmitUnderTarget() { | ||
assertThatThrownBy(() -> | ||
Stream.of("A").gather(Gatherers4j.exactSize(2)).toList() | ||
).isInstanceOf(IllegalStateException.class); | ||
} | ||
|
||
@Test | ||
void doesNotEmitOverTarget() { | ||
assertThatThrownBy(() -> | ||
Stream.of("A", "B", "C").gather(Gatherers4j.exactSize(2)).toList() | ||
).isInstanceOf(IllegalStateException.class); | ||
} | ||
|
||
@Test | ||
void emitsAtTarget() { | ||
// Arrange | ||
final Stream<String> input = Stream.of("A", "B", "C"); | ||
|
||
// Act | ||
final List<String> output = input.gather(Gatherers4j.exactSize(3)).toList(); | ||
|
||
// Assert | ||
assertThat(output).containsExactly("A", "B", "C"); | ||
} | ||
|
||
} |