diff options
author | 2021-09-29 02:45:57 +0200 | |
---|---|---|
committer | 2021-09-29 03:16:01 +0200 | |
commit | a155f6ba02e08a75ce6e474a86900b8363f506e8 (patch) | |
tree | b78804c1c0f0968a9625f0656e08f5dadc16924c /store/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationUpdateListenerEntry.java | |
parent | Simplify branding (diff) | |
download | refinery-a155f6ba02e08a75ce6e474a86900b8363f506e8.tar.gz refinery-a155f6ba02e08a75ce6e474a86900b8363f506e8.tar.zst refinery-a155f6ba02e08a75ce6e474a86900b8363f506e8.zip |
build: migration to Gradle 7
Diffstat (limited to 'store/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationUpdateListenerEntry.java')
-rw-r--r-- | store/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationUpdateListenerEntry.java | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationUpdateListenerEntry.java b/store/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationUpdateListenerEntry.java new file mode 100644 index 00000000..55aed7c8 --- /dev/null +++ b/store/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationUpdateListenerEntry.java | |||
@@ -0,0 +1,63 @@ | |||
1 | package org.eclipse.viatra.solver.data.query.internal; | ||
2 | |||
3 | import java.util.Arrays; | ||
4 | import java.util.Objects; | ||
5 | |||
6 | import org.eclipse.viatra.query.runtime.matchers.context.IQueryRuntimeContextListener; | ||
7 | import org.eclipse.viatra.query.runtime.matchers.tuple.ITuple; | ||
8 | import org.eclipse.viatra.query.runtime.matchers.tuple.Tuples; | ||
9 | import org.eclipse.viatra.solver.data.model.Tuple; | ||
10 | import org.eclipse.viatra.solver.data.query.view.RelationView; | ||
11 | |||
12 | public class RelationUpdateListenerEntry<D> { | ||
13 | final RelationView<D> key; | ||
14 | final ITuple filter; | ||
15 | final IQueryRuntimeContextListener listener; | ||
16 | |||
17 | public RelationUpdateListenerEntry(RelationView<D> key, ITuple filter, IQueryRuntimeContextListener listener) { | ||
18 | super(); | ||
19 | this.key = key; | ||
20 | this.filter = filter; | ||
21 | this.listener = listener; | ||
22 | } | ||
23 | |||
24 | public void processChange(Tuple tuple, D oldValue, D newValue) { | ||
25 | Object[] oldTuple = isMatching(key.getWrappedKey().transform(tuple, oldValue), filter); | ||
26 | Object[] newTuple = isMatching(key.getWrappedKey().transform(tuple, newValue), filter); | ||
27 | |||
28 | if(!Arrays.equals(oldTuple, newTuple)) { | ||
29 | if(oldTuple != null) { | ||
30 | listener.update(key, Tuples.flatTupleOf(oldTuple), false); | ||
31 | } | ||
32 | if(newTuple != null) { | ||
33 | listener.update(key, Tuples.flatTupleOf(newTuple), true); | ||
34 | } | ||
35 | } | ||
36 | } | ||
37 | |||
38 | private Object[] isMatching(Object[] tuple, ITuple filter) { | ||
39 | for(int i = 0; i<filter.getSize(); i++) { | ||
40 | final Object filterObject = filter.get(i); | ||
41 | if(filterObject != null && !filterObject.equals(tuple[i])) { | ||
42 | return null; | ||
43 | } | ||
44 | } | ||
45 | return tuple; | ||
46 | } | ||
47 | |||
48 | @Override | ||
49 | public int hashCode() { | ||
50 | return Objects.hash(filter, key, listener); | ||
51 | } | ||
52 | |||
53 | @Override | ||
54 | public boolean equals(Object obj) { | ||
55 | if (this == obj) | ||
56 | return true; | ||
57 | if (!(obj instanceof RelationUpdateListenerEntry)) | ||
58 | return false; | ||
59 | RelationUpdateListenerEntry<?> other = (RelationUpdateListenerEntry<?>) obj; | ||
60 | return Objects.equals(filter, other.filter) && Objects.equals(key, other.key) | ||
61 | && Objects.equals(listener, other.listener); | ||
62 | } | ||
63 | } | ||