diff options
Diffstat (limited to 'subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/util/Preconditions.java')
-rw-r--r-- | subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/util/Preconditions.java | 208 |
1 files changed, 208 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/util/Preconditions.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/util/Preconditions.java new file mode 100644 index 00000000..e9e5e3a0 --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/util/Preconditions.java | |||
@@ -0,0 +1,208 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2018, Zoltan Ujhelyi, IncQuery Labs 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.util; | ||
10 | |||
11 | import java.util.function.Supplier; | ||
12 | |||
13 | /** | ||
14 | * This class was motivated by the similar Preconditions class from Guava to provide simple precondition checking | ||
15 | * functionality. However, as starting with version 2.0 the runtime of VIATRA Query should not depend on Guava, the | ||
16 | * relevant functionality of the Preconditions checking functionality will be implemented here. | ||
17 | * | ||
18 | * @author Zoltan Ujhelyi | ||
19 | * @since 2.0 | ||
20 | * | ||
21 | */ | ||
22 | public final class Preconditions { | ||
23 | |||
24 | private Preconditions() { | ||
25 | /* Utility class constructor */ } | ||
26 | |||
27 | /** | ||
28 | * Ensures the truth of an expression involving one or more parameters to the calling method. | ||
29 | * | ||
30 | * @param expression | ||
31 | * a boolean expression | ||
32 | * @throws IllegalArgumentException | ||
33 | * if {@code expression} is false | ||
34 | */ | ||
35 | public static void checkArgument(boolean expression) { | ||
36 | if (!expression) { | ||
37 | throw new IllegalArgumentException(); | ||
38 | } | ||
39 | } | ||
40 | |||
41 | /** | ||
42 | * Ensures the truth of an expression involving one or more parameters to the calling method. | ||
43 | * | ||
44 | * @param expression | ||
45 | * a boolean expression | ||
46 | * @param errorMessage | ||
47 | * the exception message to use if the check fails | ||
48 | * @throws IllegalArgumentException | ||
49 | * if {@code expression} is false | ||
50 | */ | ||
51 | public static void checkArgument(boolean expression, String errorMessage) { | ||
52 | if (!expression) { | ||
53 | throw new IllegalArgumentException(errorMessage); | ||
54 | } | ||
55 | } | ||
56 | |||
57 | /** | ||
58 | * Ensures the truth of an expression involving one or more parameters to the calling method. | ||
59 | * | ||
60 | * @param expression | ||
61 | * a boolean expression | ||
62 | * @param errorMessageTemplate | ||
63 | * a template for the exception message should the check fail using the Java Formatter syntax; the same | ||
64 | * as used by {@link String#format(String, Object...)}. | ||
65 | * @param errorMessageArgs | ||
66 | * the arguments to be substituted into the message template. | ||
67 | * @throws IllegalArgumentException | ||
68 | * if {@code expression} is false | ||
69 | * @throws NullPointerException | ||
70 | * if the check fails and either {@code errorMessageTemplate} or {@code errorMessageArgs} is null (don't | ||
71 | * let this happen) | ||
72 | */ | ||
73 | public static void checkArgument(boolean expression, String errorMessageTemplate, Object... errorMessageArgs) { | ||
74 | if (!expression) { | ||
75 | throw new IllegalArgumentException(String.format(errorMessageTemplate, errorMessageArgs)); | ||
76 | } | ||
77 | } | ||
78 | |||
79 | /** | ||
80 | * Ensures the truth of an expression involving one or more parameters to the calling method. | ||
81 | * | ||
82 | * @param expression | ||
83 | * a boolean expression | ||
84 | * @param messageSupplier a supplier that is called to calculate the error message if necessary | ||
85 | * @throws IllegalArgumentException | ||
86 | * if {@code expression} is false | ||
87 | */ | ||
88 | public static void checkArgument(boolean expression, Supplier<String> messageSupplier) { | ||
89 | if (!expression) { | ||
90 | throw new IllegalArgumentException(messageSupplier.get()); | ||
91 | } | ||
92 | } | ||
93 | |||
94 | /** | ||
95 | * Ensures the truth of an expression involving one or more fields of a class. | ||
96 | * | ||
97 | * @param expression | ||
98 | * a boolean expression | ||
99 | * @throws IllegalStateException | ||
100 | * if {@code expression} is false | ||
101 | */ | ||
102 | public static void checkState(boolean expression) { | ||
103 | if (!expression) { | ||
104 | throw new IllegalStateException(); | ||
105 | } | ||
106 | } | ||
107 | |||
108 | /** | ||
109 | * Ensures the truth of an expression involving one or more fields of a class. | ||
110 | * | ||
111 | * @param expression | ||
112 | * a boolean expression | ||
113 | * @param errorMessage | ||
114 | * the exception message to use if the check fails | ||
115 | * @throws IllegalStateException | ||
116 | * if {@code expression} is false | ||
117 | */ | ||
118 | public static void checkState(boolean expression, String errorMessage) { | ||
119 | if (!expression) { | ||
120 | throw new IllegalStateException(errorMessage); | ||
121 | } | ||
122 | } | ||
123 | |||
124 | /** | ||
125 | * Ensures the truth of an expression involving one or more fields of a class. | ||
126 | * | ||
127 | * @param expression | ||
128 | * a boolean expression | ||
129 | * @param errorMessageTemplate | ||
130 | * a template for the exception message should the check fail using the Java Formatter syntax; the same | ||
131 | * as used by {@link String#format(String, Object...)}. | ||
132 | * @param errorMessageArgs | ||
133 | * the arguments to be substituted into the message template. | ||
134 | * @throws IllegalStateException | ||
135 | * if {@code expression} is false | ||
136 | * @throws NullPointerException | ||
137 | * if the check fails and either {@code errorMessageTemplate} or {@code errorMessageArgs} is null (don't | ||
138 | * let this happen) | ||
139 | */ | ||
140 | public static void checkState(boolean expression, String errorMessageTemplate, Object... errorMessageArgs) { | ||
141 | if (!expression) { | ||
142 | throw new IllegalStateException(String.format(errorMessageTemplate, errorMessageArgs)); | ||
143 | } | ||
144 | } | ||
145 | |||
146 | /** | ||
147 | * Ensures the truth of an expression involving one or more fields of a class. | ||
148 | * | ||
149 | * @param expression | ||
150 | * a boolean expression | ||
151 | * @param messageSupplier a supplier that is called to calculate the error message if necessary | ||
152 | * @throws IllegalStateException | ||
153 | * if {@code expression} is false | ||
154 | */ | ||
155 | public static void checkState(boolean expression, Supplier<String> messageSupplier) { | ||
156 | if (!expression) { | ||
157 | throw new IllegalStateException(messageSupplier.get()); | ||
158 | } | ||
159 | } | ||
160 | |||
161 | /** | ||
162 | * Ensures that an index is appropriate for a list or array of given size. | ||
163 | * | ||
164 | * @param index | ||
165 | * @param size | ||
166 | * @throws IndexOutOfBoundsException | ||
167 | * if index is negative or is greater or equal to size | ||
168 | */ | ||
169 | public static void checkElementIndex(int index, int size) { | ||
170 | if (index < 0 || index >= size) { | ||
171 | throw new IndexOutOfBoundsException(); | ||
172 | } | ||
173 | } | ||
174 | |||
175 | /** | ||
176 | * Ensures that an index is appropriate for a list or array of given size. | ||
177 | * | ||
178 | * @param index | ||
179 | * @param size | ||
180 | * @param errorMessageTemplate | ||
181 | * a template for the exception message should the check fail using the Java Formatter syntax; the same | ||
182 | * as used by {@link String#format(String, Object...)}. | ||
183 | * @param errorMessageArgs | ||
184 | * the arguments to be substituted into the message template. | ||
185 | * @throws IndexOutOfBoundsException | ||
186 | * if index is negative or is greater or equal to size | ||
187 | */ | ||
188 | public static void checkElementIndex(int index, int size, String errorMessageTemplate, Object... errorMessageArgs) { | ||
189 | if (index < 0 || index >= size) { | ||
190 | throw new IndexOutOfBoundsException(String.format(errorMessageTemplate, errorMessageArgs)); | ||
191 | } | ||
192 | } | ||
193 | |||
194 | /** | ||
195 | * Ensures that an index is appropriate for a list or array of given size. | ||
196 | * | ||
197 | * @param index | ||
198 | * @param size | ||
199 | * @param messageSupplier a supplier that is called to calculate the error message if necessary | ||
200 | * @throws IndexOutOfBoundsException | ||
201 | * if index is negative or is greater or equal to size | ||
202 | */ | ||
203 | public static void checkElementIndex(int index, int size, Supplier<String> messageSupplier) { | ||
204 | if (index < 0 || index >= size) { | ||
205 | throw new IndexOutOfBoundsException(messageSupplier.get()); | ||
206 | } | ||
207 | } | ||
208 | } | ||