org.jminor.common.model
Class Util

java.lang.Object
  extended by org.jminor.common.model.Util

public final class Util
extends Object

A static utility class.


Nested Class Summary
static interface Util.HashKeyProvider<K,V>
          Provides objects of type K, derived from a value of type V, for hashing said value via .hashCode().
 
Field Summary
static String CONFIGURATION_FILE
          Specifies the configuration file name to search for and parse at startup, relative to user.dir, this is done last, so that settings in the configuration file override settings gotten via runtime parameters
Value type: String
Default value: null
static String PREF_DEFAULT_USERNAME
           
static String VERSION_FILE
           
 
Method Summary
static void closeSilently(Closeable... closeables)
           
static void closeSilently(ResultSet... resultSets)
           
static void closeSilently(Statement... statements)
           
static void collate(List<?> values)
           
static String createRandomString(int minLength, int maxLength)
           
static List<Object> deserializeFromFile(File file)
           
static boolean equal(Object one, Object two)
          True if the given objects are equal.
static long getAllocatedMemory()
           
static String getArrayContentsAsString(Object[] items, boolean onePerLine)
           
static byte[] getBytesFromFile(File file)
           
static String getCollectionContentsAsString(Collection<?> collection, boolean onePerLine)
           
static String getDefaultUserName(String applicationIdentifier, String defaultName)
           
static String getDelimitedString(String[][] headers, String[][] data, String delimiter)
           
static Double getDouble(String text)
           
static long getFreeMemory()
           
static Integer getInt(String text)
           
static Long getLong(String text)
           
static long getMaxMemory()
           
static String getMemoryUsageString()
           
static Registry getRegistry()
           
static String getSystemProperties()
           
static String getTextFileContents(Class resourceClass, String resourceName)
          Fetch the entire contents of a resource textfile, and return it in a String, using the default Charset.
static String getTextFileContents(Class resourceClass, String resourceName, Charset charset)
          Fetch the entire contents of a resource textfile, and return it in a String.
static String getTextFileContents(InputStream inputStream, Charset charset)
           
static String getTextFileContents(String filename, Charset charset)
           
static ThreadLocal<Collator> getThreadLocalCollator()
           
static Class<?> getTypeClass(int sqlType)
           
static URI getURI(String urlOrPath)
           
static Collection<URI> getURIs(Collection<String> urlsOrPaths)
           
static long getUsedMemory()
           
static String getUserPreference(String key, String defaultValue)
           
static String getVersion()
           
static String getVersionAndBuildNumber()
           
static
<T> T
initializeProxy(Class<T> clazz, InvocationHandler invocationHandler)
           
static void initializeRegistry()
          Initializes a Registry if one is not running
static boolean isHostReachable(String host, int timeout)
          Returns true if the given host is reachable, false if it is not or an exception is thrown while trying
static
<K,V> Map<K,Collection<V>>
map(Collection<V> values, Util.HashKeyProvider<K,V> keyProvider)
          Maps the given values according to the keys provided by the given key provider.
static boolean notNull(Object... objects)
          Checks if any of the given objects is null
static boolean nullOrEmpty(String... strings)
           
static boolean onClasspath(String classname)
           
static String padString(String string, int length, char padChar, boolean left)
           
static void parseConfigurationFile()
          Parses the configuration file specified by the CONFIGURATION_FILE property
static void printArrayContents(Object[] objects)
           
static void printArrayContents(Object[] objects, boolean onePerLine)
           
static void printListContents(List<?> list)
           
static void printMemoryUsage(long interval)
           
static void putUserPreference(String key, String value)
           
static
<T> T
rejectNullValue(T value, String valueName)
          Throws an IllegalArgumentException complaining about valueName being null
static void require(String propertyName, String value)
           
static double roundDouble(double d, int places)
           
static void serializeToFile(Collection objects, File file)
           
static void setClipboard(String string)
           
static void setDefaultUserName(String applicationClassName, String username)
           
