aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/tuple/Tuples.java
blob: 5e41d7d8828ac3457212c58729eb232e718a08c3 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*******************************************************************************
 * 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;

/**
 * Common static factory utilities for tuples.
 * 
 * @author Gabor Bergmann
 * @since 1.7
 */
public class Tuples {

    private Tuples() {
        // Empty utility class constructor
    }
    
    /**
     * Creates a flat tuple consisting of the given elements. 
     * For low-arity tuples, specialized implementations 
     * (such as {@link FlatTuple2}) will be instantiated.
     * 
     * <p> In case the exact arity is <i>statically</i> known, 
     * it may be more efficient for the client to instantiate 
     * the appropriate specialized implementation 
     * (via {@link #staticArityFlatTupleOf(Object, Object)} etc.
     * or {@link #wideFlatTupleOf(Object...)}),
     * instead of invoking this method. 
     * This method does a runtime arity check, and therefore 
     * also appropriate if the arity is determined at runtime. 
     */
    public static Tuple flatTupleOf(Object... elements) {
        switch (elements.length) {
        case 0:
            return FlatTuple0.INSTANCE;
        case 1:
            return new FlatTuple1(elements[0]);
        case 2:
            return new FlatTuple2(elements[0], elements[1]);
        case 3:
            return new FlatTuple3(elements[0], elements[1], elements[2]);
        case 4:
            return new FlatTuple4(elements[0], elements[1], elements[2], elements[3]);
        default:
            return new FlatTuple(elements);
        }
    }
    /**
     * Creates a left inheritance tuple that extends an ancestor tuple 
     *  by the given "local" elements. 
     * For locally low-arity tuples, specialized implementations 
     * (such as {@link LeftInheritanceTuple2}) will be instantiated.
     * 
     * <p> In case the exact arity is <i>statically</i> known, 
     * it may be more efficient for the client to instantiate 
     * the appropriate specialized implementation 
     * (via {@link #staticArityLeftInheritanceTupleOf(Object, Object)} etc.
     * or {@link #wideLeftInheritanceTupleOf(Object...)}),
     * instead of invoking this method. 
     * This method does a runtime arity check, and therefore 
     * also appropriate if the arity is determined at runtime. 
     */
    public static Tuple leftInheritanceTupleOf(Tuple ancestor, Object... localElements) {
        switch (localElements.length) {
        case 0:
            return ancestor;
        case 1:
            return new LeftInheritanceTuple1(ancestor, localElements[0]);
        case 2:
            return new LeftInheritanceTuple2(ancestor, localElements[0], localElements[1]);
        case 3:
            return new LeftInheritanceTuple3(ancestor, localElements[0], localElements[1], localElements[2]);
        case 4:
            return new LeftInheritanceTuple4(ancestor, localElements[0], localElements[1], localElements[2], localElements[3]);
        default:
            return new LeftInheritanceTuple(ancestor, localElements);
        }
    }

    /**
     * Creates a flat tuple consisting of no elements. 
     */
    public static Tuple staticArityFlatTupleOf() {
        return FlatTuple0.INSTANCE;
    }
    /**
     * Creates a flat tuple consisting of the given single element. 
     */
    public static Tuple staticArityFlatTupleOf(Object element) {
        return new FlatTuple1(element);
    }
    /**
     * Creates a flat tuple consisting of the given elements. 
     */
    public static Tuple staticArityFlatTupleOf(Object element0, Object element1) {
        return new FlatTuple2(element0, element1);
    }
    /**
     * Creates a flat tuple consisting of the given elements. 
     */
    public static Tuple staticArityFlatTupleOf(Object element0, Object element1, Object element2) {
        return new FlatTuple3(element0, element1, element2);
    }
    /**
     * Creates a flat tuple consisting of the given elements. 
     */
    public static Tuple staticArityFlatTupleOf(Object element0, Object element1, Object element2, Object element3) {
        return new FlatTuple4(element0, element1, element2, element3);
    }
    /**
     * Creates a flat tuple consisting of the given elements. 
     * <p> Invoke this only if it is statically known that the tuple will be wide. 
     * Otherwise, use {@link #flatTupleOf(Object...)}.
     */
    public static Tuple wideFlatTupleOf(Object... elements) {
        return new FlatTuple(elements);
    }
    
    /**
     * Creates a left inheritance tuple consisting of the given single local element. 
     */
    public static Tuple staticArityLeftInheritanceTupleOf(Tuple ancestor, Object element) {
        return new LeftInheritanceTuple1(ancestor, element);
    }
    /**
     * Creates a left inheritance tuple consisting of the given local elements. 
     */
    public static Tuple staticArityLeftInheritanceTupleOf(Tuple ancestor, Object element0, Object element1) {
        return new LeftInheritanceTuple2(ancestor, element0, element1);
    }
    /**
     * Creates a left inheritance tuple consisting of the given local elements. 
     */
    public static Tuple staticArityLeftInheritanceTupleOf(Tuple ancestor, Object element0, Object element1, Object element2) {
        return new LeftInheritanceTuple3(ancestor, element0, element1, element2);
    }
    /**
     * Creates a left inheritance tuple consisting of the given local elements. 
     */
    public static Tuple staticArityLeftInheritanceTupleOf(Tuple ancestor, Object element0, Object element1, Object element2, Object element3) {
        return new LeftInheritanceTuple4(ancestor, element0, element1, element2, element3);
    }
    /**
     * Creates a left inheritance tuple consisting of the given local elements. 
     * <p> Invoke this only if it is statically known that the tuple will be wide. 
     * Otherwise, use {@link #leftInheritanceTupleOf(Tuple, Object...)}.
     */
    public static Tuple wideLeftInheritanceTupleOf(Tuple ancestor, Object... elements) {
        return new LeftInheritanceTuple(ancestor, elements);
    }
    
}