blob: 73c4a3f9d9deededead8ba0c2f27206595f07189 (
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
|
/*
* SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
*
* SPDX-License-Identifier: EPL-2.0
*/
package tools.refinery.store.query.viatra.internal.update;
import tools.refinery.viatra.runtime.matchers.context.IInputKey;
import tools.refinery.viatra.runtime.matchers.context.IQueryRuntimeContextListener;
import tools.refinery.viatra.runtime.matchers.tuple.ITuple;
import tools.refinery.viatra.runtime.matchers.tuple.Tuple;
import java.util.Arrays;
import java.util.Objects;
public final class RelationViewFilter {
private final IInputKey inputKey;
private final Object[] seed;
private final IQueryRuntimeContextListener listener;
public RelationViewFilter(IInputKey inputKey, ITuple seed, IQueryRuntimeContextListener listener) {
this.inputKey = inputKey;
this.seed = seedToArray(seed);
this.listener = listener;
}
public void update(Tuple updateTuple, boolean isInsertion) {
if (isMatching(updateTuple)) {
listener.update(inputKey, updateTuple, isInsertion);
}
}
private boolean isMatching(ITuple tuple) {
if (seed == null) {
return true;
}
int size = seed.length;
for (int i = 0; i < size; i++) {
var filterElement = seed[i];
if (filterElement != null && !filterElement.equals(tuple.get(i))) {
return false;
}
}
return true;
}
// Use <code>null</code> instead of an empty array to speed up comparisons.
@SuppressWarnings("squid:S1168")
private static Object[] seedToArray(ITuple seed) {
for (var element : seed.getElements()) {
if (element != null) {
return seed.getElements();
}
}
return null;
}
@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || obj.getClass() != this.getClass()) return false;
var that = (RelationViewFilter) obj;
return Objects.equals(this.inputKey, that.inputKey) && Arrays.equals(this.seed, that.seed) &&
Objects.equals(this.listener, that.listener);
}
@Override
public int hashCode() {
return Objects.hash(inputKey, Arrays.hashCode(seed), listener);
}
}
|