aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/LongAverageOperator.java
blob: d56c950758a5a756cedf0ac2fa65fb79eb54b102 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*******************************************************************************
 * Copyright (c) 2010-2018, Zoltan Ujhelyi, IncQuery Labs Ltd.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-v20.html.
 * 
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package tools.refinery.viatra.runtime.matchers.aggregators;

import java.util.OptionalDouble;
import java.util.stream.Stream;

import tools.refinery.viatra.runtime.matchers.psystem.aggregations.IMultisetAggregationOperator;

/**
 * @author Zoltan Ujhelyi
 * @since 2.0
 */
public class LongAverageOperator implements IMultisetAggregationOperator<Long, AverageAccumulator<Long>, Double> {

    public static final LongAverageOperator INSTANCE = new LongAverageOperator();
    
    private LongAverageOperator() {
        // Singleton, do not call.
    }
    
    @Override
    public String getShortDescription() {
        return "avg<Integer> incrementally computes the average of java.lang.Integer values";
    }

    @Override
    public String getName() {
        return "avg<Integer>";
    }

    @Override
    public AverageAccumulator<Long> createNeutral() {
        return new AverageAccumulator<Long>(0l, 0l);
    }

    @Override
    public boolean isNeutral(AverageAccumulator<Long> result) {
        return result.count == 0l;
    }

    @Override
    public AverageAccumulator<Long> update(AverageAccumulator<Long> oldResult, Long updateValue,
            boolean isInsertion) {
        if (isInsertion) {
            oldResult.value += updateValue;
            oldResult.count++;
        } else {
            oldResult.value -= updateValue;
            oldResult.count--;
        }
        return oldResult;
    }

    @Override
    public Double getAggregate(AverageAccumulator<Long> result) {
        return (result.count == 0)
                ? null
                : ((double)result.value)/result.count;
    }

    @Override
    public Double aggregateStream(Stream<Long> stream) {
        final OptionalDouble averageOpt = stream.mapToLong(Long::longValue).average();
        return averageOpt.isPresent() ? averageOpt.getAsDouble() : null;
    }
    
    /**
     * @since 2.4
     */
    @Override
    public AverageAccumulator<Long> clone(AverageAccumulator<Long> original) {
        return new AverageAccumulator<Long>(original.value, original.count);
    }
    
}