Class Shorts

java.lang.Object
org.apache.juneau.commons.utils.Shorts
Direct Known Subclasses:
BasicTestUtils

public class Shorts extends Object
Terse alias facade for the domain utility classes.

A single wildcard static import (import static org.apache.juneau.commons.utils.Shorts.*;) exposes the entire short-name vocabulary. Almost every method here is a one-line delegation to a canonical, fully-descriptive method on a domain class — behavior is identical to calling the canonical method directly — and each such alias documents its target via an @see tag. Canonical implementations live in ObjectUtils, StringUtils, CollectionUtils, ClassUtils, ThrowableUtils, IoUtils, FileUtils, PredicateUtils, SystemUtils, DateUtils, and ReflectionUtils.

Documented exception: the terse exception factories (rex/brex/ iaex/isex/uoex/uoroex/ioex/exex) are self-contained — they construct the exception directly rather than delegating to a domain class — so that the domain *Utils classes can stay Shorts-free (they use the package-private Exceptions helper instead). These are the only methods in this class with no @see tag, apart from the argument/state checks described next.

Argument and state checks: the req* family (req, reqnn, reqnb, reqnns, reqt, reqcat; throwing IllegalArgumentException) and the chk* family (chk, chknn; throwing IllegalStateException) are also self-contained: Shorts holds their canonical implementations. Because of that, the domain *Utils classes may import Shorts for req*/chk* calls. That is the only relaxation: a domain class may use the self-contained members of Shorts (exception factories, req*, chk*) but never a delegating alias, because that alias would call back into a domain class.

Example:

import static org.apache.juneau.commons.utils.Shorts.*; public Builder timeout(String name, Duration value) { name = reqnb("name", name); timeout = reqnn("value", value); req(! value.isNegative(), "Argument 'value' cannot be negative: %s", value); chk(! built, "Builder already used."); return this; }

Disjointness invariant: no Shorts alias name is identical to the canonical (full) method name of any method in any domain class. This lets a developer wildcard-import Shorts.* alongside any single domain *Utils.* with zero method-name collisions.

