gov.nih.nci.caarray.util
Class CaArrayUtils

java.lang.Object
  extended by gov.nih.nci.caarray.util.CaArrayUtils

public final class CaArrayUtils
extends java.lang.Object

Utility classes for our project.


Method Summary
static void bindPropertiesAsNamed(java.lang.String resourceName, com.google.inject.Binder binder)
          Load a properties file from given classpath resource, and create a constant binding to @Named(key) for each property using the given binder.
static void blankStringPropsToNull(java.lang.Object o)
          For each String bean property on o, if o is blank or empty, converts that property to null.
static ReflectionHelper createReflectionHelper(java.lang.Class<?> clazz)
          For given class, returns a ReflectionHelper instance with property accessors for the class.
static java.lang.String dequoteString(java.lang.String str)
          Removes matched quotes (single or double) from a string.
static java.io.Serializable deserialize(byte[] bytes)
          Deserializes an object from a gzipped serialized representation.
static java.io.Serializable deserialize(java.io.InputStream is)
          Deserializes an object from a gzipped serialized representation.
static java.lang.Object emptyCollectionOrMapFor(java.lang.Class<?> collectionType)
          Returns an empty collection or map of the appropriate type for a given collection class.
static
<T> T
find(java.lang.Iterable<T> iterable, com.google.common.base.Predicate<? super T> predicate)
          Returns the first element in iterable that satisfies the given predicate, or null if no elements do.
static byte[] gunzip(byte[] input)
          returns a byte array representing the input byte array, un-gzipped.
static byte[] gzip(byte[] input)
          returns a byte array representing the input byte array, gzipped.
static java.lang.String join(boolean[] values, java.lang.String separator)
           Joins the elements of the provided array into a single String containing the provided list of elements.
static java.lang.String join(double[] values, java.lang.String separator)
           Joins the elements of the provided array into a single String containing the provided list of elements.
static java.lang.String join(float[] values, java.lang.String separator)
           Joins the elements of the provided array into a single String containing the provided list of elements.
static java.lang.String join(int[] values, java.lang.String separator)
           Joins the elements of the provided array into a single String containing the provided list of elements.
static java.lang.String join(long[] values, java.lang.String separator)
           Joins the elements of the provided array into a single String containing the provided list of elements.
static java.lang.String join(short[] values, java.lang.String separator)
           Joins the elements of the provided array into a single String containing the provided list of elements.
static java.lang.String joinAsCsv(java.lang.String[] values)
          Joins the given values as a comma-separated string.
static java.net.URI makeUriQuietly(java.lang.String uri)
          Utility method to create a URI from given string without throwing a checked exception.
static java.net.URI makeUriQuietly(java.lang.String scheme, java.lang.String schemeSpecificPart)
          Utility method to create a URI from given scheme and scheme specific part without throwing a checked exception.
static
<E extends java.lang.Enum<E>>
java.util.Set<java.lang.String>
namesForEnums(java.lang.Iterable<E> enums)
          Return the constant names of the given enum instances.
static byte[] serialize(java.io.Serializable serializable)
          Serializes the given object (zipped) to a byte array.
static java.lang.String[] splitFromCsv(java.lang.String s)
           Splits the provided CSV String into an array of parsed values.
static boolean[] splitIntoBooleans(java.lang.String s, java.lang.String separator)
           Splits the provided text into an array of parsed boolean values, using specified separator.
static double[] splitIntoDoubles(java.lang.String s, java.lang.String separator)
           Splits the provided text into an array of parsed double values, using specified separator.
static float[] splitIntoFloats(java.lang.String s, java.lang.String separator)
           Splits the provided text into an array of parsed float values, using specified separator.
static int[] splitIntoInts(java.lang.String s, java.lang.String separator)
           Splits the provided text into an array of parsed int values, using specified separator.
static long[] splitIntoLongs(java.lang.String s, java.lang.String separator)
           Splits the provided text into an array of parsed long values, using specified separator.
static short[] splitIntoShorts(java.lang.String s, java.lang.String separator)
           Splits the provided text into an array of parsed short values, using specified separator.
static
<T> T
uniqueResult(java.util.Collection<T> results)
          Method to take a get a unique result from a set and return it or null.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

uniqueResult

public static <T> T uniqueResult(java.util.Collection<T> results)
Method to take a get a unique result from a set and return it or null.

Type Parameters:
T - the type of the returned object
Parameters:
results - the set of results returned from a query
Returns:
the first result in the set or null

emptyCollectionOrMapFor

