-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement uniquelyOccurring to constrain stream to elements that occu…
…r a single time
- Loading branch information
Showing
5 changed files
with
180 additions
and
0 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
71 changes: 71 additions & 0 deletions
71
src/main/java/com/ginsberg/gatherers4j/UniquelyOccurringGatherer.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,71 @@ | ||
/* | ||
* Copyright 2025 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.jspecify.annotations.Nullable; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Gatherer; | ||
|
||
public class UniquelyOccurringGatherer<INPUT extends @Nullable Object> | ||
implements Gatherer<INPUT, UniquelyOccurringGatherer.State<INPUT>, INPUT> { | ||
|
||
UniquelyOccurringGatherer() { | ||
// Nothing to do | ||
} | ||
|
||
@Override | ||
public Supplier<State<INPUT>> initializer() { | ||
return State::new; | ||
} | ||
|
||
@Override | ||
public Integrator<State<INPUT>, INPUT, INPUT> integrator() { | ||
return Integrator.ofGreedy((state, element, downstream) -> { | ||
if (!state.knownBad.contains(element)) { | ||
if (state.found.containsKey(element)) { | ||
state.knownBad.add(element); | ||
state.found.remove(element); | ||
} else { | ||
state.found.put(element, state.iteration++); | ||
} | ||
} | ||
return !downstream.isRejecting(); | ||
}); | ||
} | ||
|
||
@Override | ||
public BiConsumer<State<INPUT>, Downstream<? super INPUT>> finisher() { | ||
return (inputState, downstream) -> | ||
inputState.found | ||
.entrySet() | ||
.stream() | ||
.sorted((a, b) -> (int) (a.getValue() - b.getValue())) | ||
.forEach(it -> downstream.push(it.getKey())); | ||
} | ||
|
||
public static class State<INPUT> { | ||
long iteration = 0; | ||
final Set<INPUT> knownBad = new HashSet<>(); | ||
final Map<INPUT, Long> found = new HashMap<>(); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
src/test/java/com/ginsberg/gatherers4j/UniquelyOccurringGathererTest.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,88 @@ | ||
/* | ||
* Copyright 2025 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; | ||
|
||
class UniquelyOccurringGathererTest { | ||
|
||
@Test | ||
void allowsUniqueNull() { | ||
// Arrange | ||
final Stream<String> input = Stream.of(null, "B", "B"); | ||
|
||
//Act | ||
final List<String> output = input.gather(Gatherers4j.uniquelyOccurring()).toList(); | ||
|
||
// Assert | ||
assertThat(output).hasSize(1).containsNull(); | ||
} | ||
|
||
@Test | ||
void emitsEmptyOnNoUniqueItems() { | ||
// Arrange | ||
final Stream<String> input = Stream.of("A", "A"); | ||
|
||
//Act | ||
final List<String> output = input.gather(Gatherers4j.uniquelyOccurring()).toList(); | ||
|
||
// Assert | ||
assertThat(output).isEmpty(); | ||
} | ||
|
||
@Test | ||
void emitsInEncounterOrder() { | ||
// Arrange | ||
final Stream<String> input = Stream.of("A", "B", "C", "D", "B", "C"); | ||
|
||
//Act | ||
final List<String> output = input.gather(Gatherers4j.uniquelyOccurring()).toList(); | ||
|
||
// Assert | ||
assertThat(output).containsExactly("A", "D"); | ||
} | ||
|
||
@Test | ||
void filtersOutNonUnique() { | ||
// Arrange | ||
final Stream<String> input = Stream.of("A", "B", "A", "A"); | ||
|
||
//Act | ||
final List<String> output = input.gather(Gatherers4j.uniquelyOccurring()).toList(); | ||
|
||
// Assert | ||
assertThat(output).containsExactly("B"); | ||
} | ||
|
||
@Test | ||
void filtersOutNonUniqueNull() { | ||
// Arrange | ||
final Stream<String> input = Stream.of(null, "B", null, null); | ||
|
||
//Act | ||
final List<String> output = input.gather(Gatherers4j.uniquelyOccurring()).toList(); | ||
|
||
// Assert | ||
assertThat(output).containsExactly("B"); | ||
} | ||
|
||
} |