|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.streampark.common.util |
| 19 | + |
| 20 | +import javax.annotation.Nullable |
| 21 | + |
| 22 | +import java.util |
| 23 | +import java.util.{Collection => JavaCollection, Map => JavaMap} |
| 24 | + |
| 25 | +import scala.collection.JavaConversions._ |
| 26 | + |
| 27 | +/** @since 2.1.6 */ |
| 28 | +object AssertUtils { |
| 29 | + |
| 30 | + /** |
| 31 | + * Checks the given boolean condition, and throws an {@code IllegalArgumentException} if the |
| 32 | + * condition is not met (evaluates to {@code false}). |
| 33 | + * |
| 34 | + * @param condition |
| 35 | + * The condition to check |
| 36 | + * @throws IllegalArgumentException |
| 37 | + * Thrown, if the condition is violated. |
| 38 | + */ |
| 39 | + def required(condition: Boolean): Unit = { |
| 40 | + if (!condition) { |
| 41 | + throw new IllegalArgumentException |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Checks the given boolean condition, and throws an {@code IllegalArgumentException} if the |
| 47 | + * condition is not met (evaluates to {@code false}). The exception will have the given error |
| 48 | + * message. |
| 49 | + * |
| 50 | + * @param condition |
| 51 | + * The condition to check |
| 52 | + * @param message |
| 53 | + * The message for the {@code IllegalArgumentException} that is thrown if the check fails. |
| 54 | + * @throws IllegalArgumentException |
| 55 | + * Thrown, if the condition is violated. |
| 56 | + */ |
| 57 | + def required(condition: Boolean, @Nullable message: String): Unit = { |
| 58 | + if (!condition) { |
| 59 | + throw new IllegalArgumentException(message) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Checks the given boolean condition, and throws an {@code IllegalStateException} if the |
| 65 | + * condition is not met (evaluates to {@code false}). |
| 66 | + * |
| 67 | + * @param condition |
| 68 | + * The condition to check |
| 69 | + * @throws IllegalStateException |
| 70 | + * Thrown, if the condition is violated. |
| 71 | + */ |
| 72 | + def state(condition: Boolean): Unit = { |
| 73 | + if (!condition) { |
| 74 | + throw new IllegalStateException |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Checks the given boolean condition, and throws an IllegalStateException if the condition is not |
| 80 | + * met (evaluates to {@code false}). The exception will have the given error message. |
| 81 | + * |
| 82 | + * @param condition |
| 83 | + * The condition to check |
| 84 | + * @param message |
| 85 | + * The message for the IllegalStateException that is thrown if the check fails. |
| 86 | + * @throws IllegalStateException |
| 87 | + * Thrown, if the condition is violated. |
| 88 | + */ |
| 89 | + def state(condition: Boolean, @Nullable message: String): Unit = { |
| 90 | + if (!condition) { |
| 91 | + throw new IllegalStateException(message) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // ------------------------------------------------------------------------ |
| 96 | + // Null checks |
| 97 | + // ------------------------------------------------------------------------ |
| 98 | + /** Ensures that the given object reference is not null. Upon violation, a */ |
| 99 | + def notNull[T](@Nullable reference: T): T = { |
| 100 | + if (reference == null) { |
| 101 | + throw new NullPointerException |
| 102 | + } |
| 103 | + reference |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Ensures that the given object reference is not null. Upon violation, a NullPointerException |
| 108 | + * that is thrown if the check fails. |
| 109 | + * |
| 110 | + * @return |
| 111 | + * The object reference itself (generically typed). |
| 112 | + * @throws NullPointerException |
| 113 | + * Thrown, if the passed reference was null. |
| 114 | + */ |
| 115 | + def notNull[T](@Nullable reference: T, @Nullable message: String): T = { |
| 116 | + if (reference == null) { |
| 117 | + throw new NullPointerException(message) |
| 118 | + } |
| 119 | + reference |
| 120 | + } |
| 121 | + |
| 122 | + def isEmpty(reference: AnyRef): Boolean = !isNotEmpty(reference) |
| 123 | + |
| 124 | + def isNotEmpty(elem: AnyRef): Boolean = { |
| 125 | + elem match { |
| 126 | + case null => false |
| 127 | + case x if x.isInstanceOf[Array[_]] => elem.asInstanceOf[Array[_]].nonEmpty |
| 128 | + case x if x.isInstanceOf[CharSequence] => elem.toString.trim.nonEmpty |
| 129 | + case x if x.isInstanceOf[Traversable[_]] => x.asInstanceOf[Traversable[_]].nonEmpty |
| 130 | + case x if x.isInstanceOf[Iterable[_]] => x.asInstanceOf[Iterable[_]].nonEmpty |
| 131 | + case x if x.isInstanceOf[JavaCollection[_]] => !x.asInstanceOf[JavaCollection[_]].isEmpty |
| 132 | + case x if x.isInstanceOf[JavaMap[_, _]] => !x.asInstanceOf[JavaMap[_, _]].isEmpty |
| 133 | + case _ => true |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Assert that an Array|CharSequence|JavaCollection|Map|Iterable... must not be {@code null} and |
| 139 | + * must contain at least one element. <pre class="code">AssertUtils.notEmpty(array, "must be |
| 140 | + * contain elements");</pre> |
| 141 | + * |
| 142 | + * @param reference |
| 143 | + * the object to check |
| 144 | + * @throws IllegalArgumentException |
| 145 | + * if the object array is {@code null} or contains no elements |
| 146 | + */ |
| 147 | + def notEmpty(reference: AnyRef): Unit = { |
| 148 | + if (isEmpty(reference)) { |
| 149 | + throw new IllegalArgumentException() |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Assert that an Array|CharSequence|JavaCollection|Map|Iterable... must not be {@code null} and |
| 155 | + * must contain at least one element. <pre class="code"> AssertUtils.notEmpty(array, "must be |
| 156 | + * contain elements");</pre> |
| 157 | + * |
| 158 | + * @param reference |
| 159 | + * the object to check |
| 160 | + * @param message |
| 161 | + * the exception message to use if the assertion fails |
| 162 | + * @throws IllegalArgumentException |
| 163 | + * if the object array is {@code null} or contains no elements |
| 164 | + */ |
| 165 | + def notEmpty(@Nullable reference: AnyRef, message: String): Unit = { |
| 166 | + if (isEmpty(reference)) { |
| 167 | + throw new IllegalArgumentException(message) |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Assert that an array contains no {@code null} elements. <p>Note: Does not complain if the array |
| 173 | + * is empty! <pre class="code">AssertUtils.noNullElements(array, "The array must contain non-null |
| 174 | + * elements");</pre> |
| 175 | + * |
| 176 | + * @param array |
| 177 | + * the array to check |
| 178 | + * @param message |
| 179 | + * the exception message to use if the assertion fails |
| 180 | + * @throws IllegalArgumentException |
| 181 | + * if the object array contains a {@code null} element |
| 182 | + */ |
| 183 | + def noNullElements(@Nullable array: Array[AnyRef], message: String): Unit = { |
| 184 | + if (array != null) for (element <- array) { |
| 185 | + if (element == null) throw new IllegalArgumentException(message) |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Assert that a collection contains no {@code null} elements. <p>Note: Does not complain if the |
| 191 | + * collection is empty! <pre class="code">AssertUtils.noNullElements(collection, "Collection must |
| 192 | + * contain non-null elements");</pre> |
| 193 | + * |
| 194 | + * @param collection |
| 195 | + * the collection to check |
| 196 | + * @param message |
| 197 | + * the exception message to use if the assertion fails |
| 198 | + * @throws IllegalArgumentException |
| 199 | + * if the collection contains a {@code null} element |
| 200 | + */ |
| 201 | + def noNullElements(@Nullable collection: util.Collection[_], message: String): Unit = { |
| 202 | + if (collection != null) for (element <- collection) { |
| 203 | + if (element == null) { |
| 204 | + throw new IllegalArgumentException(message) |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * Assert that the given String is not empty; that is, it must not be {@code null} and not the |
| 211 | + * empty String. <pre class="code">AssertUtils.hasLength(name, "Name must not be empty");</pre> |
| 212 | + * |
| 213 | + * @param text |
| 214 | + * the String to check |
| 215 | + * @throws IllegalArgumentException |
| 216 | + * if the text is empty |
| 217 | + * @see |
| 218 | + * StringUtils#hasLength |
| 219 | + */ |
| 220 | + def hasLength(@Nullable text: String): Unit = { |
| 221 | + if (!getHasLength(text)) { |
| 222 | + throw new IllegalArgumentException() |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * Assert that the given String is not empty; that is, it must not be {@code null} and not the |
| 228 | + * empty String. <pre class="code">AssertUtils.hasLength(name, "Name must not be empty");</pre> |
| 229 | + * |
| 230 | + * @param text |
| 231 | + * the String to check |
| 232 | + * @param message |
| 233 | + * the exception message to use if the assertion fails |
| 234 | + * @throws IllegalArgumentException |
| 235 | + * if the text is empty |
| 236 | + * @see |
| 237 | + * StringUtils#hasLength |
| 238 | + */ |
| 239 | + def hasLength(@Nullable text: String, message: String): Unit = { |
| 240 | + if (!getHasLength(text)) { |
| 241 | + throw new IllegalArgumentException(message) |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + /** |
| 246 | + * Assert that the given String contains valid text content; that is, it must not be {@code null} |
| 247 | + * and must contain at least one non-whitespace character. <pre |
| 248 | + * class="code">AssertUtils.hasText(name, "'name' must not be empty");</pre> |
| 249 | + * |
| 250 | + * @param text |
| 251 | + * the String to check |
| 252 | + * @throws IllegalArgumentException |
| 253 | + * if the text does not contain valid text content |
| 254 | + * @see |
| 255 | + * StringUtils#hasText |
| 256 | + */ |
| 257 | + def hasText(@Nullable text: String): Unit = { |
| 258 | + if (!getHasText(text)) { |
| 259 | + throw new IllegalArgumentException() |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + /** |
| 264 | + * Assert that the given String contains valid text content; that is, it must not be {@code null} |
| 265 | + * and must contain at least one non-whitespace character. <pre |
| 266 | + * class="code">AssertUtils.hasText(name, "'name' must not be empty");</pre> |
| 267 | + * |
| 268 | + * @param text |
| 269 | + * the String to check |
| 270 | + * @param message |
| 271 | + * the exception message to use if the assertion fails |
| 272 | + * @throws IllegalArgumentException |
| 273 | + * if the text does not contain valid text content |
| 274 | + * @see |
| 275 | + * StringUtils#hasText |
| 276 | + */ |
| 277 | + def hasText(@Nullable text: String, message: String): Unit = { |
| 278 | + if (!getHasText(text)) { |
| 279 | + throw new IllegalArgumentException(message) |
| 280 | + } |
| 281 | + } |
| 282 | + |
| 283 | + private[this] def getHasLength(@Nullable str: String): Boolean = |
| 284 | + str != null && str.nonEmpty |
| 285 | + |
| 286 | + private[this] def getHasText(@Nullable str: String): Boolean = { |
| 287 | + str != null && str.nonEmpty && containsText(str) |
| 288 | + } |
| 289 | + |
| 290 | + private[this] def containsText(str: CharSequence): Boolean = { |
| 291 | + val strLen = str.length |
| 292 | + for (i <- 0 until strLen) { |
| 293 | + if (!Character.isWhitespace(str.charAt(i))) { |
| 294 | + return true |
| 295 | + } |
| 296 | + } |
| 297 | + false |
| 298 | + } |
| 299 | + |
| 300 | +} |
0 commit comments