com.ashridgetech.prevdb.common
Class Util

java.lang.Object
  |
  +--com.ashridgetech.prevdb.common.Util

public class Util
extends Object

Utility methods used by various classes.

Version:
1.0
Author:
paulb

Field Summary
static Object[] EMPTY_ARRAY
           
static Class[] EMPTY_CLASS_ARRAY
           
static String[] EMPTY_STRING_ARRAY
           
static Pattern NAME_PATTERN
          A Pattern which finds '.' , '/', '\' so it can be used for splitting file or package names.
static Pattern SPACE_PATTERN
          A Pattern which finds whitespace so it can be used for splitting space-separated lists of tokens.
 
Method Summary
static String blankToNull(String s)
          Change a null or blank String to null.
static Object callStaticMethod(Class clazz, String name)
          Get the value of a static method of a Class with no arguments.
static String capitalize(String s)
          Get a String with the first character converted to upper case.
static void checkArg(boolean b, String message)
           
static void checkNotEmpty(String s, String name)
           
static void checkNotNull(Object o, String name)
           
static void checkNull(Object o, String name)
           
static void checkState(boolean b, String message)
           
static List concat(Collection c1, Collection c2)
          Concatenate two Collections, in their natural order of iteration, into a single result List.
static String encode(String s)
          Encode a String in the manner needed for a Java source string literal, replacing characters with escape codes as needed.
static Class findClass(String className)
          Find a public class by name.
static Constructor findConstructor(Class clazz, Class argType)
          Find a public constructor with a given single argument type in a class.
static Constructor findConstructor(Class clazz, Class[] argTypes)
          Find a constructor with parameter types that are assignable from the given argTypes, not necessarily exact matches, as found by Class.getConstructor(java.lang.Class[]).
static Constructor findExactConstructor(Class clazz, Class[] argTypes)
          Find a public constructor with a given argument list in a class.
static Class findNestedClass(Class outerClass, String className)
          Find a public static nested class by name within another class.
static Object getFirstElement(Collection c)
          Get the first element of a collection.
static Class[] getTypes(Object[] objects)
          Get the types of an array of objects.
static boolean isBlank(String s)
          Test whether a String is null or empty or all whitespace.
static List list(Object o1)
          Create a List from the arguments.
static List list(Object o1, Object o2)
          Create a List from the arguments.
static List list(Object o1, Object o2, Object o3)
          Create a List from the arguments.
static String localizePath(String path)
          Localize a path name by replacing both '\' and '/' with the local File.separatorChar.
static void mkdirs(File dir)
          Check for the existence of a directory and create it if necessary, including any non-existent parent directories.
static String naturalize(String s)
          Get a String from a camel case name with spaces between each word, and underscores replaced by spaces.
static Object newInstance(Class clazz)
          Create an object of a class with no constructor argument.
static Object newInstance(Class clazz, Object arg)
          Create an object of a class with a given single constructor argument.
static Object newInstance(Class clazz, Object[] args)
          Create an object of a class with a given set of arguments.
static Object newInstance(Class clazz, Object arg0, Object arg1)
          Create an object of a class with a given pair of constructor arguments.
static Object newInstance(String className)
          Create an object of a named class with no constructor argument.
static Object newInstance(String className, Object arg)
          Create an object of a class with a given single constructor argument.
static Object newInstance(String className, Object[] args)
          Create an object of a named class with a given set of arguments.
static String nullToBlank(String s)
          Change a null or blank String to an empty String.
static String packageName(String className)
          Get the package out of a fully qualified class name.
static String pluralize(String word)
          Get a String word made plural (in English).
static String quote(String s)
          Put double quotes around a string, and encode it.
static String readFile(String fileName)
          Read from a named file into a String.
static String readReader(Reader rdr)
          Read from a Reader into a String.
static void readToPosition(BufferedReader rdr, int line, int col)
          Read from the current position in a Reader to a given relative line and column.
static String singularize(String word)
          Get a String word made singular (in English).
static String[] splitAtSpace(String s)
          Split a string at spaces.
static String[] splitName(String name)
          Split a string at periods or file separators.
static String toString(Collection c)
           
static String uncapitalize(String s)
          Get a String with the first character converted to lower case.
static String unqualifiedClassName(String className)
          Get the name of a class, without its package or containing class.
static String[] wrap(String text, int maxLength)
          Split text at whitespace so that each line is less than maxLength.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_ARRAY

public static final Object[] EMPTY_ARRAY

EMPTY_STRING_ARRAY

public static final String[] EMPTY_STRING_ARRAY

EMPTY_CLASS_ARRAY

public static final Class[] EMPTY_CLASS_ARRAY

NAME_PATTERN

public static final Pattern NAME_PATTERN
A Pattern which finds '.' , '/', '\' so it can be used for splitting file or package names.


SPACE_PATTERN

public static final Pattern SPACE_PATTERN
A Pattern which finds whitespace so it can be used for splitting space-separated lists of tokens.

Method Detail

checkNotNull

public static void checkNotNull(Object o,
                                String name)

checkNotEmpty

public static void checkNotEmpty(String s,
                                 String name)

checkNull

