org.omnifaces.util
Class Utils

java.lang.Object
  extended by org.omnifaces.util.Utils

public final class Utils
extends java.lang.Object

Collection of general utility methods that do not fit in one of the more specific classes.

Author:
Arjan Tijms, Bauke Scholtz

Method Summary
static java.io.IOException close(java.io.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 java.util.List<java.lang.String> csvToList(java.lang.String values)
          Converts comma separated values in a string into a list with those values.
static java.util.List<java.lang.String> csvToList(java.lang.String values, java.lang.String delimiter)
          Converts comma separated values in a string into a list with those values.
static java.lang.String decodeURL(java.lang.String string)
          URL-decode the given string using UTF-8.
static java.lang.String encodeURL(java.lang.String string)
          URL-encode the given string using UTF-8.
static java.lang.String formatRFC1123(java.util.Date date)
          Formats the given Date to a string in RFC1123 format.
static boolean isEmpty(java.util.Collection<?> collection)
          Returns true if the given collection is null or is empty.
static boolean isEmpty(java.util.Map<?,?> map)
          Returns true if the given map is null or is empty.
static boolean isEmpty(java.lang.Object value)
          Returns true if the given value is null or is empty.
static boolean isEmpty(java.lang.Object[] array)
          Returns true if the given array is null or is empty.
static boolean isEmpty(java.lang.String string)
          Returns true if the given string is null or is empty.
static
<T> boolean
isOneOf(T object, T... objects)
          Returns true if the given object equals one of the given objects.
static java.util.Date parseRFC1123(java.lang.String string)
          Parses the given string in RFC1123 format to a Date object.
static
<T> java.util.Map<T,T>
reverse(java.util.Map<T,T> source)
          Returns a new map that contains the reverse of the given map.
static java.lang.String serializeURLSafe(java.lang.String string)
          Serialize the given string to the short possible unique URL-safe representation.
static boolean startsWithOneOf(java.lang.String string, java.lang.String... prefixes)
          Returns true if the given string starts with one of the given prefixes.
static long stream(java.io.InputStream input, java.io.OutputStream output)
          Stream the given input to the given output by NIO ByteBuffer.
static
<T> java.util.Set<T>
unmodifiableSet(java.lang.Object... values)
          Creates an unmodifiable set based on the given values.
static java.lang.String unserializeURLSafe(java.lang.String string)
          Unserialize the given serialized URL-safe string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isEmpty

public static boolean isEmpty(java.lang.String string)
Returns true if the given string is null or is empty.

Parameters:
string - The string to be checked on emptiness.
Returns:
True if the given string is null or is empty.

isEmpty

public static boolean isEmpty(java.lang.Object[] array)
Returns true if the given array is null or is empty.

Parameters:
array - The array to be checked on emptiness.
Returns:
True if the given array is null or is empty.

isEmpty

public static boolean isEmpty(java.util.Collection<?> collection)
Returns true if the given collection is null or is empty.

Parameters:
collection - The collection to be checked on emptiness.
Returns:
True if the given collection is null or is empty.

isEmpty

public static boolean isEmpty(java.util.Map<?,?> map)
Returns true if the given map is null or is empty.

Parameters:
map - The map to be checked on emptiness.
Returns:
True if the given map is null or is empty.

isEmpty

public static boolean isEmpty(java.lang.Object value)
Returns 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.

Parameters:
value - The value to be checked on emptiness.
Returns:
True if the given value is null or is empty.

coalesce

public static <T> T coalesce(T... objects)
Returns the first non-null object of the argument list, or null if there is no such element.

Type Parameters:
T - The generic object type.
Parameters:
objects - The argument list of objects to be tested for non-null.
Returns:
The first non-null object of the argument list, or null if there is no such element.

isOneOf

public static <T> boolean isOneOf(T object,
                                  T... objects)
Returns true if the given object equals one of the given objects.

Type Parameters:
T - The generic object type.
Parameters:
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.
Returns:
true if the given object equals one of the given objects.

startsWithOneOf

public static boolean startsWithOneOf(java.lang.String string,
                                      java.lang.String... prefixes)
Returns true if the given string starts with one of the given prefixes.

Parameters:
string - The object to be checked if it starts with one of the given prefixes.
prefixes - The argument list of prefixes to be checked
Returns:
true if the given string starts with one of the given prefixes.
Since:
1.4

stream

public static long stream(java.io.InputStream input,
                          java.io.OutputStream output)
                   throws java.io.IOException
Stream the given input to the given output by NIO ByteBuffer. Both the input and output streams will implicitly be closed after streaming, regardless of whether an exception is been thrown or not.

Parameters:
input - The input stream.
output - The output stream.
Returns:
The length of the written bytes.
Throws:
java.io.IOException - When an I/O error occurs.

close

public static java.io.IOException close(java.io.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.

Parameters:
resource - The closeable resource to be closed.
Returns:
The caught IOException, or null if none is been thrown.

unmodifiableSet

public static <T> java.util.Set<T> unmodifiableSet(java.lang.Object... values)
Creates an unmodifiable set based on the given values. If one of the values is an instance of an array or a collection, then each of its values will also be merged into the set. Nested arrays or collections will result in a ClassCastException.

Parameters:
values - The values to create an unmodifiable set for.
Returns:
An unmodifiable set based on the given values.
Throws:
java.lang.ClassCastException - When one of the values or one of the arrays or collections is of wrong type.
Since:
1.1

csvToList

public static java.util.List<java.lang.String> csvToList(java.lang.String values)
Converts comma separated values in a string into a list with those values.

E.g. a string with "foo, bar, kaz" will be converted into a List with values:

Note that whitespace will be stripped. Empty entries are not supported. This method defaults to using a comma (",") as delimiter. See csvToList(String, String) for when a different delimiter is needed.

Parameters:
values - string with comma separated values
Returns:
a list with all values encountered in the values argument, can be the empty list.
Since:
1.4

csvToList

public static java.util.List<java.lang.String> csvToList(java.lang.String values,
                                                         java.lang.String delimiter)
Converts comma separated values in a string into a list with those values.

E.g. a string with "foo, bar, kaz" will be converted into a List with values:

Note that whitespace will be stripped. Empty entries are not supported.

Parameters:
values - string with comma separated values
delimiter - the delimiter used to separate the actual values in the values parameter.
Returns:
a list with all values encountered in the values argument, can be the empty list.
Since:
1.4

reverse

public static <T> java.util.Map<T,T> reverse(java.util.Map<T,T> source)
Returns a new map that contains the reverse of the given map.

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.

Parameters:
source - the map that is to be reversed
Returns:
the reverse of the given map

formatRFC1123

public static java.lang.String formatRFC1123(java.util.Date date)
Formats the given Date to a string in RFC1123 format. This format is used in HTTP headers and in JavaScript Date constructor.

Parameters:
date - The Date to be formatted to a string in RFC1123 format.
Returns:
The formatted string.
Since:
1.2

parseRFC1123

public static java.util.Date parseRFC1123(java.lang.String string)
                                   throws java.text.ParseException
Parses the given string in RFC1123 format to a Date object.

Parameters:
string - The string in RFC1123 format to be parsed to a Date object.
Returns:
The parsed Date.
Throws:
java.text.ParseException - When the given string is not in RFC1123 format.
Since:
1.2

serializeURLSafe

public static java.lang.String serializeURLSafe(java.lang.String string)
Serialize the given string to the short possible unique URL-safe representation. The current implementation will decode the given string with UTF-8 and then compress it with ZLIB using "best compression" algorithm and then Base64-encode the resulting bytes whereafter the Base64 characters /, + 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).

Parameters:
string - The string to be serialized.
Returns:
The serialized URL-safe string, or null when the given string is itself null.
Since:
1.2

unserializeURLSafe

public static java.lang.String unserializeURLSafe(java.lang.String string)
Unserialize the given serialized URL-safe string. This does the reverse of serializeURLSafe(String).

Parameters:
string - The serialized URL-safe string to be unserialized.
Returns:
The unserialized string, or null when the given string is by itself null.
Throws:
java.lang.IllegalArgumentException - When the given serialized URL-safe string is not in valid format as returned by serializeURLSafe(String).
Since:
1.2

encodeURL

public static java.lang.String encodeURL(java.lang.String string)
URL-encode the given string using UTF-8.

Parameters:
string - The string to be URL-encoded using UTF-8.
Returns:
The given string, URL-encoded using UTF-8, or null if null was given.
Throws:
java.lang.UnsupportedOperationException - When UTF-8 is not supported.
Since:
1.4

decodeURL

public static java.lang.String decodeURL(java.lang.String string)
URL-decode the given string using UTF-8.

Parameters:
string - The string to be URL-decode using UTF-8.
Returns:
The given string, URL-decode using UTF-8, or null if null was given.
Throws:
java.lang.UnsupportedOperationException - When UTF-8 is not supported.
Since:
1.4