aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/tuple/LeftInheritanceTuple4.java
blob: 4e50f1e12d0912406339d2e5f4ae5fb648609cbf (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
/*******************************************************************************
 * Copyright (c) 2010-2017, 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.tuple;

import java.util.Objects;

/**
 * @author Gabor Bergmann
 * @since 1.7
 */
public final class LeftInheritanceTuple4 extends BaseLeftInheritanceTuple {
    private final Object localElement0;
    private final Object localElement1;
    private final Object localElement2;
    private final Object localElement3;

    protected LeftInheritanceTuple4(Tuple ancestor, Object localElement0, Object localElement1, Object localElement2,
            Object localElement3) {
        super(ancestor);
        this.localElement0 = localElement0;
        this.localElement1 = localElement1;
        this.localElement2 = localElement2;
        this.localElement3 = localElement3;
        calcHash();
    }

    @Override
    public int getLocalSize() {
        return 4;
    }
    
    @Override
    public int getSize() {
        return inheritedIndex + 4;
    }

    @Override
    public Object get(int index) {
        int local = index - inheritedIndex;
        if (local < 0) 
            return ancestor.get(index);
        else if (local == 0) return localElement0;
        else if (local == 1) return localElement1;
        else if (local == 2) return localElement2;
        else if (local == 3) return localElement3;
        else throw raiseIndexingError(index);
    }

    /**
     * Optimized hash calculation
     */
    @Override
    void calcHash() {
        final int PRIME = 31;
        cachedHash = ancestor.hashCode();
        cachedHash = PRIME * cachedHash;
        if (localElement0 != null) cachedHash += localElement0.hashCode();
        cachedHash = PRIME * cachedHash;
        if (localElement1 != null) cachedHash += localElement1.hashCode();
        cachedHash = PRIME * cachedHash;
        if (localElement2 != null) cachedHash += localElement2.hashCode();
        cachedHash = PRIME * cachedHash;
        if (localElement3 != null) cachedHash += localElement3.hashCode();
    }

    @Override
    protected boolean localEquals(BaseLeftInheritanceTuple other) {
        if (other instanceof LeftInheritanceTuple4) {
            LeftInheritanceTuple4 lit = (LeftInheritanceTuple4)other;
            return Objects.equals(this.localElement0, lit.localElement0) &&
                    Objects.equals(this.localElement1, lit.localElement1) &&
                    Objects.equals(this.localElement2, lit.localElement2) &&
                    Objects.equals(this.localElement3, lit.localElement3);
        } else {
            return (4 == other.getLocalSize()) && 
                    Objects.equals(localElement0, other.get(inheritedIndex)) && 
                    Objects.equals(localElement1, other.get(inheritedIndex + 1)) && 
                    Objects.equals(localElement2, other.get(inheritedIndex + 2)) && 
                    Objects.equals(localElement3, other.get(inheritedIndex + 3));
        }
    }
}