public static void checkNull(Object o,
                             String name)

checkArg

public static void checkArg(boolean b,
                            String message)

checkState

public static void checkState(boolean b,
                              String message)

toString

public static String toString(Collection c)

getFirstElement

public static Object getFirstElement(Collection c)
Get the first element of a collection.

Parameters:
c - the collection from which to get the first element
Returns:
the first element in normal iteration order, or null if c is empty

list

public static List list(Object o1)
Create a List from the arguments.

Parameters:
o1 - the first element of the list
Returns:
the new List

list

public static List list(Object o1,
                        Object o2)
Create a List from the arguments.

Parameters:
o1 - the first element of the list
o2 - the second element of the list
Returns:
the new List

list

public static List list(Object o1,
                        Object o2,
                        Object o3)
Create a List from the arguments.

Parameters:
o1 - the first element of the list
o2 - the second element of the list
o3 - the third element of the list
Returns:
the new List

uncapitalize

public static String uncapitalize(String s)
Get a String with the first character converted to lower case.

Parameters:
s - the String to convert
Returns:
the converted String

capitalize

public static String capitalize(String s)
Get a String with the first character converted to upper case.

Parameters:
s - the String to convert
Returns:
the converted String

pluralize

public static String pluralize(String word)
Get a String word made plural (in English). Only the basic rules for English plurals are applied.

Parameters:
word - the word to convert
Returns:
the plural version of word
See Also:
singularize(java.lang.String)

singularize

public static String singularize(String word)
Get a String word made singular (in English). Only the basic rules for English plurals are applied.

Parameters:
word - the word to convert
Returns:
the plural version of word
See Also:
pluralize(java.lang.String)

naturalize

public static String naturalize(String s)
Get a String from a camel case name with spaces between each word, and underscores replaced by spaces. For example "customerStartDate" and "CUSTOMER_START_DATE" would both become "Customer Start Date".

Parameters:
s - the String to convert
Returns:
the converted String

wrap

public static String[] wrap(String text,
                            int maxLength)
Split text at whitespace so that each line is less than maxLength.

Parameters:
text - the String to split - may be null
maxLength - the maximum line length
Returns:
the split lines - an empty array if text is null or blank

isBlank

public static boolean isBlank(String s)
Test whether a String is null or empty or all whitespace.

Parameters:
s - the String to test
Returns:
true if s is either null or its length, trimmed of whitespace, is zero

blankToNull

public static String blankToNull(String s)
Change a null or blank String to null.

Parameters:
s - the String to test
Returns:
null if s is either null or its length, trimmed of whitespace, is zero, otherwise the original String
See Also:
isBlank(java.lang.String)

nullToBlank

public static String nullToBlank(String s)
Change a null or blank String to an empty String.

Parameters:
s - the String to test
Returns:
an empty String if s is either null or its length, trimmed of whitespace, is zero, otherwise the original String
See Also:
#blankToNull

packageName

public static String packageName(String className)
Get the package out of a fully qualified class name. This is assumed to be the part of the name up to the last ".". If className does not contain a "." then it is returned unchanged.

Parameters:
className - the qualified name
Returns:
the package name

unqualifiedClassName

public static String unqualifiedClassName(String className)
Get the name of a class, without its package or containing class. This is assumed to be the part of the name after the last ".". If className does not contain a "." then it is returned unchanged.

Parameters:
className - the qualified name
Returns:
the unqualified name

splitName

public static String[] splitName(String name)
Split a string at periods or file separators. This could be used to get the components of a file or package name. Leading and trailing whitespace is removed from name If the trimmed name starts with a separator, the first element of the array will be an empty string. Uses NAME_PATTERN.

