diff options
Diffstat (limited to 'subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/tuple/LeftInheritanceTuple1.java')
-rw-r--r-- | subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/tuple/LeftInheritanceTuple1.java | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/tuple/LeftInheritanceTuple1.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/tuple/LeftInheritanceTuple1.java new file mode 100644 index 00000000..61123176 --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/tuple/LeftInheritanceTuple1.java | |||
@@ -0,0 +1,83 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2017, 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.tuple; | ||
10 | |||
11 | import java.util.Objects; | ||
12 | |||
13 | /** | ||
14 | * @author Gabor Bergmann | ||
15 | * @since 1.7 | ||
16 | */ | ||
17 | public final class LeftInheritanceTuple1 extends BaseLeftInheritanceTuple { | ||
18 | /** | ||
19 | * A single substituted value after inheritedIndex. | ||
20 | */ | ||
21 | private final Object localElement; | ||
22 | |||
23 | /** | ||
24 | * @param ancestor | ||
25 | * @param localElement | ||
26 | */ | ||
27 | protected LeftInheritanceTuple1(Tuple ancestor, Object localElement) { | ||
28 | super(ancestor); | ||
29 | this.localElement = localElement; | ||
30 | calcHash(); | ||
31 | } | ||
32 | |||
33 | /** | ||
34 | * @return number of elements | ||
35 | */ | ||
36 | public int getSize() { | ||
37 | return inheritedIndex + 1; | ||
38 | } | ||
39 | |||
40 | @Override | ||
41 | public int getLocalSize() { | ||
42 | return 1; | ||
43 | } | ||
44 | |||
45 | /** | ||
46 | * @pre: 0 <= index < getSize() | ||
47 | * | ||
48 | * @return the element at the specified index | ||
49 | */ | ||
50 | public Object get(int index) { | ||
51 | int local = index - inheritedIndex; | ||
52 | if (local < 0) | ||
53 | return ancestor.get(index); | ||
54 | else if (local == 0) return localElement; | ||
55 | else throw raiseIndexingError(index); | ||
56 | } | ||
57 | |||
58 | /** | ||
59 | * Optimized hash calculation | ||
60 | */ | ||
61 | @Override | ||
62 | void calcHash() { | ||
63 | final int PRIME = 31; | ||
64 | cachedHash = ancestor.hashCode(); | ||
65 | cachedHash = PRIME * cachedHash; | ||
66 | if (localElement != null) cachedHash += localElement.hashCode(); | ||
67 | } | ||
68 | |||
69 | /** | ||
70 | * Optimized equals calculation (prediction: true, since hash values match) | ||
71 | */ | ||
72 | @Override | ||
73 | protected boolean localEquals(BaseLeftInheritanceTuple other) { | ||
74 | if (other instanceof LeftInheritanceTuple1) { | ||
75 | LeftInheritanceTuple1 lit = (LeftInheritanceTuple1)other; | ||
76 | return Objects.equals(this.localElement, lit.localElement); | ||
77 | } else { | ||
78 | return (1 == other.getLocalSize()) && | ||
79 | Objects.equals(localElement, other.get(inheritedIndex)); | ||
80 | } | ||
81 | } | ||
82 | |||
83 | } | ||