static Exception unwrapAndLog(Exception exception, Class<? extends Exception> wrappingExceptionClass, org.slf4j.Logger logger)
           
static Exception unwrapAndLog(Exception exception, Class<? extends Exception> wrappingExceptionClass, org.slf4j.Logger logger, Class<? extends Exception>... dontLog)
           
static void writeDelimitedFile(String[][] headers, String[][] data, String delimiter, File file)
           
static void writeFile(String contents, File file)
           
static void writeFile(String contents, File file, boolean append)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VERSION_FILE

public static final String VERSION_FILE
See Also:
Constant Field Values

PREF_DEFAULT_USERNAME

public static final String PREF_DEFAULT_USERNAME
See Also:
Constant Field Values

CONFIGURATION_FILE

public static final String CONFIGURATION_FILE
Specifies the configuration file name to search for and parse at startup, relative to user.dir, this is done last, so that settings in the configuration file override settings gotten via runtime parameters
Value type: String
Default value: null

See Also:
Constant Field Values
Method Detail

isHostReachable

public static boolean isHostReachable(String host,
                                      int timeout)
Returns true if the given host is reachable, false if it is not or an exception is thrown while trying

Parameters:
host - the hostname
timeout - the timeout in milliseconds
Returns:
true if the host is reachable

getUserPreference

public static String getUserPreference(String key,
                                       String defaultValue)

putUserPreference

public static void putUserPreference(String key,
                                     String value)

getDefaultUserName

public static String getDefaultUserName(String applicationIdentifier,
                                        String defaultName)

setDefaultUserName

public static void setDefaultUserName(String applicationClassName,
                                      String username)

padString

public static String padString(String string,
                               int length,
                               char padChar,
                               boolean left)

getInt

public static Integer getInt(String text)

getDouble

public static Double getDouble(String text)

getLong

public static Long getLong(String text)

printListContents

public static void printListContents(List<?> list)

printArrayContents

public static void printArrayContents(Object[] objects)

printArrayContents

public static void printArrayContents(Object[] objects,
                                      boolean onePerLine)

getCollectionContentsAsString

public static String getCollectionContentsAsString(Collection<?> collection,
                                                   boolean onePerLine)

getArrayContentsAsString

public static String getArrayContentsAsString(Object[] items,
                                              boolean onePerLine)

printMemoryUsage

public static void printMemoryUsage(long interval)

getAllocatedMemory

public static long getAllocatedMemory()

getFreeMemory

public static long getFreeMemory()

getMaxMemory

public static long getMaxMemory()

getUsedMemory

public static long getUsedMemory()

getMemoryUsageString

public static String getMemoryUsageString()

getTextFileContents

public static String getTextFileContents(Class resourceClass,
                                         String resourceName)
                                  throws IOException
Fetch the entire contents of a resource textfile, and return it in a String, using the default Charset.

Parameters:
resourceClass - the resource class
resourceName - the name of the resource to retrieve
Returns:
the contents of the resource file
Throws:
IOException - in case an IOException occurs

getTextFileContents

public static String getTextFileContents(Class resourceClass,
                                         String resourceName,
                                         Charset charset)
                                  throws IOException
Fetch the entire contents of a resource textfile, and return it in a String.

Parameters:
resourceClass - the resource class
resourceName - the name of the resource to retrieve
charset - the Charset to use when reading the file contents
Returns:
the contents of the resource file
Throws:
IOException - in case an IOException occurs

getTextFileContents

public static String getTextFileContents(String filename,
                                         Charset charset)
                                  throws IOException
Throws:
IOException

getTextFileContents

public static String getTextFileContents(InputStream inputStream,
                                         Charset charset)
                                  throws IOException
Throws:
IOException

getSystemProperties

public static String getSystemProperties()

setClipboard

public static void setClipboard(String string)

getDelimitedString

public static String getDelimitedString(String[][] headers,
                                        String[][] data,
                                        String delimiter)

writeDelimitedFile

public static void writeDelimitedFile(String[][] headers,
                                      String[][] data,
                                      String delimiter,
                                      File file)

