com.ashridgetech.jamda.common
Class Util

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

public class Util
extends java.lang.Object

Utility methods used by various classes.

Version:
1.0
Author:
paulb

Field Summary
static java.lang.Object[] EMPTY_ARRAY
           
static java.lang.Class[] EMPTY_CLASS_ARRAY
           
static java.lang.String[] EMPTY_STRING_ARRAY
           
static java.util.regex.Pattern NAME_PATTERN
          A Pattern which finds '.' , '/', '\' so it can be used for splitting file or package names.
static java.util.regex.Pattern SPACE_PATTERN
          A Pattern which finds whitespace so it can be used for splitting space-separated lists of tokens.
 
Method Summary
static java.lang.String blankToNull(java.lang.String s)
          Change a null or blank String to null.
static java.lang.Object callStaticMethod(java.lang.Class clazz, java.lang.String name)
          Get the value of a static method of a Class with no arguments.
static java.lang.String capitalize(java.lang.String s)
          Get a String with the first character converted to upper case.
static void checkArg(boolean b, java.lang.String message)
           
static void checkNotEmpty(java.lang.String s, java.lang.String name)
           
static void checkNotNull(java.lang.Object o, java.lang.String name)
           
static void checkNull(java.lang.Object o, java.lang.String name)
           
static void checkState(boolean b, java.lang.String message)
           
static java.util.List concat(java.util.Collection c1, java.util.Collection c2)
          Concatenate two Collections, in their natural order of iteration, into a single result List.
static java.lang.String encode(java.lang.String s)
          Encode a String in the manner needed for a Java source string literal, replacing characters with escape codes as needed.
static java.lang.reflect.Constructor findConstructor(java.lang.Class clazz, java.lang.Class argType)
          Find a public constructor with a given single argument type in a class.
static java.lang.reflect.Constructor findConstructor(java.lang.Class clazz, java.lang.Class[] argTypes)
          Find a public constructor with a given argument list in a class.
static java.lang.Object getFirstElement(java.util.Collection c)
          Get the first element of a collection.
static java.lang.Class[] getTypes(java.lang.Object[] objects)
          Get the types of an array of objects.
static boolean isBlank(java.lang.String s)
          Test whether a String is null or empty or all whitespace.
static java.lang.String localizePath(java.lang.String path)
          Localize a path name by replacing both '\' and '/' with the local File.separatorChar.
static void mkdirs(java.io.File dir)
          Check for the existence of a directory and create it if necessary, including any non-existent parent directories.
static java.lang.String naturalize(java.lang.String s)
          Get a String from a camel case name with spaces between each word, and underscores replaced by spaces.
static java.lang.Object newInstance(java.lang.Class clazz)
          Create an object of a class with no constructor argument.
static java.lang.Object newInstance(java.lang.Class clazz, java.lang.Object arg)
          Create an object of a class with a given single constructor argument.
static java.lang.Object newInstance(java.lang.Class clazz, java.lang.Object[] args)
          Create an object of a class with a given set of arguments.
static java.lang.Object newInstance(java.lang.String className)
          Create an object of a named class with no constructor argument.
static java.lang.Object newInstance(java.lang.String className, java.lang.Object arg)
          Create an object of a class with a given single constructor argument.
static java.lang.Object newInstance(java.lang.String className, java.lang.Object[] args)
          Create an object of a named class with a given set of arguments.
static java.lang.String nullToBlank(java.lang.String s)
          Change a null or blank String to an empty String.
static java.lang.String packageName(java.lang.String className)
          Get the package out of a fully qualified class name.
static java.lang.String pluralize(java.lang.String word)
          Get a String word made plural (in English).
static java.lang.String quote(java.lang.String s)
          Put double quotes around a string, and encode it.
static java.lang.String readFile(java.lang.String fileName)
          Read from a named file into a String.
static java.lang.String readReader(java.io.Reader rdr)
          Read from a Reader into a String.
static void readToPosition(java.io.BufferedReader rdr, int line, int col)
          Read from the current position in a Reader to a given relative line and column.
static java.util.List select(java.util.Collection inputColl, org.apache.commons.collections.Predicate predicate)
          Select from a Collection into a new List using a Predicate.
static java.lang.String[] splitAtSpace(java.lang.String s)
          Split a string at spaces.
static java.lang.String[] splitName(java.lang.String name)
          Split a string at periods or file separators.
static java.lang.String toString(java.util.Collection c)
           
static java.util.List transform(java.util.Collection inputColl, org.apache.commons.collections.Transformer transformer)
          Transform a List into a new List using a Transformer.
static java.lang.String uncapitalize(java.lang.String s)
          Get a String with the first character converted to lower case.
