
com.fluxtion.ext.streaming.builder.factory.GroupFunctionsBuilder Maven / Gradle / Ivy
/*
* Copyright (c) 2020, V12 Technology Ltd.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* .
*/
package com.fluxtion.ext.streaming.builder.factory;
import com.fluxtion.api.partition.LambdaReflection.SerializableBiConsumer;
import com.fluxtion.api.partition.LambdaReflection.SerializableBiFunction;
import com.fluxtion.api.partition.LambdaReflection.SerializableFunction;
import com.fluxtion.api.partition.LambdaReflection.SerializableTriFunction;
import com.fluxtion.ext.streaming.api.group.GroupBy;
import com.fluxtion.ext.streaming.api.stream.StreamFunctions.Average;
import com.fluxtion.ext.streaming.api.stream.StreamFunctions.Max;
import com.fluxtion.ext.streaming.api.stream.StreamFunctions.Min;
import com.fluxtion.ext.streaming.api.util.Tuple;
import static com.fluxtion.ext.streaming.builder.factory.StreamFunctionsBuilder.count;
import static com.fluxtion.ext.streaming.builder.factory.StreamFunctionsBuilder.cumSum;
import com.fluxtion.ext.streaming.builder.group.Group;
/**
* Shortcut functions to produce aggregate grouping functions on an event stream. Results are returned as a Tuple:
*
* - Tuple::key - the grouping key for the data
*
- Tuple::value - the result of the function
*
*
* @author Greg Higgins [email protected]
*/
public class GroupFunctionsBuilder {
/**
* Applies a numeric static function to an incoming field. The results are grouped by the key provider.
*
* The function processes {@link Number) inputs and produces a primitive or {@link Number} outputs. The function
* parameters are:
*
* - The new value to be processed as a number
*
- The result from the previous calculation
*
*
* Fluxtion will handle all type conversions
*
* @param Input event type
* @param Key type
* @param Value type
* @param The output type of the function
* @param keySupplier key supplier for grouping
* @param valueSupplier The value supplier to the function calculation
* @param calcFunctionClass the calculation function to apply to value
* @return The GroupBy collection holding results of the calculation
*/
public static GroupBy> groupByCalc(
SerializableFunction keySupplier,
SerializableFunction valueSupplier,
SerializableBiFunction super R, ? super R, ? extends R> calcFunctionClass
) {
Class> tupleClass = Tuple.generify();
SerializableBiConsumer, ? super Byte> consumer = tupleSetRef(Tuple::setValue);
GroupBy> build = Group.groupBy(keySupplier, tupleClass)
.init(keySupplier, Tuple::setKey)
.initCopy(Tuple::copyKey)
.mapPrimitive(valueSupplier, consumer, calcFunctionClass)
.build();
return build;
}
/**
* Applies a numeric static function to an incoming event. The results are grouped by the key provider.
*
* The function processes {@link Number) inputs and produces a primitive or {@link Number} outputs. The function
* parameters are:
*
* - The new value to be processed as a number
*
- The result from the previous calculation
*
*
* Fluxtion will handle all type conversions
*
* @param Input event type
* @param Key type
* @param The output type of the function
* @param keySupplier key supplier for grouping
* @param calcFunctionClass the calculation function to apply to value
* @return The GroupBy collection holding results of the calculation
*/
public static GroupBy> groupByCalc(
SerializableFunction keySupplier,
SerializableBiFunction super R, ? super R, ? extends R> calcFunctionClass
) {
Class> tupleClass = Tuple.generify();
SerializableBiConsumer, ? super Byte> consumer = tupleSetRef(Tuple::setValue);
GroupBy> build = Group.groupBy(keySupplier, tupleClass)
.init(keySupplier, Tuple::setKey)
.initCopy(Tuple::copyKey)
.mapPrimitive(consumer, calcFunctionClass)
.build();
return build;
}
/**
* Applies a static function to an incoming event. The results are grouped by the key provider.
*
* The function processes inputs and produces any output type. The function
* parameters are:
*
* - The new value to be processed as a number
*
- The result from the previous calculation
*
*
* @param Input event type
* @param Key type
* @param The output type of the function
* @param keySupplier key supplier for grouping
* @param calcFunctionClass the calculation function to apply to value
* @return The GroupBy collection holding results of the calculation
*/
public static GroupBy> groupByCalcComplex(
SerializableFunction keySupplier,
SerializableBiFunction super S, ? super R, ? extends R> calcFunctionClass
) {
Class> tupleClass = Tuple.generify();
GroupBy> build = Group.groupBy(keySupplier, tupleClass)
.init(keySupplier, Tuple::setKey)
.initCopy(Tuple::copyKey)
.map(Tuple::setValue, calcFunctionClass)
.build();
return build;
}
/**
* Applies a numeric instance function to an incoming field. The results are grouped by the key provider.
*
* The instance function processes {@link Number) inputs and produces a primitive or {@link Number} outputs. The function
* parameters are:
*
* - The new value to be processed as a number
*
- The result from the previous calculation
*
*
* Fluxtion will handle all type conversions
*
* @param Input event type
* @param Key type
* @param Value type
* @param The instance type containing the function
* @param The output type of the function
* @param keySupplier key supplier for grouping
* @param valueSupplier The value supplier to the function calculation
* @param calcFunctionClass the calculation function to apply to value
* @return The GroupBy collection holding results of the calculation
*/
public static GroupBy> groupByCalc(
SerializableFunction keySupplier,
SerializableFunction valueSupplier,
SerializableTriFunction calcFunctionClass
) {
Class> tupleClass = Tuple.generify();
SerializableBiConsumer, ? super Byte> consumer = tupleSetRef(Tuple::setValue);
GroupBy> build = Group.groupBy(keySupplier, tupleClass)
.init(keySupplier, Tuple::setKey)
.initCopy(Tuple::copyKey)
.mapPrimitive(valueSupplier, consumer, calcFunctionClass)
.build();
return build;
}
/**
* Applies a numeric instance function to an incoming event. The results are grouped by the key provider.
*
* The instance function processes {@link Number) inputs and produces a primitive or {@link Number} outputs. The function
* parameters are:
*
* - The new value to be processed as a number
*
- The result from the previous calculation
*
*
* Fluxtion will handle all type conversions
*
* @param Input event type
* @param Key type
* @param The instance type containing the function
* @param The output type of the function
* @param keySupplier key supplier for grouping
* @param calcFunctionClass the calculation function to apply to value
* @return The GroupBy collection holding results of the calculation
*/
public static GroupBy> groupByCalc(
SerializableFunction keySupplier,
SerializableTriFunction calcFunctionClass
) {
Class> tupleClass = Tuple.generify();
SerializableBiConsumer, ? super Byte> consumer = tupleSetRef(Tuple::setValue);
GroupBy> build = Group.groupBy(keySupplier, tupleClass)
.init(keySupplier, Tuple::setKey)
.initCopy(Tuple::copyKey)
.mapPrimitive(consumer, calcFunctionClass)
.build();
return build;
}
/**
* Applies an instance function to an incoming event. The results are grouped by the key provider.
*
* The instance function processes inputs and produces any output type. The function
* parameters are:
*
* - The new value to be processed as a number
*
- The result from the previous calculation
*
*
* Fluxtion will handle all type conversions
*
* @param Input event type
* @param Key type
* @param The instance type containing the function
* @param The output type of the function
* @param keySupplier key supplier for grouping
* @param calcFunctionClass the calculation function to apply to value
* @return The GroupBy collection holding results of the calculation
*/
public static GroupBy> groupByCalcComplex(
SerializableFunction keySupplier,
SerializableTriFunction calcFunctionClass
) {
Class> tupleClass = Tuple.generify();
GroupBy> build = Group.groupBy(keySupplier, tupleClass)
.init(keySupplier, Tuple::setKey)
.initCopy(Tuple::copyKey)
.map(Tuple::setValue, calcFunctionClass)
.build();
return build;
}
/**
* Applies a numeric static or instance function to an incoming field. The previous value is not provided as an argument.
* The results are grouped by the key provider.
*
* The function processes {@link Number) inputs and produces a primitive or {@link Number} outputs. The function
* parameters are:
*
* - The new value to be processed as a number
*
*
* Fluxtion will handle all type conversions
*
* @param Input event type
* @param Key type
* @param Value type
* @param The output type of the function
* @param keySupplier key supplier for grouping
* @param valueSupplier The value supplier to the function calculation
* @param func the calculation function to apply to value
* @return The GroupBy collection holding results of the calculation
*/
public static GroupBy> groupByCalc(
SerializableFunction keySupplier,
SerializableFunction valueSupplier,
SerializableFunction super R, ? extends R> func
) {
Class> tupleClass = Tuple.generify();
SerializableBiConsumer, ? super Byte> consumer = tupleSetRef(Tuple::setValue);
GroupBy> build = Group.groupBy(keySupplier, tupleClass)
.init(keySupplier, Tuple::setKey)
.initCopy(Tuple::copyKey)
.mapPrimitive(valueSupplier, consumer, func )
.build();
return build;
}
public static GroupBy> groupByCalc(
SerializableFunction keySupplier,
SerializableFunction super R, ? extends R> calcFunctionClass
) {
Class> tupleClass = Tuple.generify();
SerializableBiConsumer, ? super Byte> consumer = tupleSetRef(Tuple::setValue);
GroupBy> build = Group.groupBy(keySupplier, tupleClass)
.init(keySupplier, Tuple::setKey)
.initCopy(Tuple::copyKey)
.mapPrimitive(consumer, calcFunctionClass)
.build();
return build;
}
/**
* Applies an instance function to an incoming event. The previous value is not provided as an argument.
* The results are grouped by the key provider.
*
* The instance function processes inputs and produces any output type. The function
* parameters are:
*
* - The new value to be processed as a number
*
*
* Fluxtion will handle all type conversions
*
* @param Input event type
* @param Key type
* @param The output type of the function
* @param keySupplier key supplier for grouping
* @param func the calculation function to apply to value
* @return The GroupBy collection holding results of the calculation
*/
public static GroupBy> groupByCalcComplex(
SerializableFunction keySupplier,
SerializableFunction super S, ? extends R> func
) {
Class> tupleClass = Tuple.generify();
GroupBy> build = Group.groupBy(keySupplier, tupleClass)
.init(keySupplier, Tuple::setKey)
.initCopy(Tuple::copyKey)
.map(Tuple::setValue, func)
.build();
return build;
}
private static SerializableBiConsumer tupleSetRef(SerializableBiConsumer c) {
return (SerializableBiConsumer) c;
}
public static GroupBy> groupBySum(
SerializableFunction key,
SerializableFunction supplier
) {
return groupByCalc(key, supplier, cumSum());
}
public static GroupBy> groupByAvg(
SerializableFunction key,
SerializableFunction supplier
) {
return GroupFunctionsBuilder.groupByCalc(key, supplier, new Average()::addValue);
}
public static GroupBy> groupByMax(
SerializableFunction key,
SerializableFunction supplier
) {
return groupByCalc(key, supplier, new Max()::max);
}
public static GroupBy> groupByMin(
SerializableFunction key,
SerializableFunction supplier
) {
return groupByCalc(key, supplier, new Min()::min);
}
public static GroupBy> groupByCount(
SerializableFunction key
) {
return groupByCalc(key, count());
// return groupByCalc(key, AggregateFunctions::count);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy