diff --git a/README.md b/README.md index 368dded..991d2e0 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ implementation("com.ginsberg:gatherers4j:0.8.0") | `dedupeConsecutiveBy(fn)` | Remove consecutive duplicates from a stream as returned by `fn` | | `distinctBy(fn)` | Emit only distinct elements from the stream, as measured by `fn` | | `dropLast(n)` | Keep all but the last `n` elements of the stream | -| `everyNth(n)` | Limit the stream to every `n`th element | +| `everyNth(n)` | Limit the stream to every `n`th element | | `filterWithIndex(predicate)` | Filter the stream with the given `predicate`, which takes an `element` and its `index` | | `grouping()` | Group consecutive identical elements into lists | | `groupingBy(fn)` | Group consecutive elements that are identical according to `fn` into lists | diff --git a/src/main/java/com/ginsberg/gatherers4j/BigDecimalGatherer.java b/src/main/java/com/ginsberg/gatherers4j/BigDecimalGatherer.java index 1ccc748..3feae16 100644 --- a/src/main/java/com/ginsberg/gatherers4j/BigDecimalGatherer.java +++ b/src/main/java/com/ginsberg/gatherers4j/BigDecimalGatherer.java @@ -58,7 +58,7 @@ public BigDecimalGatherer treatNullAsZero() { /// When encountering a `null` value in a stream, treat it as the given `replacement` value instead. /// /// @param replacement The value to replace `null` with - public BigDecimalGatherer treatNullAs(final @Nullable BigDecimal replacement) { + public BigDecimalGatherer treatNullAs(@Nullable final BigDecimal replacement) { this.nullReplacement = replacement; return this; } diff --git a/src/main/java/com/ginsberg/gatherers4j/DedupeConsecutiveGatherer.java b/src/main/java/com/ginsberg/gatherers4j/DedupeConsecutiveGatherer.java index 607ea68..8b3da7a 100644 --- a/src/main/java/com/ginsberg/gatherers4j/DedupeConsecutiveGatherer.java +++ b/src/main/java/com/ginsberg/gatherers4j/DedupeConsecutiveGatherer.java @@ -26,9 +26,10 @@ public class DedupeConsecutiveGatherer implements Gatherer { - private final @Nullable Function mappingFunction; + @Nullable + private final Function mappingFunction; - DedupeConsecutiveGatherer(final @Nullable Function mappingFunction) { + DedupeConsecutiveGatherer(@Nullable final Function mappingFunction) { this.mappingFunction = mappingFunction; } diff --git a/src/main/java/com/ginsberg/gatherers4j/GathererUtils.java b/src/main/java/com/ginsberg/gatherers4j/GathererUtils.java index 6eddfb2..23b9a6f 100644 --- a/src/main/java/com/ginsberg/gatherers4j/GathererUtils.java +++ b/src/main/java/com/ginsberg/gatherers4j/GathererUtils.java @@ -21,13 +21,13 @@ abstract class GathererUtils { final static long NANOS_PER_MILLISECOND = 1_000_000; - static void mustNotBeNull(final @Nullable Object subject, final String message) { + static void mustNotBeNull(@Nullable final Object subject, final String message) { if (subject == null) { throw new IllegalArgumentException(message); } } - static boolean safeEquals(final @Nullable Object left, final @Nullable Object right) { + static boolean safeEquals(@Nullable final Object left, @Nullable final Object right) { if (left == null && right == null) { return true; } else if (left == null || right == null) { diff --git a/src/main/java/com/ginsberg/gatherers4j/GroupingByGatherer.java b/src/main/java/com/ginsberg/gatherers4j/GroupingByGatherer.java index 80916f6..7069e94 100644 --- a/src/main/java/com/ginsberg/gatherers4j/GroupingByGatherer.java +++ b/src/main/java/com/ginsberg/gatherers4j/GroupingByGatherer.java @@ -31,7 +31,8 @@ public class GroupingByGatherer implements Gatherer, List> { - private final @Nullable Function mappingFunction; + @Nullable + private final Function mappingFunction; GroupingByGatherer() { mappingFunction = null; diff --git a/src/main/java/com/ginsberg/gatherers4j/UniquelyOccurringGatherer.java b/src/main/java/com/ginsberg/gatherers4j/UniquelyOccurringGatherer.java index 8224706..e73e4bc 100644 --- a/src/main/java/com/ginsberg/gatherers4j/UniquelyOccurringGatherer.java +++ b/src/main/java/com/ginsberg/gatherers4j/UniquelyOccurringGatherer.java @@ -18,9 +18,8 @@ import org.jspecify.annotations.Nullable; -import java.util.HashMap; import java.util.HashSet; -import java.util.Map; +import java.util.LinkedHashSet; import java.util.Set; import java.util.function.BiConsumer; import java.util.function.Supplier; @@ -41,12 +40,12 @@ public Supplier> initializer() { @Override public Integrator, INPUT, INPUT> integrator() { return Integrator.ofGreedy((state, element, downstream) -> { - if (!state.knownBad.contains(element)) { - if (state.found.containsKey(element)) { - state.knownBad.add(element); + if (!state.duplicates.contains(element)) { + if (state.found.contains(element)) { + state.duplicates.add(element); state.found.remove(element); } else { - state.found.put(element, state.iteration++); + state.found.add(element); } } return !downstream.isRejecting(); @@ -55,17 +54,11 @@ public Integrator, INPUT, INPUT> integrator() { @Override public BiConsumer, Downstream> finisher() { - return (inputState, downstream) -> - inputState.found - .entrySet() - .stream() - .sorted((a, b) -> (int) (a.getValue() - b.getValue())) - .forEach(it -> downstream.push(it.getKey())); + return (inputState, downstream) -> inputState.found.forEach(downstream::push); } public static class State { - long iteration = 0; - final Set knownBad = new HashSet<>(); - final Map found = new HashMap<>(); + final Set duplicates = new HashSet<>(); + final Set found = new LinkedHashSet<>(); } } diff --git a/src/test/java/com/ginsberg/gatherers4j/EveryNthGathererTest.java b/src/test/java/com/ginsberg/gatherers4j/EveryNthGathererTest.java index 836fb89..63508ed 100644 --- a/src/test/java/com/ginsberg/gatherers4j/EveryNthGathererTest.java +++ b/src/test/java/com/ginsberg/gatherers4j/EveryNthGathererTest.java @@ -23,7 +23,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.jupiter.api.Assertions.*; class EveryNthGathererTest {