This class is the replacement for the former Utils grab-bag (now deleted): callers replace import static ...utils.Utils.* with import static ...utils.Shorts.*.

  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Constructor — this class is meant to be subclassed.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T[]
    a(T... x)
    Creates an array from the specified elements.
    static <E> E[][]
    a2(E[]... v)
    Creates a 2-dimensional array from the specified rows.
    static <E> List<E>
    aa(List<E> v, E... e)
    Adds the specified elements to the given list and returns it.
    static boolean
    and(boolean... v)
    Returns true if all of the specified booleans are true.
    static boolean
    any(boolean... v)
    Returns true if any of the specified booleans are true.
    static Object[]
    ao(Object... v)
    Creates an Object[] from the specified elements.
    static <E> E
    at(List<E> l, int i)
    Returns the element at the specified index of a list (null-safe).
    static boolean
    b(Object v)
    Converts the specified object to a boolean.
    static String
    Returns a blank (single-space) string if the specified string is null, else the string.
    static boolean[]
    booleans(boolean... v)
    Creates a boolean[] from the specified values.
    brex(Class<?> c, String m, Object... a)
    Creates a BeanRuntimeException with an associated class and formatted message.
    brex(String m, Object... a)
    Creates a BeanRuntimeException with a formatted message.
    Creates a BeanRuntimeException wrapping a cause.
    brex(Throwable t, Class<?> c, String m, Object... a)
    Creates a BeanRuntimeException with a cause, associated class, and formatted message.
    brex(Throwable t, String m, Object... a)
    Creates a BeanRuntimeException with a cause and formatted message.
    brex(Throwable t, ClassInfo c, String m, Object... a)
    Creates a BeanRuntimeException with a cause, associated class, and formatted message.
    brex(ClassInfo c, String m, Object... a)
    Creates a BeanRuntimeException with an associated class and formatted message.
    static boolean
    btw(int n, int lo, int hi)
    Returns true if the specified integer is within the inclusive range.
    static byte[]
    bytes(int... v)
    Creates a byte[] from the specified int values.
    static <T> T
    cast(Class<T> c, Object o)
    Casts the specified object to the specified type (null-safe).
    static <T> T
    cex(Class<T> c, Throwable t)
    Casts the specified throwable to the specified type, wrapping if necessary.
    static char[]
    chars(char... v)
    Creates a char[] from the specified values.
    static void
    chk(boolean expression, String msg, Object... args)
    Throws an IllegalStateException if the specified expression is false.
    static <T> T
    chknn(T o, String msg, Object... args)
    Throws an IllegalStateException if the specified object is null.
    static int
    cmp(Object a, Object b)
    Compares two objects for order (null-safe).
    static String
    Returns the fully-qualified class name of the specified object (null-safe).
    static String
    Returns the simple class name of the specified object (null-safe).
    static String
    Returns the qualified simple class name of the specified object (includes enclosing classes).
    static boolean
    co(String s, String x)
    Returns true if the string contains the specified substring (null-safe).
    static boolean[]
    cp(boolean[] v)
    Makes a copy of the specified array.
    static byte[]
    cp(byte[] v)
    Makes a copy of the specified array.
    static char[]
    cp(char[] v)
    Makes a copy of the specified array.
    static double[]
    cp(double[] v)
    Makes a copy of the specified array.
    static float[]
    cp(float[] v)
    Makes a copy of the specified array.
    static int[]
    cp(int[] v)
    Makes a copy of the specified array.
    static long[]
    cp(long[] v)
    Makes a copy of the specified array.
    static short[]
    cp(short[] v)
    Makes a copy of the specified array.
    static <E> Collection<E>
    cp(Collection<E> v)
    Creates a new collection from the specified collection.
    static <E> List<E>
    cp(List<E> v)
    Returns a mutable copy of the specified list.
    static <E> List<E>
    cp(List<E> v, Function<? super E,? extends E> valueMapper)
    Makes a deep copy of the specified list, transforming each value.
    static <E> List<E>
    cp(List<E> v, Function<? super E,? extends E> valueMapper, Supplier<List<E>> listFactory)
    Makes a deep copy of the specified list, transforming each value using the specified list factory.
    static <K, V> Map<K,V>
    cp(Map<K,V> v)
    Creates a new map from the specified map.
    static <K, V> Map<K,V>
    cp(Map<K,V> v, Function<? super V,? extends V> valueMapper)
    Makes a deep copy of the specified map, transforming each value.
    static <K, V> Map<K,V>
    cp(Map<K,V> v, Function<? super V,? extends V> valueMapper, Supplier<Map<K,V>> mapFactory)
    Makes a deep copy of the specified map, transforming each value using the specified map factory.
    static <E> Set<E>
    cp(Set<E> v)
    Creates a new set from the specified set.
    static <E> Set<E>
    cp(Set<E> v, Function<? super E,? extends E> valueMapper)
    Makes a deep copy of the specified set, transforming each value.
    static <E> Set<E>
    cp(Set<E> v, Function<? super E,? extends E> valueMapper, Supplier<Set<E>> setFactory)
    Makes a deep copy of the specified set, transforming each value using the specified set factory.
    static <T> T[]
    cp(T[] v)
    Makes a copy of the specified array.
    static double[]
    doubles(double... v)
    Creates a double[] from the specified values.
    static String
    Returns an empty string if the specified object is null, else its string form.
    static String
    Returns an empty string if the specified string is null, else the string.
    static boolean
    eq(boolean ci, String a, String b)
    Returns true if the two strings are equal, optionally ignoring case.
    static <T> boolean
    eq(T a, T b)
    Returns true if the two objects are equal (null-safe).
    static <T, U> boolean
    eq(T a, U b, BiPredicate<T,U> t)
    Returns true if the two objects are equal using the specified equality test.
    static <T> boolean
    eqa(T a, T... b)
    Returns true if the first object equals any of the remaining objects.
    static boolean
    Returns true if the two objects are equal ignoring case (via their string forms).
    static boolean
    Returns true if the two strings are equal ignoring case (null-safe).
    static boolean
    ew(String s, String x)
    Returns true if the string ends with the specified suffix (null-safe).
    exex(String m, Object... a)
    Creates an ExecutableException with a formatted message.
    Creates an ExecutableException wrapping a cause.
    exex(Throwable t, String m, Object... a)
    Creates an ExecutableException with a cause and formatted message.
    static String
    f(String p, Object... a)
    Formats a message using printf-style (%s/%d/…) placeholders.
    static <T extends Throwable>
    Optional<T>
    fc(Throwable e, Class<T> c)
    Searches the causal chain of the specified throwable for a cause of the specified type.
    static float[]
    floats(float... v)
    Creates a float[] from the specified values.
    static String
    fnb(String... v)
    Returns the first non-blank string from the specified values.
    static String
    fne(String... v)
    Returns the first non-empty string from the specified values.
    fs(String p, Object... a)
    Returns a supplier that lazily formats a printf-style message (deferred until Supplier.get()).
    static String
    Returns the stack trace of the specified throwable as a string.
    static <T extends Comparable<T>>
    boolean
    gt(T a, T b)
    Returns true if the first comparable is greater than the second.
    static <T extends Throwable>
    T
    gtc(Class<T> c, Throwable t)
    Returns the first cause of the specified type from the throwable's causal chain.
    static <T extends Comparable<T>>
    boolean
    gte(T a, T b)
    Returns true if the first comparable is greater than or equal to the second.
    static int
    h(Object... v)
    Computes a hash code over the specified values.
    static <T> Set<T>
    hs(T... v)
    Creates a mutable HashSet from the specified elements.
    iaex(String m, Object... a)
    Creates an IllegalArgumentException with a formatted message.
    Creates an IllegalArgumentException wrapping a cause.
    iaex(Throwable t, String m, Object... a)
    Creates an IllegalArgumentException with a cause and formatted message.
    static boolean
    Returns true if the specified string is null, empty, or whitespace-only.
    static String
    Returns the system identity string of the specified object.
    static boolean
    Returns true if the specified character sequence is null or empty.
    static boolean
    Returns true if the specified object is null or empty (string/collection/map/array).
    static boolean
    ie(Collection<?> c)
    Returns true if the specified collection is null or empty.
    static boolean
    ie(Map<?,?> m)
    Returns true if the specified map is null or empty.
    static <K, V> Map<K,V>
    im()
    Creates an unmodifiable, empty map.
    static <K, V> Map<K,V>
    im(K k1, V v1)
    Creates an unmodifiable map with 1 entry.
    static <K, V> Map<K,V>
    im(K k1, V v1, K k2, V v2)
    Creates an unmodifiable map with 2 entries.
    static <K, V> Map<K,V>
    im(K k1, V v1, K k2, V v2, K k3, V v3)
    Creates an unmodifiable map with 3 entries.
    static <K, V> Map<K,V>
    im(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
    Creates an unmodifiable map with 4 entries (keys/values in alternating order).
    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)
    Creates an unmodifiable map with 5 entries (keys/values in alternating order).
    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)
    Creates an unmodifiable map with 6 entries (keys/values in alternating order).
    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)
    Creates an unmodifiable map with 7 entries (keys/values in alternating order).
    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)
    Creates an unmodifiable map with 8 entries (keys/values in alternating order).
    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)
    Creates an unmodifiable map with 9 entries (keys/values in alternating order).
    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)
    Creates an unmodifiable map with 10 entries (keys/values in alternating order).
    static boolean
    Returns true if the specified string contains non-whitespace content.
    static boolean
    Returns true if the specified character sequence is not null and not empty.
    static boolean
    Returns true if the specified object is not null and not empty.
    static boolean
    Returns true if the specified collection is not null and not empty.
    static boolean
    ine(Map<?,?> m)
    Returns true if the specified map is not null and not empty.
    static int[]
    ints(int... v)
    Creates an int[] from the specified values.
    ioex(String m, Object... a)
    Creates an IOException with a formatted message.
    Creates an IOException wrapping a cause.
    ioex(Throwable t, String m, Object... a)
    Creates an IOException with a cause and formatted message.
    isex(String m, Object... a)
    Creates an IllegalStateException with a formatted message.
    Creates an IllegalStateException wrapping a cause.
    isex(Throwable t, String m, Object... a)
    Creates an IllegalStateException with a cause and formatted message.
    static <E> Set<E>
    ist(E... v)
    Creates an unmodifiable set from the specified elements.
    static String
    jn(String... v)
    Concatenates the specified strings.
    static <T> List<T>
    l(T... x)
    Creates a modifiable ArrayList from the specified elements.
    static <E> Lists<E>
    lb(Class<E> type)
    Creates a new Lists builder for the specified element type.
    static String
    Converts the specified string to lower case (null-safe).
    static String
    Converts the specified string to lower case using Locale.ROOT (null-safe).
    static int
    Returns the length/size of the specified object (array/collection/map/string).
    static <T> List<T>
    ll(T... v)
    Creates a mutable LinkedList from the specified elements.
    static String
    Returns the localized message of the specified throwable.
    static long[]
    longs(long... v)
    Creates a long[] from the specified values.
    static <E> List<E>
    los(int n)
    Creates a modifiable, empty ArrayList presized to the specified capacity.
    static <T extends Comparable<T>>
    boolean
    lt(T a, T b)
    Returns true if the first comparable is less than the second.
    static <T extends Comparable<T>>
    boolean
    lte(T a, T b)
    Returns true if the first comparable is less than or equal to the second.
    static <K, V> Map<K,V>
    m()
    Creates an empty modifiable map.
    static <K, V> Map<K,V>
    m(K k1, V v1)
    Creates a modifiable map with 1 entry.
    static <K, V> Map<K,V>
    m(K k1, V v1, K k2, V v2)
    Creates a modifiable map with 2 entries.
    static <K, V> Map<K,V>
    m(K k1, V v1, K k2, V v2, K k3, V v3)
    Creates a modifiable map with 3 entries.
    static <K, V> Map<K,V>
    m(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
    Creates a modifiable map with 4 entries (keys/values in alternating order).
    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)
    Creates a modifiable map with 5 entries (keys/values in alternating order).
    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)
    Creates a modifiable map with 6 entries (keys/values in alternating order).
    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)
    Creates a modifiable map with 7 entries (keys/values in alternating order).
    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)
    Creates a modifiable map with 8 entries (keys/values in alternating order).
    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)
    Creates a modifiable map with 9 entries (keys/values in alternating order).
    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)
    Creates a modifiable map with 10 entries (keys/values in alternating order).
    static <K, V> Maps<K,V>
    mb()
    Creates a new Maps builder.
    static <K, V> Maps<K,V>
    mb(Class<K> keyType, Class<V> valueType)
    Creates a new Maps builder for the specified key and value types.
    static String
    mf(String p, Object... a)
    Formats a message using MessageFormat-style {0} placeholders.
    mfs(String p, Object... a)
    Returns a supplier that lazily formats a MessageFormat-style message (deferred until Supplier.get()).
    static boolean
    n(Object... v)
    Returns true if the specified array is null or any of its elements are null.
    static <T> boolean
    n(T v)
    Returns true if the specified value is null.
    static <T> T[]
    na(Class<T> t)
    Returns null typed as an array of the specified component type.
    static <T> boolean
    neq(T a, T b)
    Returns true if the two objects are not equal (null-safe).
    static <T, U> boolean
    neq(T a, U b, BiPredicate<T,U> t)
    Returns true if the two objects are not equal using the specified equality test.
    static boolean
    Returns true if the two strings are not equal ignoring case (null-safe).
    static String
    Returns null if the specified string is empty, else the string.
    static <E> List<E>
    nie(List<E> v)
    Returns null if the specified list is empty, else the list.
    static <K, V> Map<K,V>
    nie(Map<K,V> v)
    Returns null if the specified map is empty, else the map.
    static <E> Set<E>
    nie(Set<E> v)
    Returns null if the specified set is empty, else the set.
    static <T, R> R
    nm(T v, Function<? super T,? extends R> m)
    Null-safe map: returns null when the value is null, else the mapper's result.
    static <T extends Number>
    boolean
    nm1(T v)
    Returns true if the specified number is non-null and not equal to -1.
    static boolean
    Returns true if the specified object is not null.
    static boolean
    not(boolean v)
    Returns the logical negation of the specified boolean.
    static <T> Optional<T>
    o(List<T> l, int i)
    Returns the element at the specified index of a list as an Optional.
    static <T> Optional<T>
    o(T t)
    Wraps the specified value in an Optional.
    static <T> Optional<T>
    oe()
    Returns an empty Optional.
    static <T> T
    oo(Optional<T> o, T d)
    Returns the value of the optional, or the specified default if empty.
    static <T> T
    or(T... t)
    Returns the first non-null argument, or null if all are null.
    static void
    Runs the specified snippet, silently swallowing any thrown exception.
    static String
    r(Object o)
    Returns a human-readable string representation of the specified object.
    static void
    req(boolean expression, String msg, Object... args)
    Throws an IllegalArgumentException if the specified expression is false.
    static <E> Class<E>[]
    reqcat(String name, Class<E> type, Class<?>[] value)
    Throws an IllegalArgumentException if any element of the specified class array is not a subtype of the specified type.
    static String
    reqnb(String name, String o)
    Throws an IllegalArgumentException if the specified string argument is null or blank.
    static void
    reqnn(String name1, Object o1, String name2, Object o2)
    Throws an IllegalArgumentException if either of the specified arguments is null.
    static void
    reqnn(String name1, Object o1, String name2, Object o2, String name3, Object o3)
    Throws an IllegalArgumentException if any of the three specified arguments is null.
    static void
    reqnn(String name1, Object o1, String name2, Object o2, String name3, Object o3, String name4, Object o4)
    Throws an IllegalArgumentException if any of the four specified arguments is null.
    static void
    reqnn(String name1, Object o1, String name2, Object o2, String name3, Object o3, String name4, Object o4, String name5, Object o5)
    Throws an IllegalArgumentException if any of the five specified arguments is null.
    static <T> T
    reqnn(String name, T o)
    Throws an IllegalArgumentException if the specified argument is null.
    static <T, C extends Collection<T>>
    C
    reqnns(String name, C collection)
    Throws an IllegalArgumentException if the specified collection argument or any of its elements is null.
    static <T> T[]
    reqnns(String name, T[] o)
    Throws an IllegalArgumentException if the specified array argument or any of its elements is null.
    static <T> T
    reqt(Class<T> type, Object o)
    Throws an IllegalArgumentException if the specified object is not an instance of the specified type.
    static <T> T
    reqt(Class<T> type, Object o, Supplier<? extends RuntimeException> exceptionSupplier)
    Throws the exception provided by the supplier if the specified object is not an instance of the specified type.
    rex(String m, Object... a)
    Creates a RuntimeException with a formatted message.
    Creates a RuntimeException wrapping a cause.
    rex(Throwable t, String m, Object... a)
    Creates a RuntimeException with a cause and formatted message.
    static <T> T
    rnn(T v)
    Returns the specified value, throwing NullPointerException if it is null.
    static String
    s(Object v)
    Converts the specified object to its string representation (null-safe).
    static boolean
    Makes the specified constructor accessible.
    static boolean
    sa(Field f)
    Makes the specified field accessible.
    static boolean
    Makes the specified method accessible.
    Creates a new StringBuilder initialized with the stringified form of the specified value.
    sb(Object... v)
    Creates a new StringBuilder with the stringified form of each specified value appended in order.
    static short[]
    shorts(int... v)
    Creates a short[] from the specified int values.
    static <E> SortedSet<E>
    ss(E... v)
    Creates a sorted TreeSet from the specified elements (null values filtered out).
    static <T> Set<T>
    st(T... v)
    Creates a modifiable, insertion-ordered LinkedHashSet from the specified elements.
    static <E> Sets<E>
    stb(Class<E> type)
    Creates a new Sets builder for the specified element type.
    static boolean
    sw(String s, String p)
    Returns true if the string starts with the specified prefix (null-safe).
    static int
    Returns the size of the specified object (string/collection/map/array).
    static <T> boolean
    t(Predicate<T> p, T v)
    Tests the specified value against the given predicate (null-safe).
    static <E> List<E>
    tl(Collection<E> v)
    Creates a modifiable ArrayList copy of the specified collection.
    static String
    Trims leading/trailing whitespace from the specified string (null-safe).
    static <E> Set<E>
    ts(Collection<E> v)
    Creates a modifiable LinkedHashSet copy of the specified collection.
    static <T> List<T>
    u(List<? extends T> v)
    Returns an unmodifiable view of the specified list.
    static <K, V> Map<K,V>
    u(Map<? extends K,? extends V> v)
    Returns an unmodifiable view of the specified map.
    static <T> Set<T>
    u(Set<? extends T> v)
    Returns an unmodifiable view of the specified set.
    static <T> SortedSet<T>
    u(SortedSet<T> v)
    Returns an unmodifiable view of the specified sorted set.
    static String
    Converts the specified string to upper case (null-safe).
    static String
    Converts the specified string to upper case using Locale.ROOT (null-safe).
    Creates an UnsupportedOperationException with the message "Not supported."
    uoex(String m, Object... a)
    Creates an UnsupportedOperationException with a formatted message.
    Creates an UnsupportedOperationException wrapping a cause.
    uoex(Throwable t, String m, Object... a)
    Creates an UnsupportedOperationException with a cause and formatted message.
    Creates an UnsupportedOperationException with the message "Object is read only."

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait