All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nextbreakpoint.flink.dummies.TemperatureAggregator Maven / Gradle / Ivy

There is a newer version: 0.0.6
Show newest version
/*
 * This file is part of Flink Dummies
 * https://github.com/nextbreakpoint/flink-dummies
 */
package com.nextbreakpoint.flink.dummies;

import org.apache.flink.api.common.functions.AggregateFunction;

import java.math.BigDecimal;

public class TemperatureAggregator implements AggregateFunction {
    @Override
    public TemperatureAccumulator createAccumulator() {
        return TemperatureAccumulator.builder()
                .withMaximumTemperature(new BigDecimal(0))
                .withTemperature(new BigDecimal(0))
                .withCount(0)
                .build();
    }

    @Override
    public TemperatureAccumulator add(SampledTemperature element, TemperatureAccumulator accumulator) {
        return accumulator.add(BigDecimal.valueOf(element.getValue(), 2));
    }

    @Override
    public TemperatureAccumulator merge(TemperatureAccumulator accumulator, TemperatureAccumulator otherAccumulator) {
        return accumulator.add(otherAccumulator);
    }

    @Override
    public Temperature getResult(TemperatureAccumulator accumulator) {
        return Temperature.builder()
                .withAverage(accumulator.getAverageTemperature().unscaledValue().intValue())
                .withMaximum(accumulator.getMaximumTemperature().unscaledValue().intValue())
                .build();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy