-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement movingSum, movingSumBy, movingProduct, and movingProductBy (#…
…63)
- Loading branch information
Showing
7 changed files
with
583 additions
and
9 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
93 changes: 93 additions & 0 deletions
93
src/main/java/com/ginsberg/gatherers4j/BigDecimalMovingProductGatherer.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,93 @@ | ||
/* | ||
* Copyright 2024 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.math.BigDecimal; | ||
import java.math.MathContext; | ||
import java.util.Arrays; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
public class BigDecimalMovingProductGatherer<INPUT extends @Nullable Object> | ||
extends BigDecimalGatherer<INPUT> { | ||
|
||
private final int windowSize; | ||
private boolean includePartialValues = false; | ||
|
||
BigDecimalMovingProductGatherer( | ||
final Function<INPUT, @Nullable BigDecimal> mappingFunction, | ||
final int windowSize) { | ||
super(mappingFunction); | ||
if (windowSize <= 0) { | ||
throw new IllegalArgumentException("Window size must be positive"); | ||
} | ||
this.windowSize = windowSize; | ||
} | ||
|
||
@Override | ||
public Supplier<BigDecimalGatherer.State> initializer() { | ||
return () -> new BigDecimalMovingProductGatherer.State(windowSize, includePartialValues); | ||
} | ||
|
||
/// When creating a moving product and the full size of the window has not yet been reached, the | ||
/// gatherer should emit the product for what it has. | ||
/// | ||
/// For example, if the trailing product is over 10 values, but the stream has only emitted two | ||
/// values, the gatherer should calculate the two values and emit the answer. The default is to not | ||
/// emit anything until the full size of the window has been seen. | ||
public BigDecimalMovingProductGatherer<INPUT> includePartialValues() { | ||
includePartialValues = true; | ||
return this; | ||
} | ||
|
||
/// When encountering a `null` value in a stream, treat it as `BigDecimal.ONE` instead. | ||
public BigDecimalGatherer<INPUT> treatNullAsOne() { | ||
return treatNullAs(BigDecimal.ONE); | ||
} | ||
|
||
static class State implements BigDecimalGatherer.State { | ||
final boolean includePartialValues; | ||
final BigDecimal[] series; | ||
BigDecimal product = BigDecimal.ONE; | ||
int index = 0; | ||
|
||
private State(final int lookBack, final boolean includePartialValues) { | ||
this.includePartialValues = includePartialValues; | ||
this.series = new BigDecimal[lookBack]; | ||
Arrays.fill(series, BigDecimal.ONE); | ||
} | ||
|
||
@Override | ||
public boolean canCalculate() { | ||
return includePartialValues || index >= series.length; | ||
} | ||
|
||
@Override | ||
public void add(final BigDecimal element, final MathContext mathContext) { | ||
product = product.divide(series[index % series.length], mathContext).multiply(element, mathContext); | ||
series[index % series.length] = element; | ||
index++; | ||
} | ||
|
||
@Override | ||
public BigDecimal calculate() { | ||
return product; | ||
} | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
src/main/java/com/ginsberg/gatherers4j/BigDecimalMovingSumGatherer.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 2024 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.math.BigDecimal; | ||
import java.math.MathContext; | ||
import java.util.Arrays; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
public class BigDecimalMovingSumGatherer<INPUT extends @Nullable Object> | ||
extends BigDecimalGatherer<INPUT> { | ||
|
||
private final int windowSize; | ||
private boolean includePartialValues = false; | ||
|
||
BigDecimalMovingSumGatherer( | ||
final Function<INPUT, @Nullable BigDecimal> mappingFunction, | ||
final int windowSize) { | ||
super(mappingFunction); | ||
if (windowSize <= 0) { | ||
throw new IllegalArgumentException("Window size must be positive"); | ||
} | ||
this.windowSize = windowSize; | ||
} | ||
|
||
@Override | ||
public Supplier<BigDecimalGatherer.State> initializer() { | ||
return () -> new BigDecimalMovingSumGatherer.State(windowSize, includePartialValues); | ||
} | ||
|
||
/// When creating a moving sum and the full size of the window has not yet been reached, the | ||
/// gatherer should emit the sum of what it has. | ||
/// | ||
/// For example, if the trailing sum is over 10 values, but the stream has only emitted two | ||
/// values, the gatherer should calculate the two values and emit the answer. The default is to not | ||
/// emit anything until the full size of the window has been seen. | ||
public BigDecimalMovingSumGatherer<INPUT> includePartialValues() { | ||
includePartialValues = true; | ||
return this; | ||
} | ||
|
||
static class State implements BigDecimalGatherer.State { | ||
final boolean includePartialValues; | ||
final BigDecimal[] series; | ||
BigDecimal sum = BigDecimal.ZERO; | ||
int index = 0; | ||
|
||
private State(final int lookBack, final boolean includePartialValues) { | ||
this.includePartialValues = includePartialValues; | ||
this.series = new BigDecimal[lookBack]; | ||
Arrays.fill(series, BigDecimal.ZERO); | ||
} | ||
|
||
@Override | ||
public boolean canCalculate() { | ||
return includePartialValues || index >= series.length; | ||
} | ||
|
||
@Override | ||
public void add(final BigDecimal element, final MathContext mathContext) { | ||
sum = sum.subtract(series[index % series.length]).add(element, mathContext); | ||
series[index % series.length] = element; | ||
index++; | ||
} | ||
|
||
@Override | ||
public BigDecimal calculate() { | ||
return sum; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.