|
| 1 | +/* |
| 2 | + * Copyright 2020 The Exonum Team |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.exonum.binding.core.transaction; |
| 18 | + |
| 19 | +import static com.google.common.base.Strings.lenientFormat; |
| 20 | + |
| 21 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 22 | + |
| 23 | +/** |
| 24 | + * Utility methods that helps verifying conditions conducted in expression |
| 25 | + * while transaction execution. |
| 26 | + * If the condition is not met, the {@code ExecutionPreconditions} method |
| 27 | + * throws {@link ExecutionException}. |
| 28 | + * |
| 29 | + * <p>Consider the following example: |
| 30 | + * <pre>{@code |
| 31 | + * void checkEnoughMoney(long balance, long amount) { |
| 32 | + * if(balance < amount) { |
| 33 | + * throw new ExecutionException((byte)3, "Not enough money. Operation amount is " + amount |
| 34 | + * + ", but actual balance was " + balance); |
| 35 | + * } |
| 36 | + * } |
| 37 | + * }</pre> |
| 38 | + * |
| 39 | + * <p>which can be replaced using ExecutionPreconditions: |
| 40 | + * <pre>{@code |
| 41 | + * checkExecution(amount <= balance, (byte)3, |
| 42 | + * "Not enough money. Operation amount is %s, but actual balance was %s", |
| 43 | + * amount, balance); |
| 44 | + * }</pre> |
| 45 | + * |
| 46 | + * @see ExecutionException |
| 47 | + */ |
| 48 | +public final class ExecutionPreconditions { |
| 49 | + |
| 50 | + /** |
| 51 | + * Verifies the truth of the given expression. |
| 52 | + * |
| 53 | + * @param expression a boolean expression |
| 54 | + * @param errorCode execution error code |
| 55 | + * @throws ExecutionException if {@code expression} is false |
| 56 | + */ |
| 57 | + public static void checkExecution(boolean expression, byte errorCode) { |
| 58 | + if (!expression) { |
| 59 | + throw new ExecutionException(errorCode); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Verifies the truth of the given expression. |
| 65 | + * |
| 66 | + * @param expression a boolean expression |
| 67 | + * @param errorCode execution error code |
| 68 | + * @param errorMessage execution error description to use if the check fails |
| 69 | + * @throws ExecutionException if {@code expression} is false |
| 70 | + */ |
| 71 | + public static void checkExecution(boolean expression, byte errorCode, |
| 72 | + @Nullable Object errorMessage) { |
| 73 | + if (!expression) { |
| 74 | + throw new ExecutionException(errorCode, String.valueOf(errorMessage)); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Verifies the truth of the given expression. |
| 80 | + * |
| 81 | + * @param expression a boolean expression |
| 82 | + * @param errorCode execution error code |
| 83 | + * @param errorMessageTemplate execution error description template to use if the check fails. |
| 84 | + * The template could have placeholders {@code %s} which will be replaced by arguments |
| 85 | + * resolved by position |
| 86 | + * @param errorMessageArgs arguments to be used in the template. Each argument will be converted |
| 87 | + * to string using {@link String#valueOf(Object)} |
| 88 | + * @throws ExecutionException if {@code expression} is false |
| 89 | + */ |
| 90 | + public static void checkExecution(boolean expression, byte errorCode, |
| 91 | + @Nullable String errorMessageTemplate, |
| 92 | + @Nullable Object... errorMessageArgs) { |
| 93 | + if (!expression) { |
| 94 | + throw new ExecutionException(errorCode, |
| 95 | + lenientFormat(errorMessageTemplate, errorMessageArgs)); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Verifies the truth of the given expression. |
| 101 | + * |
| 102 | + * <p>See {@link #checkExecution(boolean, byte, String, Object...)} for details. |
| 103 | + */ |
| 104 | + public static void checkExecution(boolean expression, byte errorCode, |
| 105 | + @Nullable String errorMessageTemplate, |
| 106 | + int arg1) { |
| 107 | + if (!expression) { |
| 108 | + throw new ExecutionException(errorCode, lenientFormat(errorMessageTemplate, arg1)); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Verifies the truth of the given expression. |
| 114 | + * |
| 115 | + * <p>See {@link #checkExecution(boolean, byte, String, Object...)} for details. |
| 116 | + */ |
| 117 | + public static void checkExecution(boolean expression, byte errorCode, |
| 118 | + @Nullable String errorMessageTemplate, |
| 119 | + int arg1, int arg2) { |
| 120 | + if (!expression) { |
| 121 | + throw new ExecutionException(errorCode, lenientFormat(errorMessageTemplate, arg1, arg2)); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Verifies the truth of the given expression. |
| 127 | + * |
| 128 | + * <p>See {@link #checkExecution(boolean, byte, String, Object...)} for details. |
| 129 | + */ |
| 130 | + public static void checkExecution(boolean expression, byte errorCode, |
| 131 | + @Nullable String errorMessageTemplate, |
| 132 | + int arg1, long arg2) { |
| 133 | + if (!expression) { |
| 134 | + throw new ExecutionException(errorCode, lenientFormat(errorMessageTemplate, arg1, arg2)); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Verifies the truth of the given expression. |
| 140 | + * |
| 141 | + * <p>See {@link #checkExecution(boolean, byte, String, Object...)} for details. |
| 142 | + */ |
| 143 | + public static void checkExecution(boolean expression, byte errorCode, |
| 144 | + @Nullable String errorMessageTemplate, |
| 145 | + int arg1, @Nullable Object arg2) { |
| 146 | + if (!expression) { |
| 147 | + throw new ExecutionException(errorCode, lenientFormat(errorMessageTemplate, arg1, arg2)); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Verifies the truth of the given expression. |
| 153 | + * |
| 154 | + * <p>See {@link #checkExecution(boolean, byte, String, Object...)} for details. |
| 155 | + */ |
| 156 | + public static void checkExecution(boolean expression, byte errorCode, |
| 157 | + @Nullable String errorMessageTemplate, |
| 158 | + long arg1) { |
| 159 | + if (!expression) { |
| 160 | + throw new ExecutionException(errorCode, lenientFormat(errorMessageTemplate, arg1)); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Verifies the truth of the given expression. |
| 166 | + * |
| 167 | + * <p>See {@link #checkExecution(boolean, byte, String, Object...)} for details. |
| 168 | + */ |
| 169 | + public static void checkExecution(boolean expression, byte errorCode, |
| 170 | + @Nullable String errorMessageTemplate, |
| 171 | + long arg1, int arg2) { |
| 172 | + if (!expression) { |
| 173 | + throw new ExecutionException(errorCode, lenientFormat(errorMessageTemplate, arg1, arg2)); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Verifies the truth of the given expression. |
| 179 | + * |
| 180 | + * <p>See {@link #checkExecution(boolean, byte, String, Object...)} for details. |
| 181 | + */ |
| 182 | + public static void checkExecution(boolean expression, byte errorCode, |
| 183 | + @Nullable String errorMessageTemplate, |
| 184 | + long arg1, long arg2) { |
| 185 | + if (!expression) { |
| 186 | + throw new ExecutionException(errorCode, lenientFormat(errorMessageTemplate, arg1, arg2)); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Verifies the truth of the given expression. |
| 192 | + * |
| 193 | + * <p>See {@link #checkExecution(boolean, byte, String, Object...)} for details. |
| 194 | + */ |
| 195 | + public static void checkExecution(boolean expression, byte errorCode, |
| 196 | + @Nullable String errorMessageTemplate, |
| 197 | + long arg1, @Nullable Object arg2) { |
| 198 | + if (!expression) { |
| 199 | + throw new ExecutionException(errorCode, lenientFormat(errorMessageTemplate, arg1, arg2)); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Verifies the truth of the given expression. |
| 205 | + * |
| 206 | + * <p>See {@link #checkExecution(boolean, byte, String, Object...)} for details. |
| 207 | + */ |
| 208 | + public static void checkExecution(boolean expression, byte errorCode, |
| 209 | + @Nullable String errorMessageTemplate, |
| 210 | + @Nullable Object arg1) { |
| 211 | + if (!expression) { |
| 212 | + throw new ExecutionException(errorCode, lenientFormat(errorMessageTemplate, arg1)); |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * Verifies the truth of the given expression. |
| 218 | + * |
| 219 | + * <p>See {@link #checkExecution(boolean, byte, String, Object...)} for details. |
| 220 | + */ |
| 221 | + public static void checkExecution(boolean expression, byte errorCode, |
| 222 | + @Nullable String errorMessageTemplate, |
| 223 | + @Nullable Object arg1, int arg2) { |
| 224 | + if (!expression) { |
| 225 | + throw new ExecutionException(errorCode, lenientFormat(errorMessageTemplate, arg1, arg2)); |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + /** |
| 230 | + * Verifies the truth of the given expression. |
| 231 | + * |
| 232 | + * <p>See {@link #checkExecution(boolean, byte, String, Object...)} for details. |
| 233 | + */ |
| 234 | + public static void checkExecution(boolean expression, byte errorCode, |
| 235 | + @Nullable String errorMessageTemplate, |
| 236 | + @Nullable Object arg1, long arg2) { |
| 237 | + if (!expression) { |
| 238 | + throw new ExecutionException(errorCode, lenientFormat(errorMessageTemplate, arg1, arg2)); |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + /** |
| 243 | + * Verifies the truth of the given expression. |
| 244 | + * |
| 245 | + * <p>See {@link #checkExecution(boolean, byte, String, Object...)} for details. |
| 246 | + */ |
| 247 | + public static void checkExecution(boolean expression, byte errorCode, |
| 248 | + @Nullable String errorMessageTemplate, |
| 249 | + @Nullable Object arg1, @Nullable Object arg2) { |
| 250 | + if (!expression) { |
| 251 | + throw new ExecutionException(errorCode, lenientFormat(errorMessageTemplate, arg1, arg2)); |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + /** |
| 256 | + * Verifies the truth of the given expression. |
| 257 | + * |
| 258 | + * <p>See {@link #checkExecution(boolean, byte, String, Object...)} for details. |
| 259 | + */ |
| 260 | + public static void checkExecution(boolean expression, byte errorCode, |
| 261 | + @Nullable String errorMessageTemplate, |
| 262 | + @Nullable Object arg1, @Nullable Object arg2, @Nullable Object arg3) { |
| 263 | + if (!expression) { |
| 264 | + throw new ExecutionException(errorCode, |
| 265 | + lenientFormat(errorMessageTemplate, arg1, arg2, arg3)); |
| 266 | + } |
| 267 | + } |
| 268 | + |
| 269 | + private ExecutionPreconditions() { |
| 270 | + } |
| 271 | +} |
0 commit comments