aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/util/Preconditions.java
blob: e9e5e3a00ac9ad0349b14b09a4b790fddfdcefed (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*******************************************************************************
 * Copyright (c) 2010-2018, Zoltan Ujhelyi, IncQuery Labs 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.util;

import java.util.function.Supplier;

/**
 * This class was motivated by the similar Preconditions class from Guava to provide simple precondition checking
 * functionality. However, as starting with version 2.0 the runtime of VIATRA Query should not depend on Guava, the
 * relevant functionality of the Preconditions checking functionality will be implemented here.
 * 
 * @author Zoltan Ujhelyi
 * @since 2.0
 *
 */
public final class Preconditions {

    private Preconditions() {
        /* Utility class constructor */ }

    /**
     * Ensures the truth of an expression involving one or more parameters to the calling method.
     *
     * @param expression
     *            a boolean expression
     * @throws IllegalArgumentException
     *             if {@code expression} is false
     */
    public static void checkArgument(boolean expression) {
        if (!expression) {
            throw new IllegalArgumentException();
        }
    }
    
    /**
     * Ensures the truth of an expression involving one or more parameters to the calling method.
     *
     * @param expression
     *            a boolean expression
     * @param errorMessage
     *            the exception message to use if the check fails
     * @throws IllegalArgumentException
     *             if {@code expression} is false
     */
    public static void checkArgument(boolean expression, String errorMessage) {
        if (!expression) {
            throw new IllegalArgumentException(errorMessage);
        }
    }

    /**
     * Ensures the truth of an expression involving one or more parameters to the calling method.
     *
     * @param expression
     *            a boolean expression
     * @param errorMessageTemplate
     *            a template for the exception message should the check fail using the Java Formatter syntax; the same
     *            as used by {@link String#format(String, Object...)}.
     * @param errorMessageArgs
     *            the arguments to be substituted into the message template.
     * @throws IllegalArgumentException
     *             if {@code expression} is false
     * @throws NullPointerException
     *             if the check fails and either {@code errorMessageTemplate} or {@code errorMessageArgs} is null (don't
     *             let this happen)
     */
    public static void checkArgument(boolean expression, String errorMessageTemplate, Object... errorMessageArgs) {
        if (!expression) {
            throw new IllegalArgumentException(String.format(errorMessageTemplate, errorMessageArgs));
        }
    }
    
    /**
     * Ensures the truth of an expression involving one or more parameters to the calling method.
     *
     * @param expression
     *            a boolean expression
     * @param messageSupplier a supplier that is called to calculate the error message if necessary
     * @throws IllegalArgumentException
     *             if {@code expression} is false
     */
    public static void checkArgument(boolean expression, Supplier<String> messageSupplier) {
        if (!expression) {
            throw new IllegalArgumentException(messageSupplier.get());
        }
    }
    
    /**
     * Ensures the truth of an expression involving one or more fields of a class.
     *
     * @param expression
     *            a boolean expression
     * @throws IllegalStateException
     *             if {@code expression} is false
     */
    public static void checkState(boolean expression) {
        if (!expression) {
            throw new IllegalStateException();
        }
    }
    
    /**
     * Ensures the truth of an expression involving one or more fields of a class.
     *
     * @param expression
     *            a boolean expression
     * @param errorMessage
     *            the exception message to use if the check fails
     * @throws IllegalStateException
     *             if {@code expression} is false
     */
    public static void checkState(boolean expression, String errorMessage) {
        if (!expression) {
            throw new IllegalStateException(errorMessage);
        }
    }

    /**
     * Ensures the truth of an expression involving one or more fields of a class.
     *
     * @param expression
     *            a boolean expression
     * @param errorMessageTemplate
     *            a template for the exception message should the check fail using the Java Formatter syntax; the same
     *            as used by {@link String#format(String, Object...)}.
     * @param errorMessageArgs
     *            the arguments to be substituted into the message template.
     * @throws IllegalStateException
     *             if {@code expression} is false
     * @throws NullPointerException
     *             if the check fails and either {@code errorMessageTemplate} or {@code errorMessageArgs} is null (don't
     *             let this happen)
     */
    public static void checkState(boolean expression, String errorMessageTemplate, Object... errorMessageArgs) {
        if (!expression) {
            throw new IllegalStateException(String.format(errorMessageTemplate, errorMessageArgs));
        }
    }
    
    /**
     * Ensures the truth of an expression involving one or more fields of a class.
     *
     * @param expression
     *            a boolean expression
     * @param messageSupplier a supplier that is called to calculate the error message if necessary
     * @throws IllegalStateException
     *             if {@code expression} is false
     */
    public static void checkState(boolean expression, Supplier<String> messageSupplier) {
        if (!expression) {
            throw new IllegalStateException(messageSupplier.get());
        }
    }
    
    /**
     * Ensures that an index is appropriate for a list or array of given size.
     * 
     * @param index
     * @param size
     * @throws IndexOutOfBoundsException
     *             if index is negative or is greater or equal to size
     */
    public static void checkElementIndex(int index, int size) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException();
        }
    }
    
    /**
     * Ensures that an index is appropriate for a list or array of given size.
     * 
     * @param index
     * @param size
     * @param errorMessageTemplate
     *            a template for the exception message should the check fail using the Java Formatter syntax; the same
     *            as used by {@link String#format(String, Object...)}.
     * @param errorMessageArgs
     *            the arguments to be substituted into the message template.
     * @throws IndexOutOfBoundsException
     *             if index is negative or is greater or equal to size
     */
    public static void checkElementIndex(int index, int size, String errorMessageTemplate, Object... errorMessageArgs) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException(String.format(errorMessageTemplate, errorMessageArgs));
        }
    }
    
    /**
     * Ensures that an index is appropriate for a list or array of given size.
     * 
     * @param index
     * @param size
     * @param messageSupplier a supplier that is called to calculate the error message if necessary
     * @throws IndexOutOfBoundsException
     *             if index is negative or is greater or equal to size
     */
    public static void checkElementIndex(int index, int size, Supplier<String> messageSupplier) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException(messageSupplier.get());
        }
    }
}