Skip to content

Commit 4e6d398

Browse files
authored
Feature/misc fixes (#191)
* Minor fixes and refactorings * Minor fixes around JSpecify annotations
1 parent 6f87444 commit 4e6d398

23 files changed

Lines changed: 78 additions & 57 deletions

src/main/java/com/ginsberg/gatherers4j/AccumulatingGatherer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2025 Todd Ginsberg
2+
* Copyright 2024-2026 Todd Ginsberg
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -66,7 +66,7 @@ public BiConsumer<State<OUTPUT>, Downstream<? super OUTPUT>> finisher() {
6666
};
6767
}
6868

69-
public static class State<OUTPUT> {
69+
public static class State<OUTPUT extends @Nullable Object> {
7070
@Nullable
7171
OUTPUT carriedValue;
7272
int index;

src/main/java/com/ginsberg/gatherers4j/BigDecimalExponentialMovingAverageGatherer.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2024-2026 Todd Ginsberg
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package com.ginsberg.gatherers4j;
218

319
import org.jspecify.annotations.Nullable;

src/main/java/com/ginsberg/gatherers4j/BigDecimalSimpleMovingAverageGatherer.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 Todd Ginsberg
2+
* Copyright 2024-2026 Todd Ginsberg
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -60,8 +60,8 @@ static class State implements BigDecimalGatherer.State {
6060
final boolean includePartialValues;
6161
final BigDecimal[] series;
6262
BigDecimal sum = BigDecimal.ZERO;
63-
BigDecimal count = BigDecimal.ZERO;
6463
BigDecimal average = BigDecimal.ZERO;
64+
int count = 0;
6565
int index = 0;
6666

6767
private State(final int lookBack, final boolean includePartialValues) {
@@ -72,18 +72,18 @@ private State(final int lookBack, final boolean includePartialValues) {
7272

7373
@Override
7474
public boolean canCalculate() {
75-
return includePartialValues || count.intValue() >= series.length;
75+
return includePartialValues || count >= series.length;
7676
}
7777

7878
@Override
7979
public void add(final BigDecimal element, final MathContext mathContext) {
8080
sum = sum.subtract(series[index]).add(element, mathContext);
8181
series[index % series.length] = element;
8282
index = (index + 1) % series.length;
83-
if (count.intValue() < series.length) {
84-
count = count.add(BigDecimal.ONE);
83+
if (count < series.length) {
84+
count++;
8585
}
86-
average = sum.divide(count, mathContext);
86+
average = sum.divide(BigDecimal.valueOf(count), mathContext);
8787
}
8888

8989
@Override

src/main/java/com/ginsberg/gatherers4j/DistinctGatherer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 Todd Ginsberg
2+
* Copyright 2024-2026 Todd Ginsberg
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,7 +29,7 @@
2929
public class DistinctGatherer<INPUT extends @Nullable Object>
3030
implements Gatherer<INPUT, DistinctGatherer.State, INPUT> {
3131

32-
private final Function<INPUT, Object> mappingFunction;
32+
private final Function<INPUT, @Nullable Object> mappingFunction;
3333

3434
DistinctGatherer(final Function<INPUT, @Nullable Object> mappingFunction) {
3535
this.mappingFunction = mustNotBeNull(mappingFunction, "Mapping function must not be null");

src/main/java/com/ginsberg/gatherers4j/FilterChangingGatherer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2025 Todd Ginsberg
2+
* Copyright 2024-2026 Todd Ginsberg
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,7 +25,7 @@
2525

2626
import static com.ginsberg.gatherers4j.util.GathererUtils.mustNotBeNull;
2727

28-
public class FilterChangingGatherer<INPUT>
28+
public class FilterChangingGatherer<INPUT extends @Nullable Object>
2929
implements Gatherer<INPUT, FilterChangingGatherer.State<INPUT>, INPUT> {
3030

3131
private final Order operation;
@@ -76,7 +76,7 @@ boolean allow(final @Nullable INPUT previous, final INPUT next) {
7676
return operation.allows(comparator.compare(next, previous));
7777
}
7878

79-
public static class State<INPUT> {
79+
public static class State<INPUT extends @Nullable Object> {
8080
boolean first = true;
8181
@Nullable
8282
INPUT previousElement;

src/main/java/com/ginsberg/gatherers4j/FrequencyGatherer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 Todd Ginsberg
2+
* Copyright 2024-2026 Todd Ginsberg
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -66,7 +66,8 @@ public BiConsumer<State<INPUT>, Downstream<? super WithCount<INPUT>>> finisher()
6666
return (inputState, downstream) -> {
6767
var counts = inputState.counts
6868
.entrySet()
69-
.stream().map(it -> new WithCount<>(it.getKey(), it.getValue()))
69+
.stream()
70+
.map(it -> new WithCount<INPUT>(it.getKey(), it.getValue()))
7071
.sorted(comparator());
7172
pushAll(counts, downstream);
7273
};
@@ -81,7 +82,7 @@ private Comparator<WithCount<INPUT>> comparator() {
8182
}
8283
}
8384

84-
public static class State<INPUT> {
85+
public static class State<INPUT extends @Nullable Object> {
8586
final Map<INPUT, Long> counts = new HashMap<>();
8687
}
8788
}

src/main/java/com/ginsberg/gatherers4j/Gatherers4j.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ private Gatherers4j() {
278278
/// @param validTypes A non-empty array of types to filter for
279279
/// @return A non-null `Gatherer`
280280
@SafeVarargs
281-
public static <INPUT extends @Nullable Object, OUTPUT extends @Nullable Object> Gatherer<INPUT, ?, OUTPUT> filterInstanceOf(
281+
public static <INPUT extends @Nullable Object, OUTPUT> Gatherer<INPUT, ?, OUTPUT> filterInstanceOf(
282282
final Class<? extends OUTPUT>... validTypes
283283
) {
284284
return TypeFilteringGatherer.of(validTypes);

src/main/java/com/ginsberg/gatherers4j/GroupChangingGatherer.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2025 Todd Ginsberg
2+
* Copyright 2024-2026 Todd Ginsberg
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package com.ginsberg.gatherers4j;
1818

1919
import com.ginsberg.gatherers4j.enums.Order;
20+
import org.jspecify.annotations.Nullable;
2021

2122
import java.util.ArrayList;
2223
import java.util.Collections;
@@ -28,13 +29,13 @@
2829

2930
import static com.ginsberg.gatherers4j.util.GathererUtils.mustNotBeNull;
3031

31-
public class GroupChangingGatherer<INPUT>
32+
public class GroupChangingGatherer<INPUT extends @Nullable Object>
3233
implements Gatherer<INPUT, GroupChangingGatherer.State<INPUT>, List<INPUT>> {
3334

3435
private final Order operation;
3536
private final Comparator<INPUT> comparator;
3637

37-
static <INPUT> GroupChangingGatherer<INPUT> usingComparator(
38+
static <INPUT extends @Nullable Object> GroupChangingGatherer<INPUT> usingComparator(
3839
final Order operation,
3940
final Comparator<INPUT> comparator
4041
) {
@@ -85,7 +86,7 @@ public BiConsumer<State<INPUT>, Downstream<? super List<INPUT>>> finisher() {
8586
};
8687
}
8788

88-
public static class State<INPUT> {
89+
public static class State<INPUT extends @Nullable Object> {
8990
List<INPUT> currentElements = new ArrayList<>();
9091
}
9192

src/main/java/com/ginsberg/gatherers4j/MinMaxGatherer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2025 Todd Ginsberg
2+
* Copyright 2024-2026 Todd Ginsberg
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,14 +34,14 @@ public class MinMaxGatherer<INPUT extends @Nullable Object>
3434
private final int windowSize;
3535
private boolean excludePartialValues;
3636

37-
static <INPUT> MinMaxGatherer<INPUT> runningUsingComparator(
37+
static <INPUT extends @Nullable Object> MinMaxGatherer<INPUT> runningUsingComparator(
3838
final boolean sortingMin,
3939
final Comparator<INPUT> comparator
4040
) {
4141
return new MinMaxGatherer<>(sortingMin ? comparator : comparator.reversed());
4242
}
4343

44-
static <INPUT> MinMaxGatherer<INPUT> movingUsingComparator(
44+
static <INPUT extends @Nullable Object> MinMaxGatherer<INPUT> movingUsingComparator(
4545
final int windowSize,
4646
final boolean sortingMin,
4747
final Comparator<INPUT> comparator
@@ -134,8 +134,8 @@ boolean eval(final INPUT nextValue) {
134134
}
135135
}
136136

137-
public static class MovingState<INPUT> extends State<INPUT> {
138-
private record IndexValue<INPUT>(int index, INPUT value) {
137+
public static class MovingState<INPUT extends @Nullable Object> extends State<INPUT> {
138+
private record IndexValue<INPUT extends @Nullable Object>(int index, INPUT value) {
139139
}
140140

141141
private final List<IndexValue<INPUT>> queue = new ArrayList<>();

src/main/java/com/ginsberg/gatherers4j/RepeatingGatherer.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2025 Todd Ginsberg
2+
* Copyright 2024-2026 Todd Ginsberg
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -55,8 +55,10 @@ public Supplier<RepeatingGatherer.State<INPUT>> initializer() {
5555
@Override
5656
public Integrator<RepeatingGatherer.State<INPUT>, INPUT, INPUT> integrator() {
5757
return Integrator.ofGreedy((state, element, downstream) -> {
58-
state.theStream.add(element);
59-
return repeats != 0 && !downstream.isRejecting();
58+
if(repeats != 0) {
59+
state.theStream.add(element);
60+
}
61+
return !downstream.isRejecting();
6062
});
6163
}
6264

@@ -73,7 +75,7 @@ public BiConsumer<RepeatingGatherer.State<INPUT>, Downstream<? super INPUT>> fin
7375
};
7476
}
7577

76-
public static class State<INPUT> {
78+
public static class State<INPUT extends @Nullable Object> {
7779
int repeatsRemaining;
7880
final List<INPUT> theStream = new ArrayList<>();
7981

0 commit comments

Comments
 (0)