static java.lang.String unqualifiedClassName(java.lang.String className)
          Get the name of a class, without its package or containing class.
 
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 java.lang.Object[] EMPTY_ARRAY

EMPTY_STRING_ARRAY

public static final java.lang.String[] EMPTY_STRING_ARRAY

EMPTY_CLASS_ARRAY

public static final java.lang.Class[] EMPTY_CLASS_ARRAY

NAME_PATTERN

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


SPACE_PATTERN

public static final java.util.regex.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(java.lang.Object o,
                                java.lang.String name)

checkNotEmpty

public static void checkNotEmpty(java.lang.String s,
                                 java.lang.String name)

checkNull

public static void checkNull(java.lang.Object o,
                             java.lang.String name)

checkArg

public static void checkArg(boolean b,
                            java.lang.String message)

checkState

public static void checkState(boolean b,
                              java.lang.String message)

toString

public static java.lang.String toString(java.util.Collection c)

getFirstElement

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

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

uncapitalize

public static java.lang.String uncapitalize(java.lang.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 java.lang.String capitalize(java.lang.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 java.lang.String pluralize(java.lang.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

naturalize

public static java.lang.String naturalize(java.lang.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

isBlank

public static boolean isBlank(java.lang.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 java.lang.String blankToNull(java.lang.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 java.lang.String nullToBlank(java.lang.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 java.lang.String packageName(java.lang.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 java.lang.String unqualifiedClassName(java.lang.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 java.lang.String[] splitName(java.lang.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 java.lang.String[] splitAtSpace(java.lang.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 java.lang.String localizePath(java.lang.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

findConstructor

public static java.lang.reflect.Constructor findConstructor(java.lang.Class clazz,
                                                            java.lang.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 java.lang.reflect.Constructor findConstructor(java.lang.Class clazz,
                                                            java.lang.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 java.lang.Class[] getTypes(java.lang.Object[] objects)
Get the types of an array of objects.

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

newInstance

public static java.lang.Object newInstance(java.lang.Class clazz,
                                           java.lang.Object[] args)
                                    throws java.lang.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:
java.lang.Exception - if an exception occurs calling the constructor of the object.
java.lang.IllegalArgumentException - if there is no constructor which can accept the given arguments

newInstance

public static java.lang.Object newInstance(java.lang.String className,
                                           java.lang.Object[] args)
                                    throws java.lang.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
java.lang.Exception
See Also:
newInstance( Class , Object[] )

newInstance

public static java.lang.Object newInstance(java.lang.Class clazz)
                                    throws java.lang.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
java.lang.Exception
See Also:
newInstance( Class , Object[] )

newInstance

public static java.lang.Object newInstance(java.lang.String className)
                                    throws java.lang.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
java.lang.Exception
See Also:
newInstance( Class , Object[] )

newInstance

public static java.lang.Object newInstance(java.lang.Class clazz,
                                           java.lang.Object arg)
                                    throws java.lang.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
java.lang.Exception
See Also:
newInstance( Class , Object[] )

newInstance

public static java.lang.Object newInstance(java.lang.String className,
                                           java.lang.Object arg)
                                    throws java.lang.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
java.lang.Exception
See Also:
newInstance( Class , Object[] )

callStaticMethod

public static java.lang.Object callStaticMethod(java.lang.Class clazz,
                                                java.lang.String name)
                                         throws java.lang.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:
java.lang.Exception - if an exception occurs while making the call.
java.lang.IllegalArgumentException - if there is no method with the given name with zero arguments

mkdirs

public static void mkdirs(java.io.File dir)
                   throws java.io.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:
java.io.IOException - if the directory cannot be created

concat

public static java.util.List concat(java.util.Collection c1,
                                    java.util.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

transform

public static java.util.List transform(java.util.Collection inputColl,
                                       org.apache.commons.collections.Transformer transformer)
Transform a List into a new List using a Transformer.

Parameters:
inputColl - the Collection to be transformed - this is not modified
transformer - the Transformer to apply
Returns:
the List resulting from applying transformer to each element of inputColl.

select

public static java.util.List select(java.util.Collection inputColl,
                                    org.apache.commons.collections.Predicate predicate)
Select from a Collection into a new List using a Predicate.

Parameters:
inputColl - the Collection to be transformed - this is not modified
predicate - the Predicate to apply
Returns:
the List resulting from selecting elements of inputColl for which predicate is true.

readReader

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

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

readFile

public static java.lang.String readFile(java.lang.String fileName)
                                 throws java.io.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
java.io.IOException

readToPosition

public static void readToPosition(java.io.BufferedReader rdr,
                                  int line,
                                  int col)
                           throws java.io.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
Throws:
java.io.IOException - if there is a problem reading from rdr

quote

public static java.lang.String quote(java.lang.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 java.lang.String encode(java.lang.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.