movingMedianBy()

Create a Stream that represents the moving median of BigDecimal objects mapped from a Stream<INPUT> via a function and looking back windowSize elements.

Implementation Notes

This implementation is suitable for mapping an arbitrary Stream<INPUT> to BigDecimal via a mappingFunction; for a version that operates directly on a Stream<BigDecimal>, see movingMedian(). By default, nulls are ignored and play no part in calculations, see treatNullAs() and treatNullAsZero() below for ways to change this behavior. The default MathContext for all calculations is MathContext.DECIMAL64, but this can be overridden (see withMathContext(), below).

Signatures

movingMedianBy(int windowSize, Function<INPUT, BigDecimal> mappingFunction)

  • windowSize - How many trailing elements to calculate the median from at any given point in the stream
  • mappingFunction - A non-null function to map stream INPUT elements into BigDecimal for calculation

Additional Methods

MethodPurpose
excludePartialValues()When calculating the moving median, and the full size of the window has not yet been reached, the gatherer should suppress emitting values until the lookback window is full. See example.
treatNullAsZero()When encountering a null value in a stream, treat it as BigDecimal.ZERO instead. See example.
treatNullAs(BigDecimal replacement)When encountering a null value in a stream, treat it as the given replacement value instead. See example.
withMathContext(MathContext mathContext)Replace the MathContext used for all mathematical operations performed by this gatherer. See example.
withOriginal()Include the original input value from the stream in addition to the calculated value in a WithOriginal record. See example.

Examples

Moving median of window size 3, mapped from an object

record NamedValue(String name, BigDecimal value) {}

Stream
    .of(
        new NamedValue("first",  new BigDecimal("1.0")),
        new NamedValue("second", new BigDecimal("2.0")),
        new NamedValue("third",  new BigDecimal("10.0")),
        new NamedValue("fourth", new BigDecimal("20.0")),
        new NamedValue("fifth",  new BigDecimal("30.0"))
    )
    .gather(Gatherers4j.movingMedianBy(3, NamedValue::value))
    .toList();

// [
//   BigDecimal("1.0")
//   BigDecimal("1.5")
//   BigDecimal("2.0")
//   BigDecimal("10.0")
//   BigDecimal("20.0")
// ]

Excluding partial values

Showing that in-process moving median values are not emitted for each element until the lookback window has been filled.

record NamedValue(String name, BigDecimal value) {}

Stream
    .of(
        new NamedValue("first",  new BigDecimal("1.0")),
        new NamedValue("second", new BigDecimal("2.0")),
        new NamedValue("third",  new BigDecimal("10.0")),
        new NamedValue("fourth", new BigDecimal("20.0")),
        new NamedValue("fifth",  new BigDecimal("30.0"))
    )
    .gather(Gatherers4j.movingMedianBy(3, NamedValue::value).excludePartialValues())
    .toList();

// [
//   BigDecimal("2.0")
//   BigDecimal("10.0")
//   BigDecimal("20.0")
// ]

Showing nulls are ignored by default

record NamedValue(String name, BigDecimal value) {}

Stream
    .of(
        new NamedValue("first",  null),
        new NamedValue("second", null),
        new NamedValue("third",  new BigDecimal("10.0")),
        new NamedValue("fourth", new BigDecimal("20.0")),
        new NamedValue("fifth",  new BigDecimal("30.0"))
    )
    .gather(Gatherers4j.movingMedianBy(3, NamedValue::value))
    .toList();

// [
//   BigDecimal("10.0")
//   BigDecimal("15.0")
//   BigDecimal("20.0")
// ]

Treating null as zero

record NamedValue(String name, BigDecimal value) {}

Stream
    .of(
        new NamedValue("first",  null),
        new NamedValue("second", null),
        new NamedValue("third",  new BigDecimal("10.0")),
        new NamedValue("fourth", new BigDecimal("20.0")),
        new NamedValue("fifth",  new BigDecimal("30.0"))
    )
    .gather(Gatherers4j.movingMedianBy(3, NamedValue::value).treatNullAsZero())
    .toList();

// [
//   BigDecimal("0")
//   BigDecimal("0")
//   BigDecimal("0")
//   BigDecimal("10.0")
//   BigDecimal("20.0")
// ]

Replacing null with another BigDecimal

record NamedValue(String name, BigDecimal value) {}

Stream
    .of(
        new NamedValue("first",  null),
        new NamedValue("second", null),
        new NamedValue("third",  new BigDecimal("10.0")),
        new NamedValue("fourth", new BigDecimal("20.0")),
        new NamedValue("fifth",  new BigDecimal("30.0"))
    )
    .gather(Gatherers4j.movingMedianBy(3, NamedValue::value).treatNullAs(BigDecimal.TWO))
    .toList();

// [
//   BigDecimal("2")
//   BigDecimal("2")
//   BigDecimal("2")
//   BigDecimal("10.0")
//   BigDecimal("20.0")
// ]

Specifying a new MathContext

record NamedValue(String name, BigDecimal value) {}

Stream
    .of(
        new NamedValue("first",  new BigDecimal("1.111")),
        new NamedValue("second", new BigDecimal("2.222")),
        new NamedValue("third",  new BigDecimal("10.1010")),
        new NamedValue("fourth", new BigDecimal("20.2020")),
        new NamedValue("fifth",  new BigDecimal("30.3030"))
    )
    .gather(Gatherers4j
        .movingMedianBy(2, NamedValue::value)
        .withMathContext(new MathContext(2, RoundingMode.DOWN))
    )
    .toList();

// [
//   BigDecimal("1.111")
//   BigDecimal("1.6")
//   BigDecimal("6.1")
//   BigDecimal("15")
//   BigDecimal("25")
// ]

Emitting a record containing the original and calculated values

record NamedValue(String name, BigDecimal value) {}

Stream
    .of(
        new NamedValue("first",  new BigDecimal("1.0")),
        new NamedValue("second", new BigDecimal("2.0")),
        new NamedValue("third",  new BigDecimal("10.0")),
        new NamedValue("fourth", new BigDecimal("20.0")),
        new NamedValue("fifth",  new BigDecimal("30.0"))
    )
    .gather(Gatherers4j.movingMedianBy(3, NamedValue::value).withOriginal())
    .toList();

// [
//   WithOriginal[original=NamedValue[name=first, value=1.0], calculated=1.0]
//   WithOriginal[original=NamedValue[name=second, value=2.0], calculated=1.5]
//   WithOriginal[original=NamedValue[name=third, value=10.0], calculated=2.0]
//   WithOriginal[original=NamedValue[name=fourth, value=20.0], calculated=10.0]
//   WithOriginal[original=NamedValue[name=fifth, value=30.0], calculated=20.0]
// ]