|
| 1 | +/* Copyright (C) 2008 The Guava Authors |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | +package com.github.dockerjava; |
| 16 | + |
| 17 | + |
| 18 | +public final class Preconditions { |
| 19 | + private Preconditions() {} |
| 20 | + |
| 21 | + /** |
| 22 | + * Ensures the truth of an expression involving one or more parameters to the calling method. |
| 23 | + * |
| 24 | + * @param expression a boolean expression |
| 25 | + * @param errorMessageTemplate a template for the exception message should the check fail. The |
| 26 | + * message is formed by replacing each {@code %s} placeholder in the template with an |
| 27 | + * argument. These are matched by position - the first {@code %s} gets {@code |
| 28 | + * errorMessageArgs[0]}, etc. Unmatched arguments will be appended to the formatted message |
| 29 | + * in square braces. Unmatched placeholders will be left as-is. |
| 30 | + * @param errorMessageArgs the arguments to be substituted into the message template. Arguments |
| 31 | + * are converted to strings using {@link String#valueOf(Object)}. |
| 32 | + * @throws IllegalArgumentException if {@code expression} is false |
| 33 | + * @throws NullPointerException if the check fails and either {@code errorMessageTemplate} or |
| 34 | + * {@code errorMessageArgs} is null (don't let this happen) |
| 35 | + */ |
| 36 | + public static void checkArgument(boolean expression, |
| 37 | + String errorMessageTemplate, |
| 38 | + Object... errorMessageArgs) { |
| 39 | + if (!expression) { |
| 40 | + throw new IllegalArgumentException(Preconditions.format(errorMessageTemplate, errorMessageArgs)); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Ensures that an object reference passed as a parameter to the calling method is not null. |
| 46 | + * |
| 47 | + * @param reference an object reference |
| 48 | + * @param errorMessage the exception message to use if the check fails; will be converted to a |
| 49 | + * string using {@link String#valueOf(Object)} |
| 50 | + * @return the non-null reference that was validated |
| 51 | + * @throws NullPointerException if {@code reference} is null |
| 52 | + */ |
| 53 | + public static <T> T checkNotNull(T reference, Object errorMessage) { |
| 54 | + if (reference == null) { |
| 55 | + throw new NullPointerException(String.valueOf(errorMessage)); |
| 56 | + } |
| 57 | + return reference; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Ensures the truth of an expression involving one or more parameters to the calling method. |
| 62 | + * |
| 63 | + * @param expression a boolean expression |
| 64 | + * @param errorMessage the exception message to use if the check fails; will be converted to a |
| 65 | + * string using {@link String#valueOf(Object)} |
| 66 | + * @throws IllegalArgumentException if {@code expression} is false |
| 67 | + */ |
| 68 | + public static void checkArgument(boolean expression, Object errorMessage) { |
| 69 | + if (!expression) { |
| 70 | + throw new IllegalArgumentException(String.valueOf(errorMessage)); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Ensures that an object reference passed as a parameter to the calling method is not null. |
| 76 | + * |
| 77 | + * @param reference an object reference |
| 78 | + * @return the non-null reference that was validated |
| 79 | + * @throws NullPointerException if {@code reference} is null |
| 80 | + */ |
| 81 | + public static <T> T checkNotNull(T reference) { |
| 82 | + if (reference == null) { |
| 83 | + throw new NullPointerException(); |
| 84 | + } |
| 85 | + return reference; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Ensures the truth of an expression involving the state of the calling instance, but not |
| 90 | + * involving any parameters to the calling method. |
| 91 | + * |
| 92 | + * @param expression a boolean expression |
| 93 | + * @param errorMessage the exception message to use if the check fails; will be converted to a |
| 94 | + * string using {@link String#valueOf(Object)} |
| 95 | + * @throws IllegalStateException if {@code expression} is false |
| 96 | + */ |
| 97 | + public static void checkState(boolean expression, Object errorMessage) { |
| 98 | + if (!expression) { |
| 99 | + throw new IllegalStateException(String.valueOf(errorMessage)); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Substitutes each {@code %s} in {@code template} with an argument. These are matched by |
| 105 | + * position: the first {@code %s} gets {@code args[0]}, etc. If there are more arguments than |
| 106 | + * placeholders, the unmatched arguments will be appended to the end of the formatted message in |
| 107 | + * square braces. |
| 108 | + * |
| 109 | + * @param template a non-null string containing 0 or more {@code %s} placeholders. |
| 110 | + * @param args the arguments to be substituted into the message template. Arguments are converted |
| 111 | + * to strings using {@link String#valueOf(Object)}. Arguments can be null. |
| 112 | + */ |
| 113 | + // Note that this is somewhat-improperly used from Verify.java as well. |
| 114 | + public static String format(String template, Object... args) { |
| 115 | + template = String.valueOf(template); // null -> "null" |
| 116 | + |
| 117 | + // start substituting the arguments into the '%s' placeholders |
| 118 | + StringBuilder builder = new StringBuilder(template.length() + 16 * args.length); |
| 119 | + int templateStart = 0; |
| 120 | + int i = 0; |
| 121 | + while (i < args.length) { |
| 122 | + int placeholderStart = template.indexOf("%s", templateStart); |
| 123 | + if (placeholderStart == -1) { |
| 124 | + break; |
| 125 | + } |
| 126 | + builder.append(template.substring(templateStart, placeholderStart)); |
| 127 | + builder.append(args[i++]); |
| 128 | + templateStart = placeholderStart + 2; |
| 129 | + } |
| 130 | + builder.append(template.substring(templateStart)); |
| 131 | + |
| 132 | + // if we run out of placeholders, append the extra args in square braces |
| 133 | + if (i < args.length) { |
| 134 | + builder.append(" ["); |
| 135 | + builder.append(args[i++]); |
| 136 | + while (i < args.length) { |
| 137 | + builder.append(", "); |
| 138 | + builder.append(args[i++]); |
| 139 | + } |
| 140 | + builder.append(']'); |
| 141 | + } |
| 142 | + |
| 143 | + return builder.toString(); |
| 144 | + } |
| 145 | +} |
0 commit comments