aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-query/src/main/java/tools/refinery/store/query/term/ExtremeValueAggregator.java
blob: 657cb63182bfe1f010282fabbb5754110cb51de8 (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
/*
 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.query.term;

import java.util.Comparator;
import java.util.Objects;
import java.util.SortedMap;
import java.util.TreeMap;

public class ExtremeValueAggregator<T> implements StatefulAggregator<T, T> {
	private final Class<T> type;
	private final T emptyResult;
	private final Comparator<T> comparator;

	public ExtremeValueAggregator(Class<T> type, T emptyResult) {
		this(type, emptyResult, null);
	}

	public ExtremeValueAggregator(Class<T> type, T emptyResult, Comparator<T> comparator) {
		this.type = type;
		this.emptyResult = emptyResult;
		this.comparator = comparator;
	}

	@Override
	public Class<T> getResultType() {
		return getInputType();
	}

	@Override
	public Class<T> getInputType() {
		return type;
	}

	@Override
	public StatefulAggregate<T, T> createEmptyAggregate() {
		return new Aggregate();
	}

	@Override
	public T getEmptyResult() {
		return emptyResult;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		ExtremeValueAggregator<?> that = (ExtremeValueAggregator<?>) o;
		return type.equals(that.type) && Objects.equals(emptyResult, that.emptyResult) && Objects.equals(comparator,
				that.comparator);
	}

	@Override
	public int hashCode() {
		return Objects.hash(type, emptyResult, comparator);
	}

	private class Aggregate implements StatefulAggregate<T, T> {
		private final SortedMap<T, Integer> values;

		private Aggregate() {
			values = new TreeMap<>(comparator);
		}

		private Aggregate(Aggregate other) {
			values = new TreeMap<>(other.values);
		}

		@Override
		public void add(T value) {
			values.compute(value, (ignoredValue, currentCount) -> currentCount == null ? 1 : currentCount + 1);
		}

		@Override
		public void remove(T value) {
			values.compute(value, (theValue, currentCount) -> {
				if (currentCount == null || currentCount <= 0) {
					throw new IllegalStateException("Invalid count %d for value %s".formatted(currentCount, theValue));
				}
				return currentCount.equals(1) ? null : currentCount - 1;
			});
		}

		@Override
		public T getResult() {
			return isEmpty() ? emptyResult : values.firstKey();
		}

		@Override
		public boolean isEmpty() {
			return values.isEmpty();
		}

		@Override
		public StatefulAggregate<T, T> deepCopy() {
			return new Aggregate(this);
		}

		@Override
		public boolean contains(T value) {
			return StatefulAggregate.super.contains(value);
		}
	}
}