public static java.lang.Object emptyCollectionOrMapFor(java.lang.Class<?> collectionType)
Returns an empty collection or map of the appropriate type for a given collection class. By default, returns an empty list, but will return an empty set, empty sorted set, or empty map if the passed in type is a subclass of Set, SortedSet, or Map respectively.

Parameters:
collectionType - the class of whose type to return an empty collection or map
Returns:
the empty collection or map

dequoteString

public static java.lang.String dequoteString(java.lang.String str)
Removes matched quotes (single or double) from a string. Quotes are only removed from the first and last characters of the string.

Parameters:
str - string to dequote
Returns:
the dequoted string or the original string, if no changes were made

createReflectionHelper

public static ReflectionHelper createReflectionHelper(java.lang.Class<?> clazz)
For given class, returns a ReflectionHelper instance with property accessors for the class.

Parameters:
clazz - the class
Returns:
the ReflectionHelper

blankStringPropsToNull

public static void blankStringPropsToNull(java.lang.Object o)
For each String bean property on o, if o is blank or empty, converts that property to null.

Parameters:
o - object to convert properties on.

join

public static java.lang.String join(boolean[] values,
                                    java.lang.String separator)

Joins the elements of the provided array into a single String containing the provided list of elements.

Parameters:
values - the values to join together, may be null (in which case an empty String is returned)
separator - the separator to use
Returns:
the joined String, empty if null array input

join

public static java.lang.String join(int[] values,
                                    java.lang.String separator)

Joins the elements of the provided array into a single String containing the provided list of elements.

Parameters:
values - the values to join together, may be null (in which case an empty String is returned)
separator - the separator to use
Returns:
the joined String, empty if null array input

join

public static java.lang.String join(long[] values,
                                    java.lang.String separator)

Joins the elements of the provided array into a single String containing the provided list of elements.

Parameters:
values - the values to join together, may be null (in which case an empty String is returned)
separator - the separator to use
Returns:
the joined String, empty if null array input

join

public static java.lang.String join(short[] values,
                                    java.lang.String separator)

Joins the elements of the provided array into a single String containing the provided list of elements.

Parameters:
values - the values to join together, may be null (in which case an empty String is returned)
separator - the separator to use
Returns:
the joined String, empty if null array input

join

public static java.lang.String join(double[] values,
                                    java.lang.String separator)

Joins the elements of the provided array into a single String containing the provided list of elements.

Parameters:
values - the values to join together, may be null (in which case an empty String is returned)
separator - the separator to use
Returns:
the joined String, empty if null array input

join

public static java.lang.String join(float[] values,
                                    java.lang.String separator)

Joins the elements of the provided array into a single String containing the provided list of elements.

Parameters:
values - the values to join together, may be null (in which case an empty String is returned)
separator - the separator to use
Returns:
the joined String, empty if null array input

joinAsCsv

public static java.lang.String joinAsCsv(java.lang.String[] values)
Joins the given values as a comma-separated string. Each value will be encoded in this string by escaping any commas in the value with a backslash.

Parameters:
values - the values to join (null is acceptable).
Returns:
the CSV string consisting of the values. If the values were null, an empty String.

splitIntoBooleans

public static boolean[] splitIntoBooleans(java.lang.String s,
                                          java.lang.String separator)

Splits the provided text into an array of parsed boolean values, using specified separator.

Each value should be encoded using the literal representation of the XML Schema xs:boolean type

Parameters:
s - the string to parse
separator - the separator between values
Returns:
the array of parsed values

splitIntoShorts

public static short[] splitIntoShorts(java.lang.String s,
                                      java.lang.String separator)

Splits the provided text into an array of parsed short values, using specified separator.

Each value should be encoded using the literal representation of the XML Schema xs:short type

Parameters:
s - the string to parse
separator - the separator between values
Returns:
the array of parsed values

splitIntoLongs

public static long[] splitIntoLongs(java.lang.String s,
                                    java.lang.String separator)

Splits the provided text into an array of parsed long values, using specified separator.

Each value should be encoded using the literal representation of the XML Schema xs:long type

Parameters:
s - the string to parse
separator - the separator between values
Returns:
the array of parsed values

splitIntoInts

public static int[] splitIntoInts(java.lang.String s,
                                  java.lang.String separator)

Splits the provided text into an array of parsed int values, using specified separator.

Each value should be encoded using the literal representation of the XML Schema xs:int type

Parameters:
s - the string to parse
separator - the separator between values
Returns:
the array of parsed values

splitIntoFloats

public static float[] splitIntoFloats(java.lang.String s,
                                      java.lang.String separator)

