diff options
Diffstat (limited to 'subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/LongSumOperator.java')
-rw-r--r-- | subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/LongSumOperator.java | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/LongSumOperator.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/LongSumOperator.java new file mode 100644 index 00000000..29ded090 --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/LongSumOperator.java | |||
@@ -0,0 +1,61 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2016, Gabor Bergmann, IncQueryLabs 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.stream.Stream; | ||
12 | |||
13 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.AbstractMemorylessAggregationOperator; | ||
14 | |||
15 | /** | ||
16 | * Incrementally computes the sum of java.lang.Long values | ||
17 | * @author Gabor Bergmann | ||
18 | * @since 1.4 | ||
19 | */ | ||
20 | public class LongSumOperator extends AbstractMemorylessAggregationOperator<Long, Long> { | ||
21 | public static final LongSumOperator INSTANCE = new LongSumOperator(); | ||
22 | |||
23 | private LongSumOperator() { | ||
24 | // Singleton, do not call. | ||
25 | } | ||
26 | |||
27 | @Override | ||
28 | public String getShortDescription() { | ||
29 | return "sum<Long> incrementally computes the sum of java.lang.Long values"; | ||
30 | } | ||
31 | @Override | ||
32 | public String getName() { | ||
33 | return "sum<Long>"; | ||
34 | } | ||
35 | |||
36 | @Override | ||
37 | public Long createNeutral() { | ||
38 | return 0L; | ||
39 | } | ||
40 | |||
41 | @Override | ||
42 | public boolean isNeutral(Long result) { | ||
43 | return createNeutral().equals(result); | ||
44 | } | ||
45 | |||
46 | @Override | ||
47 | public Long update(Long oldResult, Long updateValue, boolean isInsertion) { | ||
48 | return isInsertion ? | ||
49 | oldResult + updateValue : | ||
50 | oldResult - updateValue; | ||
51 | } | ||
52 | |||
53 | /** | ||
54 | * @since 2.0 | ||
55 | */ | ||
56 | @Override | ||
57 | public Long aggregateStream(Stream<Long> stream) { | ||
58 | return stream.mapToLong(Long::longValue).sum(); | ||
59 | } | ||
60 | |||
61 | } | ||