|
| 1 | +/* |
| 2 | + * Copyright 2002-2020 the original author or authors. |
| 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 | + * https://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.alibaba.ttl3.internal.util; |
| 18 | + |
| 19 | + |
| 20 | +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 21 | +// |
| 22 | +// This source code file is copied from spring v5.3.24: |
| 23 | +// |
| 24 | +// https://github.com/spring-projects/spring-framework/blob/v5.3.24/spring-core/src/main/java/org/springframework/util/Assert.java |
| 25 | +// |
| 26 | +// with adoption: |
| 27 | +// - remove unused elements |
| 28 | +// - adjust visible modifier and code format |
| 29 | +// |
| 30 | +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 31 | + |
| 32 | +import edu.umd.cs.findbugs.annotations.Nullable; |
| 33 | + |
| 34 | +/** |
| 35 | + * Assertion utility class that assists in validating arguments. |
| 36 | + * |
| 37 | + * <p>Useful for identifying programmer errors early and clearly at runtime. |
| 38 | + * |
| 39 | + * <p>For example, if the contract of a public method states it does not |
| 40 | + * allow {@code null} arguments, {@code Assert} can be used to validate that |
| 41 | + * contract. Doing this clearly indicates a contract violation when it |
| 42 | + * occurs and protects the class's invariants. |
| 43 | + * |
| 44 | + * <p>Typically used to validate method arguments rather than configuration |
| 45 | + * properties, to check for cases that are usually programmer errors rather |
| 46 | + * than configuration errors. In contrast to configuration initialization |
| 47 | + * code, there is usually no point in falling back to defaults in such methods. |
| 48 | + * |
| 49 | + * <p>This class is similar to JUnit's assertion library. If an argument value is |
| 50 | + * deemed invalid, an {@link IllegalArgumentException} is thrown (typically). |
| 51 | + * For example: |
| 52 | + * |
| 53 | + * <pre class="code"> |
| 54 | + * Assert.notNull(clazz, "The class must not be null"); |
| 55 | + * Assert.isTrue(i > 0, "The value must be greater than zero");</pre> |
| 56 | + * |
| 57 | + * <p>Mainly for internal use within the framework; for a more comprehensive suite |
| 58 | + * of assertion utilities consider {@code org.apache.commons.lang3.Validate} from |
| 59 | + * <a href="https://commons.apache.org/proper/commons-lang/">Apache Commons Lang</a>, |
| 60 | + * Google Guava's |
| 61 | + * <a href="https://github.com/google/guava/wiki/PreconditionsExplained">Preconditions</a>, |
| 62 | + * or similar third-party libraries. |
| 63 | + * |
| 64 | + * @author Keith Donald |
| 65 | + * @author Juergen Hoeller |
| 66 | + * @author Sam Brannen |
| 67 | + * @author Colin Sampaleanu |
| 68 | + * @author Rob Harrop |
| 69 | + */ |
| 70 | +final class Assert { |
| 71 | + |
| 72 | + /** |
| 73 | + * Assert a boolean expression, throwing an {@code IllegalStateException} |
| 74 | + * if the expression evaluates to {@code false}. |
| 75 | + * <p>Call {@link #isTrue} if you wish to throw an {@code IllegalArgumentException} |
| 76 | + * on an assertion failure. |
| 77 | + * <pre class="code">Assert.state(id == null, "The id property must not already be initialized");</pre> |
| 78 | + * |
| 79 | + * @param expression a boolean expression |
| 80 | + * @param message the exception message to use if the assertion fails |
| 81 | + * @throws IllegalStateException if {@code expression} is {@code false} |
| 82 | + */ |
| 83 | + public static void state(boolean expression, String message) { |
| 84 | + if (!expression) { |
| 85 | + throw new IllegalStateException(message); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Assert a boolean expression, throwing an {@code IllegalArgumentException} |
| 91 | + * if the expression evaluates to {@code false}. |
| 92 | + * <pre class="code">Assert.isTrue(i > 0, "The value must be greater than zero");</pre> |
| 93 | + * |
| 94 | + * @param expression a boolean expression |
| 95 | + * @param message the exception message to use if the assertion fails |
| 96 | + * @throws IllegalArgumentException if {@code expression} is {@code false} |
| 97 | + */ |
| 98 | + public static void isTrue(boolean expression, String message) { |
| 99 | + if (!expression) { |
| 100 | + throw new IllegalArgumentException(message); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Assert that an object is not {@code null}. |
| 106 | + * <pre class="code">Assert.notNull(clazz, "The class must not be null");</pre> |
| 107 | + * |
| 108 | + * @param object the object to check |
| 109 | + * @param message the exception message to use if the assertion fails |
| 110 | + * @throws IllegalArgumentException if the object is {@code null} |
| 111 | + */ |
| 112 | + public static void notNull(@Nullable Object object, String message) { |
| 113 | + if (object == null) { |
| 114 | + throw new IllegalArgumentException(message); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments