Interface ValueReducer<T>

Type Parameters:
T - type of reduced values
All Known Implementing Classes:
LongSumLoader, NumberSumLoader, ReducingAggregateLoader, SingleDependencyReducingAggregateLoader
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface ValueReducer<T>

Describes how to combine the current value and values aggregated over the part of the subtree (as defined by ReductionStrategy) during aggregate calculation. It's intended for those attribute loaders that calculate values using only a part of subtree (examples: only direct children, only leaves, subtree without root node). The part of subtree to use is defined by ReductionStrategy, and this interface defines the reduction. Following how AttributeLoader.Aggregate works, reduction is done by merging the current value with children values. It provides multiple methods for strategies as they can only have a limited set of data on every tree node processing.

One would want to implement this interface in case there's a need to aggregate over a custom part of a subtree. For most cases, it's better to extend ReducingAggregateLoader. It's sufficient to implement only reduce(java.util.List<T>), but one would normally want to implement other methods too -- for either gaining performance benefit (see NumberSumLoader) or providing more meaningful result.

Methods are organized to cover the needs of existing ReductionStrategy instances. This is the reason methods in ValueReducer roughly correspond to strategies in ReductionStrategy. Correspondence table is given below:

MethodStrategy
reduce(List<T> list)leaves, children, strict
convert(Supplier<T> self)leaves
merge(Supplier<T> self, Supplier<T> reduced)strict
merge(Supplier<T> self, List<T> list)subtree
Generally implementations shouldn't worry about tree traversal but can be aware of it.

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    default T
    convert(Supplier<T> self)
    Reduce a single value (self value of attribute for current row).
    default T
    merge(Supplier<T> self, Supplier<T> reduced)
    Calculates reduction of tree node where self is value of the current row, and reduced is result of flat reduction.
    default T
    merge(Supplier<T> self, List<T> list)
    Calculates reduction of tree node where self is value of the current row and list represents values passed from children (it may be or be not values of children rows depending on strategy type).
    reduce(List<T> list)
    Reduces the list of values to one value.
  • Method Details

    • reduce

      @Nullable T reduce(@NotNull List<T> list)
      Reduces the list of values to one value. Describes 'flat' reduction of elements without hierarchy. Called reducing direct children values or from merge methods if not overridden. Can be called from convert method
      Parameters:
      list - list of values, every item can be null
      Returns:
      value after reduction, null means undefined
    • convert

      @Nullable default T convert(@NotNull Supplier<T> self)
      Reduce a single value (self value of attribute for current row). If reduction describes any value conversion (null checks etc) it should be done here as well. The result must be equal to calling reduce() with one element.
      Parameters:
      self - can return null
    • merge

      @Nullable default T merge(@NotNull Supplier<T> self, @NotNull Supplier<T> reduced)
      Calculates reduction of tree node where self is value of the current row, and reduced is result of flat reduction. Value modifications can be done here if one wants to build reduction that computes value based on depth in the structure. Called from strict subtree reduction strategy or from other merge method if not optimized.
    • merge

      @Nullable default T merge(Supplier<T> self, List<T> list)
      Calculates reduction of tree node where self is value of the current row and list represents values passed from children (it may be or be not values of children rows depending on strategy type). Value modifications can be done here if one wants to build reduction that computes value based on depth in the structure. Called from full subtree reduction strategy, that is default and the most frequently used strategy for most reducing aggregates (it makes this method the most probable target for optimization).