Splits the provided text into an array of parsed float values, using specified separator.

Each value should be encoded using the literal representation of the XML Schema xs:float type

Parameters:
s - the string to parse
separator - the separator between values
Returns:
the array of parsed values

splitIntoDoubles

public static double[] splitIntoDoubles(java.lang.String s,
                                        java.lang.String separator)

Splits the provided text into an array of parsed double values, using specified separator.

Each value should be encoded using the literal representation of the XML Schema xs:double type

Parameters:
s - the string to parse
separator - the separator between values
Returns:
the array of parsed values

splitFromCsv

public static java.lang.String[] splitFromCsv(java.lang.String s)

Splits the provided CSV String into an array of parsed values.

Each value within the String will be unescaped by converting any backslash-comma combinations back to commas.

Parameters:
s - string containing a comma-separated list of strings.
Returns:
the array of parsed Strings. If s did not contain any comma separated Strings, an empty String. If s was not a valid CSV string, an IllegalArgumentException is thrown.

namesForEnums

public static <E extends java.lang.Enum<E>> java.util.Set<java.lang.String> namesForEnums(java.lang.Iterable<E> enums)
Return the constant names of the given enum instances.

Type Parameters:
E - the type of the enum instances
Parameters:
enums - the enum instances whose constant names to return
Returns:
the names, e.g. the set of enum.name() for each enum in enums

serialize

public static byte[] serialize(java.io.Serializable serializable)
Serializes the given object (zipped) to a byte array.

Parameters:
serializable - object to serialize
Returns:
the serialized object as a byte array.

deserialize

public static java.io.Serializable deserialize(byte[] bytes)
Deserializes an object from a gzipped serialized representation.

Parameters:
bytes - the byte array containing the gzipped serialized representation of an object
Returns:
the deserialized object

deserialize

public static java.io.Serializable deserialize(java.io.InputStream is)
Deserializes an object from a gzipped serialized representation.

Parameters:
is - the InputStream which will produce the gzipped serialized representation of an object
Returns:
the deserialized object

find

public static <T> T find(java.lang.Iterable<T> iterable,
                         com.google.common.base.Predicate<? super T> predicate)
Returns the first element in iterable that satisfies the given predicate, or null if no elements do. This is identical to Iterables.find from Google's Collections library, except it returns null rather than throw NoSuchElementException if there are no matches.

Type Parameters:
T - the type of elements of iterable
Parameters:
iterable - the iterable to search throw
predicate - the predicate to satisfy
Returns:
the first element matching the predicate, or null if no matches

makeUriQuietly

public static java.net.URI makeUriQuietly(java.lang.String uri)
Utility method to create a URI from given string without throwing a checked exception. This simply wraps new URI(uri), throwing a runtime exception if this results in a URISyntaxException. For use with known-good uri strings, to avoid having a try-catch block.

Parameters:
uri - the URI string
Returns:
the URI corresponding to uri

makeUriQuietly

public static java.net.URI makeUriQuietly(java.lang.String scheme,
                                          java.lang.String schemeSpecificPart)
Utility method to create a URI from given scheme and scheme specific part without throwing a checked exception. This simply wraps new URI(scheme, schemeSpecificPart), throwing a runtime exception if this results in a URISyntaxException. For use with known-good scheme and scheme specific part, to avoid having a try-catch block.

Parameters:
scheme - the scheme
schemeSpecificPart - the schemeSpecificPart
Returns:
the URI corresponding to scheme:schemeSpecificPart

gzip

public static byte[] gzip(byte[] input)
                   throws java.io.IOException
returns a byte array representing the input byte array, gzipped. Use this method with caution - for large arrays, you should use streams instead.

Parameters:
input - the content to compress
Returns:
the gzipped input
Throws:
java.io.IOException - if there is a problem zipping the content

gunzip

public static byte[] gunzip(byte[] input)
                     throws java.io.IOException
returns a byte array representing the input byte array, un-gzipped. Use this method with caution - for large arrays, you should use streams instead.

Parameters:
input - the content to uncompress, should be in gzip format
Returns:
the uncompressed input
Throws:
java.io.IOException - if there is a problem uncompressing the content, including if the input is not gzipped

bindPropertiesAsNamed

public static void bindPropertiesAsNamed(java.lang.String resourceName,
                                         com.google.inject.Binder binder)
Load a properties file from given classpath resource, and create a constant binding to @Named(key) for each property using the given binder. If the resource cannot be loaded, does nothing.

Parameters:
resourceName - the resource to load. it is loaded using the classloader of this class.
binder - the binder to use for pinding the key=property values