Skip to content

Commit

Permalink
Misc fixes
Browse files Browse the repository at this point in the history
+ Alter nullity annotation order
+ Reimplement UniquelyOccurringGatherer to use LinkedHashSet
  • Loading branch information
tginsberg committed Jan 22, 2025
1 parent a21f3a8 commit 31006b0
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`<sup>th</sup> element |
| `everyNth(n)` | Limit the stream to every `n`<sup>th</sup> 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 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public BigDecimalGatherer<INPUT> 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<INPUT> treatNullAs(final @Nullable BigDecimal replacement) {
public BigDecimalGatherer<INPUT> treatNullAs(@Nullable final BigDecimal replacement) {
this.nullReplacement = replacement;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
public class DedupeConsecutiveGatherer<INPUT extends @Nullable Object>
implements Gatherer<INPUT, DedupeConsecutiveGatherer.State, INPUT> {

private final @Nullable Function<INPUT, @Nullable Object> mappingFunction;
@Nullable
private final Function<INPUT, @Nullable Object> mappingFunction;

DedupeConsecutiveGatherer(final @Nullable Function<INPUT, @Nullable Object> mappingFunction) {
DedupeConsecutiveGatherer(@Nullable final Function<INPUT, @Nullable Object> mappingFunction) {
this.mappingFunction = mappingFunction;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/ginsberg/gatherers4j/GathererUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
public class GroupingByGatherer<INPUT extends @Nullable Object> implements
Gatherer<INPUT, GroupingByGatherer.State<INPUT>, List<INPUT>> {

private final @Nullable Function<INPUT, Object> mappingFunction;
@Nullable
private final Function<INPUT, Object> mappingFunction;

GroupingByGatherer() {
mappingFunction = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,12 +40,12 @@ public Supplier<State<INPUT>> initializer() {
@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);
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();
Expand All @@ -55,17 +54,11 @@ public Integrator<State<INPUT>, INPUT, INPUT> integrator() {

@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()));
return (inputState, downstream) -> inputState.found.forEach(downstream::push);
}

public static class State<INPUT> {
long iteration = 0;
final Set<INPUT> knownBad = new HashSet<>();
final Map<INPUT, Long> found = new HashMap<>();
final Set<INPUT> duplicates = new HashSet<>();
final Set<INPUT> found = new LinkedHashSet<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down

0 comments on commit 31006b0

Please sign in to comment.