Parameters:
name - the name to split
Returns:
the name components. If name is null or empty (as returned by isBlank(java.lang.String), an empty array is returned

splitAtSpace

public static String[] splitAtSpace(String s)
Split a string at spaces. Leading and trailing whitespace is removed from s

Parameters:
s - the String to split
Returns:
the components. If name is null or empty (as returned by isBlank(java.lang.String), an empty array is returned

localizePath

public static String localizePath(String path)
Localize a path name by replacing both '\' and '/' with the local File.separatorChar.

Parameters:
path - the name to split
Returns:
the localized name

findClass

public static Class findClass(String className)
Find a public class by name.

Parameters:
className - the name of the Class required
Returns:
the class, or null if no class with the given name is found

findNestedClass

public static Class findNestedClass(Class outerClass,
                                    String className)
Find a public static nested class by name within another class.

Parameters:
outerClass - the class that contains the required nested class
className - the name of the nested Class required (unqualified - does not include the name of the outer class)
Returns:
the class, or null if no class with the given name is found

findExactConstructor

public static Constructor findExactConstructor(Class clazz,
                                               Class[] argTypes)
Find a public constructor with a given argument list in a class.

Parameters:
clazz - the Class in which to look for the constructor
argTypes - the argument types of the required constructor
Returns:
the constructor, or null if no constructor with the required argument types is found

findConstructor

public static Constructor findConstructor(Class clazz,
                                          Class[] argTypes)
Find a constructor with parameter types that are assignable from the given argTypes, not necessarily exact matches, as found by Class.getConstructor(java.lang.Class[]).

Parameters:
clazz - the Class in which to find the constructor
argTypes - the types of the arguments to be passed to the constructor
Returns:
the Constructor or null if clazz does not have the correct constructor

findConstructor

public static Constructor findConstructor(Class clazz,
                                          Class argType)
Find a public constructor with a given single argument type in a class.

Parameters:
clazz - the Class in which to look for the constructor
argType - the argument type of the required constructor
Returns:
the constructor, or null if no constructor with the required argument type is found

getTypes

public static Class[] getTypes(Object[] objects)
Get the types of an array of objects.

Parameters:
objects - the Objects of which the types are required
Returns:
the types of the objects

newInstance

public static Object newInstance(Class clazz,
                                 Object[] args)
                          throws Exception
Create an object of a class with a given set of arguments. If an exception is thrown within the constructor, that exception is thrown, rather than, InvocationTargetException.

Parameters:
clazz - the Class of object to create
args - the arguments
Returns:
the new object
Throws:
Exception - if an exception occurs calling the constructor of the object.
IllegalArgumentException - if there is no constructor which can accept the given arguments

newInstance

public static Object newInstance(String className,
                                 Object[] args)
                          throws Exception
Create an object of a named class with a given set of arguments.

Parameters:
className - the name of the Class of object to create
args - the arguments
Exception
See Also:
newInstance( Class , Object[] )

newInstance

public static Object newInstance(Class clazz)
                          throws Exception
Create an object of a class with no constructor argument. See newInstance( Class , Object[] ) for details

Parameters:
clazz - the Class of object to create
Returns:
the new object
Exception
See Also:
newInstance( Class , Object[] )

newInstance

public static Object newInstance(String className)
                          throws Exception
Create an object of a named class with no constructor argument. See newInstance( Class , Object[] ) for details

Parameters:
className - the name of the Class of object to create
Returns:
the new object
Exception
See Also:
newInstance( Class , Object[] )

newInstance

public static Object newInstance(Class clazz,
                                 Object arg)
                          throws Exception
Create an object of a class with a given single constructor argument. See newInstance( Class , Object[] ) for details

Parameters:
clazz - the Class of object to create
arg - the argument
Returns:
the new object
Exception
See Also:
newInstance( Class , Object[] )

newInstance

public static Object newInstance(Class clazz,
                                 Object arg0,
                                 Object arg1)
                          throws Exception
Create an object of a class with a given pair of constructor arguments. See newInstance( Class , Object[] ) for details

Parameters:
clazz - the Class of object to create
arg0 - the first argument
arg1 - the second argument
Returns:
the new object
Exception
See Also:
newInstance( Class , Object[] )

newInstance

public static Object newInstance(String className,
                                 Object arg)
                          throws Exception
Create an object of a class with a given single constructor argument. See newInstance( Class , Object[] ) for details

Parameters:
className - the name of the Class of object to create
arg - the argument
Returns:
the new object
Exception
See Also:
newInstance( Class , Object[] )

callStaticMethod

public static Object callStaticMethod(Class clazz,
                                      String name)
                               throws Exception
Get the value of a static method of a Class with no arguments. If an exception is thrown, that exception is thrown, rather than, InvocationTargetException.

Parameters:
clazz - the Class of object on which the method is called
name - the name of the method to be called - must have no arguments
Returns:
the return value
Throws:
Exception - if an exception occurs while making the call.
IllegalArgumentException - if there is no method with the given name with zero arguments

mkdirs

public static void mkdirs(File dir)
                   throws IOException
Check for the existence of a directory and create it if necessary, including any non-existent parent directories.

Parameters:
dir - the directory to be created
Throws:
IOException - if the directory cannot be created

concat

public static List concat(Collection c1,
                          Collection c2)
Concatenate two Collections, in their natural order of iteration, into a single result List.

Parameters:
c1 - the first Collection
c2 - the second Collection
Returns:
the concatenated List

readReader

public static String readReader(Reader rdr)
                         throws IOException
Read from a Reader into a String.

Parameters:
rdr - the Reader from which to read
Returns:
the contents of rdr
IOException

readFile

public static String readFile(String fileName)
                       throws IOException
Read from a named file into a String.

Parameters:
fileName - the name of the file from which to read
Returns:
the contents of fileName
IOException

readToPosition

public static void readToPosition(BufferedReader rdr,
                                  int line,
                                  int col)
                           throws IOException
Read from the current position in a Reader to a given relative line and column. If line is 1, the method will only work correctly if the current position is at the start of the line.

Parameters:
rdr - the reader from which to read
line - the line to move to
col - the column to move to
Throws:
IOException - if there is a problem reading from rdr

quote

public static String quote(String s)
Put double quotes around a string, and encode it.

Parameters:
s - the String to quote
Returns:
the quoted String
See Also:
encode(java.lang.String)

encode

public static String encode(String s)
Encode a String in the manner needed for a Java source string literal, replacing characters with escape codes as needed.

Parameters:
s - the String to encode
Returns:
the encoded String


Copyright © 2003 Ashridge Technologies Ltd. All Rights Reserved.