writeFile

public static void writeFile(String contents,
                             File file)

writeFile

public static void writeFile(String contents,
                             File file,
                             boolean append)

deserializeFromFile

public static List<Object> deserializeFromFile(File file)

serializeToFile

public static void serializeToFile(Collection objects,
                                   File file)

equal

public static boolean equal(Object one,
                            Object two)
True if the given objects are equal. Both objects being null results in true.

Parameters:
one - the first object
two - the second object
Returns:
true if the given objects are equal

getVersion

public static String getVersion()

getVersionAndBuildNumber

public static String getVersionAndBuildNumber()

roundDouble

public static double roundDouble(double d,
                                 int places)

notNull

public static boolean notNull(Object... objects)
Checks if any of the given objects is null

Parameters:
objects - the objects to check
Returns:
true if none of the given objects is null

getBytesFromFile

public static byte[] getBytesFromFile(File file)
                               throws IOException
Throws:
IOException

createRandomString

public static String createRandomString(int minLength,
                                        int maxLength)

collate

public static void collate(List<?> values)

getURI

public static URI getURI(String urlOrPath)
                  throws URISyntaxException
Throws:
URISyntaxException

getURIs

public static Collection<URI> getURIs(Collection<String> urlsOrPaths)
                               throws URISyntaxException
Throws:
URISyntaxException

getThreadLocalCollator

public static ThreadLocal<Collator> getThreadLocalCollator()

rejectNullValue

public static <T> T rejectNullValue(T value,
                                    String valueName)
Throws an IllegalArgumentException complaining about valueName being null

Parameters:
value - the value to check
valueName - the name of the value being checked
Returns:
the value in case it was not null
Throws:
IllegalArgumentException - if value is null

closeSilently

public static void closeSilently(ResultSet... resultSets)

closeSilently

public static void closeSilently(Statement... statements)

closeSilently

public static void closeSilently(Closeable... closeables)

map

public static <K,V> Map<K,Collection<V>> map(Collection<V> values,
                                             Util.HashKeyProvider<K,V> keyProvider)
Maps the given values according to the keys provided by the given key provider.
 class Person {
   String name;
   Integer age;
   ...
 }

 List persons = ...;
 HashKeyProvider ageKeyProvider = new HashKeyProvider() {
   public Integer getKey(Person person) {
     return person.getAge();
   }
 };
 Map> personsByAge = Util.map(persons, ageKeyProvider);
 

Type Parameters:
K - the key type
V - the value type
Parameters:
values - the values to map
keyProvider - the object providing keys to use when hashing the values
Returns:
a map with the values hashed by their respective key values

onClasspath

public static boolean onClasspath(String classname)

require

public static void require(String propertyName,
                           String value)

nullOrEmpty

public static boolean nullOrEmpty(String... strings)

getTypeClass

public static Class<?> getTypeClass(int sqlType)
Parameters:
sqlType - the type
Returns:
the Class representing the given type

parseConfigurationFile

public static void parseConfigurationFile()
Parses the configuration file specified by the CONFIGURATION_FILE property

See Also:
CONFIGURATION_FILE

initializeRegistry

public static void initializeRegistry()
                               throws RemoteException
Initializes a Registry if one is not running

Throws:
RemoteException - in case of an exception

getRegistry

public static Registry getRegistry()
                            throws RemoteException
Returns:
the local registry
Throws:
RemoteException - in case of an exception

initializeProxy

public static <T> T initializeProxy(Class<T> clazz,
                                    InvocationHandler invocationHandler)

unwrapAndLog

public static Exception unwrapAndLog(Exception exception,
                                     Class<? extends Exception> wrappingExceptionClass,
                                     org.slf4j.Logger logger)

unwrapAndLog

public static Exception unwrapAndLog(Exception exception,
                                     Class<? extends Exception> wrappingExceptionClass,
                                     org.slf4j.Logger logger,
                                     Class<? extends Exception>... dontLog)