public final class Utils extends Object
Collection of general utility methods that do not fit in one of the more specific classes.
This class is indeed intented for internal usage only. We won't add methods here on user request. We only add methods here once we encounter non-DRY code in OmniFaces codebase. The methods may be renamed/changed without notice.
We don't stop you from using it if you think you find it useful, but you'd really better pick e.g. Google Guava or perhaps the good 'ol Apache Commons. This Utils class exists because OmniFaces intends to be free of 3rd party dependencies.
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 |
encodeURI(String string)
URI-encode 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 Object |
getDefaultValue(Class<?> cls)
Returns the default value of the given class, covering primitives.
|
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 object is null or an empty array or has an empty toString() result. |
static boolean |
isEmpty(Part part)
Returns
true if the given part 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 boolean |
isOneAnnotationPresent(Class<?> cls,
Class<? extends Annotation>... annotations)
Returns
true if the given class has at least one of the given annotations. |
static boolean |
isOneInstanceOf(Class<?> cls,
Class<?>... classes)
Returns
true if an instance of the given class could also be an instance of one of the given classes. |
static <T> boolean |
isOneOf(T object,
T... objects)
Returns
true if the given object equals one of the given objects. |
static boolean |
isSerializable(Object object)
Returns
true if the given object is serializable. |
static <E> List<E> |
iterableToList(Iterable<E> iterable)
Converts an iterable into a list.
|
static Locale |
parseLocale(Object locale)
Parses the given object representing the locale to a
Locale object. |
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(File file,
OutputStream output,
long start,
long length)
Stream a specified range of the given file to the given output via NIO
Channels and a directly allocated
NIO ByteBuffer . |
static long |
stream(InputStream input,
OutputStream output)
Stream the given input to the given output via NIO
Channels and a directly allocated NIO
ByteBuffer . |
static byte[] |
toByteArray(InputStream input)
Read the given input stream into a byte array.
|
static <E> Set<E> |
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 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(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(Part part)
true
if the given part is null or is empty.part
- The part to be checked on emptiness.true
if the given part is null or is empty.public static boolean isEmpty(Object value)
true
if the given object is null or an empty array or has an empty toString() result.value
- The value to be checked on emptiness.true
if the given object is null or an empty array or has an empty toString() result.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.@SafeVarargs 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.@SafeVarargs 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 boolean isOneInstanceOf(Class<?> cls, Class<?>... classes)
true
if an instance of the given class could also be an instance of one of the given classes.cls
- The class to be checked if it could also be an instance of one of the given classes.classes
- The argument list of classes to be tested.true
if the given class could also be an instance of one of the given classes.@SafeVarargs public static boolean isOneAnnotationPresent(Class<?> cls, Class<? extends Annotation>... annotations)
true
if the given class has at least one of the given annotations.cls
- The class to be checked if it has at least one of the given annotations.annotations
- The argument list of annotations to be tested on the given class.true
if the given clazz would be an instance of one of the given clazzes.public static Object getDefaultValue(Class<?> cls)
int.class
, then it will return 0
. Autoboxing will do the rest.
Non-primitives and void.class
will return null
.cls
- The class to obtain the default value for.public static long stream(InputStream input, OutputStream output) throws IOException
Channels
and a directly allocated NIO
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 long stream(File file, OutputStream output, long start, long length) throws IOException
Channels
and a directly allocated
NIO ByteBuffer
. The output stream will only implicitly be closed after streaming when the specified range
represents the whole file, regardless of whether an exception is been thrown or not.file
- The file.output
- The output stream.start
- The start position (offset).length
- The (intented) length of written bytes.IOException
- When an I/O error occurs.public static byte[] toByteArray(InputStream input) throws IOException
input
- The input 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 boolean isSerializable(Object object)
true
if the given object is serializable.object
- The object to be tested.true
if the given object is serializable.public static <E> Set<E> unmodifiableSet(Object... values)
ClassCastException
.E
- The expected set element type.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 <E> List<E> iterableToList(Iterable<E> 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.
E
- The generic iterable element type.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.
T
- The generic map key/value type.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 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 Locale parseLocale(Object locale)
Locale
object.
If it is null
, then return null
.
Else if it is already an instance of Locale
, then just return it.
Else if it is in pattern ISO 639 alpha-2/3, optionally followed by "_" and ISO 3166-1 alpha-2 country code, then
split the language/country and construct a new Locale
with it.
Else parse it via Locale.forLanguageTag(String)
and return it.locale
- The object representing the locale.Locale
.public static String serializeURLSafe(String string)
=
padding, 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).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 this platform does not support UTF-8.public static String decodeURL(String string)
string
- The string to be URL-decode using UTF-8.null
if null
was given.UnsupportedOperationException
- When this platform does not support UTF-8.public static String encodeURI(String string)
URLEncoder
is actually only for www (HTML) form based query string parameter
values (as used when a webbrowser submits a HTML form). URI encoding has a lot in common with URL encoding, but
the space has to be %20 and some chars doesn't necessarily need to be encoded.string
- The string to be URI-encoded using UTF-8.null
if null
was given.UnsupportedOperationException
- When this platform does not support UTF-8.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.Copyright © 2012–2019 OmniFaces. All rights reserved.