diff options
Diffstat (limited to 'subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/LongAverageOperator.java')
-rw-r--r-- | subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/LongAverageOperator.java | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/LongAverageOperator.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/LongAverageOperator.java new file mode 100644 index 00000000..d56c9507 --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/LongAverageOperator.java | |||
@@ -0,0 +1,82 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2018, Zoltan Ujhelyi, IncQuery Labs Ltd. | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.matchers.aggregators; | ||
10 | |||
11 | import java.util.OptionalDouble; | ||
12 | import java.util.stream.Stream; | ||
13 | |||
14 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.IMultisetAggregationOperator; | ||
15 | |||
16 | /** | ||
17 | * @author Zoltan Ujhelyi | ||
18 | * @since 2.0 | ||
19 | */ | ||
20 | public class LongAverageOperator implements IMultisetAggregationOperator<Long, AverageAccumulator<Long>, Double> { | ||
21 | |||
22 | public static final LongAverageOperator INSTANCE = new LongAverageOperator(); | ||
23 | |||
24 | private LongAverageOperator() { | ||
25 | // Singleton, do not call. | ||
26 | } | ||
27 | |||
28 | @Override | ||
29 | public String getShortDescription() { | ||
30 | return "avg<Integer> incrementally computes the average of java.lang.Integer values"; | ||
31 | } | ||
32 | |||
33 | @Override | ||
34 | public String getName() { | ||
35 | return "avg<Integer>"; | ||
36 | } | ||
37 | |||
38 | @Override | ||
39 | public AverageAccumulator<Long> createNeutral() { | ||
40 | return new AverageAccumulator<Long>(0l, 0l); | ||
41 | } | ||
42 | |||
43 | @Override | ||
44 | public boolean isNeutral(AverageAccumulator<Long> result) { | ||
45 | return result.count == 0l; | ||
46 | } | ||
47 | |||
48 | @Override | ||
49 | public AverageAccumulator<Long> update(AverageAccumulator<Long> oldResult, Long updateValue, | ||
50 | boolean isInsertion) { | ||
51 | if (isInsertion) { | ||
52 | oldResult.value += updateValue; | ||
53 | oldResult.count++; | ||
54 | } else { | ||
55 | oldResult.value -= updateValue; | ||
56 | oldResult.count--; | ||
57 | } | ||
58 | return oldResult; | ||
59 | } | ||
60 | |||
61 | @Override | ||
62 | public Double getAggregate(AverageAccumulator<Long> result) { | ||
63 | return (result.count == 0) | ||
64 | ? null | ||
65 | : ((double)result.value)/result.count; | ||
66 | } | ||
67 | |||
68 | @Override | ||
69 | public Double aggregateStream(Stream<Long> stream) { | ||
70 | final OptionalDouble averageOpt = stream.mapToLong(Long::longValue).average(); | ||
71 | return averageOpt.isPresent() ? averageOpt.getAsDouble() : null; | ||
72 | } | ||
73 | |||
74 | /** | ||
75 | * @since 2.4 | ||
76 | */ | ||
77 | @Override | ||
78 | public AverageAccumulator<Long> clone(AverageAccumulator<Long> original) { | ||
79 | return new AverageAccumulator<Long>(original.value, original.count); | ||
80 | } | ||
81 | |||
82 | } \ No newline at end of file | ||