public final class Utils extends Object
Modifier and Type | Field and Description |
---|---|
static Comparator<Comparable> |
NULL_COMPARATOR
This comparator also takes account with null values and sorts them first.
|
Modifier and Type | Method and Description |
---|---|
static IOException |
close(Closeable resource)
Check if the given resource is not
null and then close it, whereby any caught IOException
is been returned instead of thrown, so that the caller can if necessary handle (log) or just ignore it without
the need to put another try-catch. |
static <T> T |
coalesce(T... objects)
Returns the first non-
null object of the argument list, or null if there is no such
element. |
static boolean |
containsByClassName(Collection<?> objects,
String className)
Checks if the given collection contains an object with the given class name.
|
static List<String> |
csvToList(String values)
Converts comma separated values in a string into a list with those values.
|
static List<String> |
csvToList(String values,
String delimiter)
Converts comma separated values in a string into a list with those values.
|
static String |
decodeURL(String string)
URL-decode the given string using UTF-8.
|
static String |
encodeURL(String string)
URL-encode the given string using UTF-8.
|
static String |
escapeJS(String string,
boolean escapeSingleQuote)
Escapes the given string according the JavaScript code rules.
|
static String |
formatRFC1123(Date date)
Formats the given
Date to a string in RFC1123 format. |
static boolean |
isAnyEmpty(Object... values)
Returns
true if at least one value is empty. |
static boolean |
isBlank(String string)
Returns
true if the given string is null or is empty or contains whitespace only. |
static boolean |
isDecimal(String string)
Returns
true if the given string is parseable as a decimal. |
static boolean |
isEmpty(Collection<?> collection)
Returns
true if the given collection is null or is empty. |
static boolean |
isEmpty(Map<?,?> map)
Returns
true if the given map is null or is empty. |
static boolean |
isEmpty(Object value)
Returns
true if the given value is null or is empty. |
static boolean |
isEmpty(Object[] array)
Returns
true if the given array is null or is empty. |
static boolean |
isEmpty(String string)
Returns
true if the given string is null or is empty. |
static boolean |
isNumber(String string)
Returns
true if the given string is parseable as a number. |
static <T> boolean |
isOneOf(T object,
T... objects)
Returns
true if the given object equals one of the given objects. |
static <T> List<T> |
iterableToList(Iterable<T> iterable)
Converts an iterable into a list.
|
static <T extends Comparable<T>> |
nullSafeTreeSet(Collection<T> collection)
Create a new
TreeSet which uses NULL_COMPARATOR , add the given collection to it and return
the TreeSet . |
static Date |
parseRFC1123(String string)
Parses the given string in RFC1123 format to a
Date object. |
static <T> Map<T,T> |
reverse(Map<T,T> source)
Returns a new map that contains the reverse of the given map.
|
static String |
serializeURLSafe(String string)
Serialize the given string to the short possible unique URL-safe representation.
|
static boolean |
startsWithOneOf(String string,
String... prefixes)
Returns
true if the given string starts with one of the given prefixes. |
static long |
stream(InputStream input,
OutputStream output)
Stream the given input to the given output by NIO
ByteBuffer . |
static <T> Set<T> |
unmodifiableSet(Object... values)
Creates an unmodifiable set based on the given values.
|
static String |
unserializeURLSafe(String string)
Unserialize the given serialized URL-safe string.
|
public static final Comparator<Comparable> NULL_COMPARATOR
public static boolean isEmpty(String string)
true
if the given string is null or is empty.string
- The string to be checked on emptiness.true
if the given string is null or is empty.public static boolean isEmpty(Object[] array)
true
if the given array is null or is empty.array
- The array to be checked on emptiness.true
if the given array is null or is empty.public static boolean isEmpty(Collection<?> collection)
true
if the given collection is null or is empty.collection
- The collection to be checked on emptiness.true
if the given collection is null or is empty.public static boolean isEmpty(Map<?,?> map)
true
if the given map is null or is empty.map
- The map to be checked on emptiness.true
if the given map is null or is empty.public static boolean isEmpty(Object value)
true
if the given value is null or is empty. Types of String, Collection, Map and Array are
recognized. If none is recognized, then examine the emptiness of the toString() representation instead.value
- The value to be checked on emptiness.true
if the given value is null or is empty.public static boolean isAnyEmpty(Object... values)
true
if at least one value is empty.values
- the values to be checked on emptinesstrue
if any value is empty and false
if no values are emptypublic static boolean isBlank(String string)
true
if the given string is null or is empty or contains whitespace only. In addition to
isEmpty(String)
, this thus also returns true
when string.trim().isEmpty()
returns true
.string
- The string to be checked on blankness.public static boolean isNumber(String string)
true
if the given string is parseable as a number. I.e. it is not null, nor blank and contains solely
digits. I.e., it won't throw a NumberFormatException
when parsing as Long
.string
- The string to be checked as number.true
if the given string is parseable as a number.public static boolean isDecimal(String string)
true
if the given string is parseable as a decimal. I.e. it is not null, nor blank and contains solely
digits. I.e., it won't throw a NumberFormatException
when parsing as Double
.string
- The string to be checked as decimal.true
if the given string is parseable as a decimal.public static <T> T coalesce(T... objects)
null
object of the argument list, or null
if there is no such
element.T
- The generic object type.objects
- The argument list of objects to be tested for non-null
.null
object of the argument list, or null
if there is no such
element.public static <T> boolean isOneOf(T object, T... objects)
true
if the given object equals one of the given objects.T
- The generic object type.object
- The object to be checked if it equals one of the given objects.objects
- The argument list of objects to be tested for equality.true
if the given object equals one of the given objects.public static boolean startsWithOneOf(String string, String... prefixes)
true
if the given string starts with one of the given prefixes.string
- The object to be checked if it starts with one of the given prefixes.prefixes
- The argument list of prefixes to be checkedtrue
if the given string starts with one of the given prefixes.public static long stream(InputStream input, OutputStream output) throws IOException
ByteBuffer
. Both the input and output streams will
implicitly be closed after streaming, regardless of whether an exception is been thrown or not.input
- The input stream.output
- The output stream.IOException
- When an I/O error occurs.public static IOException close(Closeable resource)
null
and then close it, whereby any caught IOException
is been returned instead of thrown, so that the caller can if necessary handle (log) or just ignore it without
the need to put another try-catch.resource
- The closeable resource to be closed.IOException
, or null
if none is been thrown.public static <T> Set<T> unmodifiableSet(Object... values)
ClassCastException
.values
- The values to create an unmodifiable set for.ClassCastException
- When one of the values or one of the arrays or collections is of wrong type.public static <T> List<T> iterableToList(Iterable<T> iterable)
This method makes NO guarantee to whether changes to the source iterable are reflected in the returned list or not. For instance if the given iterable already is a list, it's returned directly.
iterable
- The iterable to be converted.public static List<String> csvToList(String values)
E.g. a string with "foo, bar, kaz" will be converted into a List
with values:
","
) as delimiter. See csvToList(String, String)
for when
a different delimiter is needed.values
- string with comma separated valuesvalues
argument, can be the empty list.public static List<String> csvToList(String values, String delimiter)
E.g. a string with "foo, bar, kaz" will be converted into a List
with values:
values
- string with comma separated valuesdelimiter
- the delimiter used to separate the actual values in the values
parameter.values
argument, can be the empty list.public static <T> Map<T,T> reverse(Map<T,T> source)
The reverse of a map means that every value X becomes a key X' with as corresponding value Y' the key Y that was originally associated with the value X.
source
- the map that is to be reversedpublic static boolean containsByClassName(Collection<?> objects, String className)
objects
- collection of objects to checkclassName
- name of the class to be checked forpublic static <T extends Comparable<T>> TreeSet<T> nullSafeTreeSet(Collection<T> collection)
TreeSet
which uses NULL_COMPARATOR
, add the given collection to it and return
the TreeSet
.public static String formatRFC1123(Date date)
Date
to a string in RFC1123 format. This format is used in HTTP headers and in
JavaScript Date
constructor.date
- The Date
to be formatted to a string in RFC1123 format.public static Date parseRFC1123(String string) throws ParseException
Date
object.string
- The string in RFC1123 format to be parsed to a Date
object.Date
.ParseException
- When the given string is not in RFC1123 format.public static String serializeURLSafe(String string)
/
, +
and
=
are been replaced by respectively ~
, -
and _
to make it
URL-safe (so that no platform-sensitive URL-encoding needs to be done when used in URLs).string
- The string to be serialized.null
when the given string is itself null
.public static String unserializeURLSafe(String string)
serializeURLSafe(String)
.string
- The serialized URL-safe string to be unserialized.null
when the given string is by itself null
.IllegalArgumentException
- When the given serialized URL-safe string is not in valid format as returned by
serializeURLSafe(String)
.public static String encodeURL(String string)
string
- The string to be URL-encoded using UTF-8.null
if null
was given.UnsupportedOperationException
- When UTF-8 is not supported.public static String decodeURL(String string)
string
- The string to be URL-decode using UTF-8.null
if null
was given.UnsupportedOperationException
- When UTF-8 is not supported.public static String escapeJS(String string, boolean escapeSingleQuote)
string
- The string to be escaped according the JavaScript code rules.escapeSingleQuote
- Whether to escape single quotes as well or not. Set to false
if you want
to escape it for usage in JSON.