Skip to content

Commit

Permalink
Fix #57: Use greedy integrators where possible
Browse files Browse the repository at this point in the history
+ Greedy integrators can be used anywhere the integrator does not itself initiate a short circuit (returns false). Passing along the downstream rejecting flag is allowed for greedy integrators.
  • Loading branch information
tginsberg committed Nov 23, 2024
1 parent cbc9487 commit 032577d
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 24 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
### 0.7.0
+ TBD
+ Use greedy integrators where possible (Fixes #57)

### 0.6.0
+ Implement `dropLast(n)`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ abstract public class BigDecimalGatherer<INPUT>

@Override
public Integrator<BigDecimalGatherer.State, INPUT, BigDecimal> integrator() {
return (state, element, downstream) -> {
return Integrator.ofGreedy((state, element, downstream) -> {
final BigDecimal mappedElement = element == null ? nullReplacement : mappingFunction.apply(element);
if (mappedElement != null) {
state.add(mappedElement, mathContext);
Expand All @@ -45,7 +45,7 @@ public Integrator<BigDecimalGatherer.State, INPUT, BigDecimal> integrator() {
}
}
return !downstream.isRejecting();
};
});
}

/// When encountering a `null` value in a stream, treat it as `BigDecimal.ZERO` instead.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Supplier<State> initializer() {

@Override
public Integrator<DedupeConsecutiveGatherer.State, INPUT, INPUT> integrator() {
return (state, element, downstream) -> {
return Integrator.ofGreedy((state, element, downstream) -> {
final Object mapped = mappingFunction == null ? element : mappingFunction.apply(element);
if (!state.hasValue) {
state.hasValue = true;
Expand All @@ -48,7 +48,7 @@ public Integrator<DedupeConsecutiveGatherer.State, INPUT, INPUT> integrator() {
return downstream.push(element);
}
return !downstream.isRejecting();
};
});
}

public static class State {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/ginsberg/gatherers4j/DistinctGatherer.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ public Supplier<State> initializer() {

@Override
public Integrator<DistinctGatherer.State, INPUT, INPUT> integrator() {
return (state, element, downstream) -> {
return Integrator.ofGreedy((state, element, downstream) -> {
if (state.knownObjects.add(byFunction.apply(element))) {
downstream.push(element);
}
return !downstream.isRejecting();
};
});
}

public static class State {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/ginsberg/gatherers4j/DropLastGatherer.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public Supplier<State<INPUT>> initializer() {

@Override
public Integrator<State<INPUT>, INPUT, INPUT> integrator() {
return (state, element, downstream) -> {
return Integrator.ofGreedy((state, element, downstream) -> {
state.elements.add(element);
return !downstream.isRejecting();
};
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public Supplier<FilteringWithIndexGatherer.State> initializer() {

@Override
public Integrator<FilteringWithIndexGatherer.State, INPUT, INPUT> integrator() {
return (state, element, downstream) -> {
return Integrator.ofGreedy((state, element, downstream) -> {
if (predicate.test(state.index++, element)) {
downstream.push(element);
}
return !downstream.isRejecting();
};
});
}

public static class State {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/ginsberg/gatherers4j/IndexingGatherer.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public Supplier<IndexingGatherer.State> initializer() {

@Override
public Integrator<IndexingGatherer.State, INPUT, IndexedValue<INPUT>> integrator() {
return (state, element, downstream) -> downstream.push(new IndexedValue<>(state.index++, element));
return Integrator.ofGreedy((state, element, downstream) ->
downstream.push(new IndexedValue<>(state.index++, element))
);
}

public static class State {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/ginsberg/gatherers4j/LastGatherer.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public Supplier<State<INPUT>> initializer() {

@Override
public Integrator<State<INPUT>, INPUT, INPUT> integrator() {
return Integrator.ofGreedy((state, element, _) -> {
return Integrator.ofGreedy((state, element, downstream) -> {
if (state.elements.size() == lastCount) {
state.elements.removeFirst();
}
state.elements.add(element);
return true;
return !downstream.isRejecting();
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/ginsberg/gatherers4j/MinMaxGatherer.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Supplier<State<INPUT, MAPPED>> initializer() {

@Override
public Integrator<State<INPUT, MAPPED>, INPUT, INPUT> integrator() {
return (state, element, downstream) -> {
return Integrator.ofGreedy((state, element, downstream) -> {
final MAPPED mapped = element == null ? null : mappingFunction.apply(element);
if (mapped == null) {
return !downstream.isRejecting();
Expand All @@ -55,7 +55,7 @@ public Integrator<State<INPUT, MAPPED>, INPUT, INPUT> integrator() {
}
}
return !downstream.isRejecting();
};
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ public Supplier<ReversingGatherer.State<INPUT>> initializer() {

@Override
public Integrator<State<INPUT>, INPUT, INPUT> integrator() {
return (state, element, _) -> state.inputs.add(element);
return Integrator.ofGreedy((state, element, downstream) -> {
state.inputs.add(element);
return !downstream.isRejecting();
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ public Supplier<ShufflingGatherer.State<INPUT>> initializer() {

@Override
public Integrator<ShufflingGatherer.State<INPUT>, INPUT, INPUT> integrator() {
return (state, element, _) -> state.inputs.add(element);
return Integrator.ofGreedy((state, element, downstream) -> {
state.inputs.add(element);
return !downstream.isRejecting();
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ public Supplier<State> initializer() {

@Override
public Integrator<State, INPUT, INPUT> integrator() {
return (state, element, downstream) -> {
return Integrator.ofGreedy((state, element, downstream) -> {
if (!downstream.isRejecting() && state.attempt()) {
downstream.push(element);
}
return !downstream.isRejecting();
};
});
}

public static class State {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/ginsberg/gatherers4j/ZipWithGatherer.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ public Integrator<Void, FIRST, Pair<FIRST, SECOND>> integrator() {
return (_, element, downstream) -> {
boolean advanced = otherSpliterator.tryAdvance(it -> downstream.push(new Pair<>(element, it)));
if (!advanced && argumentWhenSourceLonger != null) {
downstream.push(new Pair<>(element, argumentWhenSourceLonger.apply(element)));
return downstream.push(new Pair<>(element, argumentWhenSourceLonger.apply(element)));
}
return !downstream.isRejecting();
return advanced && !downstream.isRejecting();
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ public Supplier<State<INPUT>> initializer() {

@Override
public Integrator<State<INPUT>, INPUT, List<INPUT>> integrator() {
return (state, element, downstream) -> {
return Integrator.ofGreedy((state, element, downstream) -> {
if (!state.hasValue) {
state.hasValue = true;
} else {
downstream.push(List.of(state.value, element));
}
state.value = element;
return !downstream.isRejecting();
};
});

}

Expand Down

0 comments on commit 032577d

Please sign in to comment.