aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/ExtremumOperator.java
blob: ee4ceeb86d4559c032ed3161ff0f84e47eed5037 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*******************************************************************************
 * Copyright (c) 2010-2016, Gabor Bergmann, IncQueryLabs 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.Comparator;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.stream.Stream;

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

/**
 * Incrementally computes the minimum or maximum of java.lang.Comparable values, using the default comparison
 * 
 * @author Gabor Bergmann
 * @since 1.4
 */
public class ExtremumOperator<T extends Comparable<T>>
        implements IMultisetAggregationOperator<T, SortedMap<T, Integer>, T> {
    
    public enum Extreme {
        MIN, MAX;
        
        /**
         * @since 2.0
         */
        public <T> T pickFrom(SortedMap<T, Integer> nonEmptyMultiSet) {
            switch(this) {
            case MIN: 
                return nonEmptyMultiSet.firstKey(); 
            case MAX:
                return nonEmptyMultiSet.lastKey();
            default:
                return null;
            }
        }
    }

    private static final ExtremumOperator MIN_OP = new ExtremumOperator<>(Extreme.MIN);
    private static final ExtremumOperator MAX_OP = new ExtremumOperator<>(Extreme.MAX);

    public static <T extends Comparable<T>> ExtremumOperator<T> getMin() {
        return MIN_OP;
    }
    public static <T extends Comparable<T>> ExtremumOperator<T> getMax() {
        return MAX_OP;
    }
    
    Extreme extreme;
    private ExtremumOperator(Extreme extreme) {
        super();
        this.extreme = extreme;
    }

    @Override
    public String getShortDescription() {
        String opName = getName();
        return String.format(
                "%s incrementally computes the %simum of java.lang.Comparable values, using the default comparison",
                opName, opName);
    }

    @Override
    public String getName() {
        return extreme.name().toLowerCase();
    }

    /**
     * @since 2.0
     */
    @Override
    public SortedMap<T, Integer> createNeutral() {
        return new TreeMap<>();
    }

    /**
     * @since 2.0
     */
    @Override
    public boolean isNeutral(SortedMap<T, Integer> result) {
        return result.isEmpty();
    }

    /**
     * @since 2.0
     */
    @Override
    public SortedMap<T, Integer> update(SortedMap<T, Integer> oldResult, T updateValue, boolean isInsertion) {
        oldResult.compute(updateValue, (value, c) -> {
            int count = (c == null) ? 0 : c;
            int result = (isInsertion) ? count+1 : count-1;  
            return (result == 0) ? null : result;
        });
        return oldResult;
    }

    /**
     * @since 2.0
     */
    @Override
    public T getAggregate(SortedMap<T, Integer> result) {
        return result.isEmpty() ? null : 
            extreme.pickFrom(result);
    }
    
    /**
     * @since 2.0
     */
    @Override
    public T aggregateStream(Stream<T> stream) {
        switch (extreme) {
        case MIN:
            return stream.min(Comparator.naturalOrder()).orElse(null);
        case MAX:
            return stream.max(Comparator.naturalOrder()).orElse(null);
        default:
            return null;
        }
    }
    
    /**
     * @since 2.4
     */
    @Override
    public SortedMap<T, Integer> clone(SortedMap<T, Integer> original) {
        return new TreeMap<T, Integer>(original);
    }
    
}