001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.commons.utils; 018 019import java.io.*; 020import java.lang.reflect.*; 021import java.util.*; 022import java.util.function.*; 023 024import org.apache.juneau.commons.collections.*; 025import org.apache.juneau.commons.function.*; 026import org.apache.juneau.commons.reflect.*; 027 028/** 029 * Terse alias facade for the domain utility classes. 030 * 031 * <p>A single wildcard static import 032 * ({@code import static org.apache.juneau.commons.utils.Shorts.*;}) exposes the entire 033 * short-name vocabulary. Almost every method here is a one-line delegation to a canonical, 034 * fully-descriptive method on a domain class — behavior is identical to calling the canonical 035 * method directly — and each such alias documents its target via an {@code @see} tag. Canonical 036 * implementations live in {@link ObjectUtils}, {@link StringUtils}, {@link CollectionUtils}, 037 * {@link ClassUtils}, {@link ThrowableUtils}, {@link IoUtils}, {@link FileUtils}, 038 * {@link PredicateUtils}, {@link SystemUtils}, {@link DateUtils}, and 039 * {@link ReflectionUtils}. 040 * 041 * <p><b>Documented exception:</b> the terse exception factories ({@code rex}/{@code brex}/ 042 * {@code iaex}/{@code isex}/{@code uoex}/{@code uoroex}/{@code ioex}/{@code exex}) are 043 * self-contained — they construct the exception directly rather than delegating to a domain 044 * class — so that the domain {@code *Utils} classes can stay {@code Shorts}-free (they use the 045 * package-private {@code Exceptions} helper instead). These are the only methods in this class 046 * with no {@code @see} tag, apart from the argument/state checks described next. 047 * 048 * <p><b>Argument and state checks:</b> the {@code req*} family ({@code req}, {@code reqnn}, 049 * {@code reqnb}, {@code reqnns}, {@code reqt}, {@code reqcat}; throwing 050 * {@link IllegalArgumentException}) and the {@code chk*} family ({@code chk}, {@code chknn}; 051 * throwing {@link IllegalStateException}) are also self-contained: {@code Shorts} holds their 052 * canonical implementations. Because of that, the domain 053 * {@code *Utils} classes may import {@code Shorts} for {@code req*}/{@code chk*} calls. That is the 054 * only relaxation: a domain class may use the self-contained members of {@code Shorts} (exception 055 * factories, {@code req*}, {@code chk*}) but never a delegating alias, because that alias would 056 * call back into a domain class. 057 * 058 * <h5 class='section'>Example:</h5> 059 * <p class='bjava'> 060 * <jk>import static</jk> org.apache.juneau.commons.utils.Shorts.*; 061 * 062 * <jk>public</jk> Builder timeout(String <jv>name</jv>, Duration <jv>value</jv>) { 063 * <jf>name</jf> = <jsm>reqnb</jsm>(<js>"name"</js>, <jv>name</jv>); 064 * <jf>timeout</jf> = <jsm>reqnn</jsm>(<js>"value"</js>, <jv>value</jv>); 065 * <jsm>req</jsm>(! <jv>value</jv>.isNegative(), <js>"Argument 'value' cannot be negative: %s"</js>, <jv>value</jv>); 066 * <jsm>chk</jsm>(! <jf>built</jf>, <js>"Builder already used."</js>); 067 * <jk>return this</jk>; 068 * } 069 * </p> 070 * 071 * <p><b>Disjointness invariant:</b> no {@code Shorts} alias name is identical to the canonical 072 * (full) method name of any method in any domain class. This lets a developer wildcard-import 073 * {@code Shorts.*} alongside any single domain {@code *Utils.*} with zero method-name 074 * collisions. 075 * 076 * <p>This class is the replacement for the former {@code Utils} grab-bag (now deleted): callers 077 * replace {@code import static ...utils.Utils.*} with {@code import static ...utils.Shorts.*}. 078 */ 079@SuppressWarnings({ 080 "java:S107", // The reqnn() and m() overloads take name/value and key/value pairs as positional arguments by design. 081 "java:S1118", // Utility facade with static methods only. 082 "java:S6539", // Monster class; Shorts is intentionally a single terse-alias factory facade, each method tiny and independent 083 "unchecked" // reqt() and reqcat() cast only after the isInstance/isAssignableFrom checks confirm the element type. 084}) 085public class Shorts { 086 087 /** Constructor — this class is meant to be subclassed. */ 088 protected Shorts() {} 089 090 // ---- ObjectUtils ---- 091 092 /** 093 * Returns <jk>true</jk> if the specified object is not null. 094 * 095 * @param o The object to check. 096 * @return <jk>true</jk> if the object is not null. 097 * @see ObjectUtils#isNotNull(Object) 098 */ 099 public static boolean nn(Object o) { return ObjectUtils.isNotNull(o); } 100 101 /** 102 * Returns <jk>true</jk> if the specified value is null. 103 * 104 * @param <T> The value type. 105 * @param v The value to check. 106 * @return <jk>true</jk> if the value is null. 107 * @see ObjectUtils#isNull(Object) 108 */ 109 public static <T> boolean n(T v) { return ObjectUtils.isNull(v); } 110 111 /** 112 * Returns <jk>true</jk> if the specified array is null or any of its elements are null. 113 * 114 * @param v The values to check. 115 * @return <jk>true</jk> if the array is null or contains a null element. 116 * @see ObjectUtils#isNull(Object...) 117 */ 118 public static boolean n(Object...v) { return ObjectUtils.isNull(v); } 119 120 /** 121 * Returns <jk>true</jk> if the two objects are equal (null-safe). 122 * 123 * @param <T> The object type. 124 * @param a The first object. 125 * @param b The second object. 126 * @return <jk>true</jk> if the objects are equal. 127 * @see ObjectUtils#equal(Object,Object) 128 */ 129 public static <T> boolean eq(T a, T b) { return ObjectUtils.equal(a, b); } 130 131 /** 132 * Returns <jk>true</jk> if the two strings are equal, optionally ignoring case. 133 * 134 * @param ci If <jk>true</jk>, comparison is case-insensitive. 135 * @param a The first string. 136 * @param b The second string. 137 * @return <jk>true</jk> if the strings are equal. 138 * @see ObjectUtils#equal(boolean,String,String) 139 */ 140 public static boolean eq(boolean ci, String a, String b) { return ObjectUtils.equal(ci, a, b); } 141 142 /** 143 * Returns <jk>true</jk> if the two objects are equal using the specified equality test. 144 * 145 * @param <T> The first object type. 146 * @param <U> The second object type. 147 * @param a The first object. 148 * @param b The second object. 149 * @param t The equality predicate. 150 * @return <jk>true</jk> if the objects are equal per the predicate. 151 * @see ObjectUtils#equal(Object,Object,BiPredicate) 152 */ 153 public static <T,U> boolean eq(T a, U b, BiPredicate<T,U> t) { return ObjectUtils.equal(a, b, t); } 154 155 /** 156 * Returns <jk>true</jk> if the first object equals any of the remaining objects. 157 * 158 * @param <T> The object type. 159 * @param a The object to look for. 160 * @param b The candidate values. 161 * @return <jk>true</jk> if a match is found. 162 * @see ObjectUtils#equalsAny(Object,Object...) 163 */ 164 @SafeVarargs 165 public static <T> boolean eqa(T a, T...b) { return ObjectUtils.equalsAny(a, b); } 166 167 /** 168 * Returns <jk>true</jk> if the two objects are equal ignoring case (via their string forms). 169 * 170 * @param a The first object. 171 * @param b The second object. 172 * @return <jk>true</jk> if the objects are equal ignoring case. 173 * @see ObjectUtils#equalIgnoreCase(Object,Object) 174 */ 175 public static boolean eqic(Object a, Object b) { return ObjectUtils.equalIgnoreCase(a, b); } 176 177 /** 178 * Returns <jk>true</jk> if the two objects are not equal (null-safe). 179 * 180 * @param <T> The object type. 181 * @param a The first object. 182 * @param b The second object. 183 * @return <jk>true</jk> if the objects are not equal. 184 * @see ObjectUtils#notEqual(Object,Object) 185 */ 186 public static <T> boolean neq(T a, T b) { return ObjectUtils.notEqual(a, b); } 187 188 /** 189 * Returns <jk>true</jk> if the two objects are not equal using the specified equality test. 190 * 191 * @param <T> The first object type. 192 * @param <U> The second object type. 193 * @param a The first object. 194 * @param b The second object. 195 * @param t The equality predicate. 196 * @return <jk>true</jk> if the objects are not equal per the predicate. 197 * @see ObjectUtils#notEqual(Object,Object,BiPredicate) 198 */ 199 public static <T,U> boolean neq(T a, U b, BiPredicate<T,U> t) { return ObjectUtils.notEqual(a, b, t); } 200 201 /** 202 * Compares two objects for order (null-safe). 203 * 204 * @param a The first object. 205 * @param b The second object. 206 * @return A negative, zero, or positive integer as {@code a} is less than, equal to, or greater than {@code b}. 207 * @see ObjectUtils#compare(Object,Object) 208 */ 209 public static int cmp(Object a, Object b) { return ObjectUtils.compare(a, b); } 210 211 /** 212 * Returns <jk>true</jk> if the first comparable is less than the second. 213 * 214 * @param <T> The comparable type. 215 * @param a The first value. 216 * @param b The second value. 217 * @return <jk>true</jk> if {@code a < b}. 218 * @see ObjectUtils#lessThan(Comparable,Comparable) 219 */ 220 public static <T extends Comparable<T>> boolean lt(T a, T b) { return ObjectUtils.lessThan(a, b); } 221 222 /** 223 * Returns <jk>true</jk> if the first comparable is less than or equal to the second. 224 * 225 * @param <T> The comparable type. 226 * @param a The first value. 227 * @param b The second value. 228 * @return <jk>true</jk> if {@code a <= b}. 229 * @see ObjectUtils#lessThanOrEqual(Comparable,Comparable) 230 */ 231 public static <T extends Comparable<T>> boolean lte(T a, T b) { return ObjectUtils.lessThanOrEqual(a, b); } 232 233 /** 234 * Returns <jk>true</jk> if the first comparable is greater than the second. 235 * 236 * @param <T> The comparable type. 237 * @param a The first value. 238 * @param b The second value. 239 * @return <jk>true</jk> if {@code a > b}. 240 * @see ObjectUtils#greaterThan(Comparable,Comparable) 241 */ 242 public static <T extends Comparable<T>> boolean gt(T a, T b) { return ObjectUtils.greaterThan(a, b); } 243 244 /** 245 * Returns <jk>true</jk> if the first comparable is greater than or equal to the second. 246 * 247 * @param <T> The comparable type. 248 * @param a The first value. 249 * @param b The second value. 250 * @return <jk>true</jk> if {@code a >= b}. 251 * @see ObjectUtils#greaterThanOrEqual(Comparable,Comparable) 252 */ 253 public static <T extends Comparable<T>> boolean gte(T a, T b) { return ObjectUtils.greaterThanOrEqual(a, b); } 254 255 /** 256 * Returns the first non-null argument, or null if all are null. 257 * 258 * @param <T> The value type. 259 * @param t The candidate values in priority order. 260 * @return The first non-null value, or null. 261 * @see ObjectUtils#coalesce(Object...) 262 */ 263 @SafeVarargs 264 public static <T> T or(T...t) { return ObjectUtils.coalesce(t); } 265 266 /** 267 * Returns <jk>true</jk> if any of the specified booleans are <jk>true</jk>. 268 * 269 * @param v The booleans to check. 270 * @return <jk>true</jk> if at least one value is <jk>true</jk>. 271 * @see ObjectUtils#anyTrue(boolean...) 272 */ 273 public static boolean any(boolean...v) { return ObjectUtils.anyTrue(v); } 274 275 /** 276 * Null-safe map: returns <jk>null</jk> when the value is <jk>null</jk>, else the mapper's result. 277 * 278 * <p> 279 * Replaces <c>x == null ? null : f(x)</c>. This is not {@link #or(Object...)}, which returns its first non-null 280 * argument (so <c>x</c>, not <c>f(x)</c>) and evaluates <c>f(x)</c> even when <c>x</c> is null. 281 * 282 * @param <T> The value type. 283 * @param <R> The result type. 284 * @param v The value to map. Can be <jk>null</jk>. 285 * @param m The mapping function; never called with <jk>null</jk>. 286 * @return The mapped value, or <jk>null</jk>. 287 * @see ObjectUtils#mapOrNull(Object, Function) 288 */ 289 public static <T,R> R nm(T v, Function<? super T,? extends R> m) { return ObjectUtils.mapOrNull(v, m); } 290 291 /** 292 * Returns <jk>true</jk> if all of the specified booleans are <jk>true</jk>. 293 * 294 * @param v The booleans to check. 295 * @return <jk>true</jk> if every value is <jk>true</jk>. 296 * @see ObjectUtils#allTrue(boolean...) 297 */ 298 @SafeVarargs 299 public static boolean and(boolean...v) { return ObjectUtils.allTrue(v); } 300 301 /** 302 * Returns the logical negation of the specified boolean. 303 * 304 * @param v The value to negate. 305 * @return {@code !v}. 306 * @see ObjectUtils#negate(boolean) 307 */ 308 public static boolean not(boolean v) { return ObjectUtils.negate(v); } 309 310 /** 311 * Converts the specified object to a boolean. 312 * 313 * @param v The value to convert. 314 * @return The boolean value. 315 * @see ObjectUtils#toBoolean(Object) 316 */ 317 public static boolean b(Object v) { return ObjectUtils.toBoolean(v); } 318 319 /** 320 * Converts the specified object to its string representation (null-safe). 321 * 322 * @param v The value to stringify. 323 * @return The string form, or null if the value is null. 324 * @see ObjectUtils#stringify(Object) 325 */ 326 public static String s(Object v) { return ObjectUtils.stringify(v); } 327 328 /** 329 * Returns the system identity string of the specified object. 330 * 331 * @param o The object. Can be <jk>null</jk> (returns <jk>null</jk>). 332 * @return The identity string (class name + identity hashcode), or <jk>null</jk> if the object is <jk>null</jk>. 333 * @see ObjectUtils#identity(Object) 334 */ 335 public static String id(Object o) { return ObjectUtils.identity(o); } 336 337 /** 338 * Returns <jk>true</jk> if the specified integer is within the inclusive range. 339 * 340 * @param n The value to test. 341 * @param lo The inclusive lower bound. 342 * @param hi The inclusive upper bound. 343 * @return <jk>true</jk> if {@code lo <= n <= hi}. 344 * @see ObjectUtils#isBetween(int,int,int) 345 */ 346 public static boolean btw(int n, int lo, int hi) { return ObjectUtils.isBetween(n, lo, hi); } 347 348 /** 349 * Returns <jk>true</jk> if the specified number is non-null and not equal to -1. 350 * 351 * @param <T> The number type. 352 * @param v The value to test. 353 * @return <jk>true</jk> if the value is not -1. 354 * @see ObjectUtils#isNotMinusOne(Number) 355 */ 356 public static <T extends Number> boolean nm1(T v) { return ObjectUtils.isNotMinusOne(v); } 357 358 /** 359 * Computes a hash code over the specified values. 360 * 361 * @param v The values to hash. Must not be <jk>null</jk> (a <jk>null</jk> array throws {@link IllegalArgumentException}). 362 * @return The combined hash code. 363 * @see ObjectUtils#hash(Object...) 364 */ 365 public static int h(Object...v) { return ObjectUtils.hash(v); } 366 367 /** 368 * Returns <jk>true</jk> if the specified object is null or empty (string/collection/map/array). 369 * 370 * @param o The object to check. 371 * @return <jk>true</jk> if the object is null or empty. 372 * @see ObjectUtils#isEmpty(Object) 373 */ 374 public static boolean ie(Object o) { return ObjectUtils.isEmpty(o); } 375 376 /** 377 * Returns <jk>true</jk> if the specified object is not null and not empty. 378 * 379 * @param o The object to check. 380 * @return <jk>true</jk> if the object is non-empty. 381 * @see ObjectUtils#isNotEmpty(Object) 382 */ 383 public static boolean ine(Object o) { return ObjectUtils.isNotEmpty(o); } 384 385 /** 386 * Wraps the specified value in an {@link Optional}. 387 * 388 * @param <T> The value type. 389 * @param t The value (may be null). 390 * @return An optional containing the value, or empty if null. 391 * @see ObjectUtils#optional(Object) 392 */ 393 public static <T> Optional<T> o(T t) { return ObjectUtils.optional(t); } 394 395 /** 396 * Returns the element at the specified index of a list as an {@link Optional}. 397 * 398 * @param <T> The element type. 399 * @param l The list. 400 * @param i The index. 401 * @return An optional containing the element, or empty if out of bounds. 402 * @see CollectionUtils#optionalAt(List,int) 403 */ 404 public static <T> Optional<T> o(List<T> l, int i) { return CollectionUtils.optionalAt(l, i); } 405 406 /** 407 * Returns an empty {@link Optional}. 408 * 409 * @param <T> The value type. 410 * @return An empty optional. 411 * @see ObjectUtils#emptyOptional() 412 */ 413 public static <T> Optional<T> oe() { return ObjectUtils.emptyOptional(); } 414 415 /** 416 * Returns the value of the optional, or the specified default if empty. 417 * 418 * @param <T> The value type. 419 * @param o The optional. 420 * @param d The default value. 421 * @return The optional's value, or {@code d} if empty. 422 * @see ObjectUtils#orElse(Optional,Object) 423 */ 424 public static <T> T oo(Optional<T> o, T d) { return ObjectUtils.orElse(o, d); } 425 426 /** 427 * Returns the specified value, throwing {@link NullPointerException} if it is null. 428 * 429 * @param <T> The value type. 430 * @param v The value. 431 * @return The non-null value. 432 * @see ObjectUtils#requireNonNull(Object) 433 */ 434 public static <T> T rnn(T v) { return ObjectUtils.requireNonNull(v); } 435 436 /** 437 * Returns the size of the specified object (string/collection/map/array). 438 * 439 * @param o The object. 440 * @return The size, or 0 if null. 441 * @see ObjectUtils#size(Object) 442 */ 443 public static int sz(Object o) { return ObjectUtils.size(o); } 444 445 // ---- Argument checks (req*) and state checks (chk*) — canonical ---- 446 447 // Self-contained, like the exception factories: these are the canonical implementations, 448 // so they carry no @see. 449 450 /** 451 * Throws an {@link IllegalArgumentException} if the specified expression is <jk>false</jk>. 452 * 453 * <h5 class='section'>Example:</h5> 454 * <p class='bjava'> 455 * <jk>import static</jk> org.apache.juneau.commons.utils.Shorts.*; 456 * 457 * <jk>public</jk> Builder timeout(Duration <jv>value</jv>) { 458 * <jsm>req</jsm>(! <jv>value</jv>.isNegative(), <js>"Argument 'value' cannot be negative: %s"</js>, <jv>value</jv>); 459 * ... 460 * } 461 * </p> 462 * 463 * @param expression The boolean expression to check. 464 * @param msg The exception message format string (see {@link StringUtils#format(String, Object...)}). 465 * @param args The exception message args. 466 * @throws IllegalArgumentException If the expression is <jk>false</jk>. 467 */ 468 public static void req(boolean expression, String msg, Object...args) throws IllegalArgumentException { 469 if (! expression) 470 throw iaex(msg, args); 471 } 472 473 /** 474 * Throws an {@link IllegalArgumentException} if the specified argument is <jk>null</jk>. 475 * 476 * <h5 class='section'>Example:</h5> 477 * <p class='bjava'> 478 * <jk>import static</jk> org.apache.juneau.commons.utils.Shorts.*; 479 * 480 * <jk>public</jk> Builder timeout(Duration <jv>value</jv>) { 481 * <jf>timeout</jf> = <jsm>reqnn</jsm>(<js>"value"</js>, <jv>value</jv>); 482 * <jk>return this</jk>; 483 * } 484 * </p> 485 * 486 * @param <T> The argument data type. 487 * @param name The argument name. 488 * @param o The object to check. 489 * @return The same argument. 490 * @throws IllegalArgumentException If the argument is <jk>null</jk>. 491 */ 492 public static <T> T reqnn(String name, T o) throws IllegalArgumentException { 493 if (o == null) 494 throw iaex("Argument '%s' cannot be null.", name); 495 return o; 496 } 497 498 /** 499 * Throws an {@link IllegalArgumentException} if either of the specified arguments is <jk>null</jk>. 500 * 501 * <p> 502 * Arguments are checked in order, so the exception names the first <jk>null</jk> argument. 503 * 504 * <h5 class='section'>Example:</h5> 505 * <p class='bjava'> 506 * <jk>import static</jk> org.apache.juneau.commons.utils.Shorts.*; 507 * 508 * <jk>public</jk> Foo(String <jv>name</jv>, Object <jv>value</jv>) { 509 * <jsm>reqnn</jsm>(<js>"name"</js>, <jv>name</jv>, <js>"value"</js>, <jv>value</jv>); 510 * ... 511 * } 512 * </p> 513 * 514 * @param name1 The first argument name. 515 * @param o1 The first object to check. 516 * @param name2 The second argument name. 517 * @param o2 The second object to check. 518 * @throws IllegalArgumentException If any argument is <jk>null</jk>. 519 */ 520 public static void reqnn(String name1, Object o1, String name2, Object o2) throws IllegalArgumentException { 521 reqnn(name1, o1); 522 reqnn(name2, o2); 523 } 524 525 /** 526 * Throws an {@link IllegalArgumentException} if any of the three specified arguments is <jk>null</jk>. 527 * 528 * <p> 529 * Arguments are checked in order, so the exception names the first <jk>null</jk> argument. 530 * 531 * <h5 class='section'>Example:</h5> 532 * <p class='bjava'> 533 * <jk>import static</jk> org.apache.juneau.commons.utils.Shorts.*; 534 * 535 * <jsm>reqnn</jsm>(<js>"a"</js>, <jv>a</jv>, <js>"b"</js>, <jv>b</jv>, <js>"c"</js>, <jv>c</jv>); 536 * </p> 537 * 538 * @param name1 The first argument name. 539 * @param o1 The first object to check. 540 * @param name2 The second argument name. 541 * @param o2 The second object to check. 542 * @param name3 The third argument name. 543 * @param o3 The third object to check. 544 * @throws IllegalArgumentException If any argument is <jk>null</jk>. 545 */ 546 public static void reqnn(String name1, Object o1, String name2, Object o2, String name3, Object o3) throws IllegalArgumentException { 547 reqnn(name1, o1); 548 reqnn(name2, o2); 549 reqnn(name3, o3); 550 } 551 552 /** 553 * Throws an {@link IllegalArgumentException} if any of the four specified arguments is <jk>null</jk>. 554 * 555 * <p> 556 * Arguments are checked in order, so the exception names the first <jk>null</jk> argument. 557 * 558 * <h5 class='section'>Example:</h5> 559 * <p class='bjava'> 560 * <jk>import static</jk> org.apache.juneau.commons.utils.Shorts.*; 561 * 562 * <jsm>reqnn</jsm>(<js>"a"</js>, <jv>a</jv>, <js>"b"</js>, <jv>b</jv>, <js>"c"</js>, <jv>c</jv>, <js>"d"</js>, <jv>d</jv>); 563 * </p> 564 * 565 * @param name1 The first argument name. 566 * @param o1 The first object to check. 567 * @param name2 The second argument name. 568 * @param o2 The second object to check. 569 * @param name3 The third argument name. 570 * @param o3 The third object to check. 571 * @param name4 The fourth argument name. 572 * @param o4 The fourth object to check. 573 * @throws IllegalArgumentException If any argument is <jk>null</jk>. 574 */ 575 public static void reqnn(String name1, Object o1, String name2, Object o2, String name3, Object o3, String name4, Object o4) throws IllegalArgumentException { 576 reqnn(name1, o1); 577 reqnn(name2, o2); 578 reqnn(name3, o3); 579 reqnn(name4, o4); 580 } 581 582 /** 583 * Throws an {@link IllegalArgumentException} if any of the five specified arguments is <jk>null</jk>. 584 * 585 * <p> 586 * Arguments are checked in order, so the exception names the first <jk>null</jk> argument. 587 * 588 * <h5 class='section'>Example:</h5> 589 * <p class='bjava'> 590 * <jk>import static</jk> org.apache.juneau.commons.utils.Shorts.*; 591 * 592 * <jsm>reqnn</jsm>(<js>"a"</js>, <jv>a</jv>, <js>"b"</js>, <jv>b</jv>, <js>"c"</js>, <jv>c</jv>, <js>"d"</js>, <jv>d</jv>, <js>"e"</js>, <jv>e</jv>); 593 * </p> 594 * 595 * @param name1 The first argument name. 596 * @param o1 The first object to check. 597 * @param name2 The second argument name. 598 * @param o2 The second object to check. 599 * @param name3 The third argument name. 600 * @param o3 The third object to check. 601 * @param name4 The fourth argument name. 602 * @param o4 The fourth object to check. 603 * @param name5 The fifth argument name. 604 * @param o5 The fifth object to check. 605 * @throws IllegalArgumentException If any argument is <jk>null</jk>. 606 */ 607 public static void reqnn(String name1, Object o1, String name2, Object o2, String name3, Object o3, String name4, Object o4, String name5, Object o5) 608 throws IllegalArgumentException { 609 reqnn(name1, o1); 610 reqnn(name2, o2); 611 reqnn(name3, o3); 612 reqnn(name4, o4); 613 reqnn(name5, o5); 614 } 615 616 /** 617 * Throws an {@link IllegalArgumentException} if the specified string argument is <jk>null</jk> or blank. 618 * 619 * <h5 class='section'>Example:</h5> 620 * <p class='bjava'> 621 * <jk>import static</jk> org.apache.juneau.commons.utils.Shorts.*; 622 * 623 * <jk>public</jk> Builder name(String <jv>value</jv>) { 624 * <jf>name</jf> = <jsm>reqnb</jsm>(<js>"value"</js>, <jv>value</jv>); 625 * <jk>return this</jk>; 626 * } 627 * </p> 628 * 629 * @param name The argument name. 630 * @param o The string to check. 631 * @return The same string. 632 * @throws IllegalArgumentException If the string is <jk>null</jk> or blank. 633 */ 634 public static String reqnb(String name, String o) throws IllegalArgumentException { 635 reqnn(name, o); 636 req(! o.isBlank(), "Argument '%s' cannot be blank.", name); 637 return o; 638 } 639 640 /** 641 * Throws an {@link IllegalArgumentException} if the specified array argument or any of its elements is <jk>null</jk>. 642 * 643 * <h5 class='section'>Example:</h5> 644 * <p class='bjava'> 645 * <jk>import static</jk> org.apache.juneau.commons.utils.Shorts.*; 646 * 647 * <jk>public</jk> Builder tags(String...<jv>values</jv>) { 648 * <jf>tags</jf> = <jsm>l</jsm>(<jsm>reqnns</jsm>(<js>"values"</js>, <jv>values</jv>)); 649 * <jk>return this</jk>; 650 * } 651 * </p> 652 * 653 * @param <T> The element type. 654 * @param name The argument name. 655 * @param o The array to check. 656 * @return The same array. 657 * @throws IllegalArgumentException If the array or any of its elements is <jk>null</jk>. 658 */ 659 public static <T> T[] reqnns(String name, T[] o) throws IllegalArgumentException { 660 reqnn(name, o); 661 for (var i = 0; i < o.length; i++) 662 req(o[i] != null, "Argument '%s' parameter %s cannot be null.", name, i); 663 return o; 664 } 665 666 /** 667 * Throws an {@link IllegalArgumentException} if the specified collection argument or any of its elements is <jk>null</jk>. 668 * 669 * <h5 class='section'>Example:</h5> 670 * <p class='bjava'> 671 * <jk>import static</jk> org.apache.juneau.commons.utils.Shorts.*; 672 * 673 * <jk>public</jk> Builder tags(List<String> <jv>values</jv>) { 674 * <jf>tags</jf> = <jsm>tl</jsm>(<jsm>reqnns</jsm>(<js>"values"</js>, <jv>values</jv>)); 675 * <jk>return this</jk>; 676 * } 677 * </p> 678 * 679 * @param <T> The element type. 680 * @param <C> The collection type. 681 * @param name The argument name. 682 * @param collection The collection to check. 683 * @return The same collection. 684 * @throws IllegalArgumentException If the collection or any of its elements is <jk>null</jk>. 685 */ 686 public static <T,C extends Collection<T>> C reqnns(String name, C collection) throws IllegalArgumentException { 687 reqnn(name, collection); 688 var i = 0; 689 for (var element : collection) 690 req(element != null, "Argument '%s' element at index %s cannot be null.", name, i++); 691 return collection; 692 } 693 694 /** 695 * Throws an {@link IllegalArgumentException} if the specified object is not an instance of the specified type. 696 * 697 * <h5 class='section'>Example:</h5> 698 * <p class='bjava'> 699 * <jk>import static</jk> org.apache.juneau.commons.utils.Shorts.*; 700 * 701 * <jk>public void</jk> setValue(Object <jv>value</jv>) { 702 * String <jv>s</jv> = <jsm>reqt</jsm>(String.<jk>class</jk>, <jv>value</jv>); 703 * ... 704 * } 705 * </p> 706 * 707 * @param <T> The expected type. 708 * @param type The expected class type. Must not be <jk>null</jk> (a <jk>null</jk> type throws {@link IllegalArgumentException}). 709 * @param o The object to check. Must not be <jk>null</jk> (a <jk>null</jk> object throws {@link IllegalArgumentException}). 710 * @return The object cast to the specified type. 711 * @throws IllegalArgumentException If the object is not an instance of the specified type, or if either argument is <jk>null</jk>. 712 */ 713 public static <T> T reqt(Class<T> type, Object o) throws IllegalArgumentException { 714 reqnn("type", type); 715 reqnn("o", o); 716 if (! type.isInstance(o)) 717 throw iaex("Object is not an instance of %s: %s", ClassUtils.className(type), ClassUtils.className(o)); 718 return (T)o; 719 } 720 721 /** 722 * Throws the exception provided by the supplier if the specified object is not an instance of the specified type. 723 * 724 * <h5 class='section'>Example:</h5> 725 * <p class='bjava'> 726 * <jk>import static</jk> org.apache.juneau.commons.utils.Shorts.*; 727 * 728 * <jk>public void</jk> setValue(Object <jv>value</jv>) { 729 * String <jv>s</jv> = <jsm>reqt</jsm>(String.<jk>class</jk>, <jv>value</jv>, () -> <jsm>isex</jsm>(<js>"Invalid value type"</js>)); 730 * ... 731 * } 732 * </p> 733 * 734 * @param <T> The expected type. 735 * @param type The expected class type. Must not be <jk>null</jk> (a <jk>null</jk> type throws {@link IllegalArgumentException}). 736 * @param o The object to check. Must not be <jk>null</jk> (a <jk>null</jk> object throws {@link IllegalArgumentException}). 737 * @param exceptionSupplier The supplier that provides the exception to throw if validation fails. Must not be <jk>null</jk>. 738 * @return The object cast to the specified type. 739 * @throws RuntimeException If the object is not an instance of the specified type (the exception is provided by the supplier). 740 */ 741 @SuppressWarnings({ 742 "java:S112" // Exception type comes from caller's supplier 743 }) 744 public static <T> T reqt(Class<T> type, Object o, Supplier<? extends RuntimeException> exceptionSupplier) { 745 reqnn("type", type); 746 reqnn("o", o); 747 if (! type.isInstance(o)) 748 throw exceptionSupplier.get(); 749 return (T)o; 750 } 751 752 /** 753 * Throws an {@link IllegalArgumentException} if any element of the specified class array is not a subtype of the specified type. 754 * 755 * <h5 class='section'>Example:</h5> 756 * <p class='bjava'> 757 * <jk>import static</jk> org.apache.juneau.commons.utils.Shorts.*; 758 * 759 * <jk>public</jk> Builder serializers(Class<?>...<jv>values</jv>) { 760 * <jf>serializers</jf> = <jsm>reqcat</jsm>(<js>"values"</js>, Serializer.<jk>class</jk>, <jv>values</jv>); 761 * <jk>return this</jk>; 762 * } 763 * </p> 764 * 765 * @param <E> The element type. 766 * @param name The argument name. 767 * @param type The expected parent class. Must not be <jk>null</jk> (unguarded — a <jk>null</jk> type throws {@link NullPointerException}). 768 * @param value The array value being checked. Must not be <jk>null</jk> (unguarded — a <jk>null</jk> array throws {@link NullPointerException}). 769 * @return The value cast to the specified array type. 770 * @throws IllegalArgumentException If any element is not a subtype of the specified type. 771 */ 772 public static <E> Class<E>[] reqcat(String name, Class<E> type, Class<?>[] value) throws IllegalArgumentException { 773 for (var i = 0; i < value.length; i++) 774 if (! type.isAssignableFrom(value[i])) 775 throw iaex("Arg %s did not have arg of type %s at index %s: %s", name, ClassUtils.className(type), i, ClassUtils.className(value[i])); 776 return (Class<E>[])value; 777 } 778 779 /** 780 * Throws an {@link IllegalStateException} if the specified expression is <jk>false</jk>. 781 * 782 * <p> 783 * The state-check counterpart of {@link #req(boolean, String, Object...)}: use it for object or 784 * lifecycle state rather than method arguments. 785 * 786 * <h5 class='section'>Example:</h5> 787 * <p class='bjava'> 788 * <jk>import static</jk> org.apache.juneau.commons.utils.Shorts.*; 789 * 790 * <jk>public void</jk> start() { 791 * <jsm>chk</jsm>(! <jf>started</jf>, <js>"Already started: %s"</js>, <jf>name</jf>); 792 * ... 793 * } 794 * </p> 795 * 796 * @param expression The boolean expression to check. 797 * @param msg The exception message format string (see {@link StringUtils#format(String, Object...)}). 798 * @param args The exception message args. 799 * @throws IllegalStateException If the expression is <jk>false</jk>. 800 */ 801 public static void chk(boolean expression, String msg, Object...args) throws IllegalStateException { 802 if (! expression) 803 throw isex(msg, args); 804 } 805 806 /** 807 * Throws an {@link IllegalStateException} if the specified object is <jk>null</jk>. 808 * 809 * <p> 810 * The state-check counterpart of {@link #reqnn(String, Object)}: use it for object or 811 * lifecycle state rather than method arguments. 812 * 813 * <h5 class='section'>Example:</h5> 814 * <p class='bjava'> 815 * <jk>import static</jk> org.apache.juneau.commons.utils.Shorts.*; 816 * 817 * <jk>public</jk> Connection connection() { 818 * <jk>return</jk> <jsm>chknn</jsm>(<jf>connection</jf>, <js>"Not connected: %s"</js>, <jf>name</jf>); 819 * } 820 * </p> 821 * 822 * @param <T> The object type. 823 * @param o The object to check. Can be <jk>null</jk>. 824 * @param msg The exception message format string (see {@link StringUtils#format(String, Object...)}). 825 * @param args The exception message args. 826 * @return The same object. 827 * @throws IllegalStateException If the object is <jk>null</jk>. 828 */ 829 public static <T> T chknn(T o, String msg, Object...args) throws IllegalStateException { 830 if (o == null) 831 throw isex(msg, args); 832 return o; 833 } 834 835 // ---- StringUtils ---- 836 837 /** 838 * Returns <jk>true</jk> if the specified character sequence is null or empty. 839 * 840 * @param s The sequence to check. 841 * @return <jk>true</jk> if null or zero-length. 842 * @see StringUtils#isEmpty(CharSequence) 843 */ 844 public static boolean ie(CharSequence s) { return StringUtils.isEmpty(s); } 845 846 /** 847 * Returns <jk>true</jk> if the specified character sequence is not null and not empty. 848 * 849 * @param s The sequence to check. 850 * @return <jk>true</jk> if non-empty. 851 * @see StringUtils#isNotEmpty(CharSequence) 852 */ 853 public static boolean ine(CharSequence s) { return StringUtils.isNotEmpty(s); } 854 855 /** 856 * Formats a message using printf-style ({@code %s}/{@code %d}/…) placeholders. 857 * 858 * <p>For MessageFormat-style ({@code {0}}) patterns, use {@link #mf(String,Object...)}. 859 * 860 * @param p The pattern. 861 * @param a The arguments. 862 * @return The formatted string. 863 * @see StringUtils#format(String,Object...) 864 */ 865 public static String f(String p, Object...a) { return StringUtils.format(p, a); } 866 867 /** 868 * Returns a supplier that lazily formats a printf-style message (deferred until {@link Supplier#get()}). 869 * 870 * <p>For MessageFormat-style ({@code {0}}) patterns, use {@link #mfs(String,Object...)}. 871 * 872 * @param p The pattern. 873 * @param a The arguments. 874 * @return A supplier of the formatted string. 875 * @see StringUtils#format(String,Object...) 876 */ 877 public static Supplier<String> fs(String p, Object...a) { return () -> StringUtils.format(p, a); } 878 879 /** 880 * Formats a message using {@link java.text.MessageFormat}-style {0} placeholders. 881 * 882 * <p>MessageFormat counterpart to {@link #f(String,Object...)}, for patterns that genuinely need 883 * MessageFormat semantics (e.g. externally-authored i18n {@code .properties} strings). 884 * 885 * @param p The pattern. 886 * @param a The arguments. 887 * @return The formatted string. 888 * @see StringUtils#mformat(String,Object...) 889 */ 890 public static String mf(String p, Object...a) { return StringUtils.mformat(p, a); } 891 892 /** 893 * Returns a supplier that lazily formats a MessageFormat-style message (deferred until {@link Supplier#get()}). 894 * 895 * @param p The pattern. 896 * @param a The arguments. 897 * @return A supplier of the formatted string. 898 * @see StringUtils#mformat(String,Object...) 899 */ 900 public static Supplier<String> mfs(String p, Object...a) { return () -> StringUtils.mformat(p, a); } 901 902 /** 903 * Returns a human-readable string representation of the specified object. 904 * 905 * @param o The object. Can be <jk>null</jk> (returns <jk>null</jk>). 906 * @return The readable representation, or <jk>null</jk> if the object is <jk>null</jk>. 907 * @see StringUtils#readable(Object) 908 */ 909 public static String r(Object o) { return StringUtils.readable(o); } 910 911 /** 912 * Returns <jk>true</jk> if the specified string is null, empty, or whitespace-only. 913 * 914 * @param s The string to check. 915 * @return <jk>true</jk> if blank. 916 * @see StringUtils#isBlank(CharSequence) 917 */ 918 public static boolean ib(String s) { return StringUtils.isBlank(s); } 919 920 /** 921 * Returns <jk>true</jk> if the specified string contains non-whitespace content. 922 * 923 * @param s The string to check. 924 * @return <jk>true</jk> if not blank. 925 * @see StringUtils#isNotBlank(CharSequence) 926 */ 927 public static boolean inb(String s) { return StringUtils.isNotBlank(s); } 928 929 /** 930 * Converts the specified string to lower case (null-safe). 931 * 932 * @param s The string. 933 * @return The lower-cased string, or null if null. 934 * @see StringUtils#lowerCase(String) 935 */ 936 public static String lc(String s) { return StringUtils.lowerCase(s); } 937 938 /** 939 * Converts the specified string to lower case using {@link Locale#ROOT} (null-safe). 940 * 941 * <p> 942 * Locale-independent — use for parse/match/normalize/key-fold code paths (as opposed to {@link #lc(String)}, 943 * which uses the default locale and is intended for display casing). 944 * 945 * @param s The string. 946 * @return The lower-cased string, or null if null. 947 * @see StringUtils#lowerCaseRoot(String) 948 */ 949 public static String lcr(String s) { return StringUtils.lowerCaseRoot(s); } 950 951 /** 952 * Converts the specified string to upper case (null-safe). 953 * 954 * @param s The string. 955 * @return The upper-cased string, or null if null. 956 * @see StringUtils#upperCase(String) 957 */ 958 public static String uc(String s) { return StringUtils.upperCase(s); } 959 960 /** 961 * Converts the specified string to upper case using {@link Locale#ROOT} (null-safe). 962 * 963 * <p> 964 * Locale-independent — use for parse/match/normalize/key-fold code paths (as opposed to {@link #uc(String)}, 965 * which uses the default locale and is intended for display casing). 966 * 967 * @param s The string. 968 * @return The upper-cased string, or null if null. 969 * @see StringUtils#upperCaseRoot(String) 970 */ 971 public static String ucr(String s) { return StringUtils.upperCaseRoot(s); } 972 973 /** 974 * Trims leading/trailing whitespace from the specified string (null-safe). 975 * 976 * @param s The string. 977 * @return The trimmed string, or null if null. 978 * @see StringUtils#trim(String) 979 */ 980 public static String tr(String s) { return StringUtils.trim(s); } 981 982 /** 983 * Returns <jk>true</jk> if the string starts with the specified prefix (null-safe). 984 * 985 * @param s The string. 986 * @param p The prefix. 987 * @return <jk>true</jk> if it starts with the prefix. 988 * @see StringUtils#startsWith(String,String) 989 */ 990 public static boolean sw(String s, String p) { return StringUtils.startsWith(s, p); } 991 992 /** 993 * Returns <jk>true</jk> if the string ends with the specified suffix (null-safe). 994 * 995 * @param s The string. 996 * @param x The suffix. 997 * @return <jk>true</jk> if it ends with the suffix. 998 * @see StringUtils#endsWith(String,String) 999 */ 1000 public static boolean ew(String s, String x) { return StringUtils.endsWith(s, x); } 1001 1002 /** 1003 * Returns <jk>true</jk> if the string contains the specified substring (null-safe). 1004 * 1005 * @param s The string. 1006 * @param x The substring. 1007 * @return <jk>true</jk> if it contains the substring. 1008 * @see StringUtils#contains(String,String) 1009 */ 1010 public static boolean co(String s, String x) { return StringUtils.contains(s, x); } 1011 1012 /** 1013 * Returns <jk>true</jk> if the two strings are equal ignoring case (null-safe). 1014 * 1015 * @param a The first string. 1016 * @param b The second string. 1017 * @return <jk>true</jk> if equal ignoring case. 1018 * @see StringUtils#equalsIgnoreCase(String,String) 1019 */ 1020 public static boolean eqic(String a, String b) { return StringUtils.equalsIgnoreCase(a, b); } 1021 1022 /** 1023 * Returns <jk>true</jk> if the two strings are not equal ignoring case (null-safe). 1024 * 1025 * @param a The first string. 1026 * @param b The second string. 1027 * @return <jk>true</jk> if not equal ignoring case. 1028 * @see StringUtils#notEqualsIgnoreCase(String,String) 1029 */ 1030 public static boolean neqic(String a, String b) { return StringUtils.notEqualsIgnoreCase(a, b); } 1031 1032 /** 1033 * Returns an empty string if the specified string is null, else the string. 1034 * 1035 * @param s The string. 1036 * @return The string, or an empty string if null. 1037 * @see StringUtils#emptyIfNull(String) 1038 */ 1039 public static String ein(String s) { return StringUtils.emptyIfNull(s); } 1040 1041 /** 1042 * Returns an empty string if the specified object is null, else its string form. 1043 * 1044 * @param o The object. 1045 * @return The string form, or an empty string if null. 1046 * @see StringUtils#emptyIfNull(Object) 1047 */ 1048 public static String ein(Object o) { return StringUtils.emptyIfNull(o); } 1049 1050 /** 1051 * Returns a blank (single-space) string if the specified string is null, else the string. 1052 * 1053 * @param s The string. 1054 * @return The string, or a blank string if null. 1055 * @see StringUtils#blankIfNull(String) 1056 */ 1057 public static String bin(String s) { return StringUtils.blankIfNull(s); } 1058 1059 /** 1060 * Returns null if the specified string is empty, else the string. 1061 * 1062 * @param s The string. 1063 * @return The string, or null if empty. 1064 * @see StringUtils#nullIfEmpty(String) 1065 */ 1066 public static String nie(String s) { return StringUtils.nullIfEmpty(s); } 1067 1068 /** 1069 * Creates a new {@link StringBuilder} initialized with the stringified form of the specified value. 1070 * 1071 * @param v The initial value. Can be <jk>null</jk> (renders as {@code "null"}). 1072 * @return A new string builder. 1073 * @see StringUtils#newStringBuilder(Object) 1074 */ 1075 public static StringBuilder sb(Object v) { return StringUtils.newStringBuilder(v); } 1076 1077 /** 1078 * Creates a new {@link StringBuilder} with the stringified form of each specified value appended in order. 1079 * 1080 * @param v The values to append in order (empty builder for no args). 1081 * @return A new string builder. 1082 * @see StringUtils#newStringBuilder(Object...) 1083 */ 1084 public static StringBuilder sb(Object...v) { return StringUtils.newStringBuilder(v); } 1085 1086 /** 1087 * Returns the fully-qualified class name of the specified object (null-safe). 1088 * 1089 * @param o The object. 1090 * @return The class name, or null if the object is null. 1091 * @see ClassUtils#className(Object) 1092 */ 1093 public static String cn(Object o) { return ClassUtils.className(o); } 1094 1095 /** 1096 * Returns the simple class name of the specified object (null-safe). 1097 * 1098 * @param o The object. 1099 * @return The simple class name, or null if the object is null. 1100 * @see ClassUtils#classNameSimple(Object) 1101 */ 1102 public static String cns(Object o) { return ClassUtils.classNameSimple(o); } 1103 1104 /** 1105 * Returns the qualified simple class name of the specified object (includes enclosing classes). 1106 * 1107 * @param o The object. 1108 * @return The qualified simple class name. 1109 * @see ClassUtils#classNameSimpleQualified(Object) 1110 */ 1111 public static String cnsq(Object o) { return ClassUtils.classNameSimpleQualified(o); } 1112 1113 /** 1114 * Returns the first non-empty string from the specified values. 1115 * 1116 * @param v The candidate strings. 1117 * @return The first non-empty string, or null if all are empty. 1118 * @see StringUtils#firstNonEmpty(String...) 1119 */ 1120 @SafeVarargs 1121 public static String fne(String...v) { return StringUtils.firstNonEmpty(v); } 1122 1123 /** 1124 * Returns the first non-blank string from the specified values. 1125 * 1126 * @param v The candidate strings. 1127 * @return The first non-blank string, or null if all are blank. 1128 * @see StringUtils#firstNonBlank(String...) 1129 */ 1130 @SafeVarargs 1131 public static String fnb(String...v) { return StringUtils.firstNonBlank(v); } 1132 1133 /** 1134 * Concatenates the specified strings. 1135 * 1136 * @param v The strings to join. 1137 * @return The concatenated string. 1138 * @see StringUtils#join(String...) 1139 */ 1140 @SafeVarargs 1141 public static String jn(String...v) { return StringUtils.join(v); } 1142 1143 // ---- CollectionUtils ---- 1144 1145 /** 1146 * Creates an array from the specified elements. 1147 * 1148 * @param <T> The element type. 1149 * @param x The elements. Can be <jk>null</jk> (returned as-is). 1150 * @return A new array, or <jk>null</jk> if the array was <jk>null</jk>. 1151 * @see CollectionUtils#array(Object...) 1152 */ 1153 @SafeVarargs 1154 public static <T> T[] a(T...x) { return CollectionUtils.array(x); } 1155 1156 /** 1157 * Creates a modifiable {@link ArrayList} from the specified elements. 1158 * 1159 * @param <T> The element type. 1160 * @param x The elements. Can be <jk>null</jk> (returns <jk>null</jk>). 1161 * @return A new modifiable list, or <jk>null</jk> if the array was <jk>null</jk>. 1162 * @see CollectionUtils#list(Object...) 1163 */ 1164 @SafeVarargs 1165 public static <T> List<T> l(T...x) { return CollectionUtils.list(x); } 1166 1167 /** 1168 * Creates an empty modifiable map. 1169 * 1170 * @param <K> The key type. 1171 * @param <V> The value type. 1172 * @return An empty modifiable map. 1173 * @see CollectionUtils#map() 1174 */ 1175 public static <K,V> Map<K,V> m() { return CollectionUtils.map(); } 1176 1177 /** 1178 * Creates a modifiable map with 1 entry. 1179 * 1180 * @param <K> The key type. 1181 * @param <V> The value type. 1182 * @param k1 The key. 1183 * @param v1 The value. 1184 * @return A modifiable map. 1185 * @see CollectionUtils#map(Object,Object) 1186 */ 1187 public static <K,V> Map<K,V> m(K k1, V v1) { return CollectionUtils.map(k1, v1); } 1188 1189 /** 1190 * Creates a modifiable map with 2 entries. 1191 * 1192 * @param <K> The key type. 1193 * @param <V> The value type. 1194 * @param k1 The first key. 1195 * @param v1 The first value. 1196 * @param k2 The second key. 1197 * @param v2 The second value. 1198 * @return A modifiable map. 1199 * @see CollectionUtils#map(Object,Object,Object,Object) 1200 */ 1201 public static <K,V> Map<K,V> m(K k1, V v1, K k2, V v2) { return CollectionUtils.map(k1, v1, k2, v2); } 1202 1203 /** 1204 * Creates a modifiable map with 3 entries. 1205 * 1206 * @param <K> The key type. 1207 * @param <V> The value type. 1208 * @param k1 The first key. 1209 * @param v1 The first value. 1210 * @param k2 The second key. 1211 * @param v2 The second value. 1212 * @param k3 The third key. 1213 * @param v3 The third value. 1214 * @return A modifiable map. 1215 * @see CollectionUtils#map(Object,Object,Object,Object,Object,Object) 1216 */ 1217 public static <K,V> Map<K,V> m(K k1, V v1, K k2, V v2, K k3, V v3) { return CollectionUtils.map(k1, v1, k2, v2, k3, v3); } 1218 1219 /** 1220 * Creates a modifiable map with 4 entries (keys/values in alternating order). 1221 * 1222 * @param <K> The key type. 1223 * @param <V> The value type. 1224 * @param k1 Key 1. @param v1 Value 1. @param k2 Key 2. @param v2 Value 2. 1225 * @param k3 Key 3. @param v3 Value 3. @param k4 Key 4. @param v4 Value 4. 1226 * @return A modifiable map. 1227 * @see CollectionUtils#map(Object,Object,Object,Object,Object,Object,Object,Object) 1228 */ 1229 public static <K,V> Map<K,V> m(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { return CollectionUtils.map(k1, v1, k2, v2, k3, v3, k4, v4); } 1230 1231 /** 1232 * Creates a modifiable map with 5 entries (keys/values in alternating order). 1233 * 1234 * @param <K> The key type. 1235 * @param <V> The value type. 1236 * @param k1 Key 1. @param v1 Value 1. @param k2 Key 2. @param v2 Value 2. @param k3 Key 3. @param v3 Value 3. 1237 * @param k4 Key 4. @param v4 Value 4. @param k5 Key 5. @param v5 Value 5. 1238 * @return A modifiable map. 1239 * @see CollectionUtils#map(Object,Object,Object,Object,Object,Object,Object,Object,Object,Object) 1240 */ 1241 public static <K,V> Map<K,V> m(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) { return CollectionUtils.map(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5); } 1242 1243 /** 1244 * Creates a modifiable map with 6 entries (keys/values in alternating order). 1245 * 1246 * @param <K> The key type. 1247 * @param <V> The value type. 1248 * @param k1 Key 1. @param v1 Value 1. @param k2 Key 2. @param v2 Value 2. @param k3 Key 3. @param v3 Value 3. 1249 * @param k4 Key 4. @param v4 Value 4. @param k5 Key 5. @param v5 Value 5. @param k6 Key 6. @param v6 Value 6. 1250 * @return A modifiable map. 1251 * @see CollectionUtils#map(Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object) 1252 */ 1253 public static <K,V> Map<K,V> m(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) { return CollectionUtils.map(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6); } 1254 1255 /** 1256 * Creates a modifiable map with 7 entries (keys/values in alternating order). 1257 * 1258 * @param <K> The key type. 1259 * @param <V> The value type. 1260 * @param k1 Key 1. @param v1 Value 1. @param k2 Key 2. @param v2 Value 2. @param k3 Key 3. @param v3 Value 3. @param k4 Key 4. @param v4 Value 4. 1261 * @param k5 Key 5. @param v5 Value 5. @param k6 Key 6. @param v6 Value 6. @param k7 Key 7. @param v7 Value 7. 1262 * @return A modifiable map. 1263 * @see CollectionUtils#map(Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object) 1264 */ 1265 public static <K,V> Map<K,V> m(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) { return CollectionUtils.map(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7); } 1266 1267 /** 1268 * Creates a modifiable map with 8 entries (keys/values in alternating order). 1269 * 1270 * @param <K> The key type. 1271 * @param <V> The value type. 1272 * @param k1 Key 1. @param v1 Value 1. @param k2 Key 2. @param v2 Value 2. @param k3 Key 3. @param v3 Value 3. @param k4 Key 4. @param v4 Value 4. 1273 * @param k5 Key 5. @param v5 Value 5. @param k6 Key 6. @param v6 Value 6. @param k7 Key 7. @param v7 Value 7. @param k8 Key 8. @param v8 Value 8. 1274 * @return A modifiable map. 1275 * @see CollectionUtils#map(Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object) 1276 */ 1277 public static <K,V> Map<K,V> m(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8) { return CollectionUtils.map(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8); } 1278 1279 /** 1280 * Creates a modifiable map with 9 entries (keys/values in alternating order). 1281 * 1282 * @param <K> The key type. 1283 * @param <V> The value type. 1284 * @param k1 Key 1. @param v1 Value 1. @param k2 Key 2. @param v2 Value 2. @param k3 Key 3. @param v3 Value 3. @param k4 Key 4. @param v4 Value 4. @param k5 Key 5. @param v5 Value 5. 1285 * @param k6 Key 6. @param v6 Value 6. @param k7 Key 7. @param v7 Value 7. @param k8 Key 8. @param v8 Value 8. @param k9 Key 9. @param v9 Value 9. 1286 * @return A modifiable map. 1287 * @see CollectionUtils#map(Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object) 1288 */ 1289 public static <K,V> Map<K,V> m(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9) { return CollectionUtils.map(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9); } 1290 1291 /** 1292 * Creates a modifiable map with 10 entries (keys/values in alternating order). 1293 * 1294 * @param <K> The key type. 1295 * @param <V> The value type. 1296 * @param k1 Key 1. @param v1 Value 1. @param k2 Key 2. @param v2 Value 2. @param k3 Key 3. @param v3 Value 3. @param k4 Key 4. @param v4 Value 4. @param k5 Key 5. @param v5 Value 5. 1297 * @param k6 Key 6. @param v6 Value 6. @param k7 Key 7. @param v7 Value 7. @param k8 Key 8. @param v8 Value 8. @param k9 Key 9. @param v9 Value 9. @param k10 Key 10. @param v10 Value 10. 1298 * @return A modifiable map. 1299 * @see CollectionUtils#map(Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object) 1300 */ 1301 public static <K,V> Map<K,V> m(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10) { return CollectionUtils.map(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9, k10, v10); } 1302 1303 /** 1304 * Returns the element at the specified index of a list (null-safe). 1305 * 1306 * @param <E> The element type. 1307 * @param l The list. 1308 * @param i The index. 1309 * @return The element, or null if out of bounds. 1310 * @see CollectionUtils#elementAt(List,int) 1311 */ 1312 public static <E> E at(List<E> l, int i) { return CollectionUtils.elementAt(l, i); } 1313 1314 /** 1315 * Returns <jk>null</jk> typed as an array of the specified component type. 1316 * 1317 * <p>Intentional <jk>null</jk> return (e.g. for assertion testing). 1318 * 1319 * @param <T> The component type. 1320 * @param t The component class. 1321 * @return <jk>null</jk>. 1322 * @see CollectionUtils#nullArray(Class) 1323 */ 1324 public static <T> T[] na(Class<T> t) { return CollectionUtils.nullArray(t); } 1325 1326 /** 1327 * Returns null if the specified map is empty, else the map. 1328 * 1329 * @param <K> The key type. 1330 * @param <V> The value type. 1331 * @param v The map. 1332 * @return The map, or null if empty. 1333 * @see CollectionUtils#nullIfEmpty(Map) 1334 */ 1335 public static <K,V> Map<K,V> nie(Map<K,V> v) { return CollectionUtils.nullIfEmpty(v); } 1336 1337 /** 1338 * Returns null if the specified list is empty, else the list. 1339 * 1340 * @param <E> The element type. 1341 * @param v The list. 1342 * @return The list, or null if empty. 1343 * @see CollectionUtils#nullIfEmpty(List) 1344 */ 1345 public static <E> List<E> nie(List<E> v) { return CollectionUtils.nullIfEmpty(v); } 1346 1347 /** 1348 * Returns null if the specified set is empty, else the set. 1349 * 1350 * @param <E> The element type. 1351 * @param v The set. 1352 * @return The set, or null if empty. 1353 * @see CollectionUtils#nullIfEmpty(Set) 1354 */ 1355 public static <E> Set<E> nie(Set<E> v) { return CollectionUtils.nullIfEmpty(v); } 1356 1357 /** 1358 * Returns <jk>true</jk> if the specified collection is null or empty. 1359 * 1360 * @param c The collection. 1361 * @return <jk>true</jk> if null or empty. 1362 * @see CollectionUtils#isEmpty(Collection) 1363 */ 1364 public static boolean ie(Collection<?> c) { return CollectionUtils.isEmpty(c); } 1365 1366 /** 1367 * Returns <jk>true</jk> if the specified map is null or empty. 1368 * 1369 * @param m The map. 1370 * @return <jk>true</jk> if null or empty. 1371 * @see CollectionUtils#isEmpty(Map) 1372 */ 1373 public static boolean ie(Map<?,?> m) { return CollectionUtils.isEmpty(m); } 1374 1375 /** 1376 * Returns <jk>true</jk> if the specified collection is not null and not empty. 1377 * 1378 * @param c The collection. 1379 * @return <jk>true</jk> if non-empty. 1380 * @see CollectionUtils#isNotEmpty(Collection) 1381 */ 1382 public static boolean ine(Collection<?> c) { return CollectionUtils.isNotEmpty(c); } 1383 1384 /** 1385 * Returns <jk>true</jk> if the specified map is not null and not empty. 1386 * 1387 * @param m The map. 1388 * @return <jk>true</jk> if non-empty. 1389 * @see CollectionUtils#isNotEmpty(Map) 1390 */ 1391 public static boolean ine(Map<?,?> m) { return CollectionUtils.isNotEmpty(m); } 1392 1393 /** 1394 * Creates a 2-dimensional array from the specified rows. 1395 * 1396 * @param <E> The element type. 1397 * @param v The rows. 1398 * @return A 2-dimensional array. 1399 * @see CollectionUtils#array2d(Object[][]) 1400 */ 1401 @SafeVarargs 1402 public static <E> E[][] a2(E[]...v) { return CollectionUtils.array2d(v); } 1403 1404 /** 1405 * Creates an {@code Object[]} from the specified elements. 1406 * 1407 * @param v The elements. 1408 * @return A new object array. 1409 * @see CollectionUtils#objectArray(Object...) 1410 */ 1411 public static Object[] ao(Object...v) { return CollectionUtils.objectArray(v); } 1412 1413 /** 1414 * Creates a mutable {@link LinkedList} from the specified elements. 1415 * 1416 * @param <T> The element type. 1417 * @param v The elements. 1418 * @return A new linked list. 1419 * @see CollectionUtils#linkedList(Object...) 1420 */ 1421 @SafeVarargs 1422 public static <T> List<T> ll(T...v) { return CollectionUtils.linkedList(v); } 1423 1424 /** 1425 * Creates a mutable {@link HashSet} from the specified elements. 1426 * 1427 * @param <T> The element type. 1428 * @param v The elements. 1429 * @return A new hash set. 1430 * @see CollectionUtils#hashSet(Object...) 1431 */ 1432 @SafeVarargs 1433 public static <T> Set<T> hs(T...v) { return CollectionUtils.hashSet(v); } 1434 1435 /** 1436 * Creates a modifiable, insertion-ordered {@link LinkedHashSet} from the specified elements. 1437 * 1438 * @param <T> The element type. 1439 * @param v The elements. 1440 * @return A new modifiable set. 1441 * @see CollectionUtils#set(Object...) 1442 */ 1443 @SafeVarargs 1444 public static <T> Set<T> st(T...v) { return CollectionUtils.set(v); } 1445 1446 /** 1447 * Creates a sorted {@link TreeSet} from the specified elements (null values filtered out). 1448 * 1449 * @param <E> The element type. 1450 * @param v The elements. 1451 * @return A new sorted set. 1452 * @see CollectionUtils#sortedSet(Object...) 1453 */ 1454 @SafeVarargs 1455 public static <E> SortedSet<E> ss(E...v) { return CollectionUtils.sortedSet(v); } 1456 1457 /** 1458 * Creates a modifiable {@link ArrayList} copy of the specified collection. 1459 * 1460 * @param <E> The element type. 1461 * @param v The collection to copy from. Can be <jk>null</jk> (returns an empty list, not <jk>null</jk>). 1462 * @return A new modifiable list. 1463 * @see CollectionUtils#toList(Collection) 1464 */ 1465 public static <E> List<E> tl(Collection<E> v) { return CollectionUtils.toList(v); } 1466 1467 /** 1468 * Creates a modifiable {@link LinkedHashSet} copy of the specified collection. 1469 * 1470 * @param <E> The element type. 1471 * @param v The collection to copy from. Can be <jk>null</jk> (returns <jk>null</jk>). 1472 * @return A new modifiable set, or <jk>null</jk> if the input was <jk>null</jk>. 1473 * @see CollectionUtils#toSet(Collection) 1474 */ 1475 public static <E> Set<E> ts(Collection<E> v) { return CollectionUtils.toSet(v); } 1476 1477 /** 1478 * Creates a modifiable, empty {@link ArrayList} presized to the specified capacity. 1479 * 1480 * @param <E> The element type. 1481 * @param n The initial capacity. 1482 * @return A new modifiable, empty list. 1483 * @see CollectionUtils#listOfSize(int) 1484 */ 1485 public static <E> List<E> los(int n) { return CollectionUtils.listOfSize(n); } 1486 1487 /** 1488 * Creates an unmodifiable set from the specified elements. Unlike {@link Set#of(Object...)}, 1489 * permits a <jk>null</jk> element and preserves insertion order. 1490 * 1491 * @param <E> The element type. 1492 * @param v The elements. Must not be <jk>null</jk> (a <jk>null</jk> array throws {@link IllegalArgumentException}). 1493 * @return A new unmodifiable set. 1494 * @see CollectionUtils#immutableSet(Object...) 1495 */ 1496 @SafeVarargs 1497 public static <E> Set<E> ist(E...v) { return CollectionUtils.immutableSet(v); } 1498 1499 /** 1500 * Creates an unmodifiable, empty map. 1501 * 1502 * @param <K> The key type. 1503 * @param <V> The value type. 1504 * @return A new unmodifiable empty map. 1505 * @see CollectionUtils#immutableMap() 1506 */ 1507 public static <K,V> Map<K,V> im() { return CollectionUtils.immutableMap(); } 1508 1509 /** 1510 * Creates an unmodifiable map with 1 entry. Unlike {@link Map#of(Object,Object)}, permits a 1511 * <jk>null</jk> key or value and preserves insertion order. Mutating the returned map (e.g. 1512 * <jk>put</jk>/<jk>remove</jk>/<jk>clear</jk>) throws {@link UnsupportedOperationException}. 1513 * 1514 * @param <K> The key type. 1515 * @param <V> The value type. 1516 * @param k1 The key. 1517 * @param v1 The value. 1518 * @return A new unmodifiable map. 1519 * @see CollectionUtils#immutableMap(Object,Object) 1520 */ 1521 public static <K,V> Map<K,V> im(K k1, V v1) { return CollectionUtils.immutableMap(k1, v1); } 1522 1523 /** 1524 * Creates an unmodifiable map with 2 entries. Unlike {@link Map#of(Object,Object,Object,Object)}, permits 1525 * <jk>null</jk> keys/values and preserves insertion order. Mutating the returned map (e.g. 1526 * <jk>put</jk>/<jk>remove</jk>/<jk>clear</jk>) throws {@link UnsupportedOperationException}. 1527 * 1528 * @param <K> The key type. 1529 * @param <V> The value type. 1530 * @param k1 The first key. 1531 * @param v1 The first value. 1532 * @param k2 The second key. 1533 * @param v2 The second value. 1534 * @return A new unmodifiable map. 1535 * @throws IllegalArgumentException if <jv>k1</jv> and <jv>k2</jv> are duplicate keys. 1536 * @see CollectionUtils#immutableMap(Object,Object,Object,Object) 1537 */ 1538 public static <K,V> Map<K,V> im(K k1, V v1, K k2, V v2) { return CollectionUtils.immutableMap(k1, v1, k2, v2); } 1539 1540 /** 1541 * Creates an unmodifiable map with 3 entries. Unlike {@link Map#of(Object,Object,Object,Object,Object,Object)}, 1542 * permits <jk>null</jk> keys/values and preserves insertion order. Mutating the returned map (e.g. 1543 * <jk>put</jk>/<jk>remove</jk>/<jk>clear</jk>) throws {@link UnsupportedOperationException}. 1544 * 1545 * @param <K> The key type. 1546 * @param <V> The value type. 1547 * @param k1 The first key. 1548 * @param v1 The first value. 1549 * @param k2 The second key. 1550 * @param v2 The second value. 1551 * @param k3 The third key. 1552 * @param v3 The third value. 1553 * @return A new unmodifiable map. 1554 * @throws IllegalArgumentException if any of the keys are duplicated. 1555 * @see CollectionUtils#immutableMap(Object,Object,Object,Object,Object,Object) 1556 */ 1557 public static <K,V> Map<K,V> im(K k1, V v1, K k2, V v2, K k3, V v3) { return CollectionUtils.immutableMap(k1, v1, k2, v2, k3, v3); } 1558 1559 /** 1560 * Creates an unmodifiable map with 4 entries (keys/values in alternating order). Unlike 1561 * {@link Map#of(Object,Object,Object,Object,Object,Object,Object,Object)}, permits <jk>null</jk> keys/values 1562 * and preserves insertion order. Mutating the returned map (e.g. <jk>put</jk>/<jk>remove</jk>/<jk>clear</jk>) 1563 * throws {@link UnsupportedOperationException}. 1564 * 1565 * @param <K> The key type. 1566 * @param <V> The value type. 1567 * @param k1 Key 1. @param v1 Value 1. @param k2 Key 2. @param v2 Value 2. 1568 * @param k3 Key 3. @param v3 Value 3. @param k4 Key 4. @param v4 Value 4. 1569 * @return A new unmodifiable map. 1570 * @throws IllegalArgumentException if any of the keys are duplicated. 1571 * @see CollectionUtils#immutableMap(Object,Object,Object,Object,Object,Object,Object,Object) 1572 */ 1573 public static <K,V> Map<K,V> im(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { return CollectionUtils.immutableMap(k1, v1, k2, v2, k3, v3, k4, v4); } 1574 1575 /** 1576 * Creates an unmodifiable map with 5 entries (keys/values in alternating order). Unlike 1577 * {@link Map#of(Object,Object,Object,Object,Object,Object,Object,Object,Object,Object)}, permits <jk>null</jk> 1578 * keys/values and preserves insertion order. Mutating the returned map (e.g. 1579 * <jk>put</jk>/<jk>remove</jk>/<jk>clear</jk>) throws {@link UnsupportedOperationException}. 1580 * 1581 * @param <K> The key type. 1582 * @param <V> The value type. 1583 * @param k1 Key 1. @param v1 Value 1. @param k2 Key 2. @param v2 Value 2. @param k3 Key 3. @param v3 Value 3. 1584 * @param k4 Key 4. @param v4 Value 4. @param k5 Key 5. @param v5 Value 5. 1585 * @return A new unmodifiable map. 1586 * @throws IllegalArgumentException if any of the keys are duplicated. 1587 * @see CollectionUtils#immutableMap(Object,Object,Object,Object,Object,Object,Object,Object,Object,Object) 1588 */ 1589 public static <K,V> Map<K,V> im(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) { return CollectionUtils.immutableMap(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5); } 1590 1591 /** 1592 * Creates an unmodifiable map with 6 entries (keys/values in alternating order). Unlike 1593 * {@link Map#of(Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object)}, permits 1594 * <jk>null</jk> keys/values and preserves insertion order. Mutating the returned map (e.g. 1595 * <jk>put</jk>/<jk>remove</jk>/<jk>clear</jk>) throws {@link UnsupportedOperationException}. 1596 * 1597 * @param <K> The key type. 1598 * @param <V> The value type. 1599 * @param k1 Key 1. @param v1 Value 1. @param k2 Key 2. @param v2 Value 2. @param k3 Key 3. @param v3 Value 3. 1600 * @param k4 Key 4. @param v4 Value 4. @param k5 Key 5. @param v5 Value 5. @param k6 Key 6. @param v6 Value 6. 1601 * @return A new unmodifiable map. 1602 * @throws IllegalArgumentException if any of the keys are duplicated. 1603 * @see CollectionUtils#immutableMap(Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object) 1604 */ 1605 public static <K,V> Map<K,V> im(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) { return CollectionUtils.immutableMap(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6); } 1606 1607 /** 1608 * Creates an unmodifiable map with 7 entries (keys/values in alternating order). Unlike 1609 * {@link Map#of(Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object)}, 1610 * permits <jk>null</jk> keys/values and preserves insertion order. Mutating the returned map (e.g. 1611 * <jk>put</jk>/<jk>remove</jk>/<jk>clear</jk>) throws {@link UnsupportedOperationException}. 1612 * 1613 * @param <K> The key type. 1614 * @param <V> The value type. 1615 * @param k1 Key 1. @param v1 Value 1. @param k2 Key 2. @param v2 Value 2. @param k3 Key 3. @param v3 Value 3. @param k4 Key 4. @param v4 Value 4. 1616 * @param k5 Key 5. @param v5 Value 5. @param k6 Key 6. @param v6 Value 6. @param k7 Key 7. @param v7 Value 7. 1617 * @return A new unmodifiable map. 1618 * @throws IllegalArgumentException if any of the keys are duplicated. 1619 * @see CollectionUtils#immutableMap(Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object) 1620 */ 1621 public static <K,V> Map<K,V> im(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) { return CollectionUtils.immutableMap(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7); } 1622 1623 /** 1624 * Creates an unmodifiable map with 8 entries (keys/values in alternating order). Unlike 1625 * {@link Map#of(Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object)}, 1626 * permits <jk>null</jk> keys/values and preserves insertion order. Mutating the returned map (e.g. 1627 * <jk>put</jk>/<jk>remove</jk>/<jk>clear</jk>) throws {@link UnsupportedOperationException}. 1628 * 1629 * @param <K> The key type. 1630 * @param <V> The value type. 1631 * @param k1 Key 1. @param v1 Value 1. @param k2 Key 2. @param v2 Value 2. @param k3 Key 3. @param v3 Value 3. @param k4 Key 4. @param v4 Value 4. 1632 * @param k5 Key 5. @param v5 Value 5. @param k6 Key 6. @param v6 Value 6. @param k7 Key 7. @param v7 Value 7. @param k8 Key 8. @param v8 Value 8. 1633 * @return A new unmodifiable map. 1634 * @throws IllegalArgumentException if any of the keys are duplicated. 1635 * @see CollectionUtils#immutableMap(Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object) 1636 */ 1637 public static <K,V> Map<K,V> im(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8) { return CollectionUtils.immutableMap(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8); } 1638 1639 /** 1640 * Creates an unmodifiable map with 9 entries (keys/values in alternating order). Unlike 1641 * {@link Map#of(Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object)}, 1642 * permits <jk>null</jk> keys/values and preserves insertion order. Mutating the returned map (e.g. 1643 * <jk>put</jk>/<jk>remove</jk>/<jk>clear</jk>) throws {@link UnsupportedOperationException}. 1644 * 1645 * @param <K> The key type. 1646 * @param <V> The value type. 1647 * @param k1 Key 1. @param v1 Value 1. @param k2 Key 2. @param v2 Value 2. @param k3 Key 3. @param v3 Value 3. @param k4 Key 4. @param v4 Value 4. @param k5 Key 5. @param v5 Value 5. 1648 * @param k6 Key 6. @param v6 Value 6. @param k7 Key 7. @param v7 Value 7. @param k8 Key 8. @param v8 Value 8. @param k9 Key 9. @param v9 Value 9. 1649 * @return A new unmodifiable map. 1650 * @throws IllegalArgumentException if any of the keys are duplicated. 1651 * @see CollectionUtils#immutableMap(Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object) 1652 */ 1653 public static <K,V> Map<K,V> im(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9) { return CollectionUtils.immutableMap(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9); } 1654 1655 /** 1656 * Creates an unmodifiable map with 10 entries (keys/values in alternating order). Unlike 1657 * {@link Map#of(Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object)}, 1658 * permits <jk>null</jk> keys/values and preserves insertion order. Mutating the returned map (e.g. 1659 * <jk>put</jk>/<jk>remove</jk>/<jk>clear</jk>) throws {@link UnsupportedOperationException}. 1660 * 1661 * @param <K> The key type. 1662 * @param <V> The value type. 1663 * @param k1 Key 1. @param v1 Value 1. @param k2 Key 2. @param v2 Value 2. @param k3 Key 3. @param v3 Value 3. @param k4 Key 4. @param v4 Value 4. @param k5 Key 5. @param v5 Value 5. 1664 * @param k6 Key 6. @param v6 Value 6. @param k7 Key 7. @param v7 Value 7. @param k8 Key 8. @param v8 Value 8. @param k9 Key 9. @param v9 Value 9. @param k10 Key 10. @param v10 Value 10. 1665 * @return A new unmodifiable map. 1666 * @throws IllegalArgumentException if any of the keys are duplicated. 1667 * @see CollectionUtils#immutableMap(Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object) 1668 */ 1669 public static <K,V> Map<K,V> im(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10) { return CollectionUtils.immutableMap(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9, k10, v10); } 1670 1671 /** 1672 * Returns an unmodifiable view of the specified list. 1673 * 1674 * @param <T> The element type. 1675 * @param v The list. Can be <jk>null</jk> (returns <jk>null</jk>). 1676 * @return An unmodifiable list, or <jk>null</jk> if the argument was <jk>null</jk>. 1677 * @see CollectionUtils#unmodifiable(List) 1678 */ 1679 public static <T> List<T> u(List<? extends T> v) { return CollectionUtils.unmodifiable(v); } 1680 1681 /** 1682 * Returns an unmodifiable view of the specified map. 1683 * 1684 * @param <K> The key type. 1685 * @param <V> The value type. 1686 * @param v The map. Can be <jk>null</jk> (returns <jk>null</jk>). 1687 * @return An unmodifiable map, or <jk>null</jk> if the argument was <jk>null</jk>. 1688 * @see CollectionUtils#unmodifiable(Map) 1689 */ 1690 public static <K,V> Map<K,V> u(Map<? extends K,? extends V> v) { return CollectionUtils.unmodifiable(v); } 1691 1692 /** 1693 * Returns an unmodifiable view of the specified set. 1694 * 1695 * @param <T> The element type. 1696 * @param v The set. Can be <jk>null</jk> (returns <jk>null</jk>). 1697 * @return An unmodifiable set, or <jk>null</jk> if the argument was <jk>null</jk>. 1698 * @see CollectionUtils#unmodifiable(Set) 1699 */ 1700 public static <T> Set<T> u(Set<? extends T> v) { return CollectionUtils.unmodifiable(v); } 1701 1702 /** 1703 * Returns an unmodifiable view of the specified sorted set. 1704 * 1705 * @param <T> The element type. 1706 * @param v The sorted set. Can be <jk>null</jk> (returns <jk>null</jk>). 1707 * @return An unmodifiable sorted set, or <jk>null</jk> if the argument was <jk>null</jk>. 1708 * @see CollectionUtils#unmodifiable(SortedSet) 1709 */ 1710 public static <T> SortedSet<T> u(SortedSet<T> v) { return CollectionUtils.unmodifiable(v); } 1711 1712 /** 1713 * Returns the length/size of the specified object (array/collection/map/string). 1714 * 1715 * @param v The object. 1716 * @return The length, or 0 if null. 1717 * @see CollectionUtils#length(Object) 1718 */ 1719 public static int len(Object v) { return CollectionUtils.length(v); } 1720 1721 /** 1722 * Adds the specified elements to the given list and returns it. 1723 * 1724 * @param <E> The element type. 1725 * @param v The list to add to. Can be <jk>null</jk> (a new list is created and returned). 1726 * @param e The elements to add. Can be <jk>null</jk> (treated as a no-op — the value is returned unchanged). 1727 * @return The same list (or a newly-created list if <jv>v</jv> was <jk>null</jk>). 1728 * @see CollectionUtils#addAll(List,Object...) 1729 */ 1730 @SafeVarargs 1731 public static <E> List<E> aa(List<E> v, E...e) { return CollectionUtils.addAll(v, e); } 1732 1733 /** 1734 * Creates an {@code int[]} from the specified values. 1735 * 1736 * @param v The values. 1737 * @return A new int array. 1738 * @see CollectionUtils#intArray(int...) 1739 */ 1740 public static int[] ints(int...v) { return CollectionUtils.intArray(v); } 1741 1742 /** 1743 * Creates a {@code long[]} from the specified values. 1744 * 1745 * @param v The values. 1746 * @return A new long array. 1747 * @see CollectionUtils#longArray(long...) 1748 */ 1749 public static long[] longs(long...v) { return CollectionUtils.longArray(v); } 1750 1751 /** 1752 * Creates a {@code byte[]} from the specified int values. 1753 * 1754 * @param v The values. 1755 * @return A new byte array. 1756 * @see CollectionUtils#byteArray(int...) 1757 */ 1758 public static byte[] bytes(int...v) { return CollectionUtils.byteArray(v); } 1759 1760 /** 1761 * Creates a {@code char[]} from the specified values. 1762 * 1763 * @param v The values. 1764 * @return A new char array. 1765 * @see CollectionUtils#charArray(char...) 1766 */ 1767 public static char[] chars(char...v) { return CollectionUtils.charArray(v); } 1768 1769 /** 1770 * Creates a {@code float[]} from the specified values. 1771 * 1772 * @param v The values. 1773 * @return A new float array. 1774 * @see CollectionUtils#floatArray(float...) 1775 */ 1776 public static float[] floats(float...v) { return CollectionUtils.floatArray(v); } 1777 1778 /** 1779 * Creates a {@code double[]} from the specified values. 1780 * 1781 * @param v The values. 1782 * @return A new double array. 1783 * @see CollectionUtils#doubleArray(double...) 1784 */ 1785 public static double[] doubles(double...v) { return CollectionUtils.doubleArray(v); } 1786 1787 /** 1788 * Creates a {@code boolean[]} from the specified values. 1789 * 1790 * @param v The values. 1791 * @return A new boolean array. 1792 * @see CollectionUtils#booleanArray(boolean...) 1793 */ 1794 public static boolean[] booleans(boolean...v) { return CollectionUtils.booleanArray(v); } 1795 1796 /** 1797 * Creates a {@code short[]} from the specified int values. 1798 * 1799 * @param v The values. 1800 * @return A new short array. 1801 * @see CollectionUtils#shortArray(int...) 1802 */ 1803 public static short[] shorts(int...v) { return CollectionUtils.shortArray(v); } 1804 1805 /** 1806 * Creates a new {@link Lists} builder for the specified element type. 1807 * 1808 * @param <E> The element type. 1809 * @param type The element type. 1810 * @return A new list builder. 1811 * @see CollectionUtils#listBuilder(Class) 1812 */ 1813 public static <E> Lists<E> lb(Class<E> type) { return CollectionUtils.listBuilder(type); } 1814 1815 /** 1816 * Creates a new {@link Maps} builder. 1817 * 1818 * @param <K> The key type. 1819 * @param <V> The value type. 1820 * @return A new map builder. 1821 * @see CollectionUtils#mapBuilder() 1822 */ 1823 public static <K,V> Maps<K,V> mb() { return CollectionUtils.mapBuilder(); } 1824 1825 /** 1826 * Creates a new {@link Maps} builder for the specified key and value types. 1827 * 1828 * @param <K> The key type. 1829 * @param <V> The value type. 1830 * @param keyType The key type. 1831 * @param valueType The value type. 1832 * @return A new map builder. 1833 * @see CollectionUtils#mapBuilder(Class,Class) 1834 */ 1835 public static <K,V> Maps<K,V> mb(Class<K> keyType, Class<V> valueType) { return CollectionUtils.mapBuilder(keyType, valueType); } 1836 1837 /** 1838 * Creates a new {@link Sets} builder for the specified element type. 1839 * 1840 * @param <E> The element type. 1841 * @param type The element type. 1842 * @return A new set builder. 1843 * @see CollectionUtils#setBuilder(Class) 1844 */ 1845 public static <E> Sets<E> stb(Class<E> type) { return CollectionUtils.setBuilder(type); } 1846 1847 // ---- CopyUtils ---- 1848 1849 /** 1850 * Creates a new collection from the specified collection. 1851 * 1852 * @param <E> The element type. 1853 * @param v The value to copy from. Can be <jk>null</jk> (returns <jk>null</jk>). 1854 * @return A new {@link LinkedHashSet}, or <jk>null</jk> if the input was null. 1855 * @see CopyUtils#copyOf(Collection) 1856 */ 1857 public static <E> Collection<E> cp(Collection<E> v) { return CopyUtils.copyOf(v); } 1858 1859 /** 1860 * Returns a mutable copy of the specified list. 1861 * 1862 * @param <E> The element type. 1863 * @param v The list to copy. Can be <jk>null</jk> (returns <jk>null</jk>). 1864 * @return A new list containing the same elements, or <jk>null</jk> if the list was <jk>null</jk>. 1865 * @see CopyUtils#copyOf(List) 1866 */ 1867 public static <E> List<E> cp(List<E> v) { return CopyUtils.copyOf(v); } 1868 1869 /** 1870 * Makes a deep copy of the specified list, transforming each value. 1871 * 1872 * @param <E> The element type. 1873 * @param v The list to copy. 1874 * @param valueMapper The function to apply to each value in the list. 1875 * @return A new transformed list, or <jk>null</jk> if the list being copied was null. 1876 * @see CopyUtils#copyOf(List,Function) 1877 */ 1878 public static <E> List<E> cp(List<E> v, Function<? super E,? extends E> valueMapper) { return CopyUtils.copyOf(v, valueMapper); } 1879 1880 /** 1881 * Makes a deep copy of the specified list, transforming each value using the specified list factory. 1882 * 1883 * @param <E> The element type. 1884 * @param v The list to copy. 1885 * @param valueMapper The function to apply to each value in the list. 1886 * @param listFactory The factory for creating the list. 1887 * @return A new transformed list, or <jk>null</jk> if the list being copied was null. 1888 * @see CopyUtils#copyOf(List,Function,Supplier) 1889 */ 1890 public static <E> List<E> cp(List<E> v, Function<? super E,? extends E> valueMapper, Supplier<List<E>> listFactory) { return CopyUtils.copyOf(v, valueMapper, listFactory); } 1891 1892 /** 1893 * Creates a new map from the specified map. 1894 * 1895 * @param <K> The key type. 1896 * @param <V> The value type. 1897 * @param v The value to copy from. Can be <jk>null</jk> (returns <jk>null</jk>). 1898 * @return A new {@link LinkedHashMap}, or <jk>null</jk> if the input was null. 1899 * @see CopyUtils#copyOf(Map) 1900 */ 1901 public static <K,V> Map<K,V> cp(Map<K,V> v) { return CopyUtils.copyOf(v); } 1902 1903 /** 1904 * Makes a deep copy of the specified map, transforming each value. 1905 * 1906 * @param <K> The key type. 1907 * @param <V> The value type. 1908 * @param v The map to copy. 1909 * @param valueMapper The function to apply to each value in the map. 1910 * @return A new transformed map, or <jk>null</jk> if the map being copied was null. 1911 * @see CopyUtils#copyOf(Map,Function) 1912 */ 1913 public static <K,V> Map<K,V> cp(Map<K,V> v, Function<? super V,? extends V> valueMapper) { return CopyUtils.copyOf(v, valueMapper); } 1914 1915 /** 1916 * Makes a deep copy of the specified map, transforming each value using the specified map factory. 1917 * 1918 * @param <K> The key type. 1919 * @param <V> The value type. 1920 * @param v The map to copy. 1921 * @param valueMapper The function to apply to each value in the map. 1922 * @param mapFactory The factory for creating the map. 1923 * @return A new transformed map, or <jk>null</jk> if the map being copied was null. 1924 * @see CopyUtils#copyOf(Map,Function,Supplier) 1925 */ 1926 public static <K,V> Map<K,V> cp(Map<K,V> v, Function<? super V,? extends V> valueMapper, Supplier<Map<K,V>> mapFactory) { return CopyUtils.copyOf(v, valueMapper, mapFactory); } 1927 1928 /** 1929 * Creates a new set from the specified set. 1930 * 1931 * @param <E> The element type. 1932 * @param v The value to copy from. Can be <jk>null</jk> (returns <jk>null</jk>). 1933 * @return A new {@link LinkedHashSet}, or <jk>null</jk> if the input was null. 1934 * @see CopyUtils#copyOf(Set) 1935 */ 1936 public static <E> Set<E> cp(Set<E> v) { return CopyUtils.copyOf(v); } 1937 1938 /** 1939 * Makes a deep copy of the specified set, transforming each value. 1940 * 1941 * @param <E> The element type. 1942 * @param v The set to copy. 1943 * @param valueMapper The function to apply to each value in the set. 1944 * @return A new transformed set, or <jk>null</jk> if the set being copied was null. 1945 * @see CopyUtils#copyOf(Set,Function) 1946 */ 1947 public static <E> Set<E> cp(Set<E> v, Function<? super E,? extends E> valueMapper) { return CopyUtils.copyOf(v, valueMapper); } 1948 1949 /** 1950 * Makes a deep copy of the specified set, transforming each value using the specified set factory. 1951 * 1952 * @param <E> The element type. 1953 * @param v The set to copy. 1954 * @param valueMapper The function to apply to each value in the set. 1955 * @param setFactory The factory for creating sets. 1956 * @return A new transformed set, or <jk>null</jk> if the set being copied was null. 1957 * @see CopyUtils#copyOf(Set,Function,Supplier) 1958 */ 1959 public static <E> Set<E> cp(Set<E> v, Function<? super E,? extends E> valueMapper, Supplier<Set<E>> setFactory) { return CopyUtils.copyOf(v, valueMapper, setFactory); } 1960 1961 /** 1962 * Makes a copy of the specified array. 1963 * 1964 * @param <T> The element type. 1965 * @param v The array to copy. Can be <jk>null</jk> (returns <jk>null</jk>). 1966 * @return A new copy of the array, or <jk>null</jk> if the array was <jk>null</jk>. 1967 * @see CopyUtils#copyOf(Object[]) 1968 */ 1969 public static <T> T[] cp(T[] v) { return CopyUtils.copyOf(v); } 1970 1971 /** 1972 * Makes a copy of the specified array. 1973 * 1974 * @param v The array to copy. Can be <jk>null</jk> (returns <jk>null</jk>). 1975 * @return A new copy of the array, or <jk>null</jk> if the array was <jk>null</jk>. 1976 * @see CopyUtils#copyOf(byte[]) 1977 */ 1978 public static byte[] cp(byte[] v) { return CopyUtils.copyOf(v); } 1979 1980 /** 1981 * Makes a copy of the specified array. 1982 * 1983 * @param v The array to copy. Can be <jk>null</jk> (returns <jk>null</jk>). 1984 * @return A new copy of the array, or <jk>null</jk> if the array was <jk>null</jk>. 1985 * @see CopyUtils#copyOf(char[]) 1986 */ 1987 public static char[] cp(char[] v) { return CopyUtils.copyOf(v); } 1988 1989 /** 1990 * Makes a copy of the specified array. 1991 * 1992 * @param v The array to copy. Can be <jk>null</jk> (returns <jk>null</jk>). 1993 * @return A new copy of the array, or <jk>null</jk> if the array was <jk>null</jk>. 1994 * @see CopyUtils#copyOf(int[]) 1995 */ 1996 public static int[] cp(int[] v) { return CopyUtils.copyOf(v); } 1997 1998 /** 1999 * Makes a copy of the specified array. 2000 * 2001 * @param v The array to copy. Can be <jk>null</jk> (returns <jk>null</jk>). 2002 * @return A new copy of the array, or <jk>null</jk> if the array was <jk>null</jk>. 2003 * @see CopyUtils#copyOf(long[]) 2004 */ 2005 public static long[] cp(long[] v) { return CopyUtils.copyOf(v); } 2006 2007 /** 2008 * Makes a copy of the specified array. 2009 * 2010 * @param v The array to copy. Can be <jk>null</jk> (returns <jk>null</jk>). 2011 * @return A new copy of the array, or <jk>null</jk> if the array was <jk>null</jk>. 2012 * @see CopyUtils#copyOf(short[]) 2013 */ 2014 public static short[] cp(short[] v) { return CopyUtils.copyOf(v); } 2015 2016 /** 2017 * Makes a copy of the specified array. 2018 * 2019 * @param v The array to copy. Can be <jk>null</jk> (returns <jk>null</jk>). 2020 * @return A new copy of the array, or <jk>null</jk> if the array was <jk>null</jk>. 2021 * @see CopyUtils#copyOf(boolean[]) 2022 */ 2023 public static boolean[] cp(boolean[] v) { return CopyUtils.copyOf(v); } 2024 2025 /** 2026 * Makes a copy of the specified array. 2027 * 2028 * @param v The array to copy. Can be <jk>null</jk> (returns <jk>null</jk>). 2029 * @return A new copy of the array, or <jk>null</jk> if the array was <jk>null</jk>. 2030 * @see CopyUtils#copyOf(double[]) 2031 */ 2032 public static double[] cp(double[] v) { return CopyUtils.copyOf(v); } 2033 2034 /** 2035 * Makes a copy of the specified array. 2036 * 2037 * @param v The array to copy. Can be <jk>null</jk> (returns <jk>null</jk>). 2038 * @return A new copy of the array, or <jk>null</jk> if the array was <jk>null</jk>. 2039 * @see CopyUtils#copyOf(float[]) 2040 */ 2041 public static float[] cp(float[] v) { return CopyUtils.copyOf(v); } 2042 2043 // ---- ClassUtils ---- 2044 2045 /** 2046 * Casts the specified object to the specified type (null-safe). 2047 * 2048 * @param <T> The target type. 2049 * @param c The target class. 2050 * @param o The object to cast. 2051 * @return The cast object, or null if the object is null. 2052 * @see ClassUtils#castTo(Class,Object) 2053 */ 2054 public static <T> T cast(Class<T> c, Object o) { return ClassUtils.castTo(c, o); } 2055 2056 /** 2057 * Makes the specified method accessible. 2058 * 2059 * @param m The method. Must not be <jk>null</jk> (a <jk>null</jk> argument throws {@link IllegalArgumentException}). 2060 * @return <jk>true</jk> if the method was made accessible. 2061 * @see ClassUtils#setAccessible(Method) 2062 */ 2063 public static boolean sa(Method m) { return ClassUtils.setAccessible(m); } 2064 2065 /** 2066 * Makes the specified field accessible. 2067 * 2068 * @param f The field. Must not be <jk>null</jk> (a <jk>null</jk> argument throws {@link IllegalArgumentException}). 2069 * @return <jk>true</jk> if the field was made accessible. 2070 * @see ClassUtils#setAccessible(Field) 2071 */ 2072 public static boolean sa(Field f) { return ClassUtils.setAccessible(f); } 2073 2074 /** 2075 * Makes the specified constructor accessible. 2076 * 2077 * @param c The constructor. Must not be <jk>null</jk> (a <jk>null</jk> argument throws {@link IllegalArgumentException}). 2078 * @return <jk>true</jk> if the constructor was made accessible. 2079 * @see ClassUtils#setAccessible(Constructor) 2080 */ 2081 public static boolean sa(Constructor<?> c) { return ClassUtils.setAccessible(c); } 2082 2083 // ---- ThrowableUtils ---- 2084 2085 // Self-contained exception factories (Principle A — no delegation, no log side-effect). 2086 2087 /** Creates a {@link RuntimeException} with a formatted message. */ 2088 public static RuntimeException rex(String m, Object...a) { return new RuntimeException(StringUtils.format(m, a)); } 2089 2090 /** Creates a {@link RuntimeException} wrapping a cause. */ 2091 public static RuntimeException rex(Throwable t) { return new RuntimeException(t); } 2092 2093 /** Creates a {@link RuntimeException} with a cause and formatted message. */ 2094 public static RuntimeException rex(Throwable t, String m, Object...a) { return new RuntimeException(StringUtils.format(m, a), t); } 2095 2096 /** Creates an {@link IllegalArgumentException} with a formatted message. */ 2097 public static IllegalArgumentException iaex(String m, Object...a) { return new IllegalArgumentException(StringUtils.format(m, a)); } 2098 2099 /** Creates an {@link IllegalArgumentException} wrapping a cause. */ 2100 public static IllegalArgumentException iaex(Throwable t) { return new IllegalArgumentException(t); } 2101 2102 /** Creates an {@link IllegalArgumentException} with a cause and formatted message. */ 2103 public static IllegalArgumentException iaex(Throwable t, String m, Object...a) { return new IllegalArgumentException(StringUtils.format(m, a), t); } 2104 2105 /** Creates a {@link BeanRuntimeException} with a formatted message. */ 2106 public static BeanRuntimeException brex(String m, Object...a) { return new BeanRuntimeException(m, a); } 2107 2108 /** Creates a {@link BeanRuntimeException} wrapping a cause. */ 2109 public static BeanRuntimeException brex(Throwable t) { return new BeanRuntimeException(t); } 2110 2111 /** Creates a {@link BeanRuntimeException} with a cause and formatted message. */ 2112 public static BeanRuntimeException brex(Throwable t, String m, Object...a) { return new BeanRuntimeException(StringUtils.format(m, a), t); } 2113 2114 /** Creates a {@link BeanRuntimeException} with an associated class and formatted message. */ 2115 public static BeanRuntimeException brex(Class<?> c, String m, Object...a) { return new BeanRuntimeException(c, m, a); } 2116 2117 /** Creates a {@link BeanRuntimeException} with an associated class and formatted message. */ 2118 public static BeanRuntimeException brex(ClassInfo c, String m, Object...a) { return new BeanRuntimeException(c.inner(), m, a); } 2119 2120 /** Creates a {@link BeanRuntimeException} with a cause, associated class, and formatted message. */ 2121 public static BeanRuntimeException brex(Throwable t, Class<?> c, String m, Object...a) { return new BeanRuntimeException(t, c, m, a); } 2122 2123 /** Creates a {@link BeanRuntimeException} with a cause, associated class, and formatted message. */ 2124 public static BeanRuntimeException brex(Throwable t, ClassInfo c, String m, Object...a) { return new BeanRuntimeException(t, c.inner(), m, a); } 2125 2126 /** Creates an {@link IOException} with a formatted message. */ 2127 public static IOException ioex(String m, Object...a) { return new IOException(StringUtils.format(m, a)); } 2128 2129 /** Creates an {@link IOException} wrapping a cause. */ 2130 public static IOException ioex(Throwable t) { return new IOException(t); } 2131 2132 /** Creates an {@link IOException} with a cause and formatted message. */ 2133 public static IOException ioex(Throwable t, String m, Object...a) { return new IOException(StringUtils.format(m, a), t); } 2134 2135 /** Creates an {@link ExecutableException} with a formatted message. */ 2136 public static ExecutableException exex(String m, Object...a) { return new ExecutableException(m, a); } 2137 2138 /** Creates an {@link ExecutableException} wrapping a cause. */ 2139 public static ExecutableException exex(Throwable t) { return new ExecutableException(t); } 2140 2141 /** Creates an {@link ExecutableException} with a cause and formatted message. */ 2142 public static ExecutableException exex(Throwable t, String m, Object...a) { return new ExecutableException(t, m, a); } 2143 2144 /** Creates an {@link IllegalStateException} with a formatted message. */ 2145 public static IllegalStateException isex(String m, Object...a) { return new IllegalStateException(StringUtils.format(m, a)); } 2146 2147 /** Creates an {@link IllegalStateException} wrapping a cause. */ 2148 public static IllegalStateException isex(Throwable t) { return new IllegalStateException(t); } 2149 2150 /** Creates an {@link IllegalStateException} with a cause and formatted message. */ 2151 public static IllegalStateException isex(Throwable t, String m, Object...a) { return new IllegalStateException(StringUtils.format(m, a), t); } 2152 2153 /** Creates an {@link UnsupportedOperationException} with the message "Not supported." */ 2154 public static UnsupportedOperationException uoex() { return new UnsupportedOperationException("Not supported."); } 2155 2156 /** Creates an {@link UnsupportedOperationException} with a formatted message. */ 2157 public static UnsupportedOperationException uoex(String m, Object...a) { return new UnsupportedOperationException(StringUtils.format(m, a)); } 2158 2159 /** Creates an {@link UnsupportedOperationException} wrapping a cause. */ 2160 public static UnsupportedOperationException uoex(Throwable t) { return new UnsupportedOperationException(t); } 2161 2162 /** Creates an {@link UnsupportedOperationException} with a cause and formatted message. */ 2163 public static UnsupportedOperationException uoex(Throwable t, String m, Object...a) { return new UnsupportedOperationException(StringUtils.format(m, a), t); } 2164 2165 /** Creates an {@link UnsupportedOperationException} with the message "Object is read only." */ 2166 public static UnsupportedOperationException uoroex() { return new UnsupportedOperationException("Object is read only."); } 2167 2168 /** 2169 * Returns the stack trace of the specified throwable as a string. 2170 * 2171 * @param t The throwable. Must not be <jk>null</jk> (a <jk>null</jk> argument throws {@link NullPointerException}). 2172 * @return The stack trace string. 2173 * @see ThrowableUtils#getStackTrace(Throwable) 2174 */ 2175 public static String gst(Throwable t) { return ThrowableUtils.getStackTrace(t); } 2176 2177 /** 2178 * Casts the specified throwable to the specified type, wrapping if necessary. 2179 * 2180 * @param <T> The target type. 2181 * @param c The target class. Must not be <jk>null</jk>. 2182 * @param t The throwable. Can be <jk>null</jk> (produces a wrapper with a <jk>null</jk> cause). 2183 * @return The cast (or wrapped) throwable. 2184 * @see ThrowableUtils#castException(Class,Throwable) 2185 */ 2186 public static <T> T cex(Class<T> c, Throwable t) { return ThrowableUtils.castException(c, t); } 2187 2188 /** 2189 * Searches the causal chain of the specified throwable for a cause of the specified type. 2190 * 2191 * @param <T> The cause type. 2192 * @param e The throwable to search. Can be <jk>null</jk> (returns an empty {@link Optional}). 2193 * @param c The cause class to look for. Must not be <jk>null</jk>. 2194 * @return An optional containing the matching cause, if found. 2195 * @see ThrowableUtils#findCause(Throwable,Class) 2196 */ 2197 public static <T extends Throwable> Optional<T> fc(Throwable e, Class<T> c) { return ThrowableUtils.findCause(e, c); } 2198 2199 /** 2200 * Returns the first cause of the specified type from the throwable's causal chain. 2201 * 2202 * @param <T> The cause type. 2203 * @param c The cause class to look for. Must not be <jk>null</jk>. 2204 * @param t The throwable to search. Can be <jk>null</jk> (returns <jk>null</jk>). 2205 * @return The matching cause, or null if not found. 2206 * @see ThrowableUtils#getThrowableCause(Class,Throwable) 2207 */ 2208 public static <T extends Throwable> T gtc(Class<T> c, Throwable t) { return ThrowableUtils.getThrowableCause(c, t); } 2209 2210 /** 2211 * Returns the localized message of the specified throwable. 2212 * 2213 * @param t The throwable. Must not be <jk>null</jk> (a <jk>null</jk> argument throws {@link NullPointerException}). 2214 * @return The localized message, or <jk>null</jk> if the throwable has no localized message. 2215 * @see ThrowableUtils#localizedMessage(Throwable) 2216 */ 2217 public static String lm(Throwable t) { return ThrowableUtils.localizedMessage(t); } 2218 2219 /** 2220 * Runs the specified snippet, silently swallowing any thrown exception. 2221 * 2222 * @param s The snippet to run. 2223 * @see ThrowableUtils#runQuietly(Snippet) 2224 */ 2225 public static void quiet(Snippet s) { ThrowableUtils.runQuietly(s); } 2226 2227 // ---- PredicateUtils ---- 2228 2229 /** 2230 * Tests the specified value against the given predicate (null-safe). 2231 * 2232 * @param <T> The value type. 2233 * @param p The predicate. Can be <jk>null</jk> (returns <jk>true</jk>). 2234 * @param v The value to test. Can be <jk>null</jk>. 2235 * @return The result of the predicate, or <jk>true</jk> if the predicate is <jk>null</jk>. 2236 * @see PredicateUtils#test(Predicate,Object) 2237 */ 2238 public static <T> boolean t(Predicate<T> p, T v) { return PredicateUtils.test(p, v); } 2239 2240}