edu.internet2.middleware.subject
Class SubjectUtils

java.lang.Object
  extended by edu.internet2.middleware.subject.SubjectUtils

public class SubjectUtils
extends Object


Field Summary
static String DATE_FORMAT
          string format of dates
static String DATE_MINUTES_SECONDS_FORMAT
          format including minutes and seconds: yyyy/MM/dd HH:mm:ss
static String DATE_MINUTES_SECONDS_NO_SLASH_FORMAT
          format including minutes and seconds: yyyyMMdd HH:mm:ss
static int DEFAULT_BUFFER_SIZE
          The name says it all.
static int NOT_FOUND
          special number when a number is not found
static String TIMESTAMP_FORMAT
          format on screen of config for milestone: yyyy/MM/dd HH:mm:ss.SSS
static String TIMESTAMP_NO_SLASH_FORMAT
          format on screen of config for milestone: yyyyMMdd HH:mm:ss.SSS
 
Constructor Summary
SubjectUtils()
           
 
Method Summary
static void assertion(boolean isTrue, String reason)
          If false, throw an assertException, and give a reason
static void assignField(Class theClass, Object invokeOn, String fieldName, Object dataToAssign, boolean callOnSupers, boolean overrideSecurity, boolean typeCast, Class<? extends Annotation> annotationWithValueOverride)
          assign data to a field
static void assignField(Class theClass, Object invokeOn, String fieldName, Object dataToAssign, Class<? extends Annotation> annotationWithValueOverride)
          assign data to a field.
static void assignField(Field field, Object invokeOn, Object dataToAssign, boolean overrideSecurity, boolean typeCast)
          assign data to a field
static void assignField(Field field, Object invokeOn, Object dataToAssign, boolean overrideSecurity, boolean typeCast, Class<? extends Annotation> annotationWithValueOverride)
          assign data to a field
static void assignField(Object invokeOn, String fieldName, Object dataToAssign)
          assign data to a field.
static BigDecimal bigDecimalObjectValue(Object input)
          Convert an object to a byte, allow nulls
static Boolean booleanObjectValue(Object object)
          get the Boolean value for an object
static boolean booleanValue(Object object)
          get the boolean value for an object, cant be null or blank
static boolean booleanValue(Object object, boolean defaultBoolean)
          get the boolean value for an object
static Byte byteObjectValue(Object input)
          Convert an object to a byte, allow nulls
static byte byteValue(Object input)
          convert an object to a byte
static Character charObjectValue(Object input)
          get the Character wrapper value for the input
static char charValue(Object input)
          convert an object to a char
static ClassLoader classLoader()
          fast class loader
static String className(Object object)
          null safe classname method, gets the unenhanced name
static String classNameCollection(Object object)
          null safe classname method, max out at 20
static void closeQuietly(Connection connection)
          close a connection null safe and dont throw exception
static void closeQuietly(InputStream input)
          Unconditionally close an InputStream.
static void closeQuietly(OutputStream output)
          Unconditionally close an OutputStream.
static void closeQuietly(Reader input)
          Unconditionally close an Reader.
static void closeQuietly(ResultSet resultSet)
          close a resultSet null safe and dont throw exception
static void closeQuietly(org.hsqldb.Session session)
          close a session null safe and dont throw exception
static void closeQuietly(Statement statement)
          close a statement null safe and dont throw exception
static void closeQuietly(Writer writer)
          close a writer quietly
static URL computeUrl(String resourceName, boolean canBeNull)
          compute a url of a resource
static void copy(InputStream input, Writer output, String encoding)
          Copy and convert bytes from an InputStream to chars on a Writer, using the specified encoding.
static int copy(Reader input, Writer output)
          Copy chars from a Reader to a Writer.
static Date dateValue(Object inputObject)
           Convert an object to a java.util.Date.
static String defaultIfBlank(String string, String defaultStringIfBlank)
          return the string or the other if the first is blank
static
<T> T
defaultIfNull(T theValue, T defaultIfTheValueIsNull)
          genericized method to see if first is null, if so then return second, else first.
static Double doubleObjectValue(Object input, boolean allowNullBlank)
          get the Double value of an object
static double doubleValue(Object input)
          get the double value of an object
static double doubleValueNoError(Object input)
          get the double value of an object, do not throw an exception if there is an error
static Field field(Class theClass, String fieldName, boolean callOnSupers, boolean throwExceptionIfNotFound)
          get a field object for a class, potentially in superclasses
static String fileCanonicalPath(File file)
          get canonical path of file
static File fileFromResourceName(String resourceName)
          get a file name from a resource name
static Float floatObjectValue(Object input, boolean allowNullBlank)
          get the Float value of an object
static float floatValue(Object input)
          get the float value of an object
static float floatValueNoError(Object input)
          get the float value of an object, do not throw an exception if there is an error
static Class forName(String origClassName)
          Returns the class object.
static boolean injectInException(Throwable t, String message)
          If we can, inject this into the exception, else return false
static Integer intObjectValue(Object input, boolean allowNullBlank)
          get the Integer value of an object
static int intValue(Object input)
          convert an object to a int
static int intValue(Object input, int valueIfNull)
          convert an object to a int
static int intValueNoError(Object input)
          get the int value of an object, do not throw an exception if there is an error
static boolean isBlank(Object input)
          See if the input is null or if string, if it is empty or blank (whitespace)
static Iterator iterator(Object collection)
          null safe iterator getter if the type if collection
static int length(Object arrayOrCollection)
          Null safe array length or map
static Long longObjectValue(Object input, boolean allowNullBlank)
          get the Long value of an object
static long longValue(Object input)
          convert an object to a long
static long longValue(Object input, long valueIfNull)
          convert an object to a long
static long longValueNoError(Object input)
          get the long value of an object, do not throw an exception if there is an error
static
<T> T
newInstance(Class<T> theClass)
          Construct a class
static Object next(Object arrayOrCollection, Iterator iterator, int index)
          If array, get the element based on index, if Collection, get it based on iterator.
static boolean nullOrBlank(Object object)
          is an object null or blank
static Properties propertiesFromResourceName(String resourceName)
          read properties from a resource, dont modify the properties returned since they are cached
static Short shortObjectValue(Object input)
          get the Short value of an object.
static short shortValue(Object input)
          convert an object to a short
static Timestamp stringToTimestamp(String input)
          convert a string to timestamp based on the following formats: yyyyMMdd yyyy/MM/dd HH:mm:ss yyyy/MM/dd HH:mm:ss.SSS yyyy/MM/dd HH:mm:ss.SSSSSS
static String stringValue(Date date)
          convert a date to the standard string yyyymmdd
static String stringValue(Object input)
          convert an object to a string
static String substituteExpressionLanguage(String stringToParse, Map<String,Object> variableMap)
          substitute an EL for objects
static String timestampToString(Date timestamp)
          Convert a timestamp into a string: yyyy/MM/dd HH:mm:ss.SSS
static String toString(InputStream input, String encoding)
          Get the contents of an InputStream as a String.
static String toStringForLog(Object object)
          print out various types of objects
static String toStringForLog(Object object, int maxChars)
          print out various types of objects
static Timestamp toTimestamp(Object input)
           Convert a string or object to a timestamp (could be string, date, timestamp, etc) yyyymmdd or yyyy/MM/dd HH:mm:ss or yyyy/MM/dd HH:mm:ss.SSS or yyyy/MM/dd HH:mm:ss.SSSSSS
static
<T> T
typeCast(Object value, Class<T> theClass)
          If necessary, convert an object to another type.
static
<T> T
typeCast(Object value, Class<T> theClass, boolean convertNullToDefaultPrimitive, boolean useNewInstanceHooks)
          If necessary, convert an object to another type.
static Class unenhanceClass(Class theClass)
          if a class is enhanced, get the unenhanced version
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DATE_FORMAT

public static final String DATE_FORMAT
string format of dates

See Also:
Constant Field Values

DATE_MINUTES_SECONDS_FORMAT

public static final String DATE_MINUTES_SECONDS_FORMAT
format including minutes and seconds: yyyy/MM/dd HH:mm:ss

See Also:
Constant Field Values

DATE_MINUTES_SECONDS_NO_SLASH_FORMAT

public static final String DATE_MINUTES_SECONDS_NO_SLASH_FORMAT
format including minutes and seconds: yyyyMMdd HH:mm:ss

See Also:
Constant Field Values

TIMESTAMP_FORMAT

public static final String TIMESTAMP_FORMAT
format on screen of config for milestone: yyyy/MM/dd HH:mm:ss.SSS

See Also:
Constant Field Values

TIMESTAMP_NO_SLASH_FORMAT

public static final String TIMESTAMP_NO_SLASH_FORMAT
format on screen of config for milestone: yyyyMMdd HH:mm:ss.SSS

See Also:
Constant Field Values

NOT_FOUND

public static final int NOT_FOUND
special number when a number is not found

See Also:
Constant Field Values

DEFAULT_BUFFER_SIZE

public static final int DEFAULT_BUFFER_SIZE
The name says it all.

See Also:
Constant Field Values
Constructor Detail

SubjectUtils

public SubjectUtils()
Method Detail

injectInException

public static boolean injectInException(Throwable t,
                                        String message)
If we can, inject this into the exception, else return false

Parameters:
t -
message -
Returns:
true if success, false if not

assignField

public static void assignField(Class theClass,
                               Object invokeOn,
                               String fieldName,
                               Object dataToAssign,
                               boolean callOnSupers,
                               boolean overrideSecurity,
                               boolean typeCast,
                               Class<? extends Annotation> annotationWithValueOverride)
assign data to a field

Parameters:
theClass - the class which has the method
invokeOn - to call on or null for static
fieldName - method name to call
dataToAssign - data
callOnSupers - if static and method not exists, try on supers
overrideSecurity - true to call on protected or private etc methods
typeCast - true if we should typecast
annotationWithValueOverride - annotation with value of override

assignField

public static void assignField(Class theClass,
                               Object invokeOn,
                               String fieldName,
                               Object dataToAssign,
                               Class<? extends Annotation> annotationWithValueOverride)
assign data to a field. Will find the field in superclasses, will typecast, and will override security (private, protected, etc)

Parameters:
theClass - the class which has the method
invokeOn - to call on or null for static
fieldName - method name to call
dataToAssign - data
annotationWithValueOverride - annotation with value of override

assignField

public static void assignField(Field field,
                               Object invokeOn,
                               Object dataToAssign,
                               boolean overrideSecurity,
                               boolean typeCast)
assign data to a field

Parameters:
field - is the field to assign to
invokeOn - to call on or null for static
dataToAssign - data
overrideSecurity - true to call on protected or private etc methods
typeCast - true if we should typecast

assignField

public static void assignField(Field field,
                               Object invokeOn,
                               Object dataToAssign,
                               boolean overrideSecurity,
                               boolean typeCast,
                               Class<? extends Annotation> annotationWithValueOverride)
assign data to a field

Parameters:
field - is the field to assign to
invokeOn - to call on or null for static
dataToAssign - data
overrideSecurity - true to call on protected or private etc methods
typeCast - true if we should typecast
annotationWithValueOverride - annotation with value of override, or null if none

assignField

public static void assignField(Object invokeOn,
                               String fieldName,
                               Object dataToAssign)
assign data to a field. Will find the field in superclasses, will typecast, and will override security (private, protected, etc)

Parameters:
invokeOn - to call on or null for static
fieldName - method name to call
dataToAssign - data

field

public static Field field(Class theClass,
                          String fieldName,
                          boolean callOnSupers,
                          boolean throwExceptionIfNotFound)
get a field object for a class, potentially in superclasses

Parameters:
theClass -
fieldName -
callOnSupers - true if superclasses should be looked in for the field
throwExceptionIfNotFound - will throw runtime exception if not found
Returns:
the field object or null if not found (or exception if param is set)

typeCast

public static <T> T typeCast(Object value,
                             Class<T> theClass)
If necessary, convert an object to another type. if type is Object.class, just return the input. Do not convert null to an empty primitive

Type Parameters:
T - is template type
Parameters:
value -
theClass -
Returns:
the object of that instance converted into something else

typeCast

public static <T> T typeCast(Object value,
                             Class<T> theClass,
                             boolean convertNullToDefaultPrimitive,
                             boolean useNewInstanceHooks)
If necessary, convert an object to another type. if type is Object.class, just return the input

Type Parameters:
T - is the type to return
Parameters:
value -
theClass -
convertNullToDefaultPrimitive - if the value is null, and theClass is primitive, should we convert the null to a primitive default value
useNewInstanceHooks - if theClass is not recognized, then honor the string "null", "newInstance", or get a constructor with one param, and call it
Returns:
the object of that instance converted into something else

dateValue

public static Date dateValue(Object inputObject)
 Convert an object to a java.util.Date.  allows, dates, null, blank, 
 yyyymmdd or yyyymmdd hh24:mm:ss
 or yyyy/MM/dd HH:mm:ss.SSS
 

Parameters:
inputObject - is the String or Date to convert
Returns:
the Date

isBlank

public static boolean isBlank(Object input)
See if the input is null or if string, if it is empty or blank (whitespace)

Parameters:
input -
Returns:
true if blank

intObjectValue

public static Integer intObjectValue(Object input,
                                     boolean allowNullBlank)
get the Integer value of an object

Parameters:
input - is a number or String
allowNullBlank - true if convert null or blank to null
Returns:
the Integer equivalent

intValue

public static int intValue(Object input)
convert an object to a int

Parameters:
input -
Returns:
the number

intValue

public static int intValue(Object input,
                           int valueIfNull)
convert an object to a int

Parameters:
input -
valueIfNull - is if the input is null or empty, return this value
Returns:
the number

intValueNoError

public static int intValueNoError(Object input)
get the int value of an object, do not throw an exception if there is an error

Parameters:
input - is a number or String
Returns:
the int equivalent

stringValue

public static String stringValue(Date date)
convert a date to the standard string yyyymmdd

Parameters:
date -
Returns:
the string value

stringValue

public static String stringValue(Object input)
convert an object to a string

Parameters:
input - is the object to convert
Returns:
the String conversion of the object

timestampToString

public static String timestampToString(Date timestamp)
Convert a timestamp into a string: yyyy/MM/dd HH:mm:ss.SSS

Parameters:
timestamp -
Returns:
the string representation

toString

public static String toString(InputStream input,
                              String encoding)
                       throws IOException
Get the contents of an InputStream as a String.

Parameters:
input - the InputStream to read from
encoding - The name of a supported character encoding. See the IANA Charset Registry for a list of valid encoding types.
Returns:
the requested String
Throws:
IOException - In case of an I/O problem

toTimestamp

public static Timestamp toTimestamp(Object input)
 Convert a string or object to a timestamp (could be string, date, timestamp, etc)
 yyyymmdd
 or
 yyyy/MM/dd HH:mm:ss
 or
 yyyy/MM/dd HH:mm:ss.SSS
 or
 yyyy/MM/dd HH:mm:ss.SSSSSS
 
 

Parameters:
input -
Returns:
the timestamp
Throws:
RuntimeException - if invalid format

className

public static String className(Object object)
null safe classname method, gets the unenhanced name

Parameters:
object -
Returns:
the classname

classNameCollection

public static String classNameCollection(Object object)
null safe classname method, max out at 20

Parameters:
object -
Returns:
the classname

bigDecimalObjectValue

public static BigDecimal bigDecimalObjectValue(Object input)
Convert an object to a byte, allow nulls

Parameters:
input -
Returns:
the boolean object value

booleanObjectValue

public static Boolean booleanObjectValue(Object object)
get the Boolean value for an object

Parameters:
object -
Returns:
the Boolean or null if null or empty

booleanValue

public static boolean booleanValue(Object object)
get the boolean value for an object, cant be null or blank

Parameters:
object -
Returns:
the boolean

booleanValue

public static boolean booleanValue(Object object,
                                   boolean defaultBoolean)
get the boolean value for an object

Parameters:
object -
defaultBoolean - if object is null or empty
Returns:
the boolean

byteObjectValue

public static Byte byteObjectValue(Object input)
Convert an object to a byte, allow nulls

Parameters:
input -
Returns:
the boolean object value

byteValue

public static byte byteValue(Object input)
convert an object to a byte

Parameters:
input -
Returns:
the byte

charObjectValue

public static Character charObjectValue(Object input)
get the Character wrapper value for the input

Parameters:
input - allow null, return null
Returns:
the Character object wrapper

charValue

public static char charValue(Object input)
convert an object to a char

Parameters:
input -
Returns:
the number

doubleObjectValue

public static Double doubleObjectValue(Object input,
                                       boolean allowNullBlank)
get the Double value of an object

Parameters:
input - is a number or String
allowNullBlank - used to default to false, if true, return null if nul inputted
Returns:
the Double equivalent

doubleValue

public static double doubleValue(Object input)
get the double value of an object

Parameters:
input - is a number or String
Returns:
the double equivalent

doubleValueNoError

public static double doubleValueNoError(Object input)
get the double value of an object, do not throw an exception if there is an error

Parameters:
input - is a number or String
Returns:
the double equivalent

floatObjectValue

public static Float floatObjectValue(Object input,
                                     boolean allowNullBlank)
get the Float value of an object

Parameters:
input - is a number or String
allowNullBlank - true if allow null or blank
Returns:
the Float equivalent

floatValue

public static float floatValue(Object input)
get the float value of an object

Parameters:
input - is a number or String
Returns:
the float equivalent

floatValueNoError

public static float floatValueNoError(Object input)
get the float value of an object, do not throw an exception if there is an error

Parameters:
input - is a number or String
Returns:
the float equivalent

longObjectValue

public static Long longObjectValue(Object input,
                                   boolean allowNullBlank)
get the Long value of an object

Parameters:
input - is a number or String
allowNullBlank - true if null or blank converts to null
Returns:
the Long equivalent

longValue

public static long longValue(Object input)
convert an object to a long

Parameters:
input -
Returns:
the number

longValue

public static long longValue(Object input,
                             long valueIfNull)
convert an object to a long

Parameters:
input -
valueIfNull - is if the input is null or empty, return this value
Returns:
the number

longValueNoError

public static long longValueNoError(Object input)
get the long value of an object, do not throw an exception if there is an error

Parameters:
input - is a number or String
Returns:
the long equivalent

shortObjectValue

public static Short shortObjectValue(Object input)
get the Short value of an object. converts null or blank to null

Parameters:
input - is a number or String
Returns:
the Long equivalent

shortValue

public static short shortValue(Object input)
convert an object to a short

Parameters:
input -
Returns:
the number

forName

public static Class forName(String origClassName)
Returns the class object.

Parameters:
origClassName - is fully qualified
Returns:
the class

newInstance

public static <T> T newInstance(Class<T> theClass)
Construct a class

Type Parameters:
T - template type
Parameters:
theClass -
Returns:
the instance

toStringForLog

public static String toStringForLog(Object object)
print out various types of objects

Parameters:
object -
Returns:
the string value

toStringForLog

public static String toStringForLog(Object object,
                                    int maxChars)
print out various types of objects

Parameters:
object -
maxChars - is the max chars that should be returned (abbreviate if longer), or -1 for any amount
Returns:
the string value

copy

public static int copy(Reader input,
                       Writer output)
                throws IOException
Copy chars from a Reader to a Writer.

Parameters:
input - the Reader to read from
output - the Writer to write to
Returns:
the number of characters copied
Throws:
IOException - In case of an I/O problem

copy

public static void copy(InputStream input,
                        Writer output,
                        String encoding)
                 throws IOException
Copy and convert bytes from an InputStream to chars on a Writer, using the specified encoding.

Parameters:
input - the InputStream to read from
output - the Writer to write to
encoding - The name of a supported character encoding. See the IANA Charset Registry for a list of valid encoding types.
Throws:
IOException - In case of an I/O problem

stringToTimestamp

public static Timestamp stringToTimestamp(String input)
convert a string to timestamp based on the following formats:
 yyyyMMdd
 yyyy/MM/dd HH:mm:ss
 yyyy/MM/dd HH:mm:ss.SSS
 yyyy/MM/dd HH:mm:ss.SSSSSS
 

Parameters:
input -
Returns:
the timestamp object

unenhanceClass

public static Class unenhanceClass(Class theClass)
if a class is enhanced, get the unenhanced version

Parameters:
theClass -
Returns:
the unenhanced version

iterator

public static Iterator iterator(Object collection)
null safe iterator getter if the type if collection

Parameters:
collection -
Returns:
the iterator

length

public static int length(Object arrayOrCollection)
Null safe array length or map

Parameters:
arrayOrCollection -
Returns:
the length of the array (0 for null)

next

public static Object next(Object arrayOrCollection,
                          Iterator iterator,
                          int index)
If array, get the element based on index, if Collection, get it based on iterator.

Parameters:
arrayOrCollection -
iterator -
index -
Returns:
the object

nullOrBlank

public static boolean nullOrBlank(Object object)
is an object null or blank

Parameters:
object -
Returns:
true if null or blank

defaultIfNull

public static <T> T defaultIfNull(T theValue,
                                  T defaultIfTheValueIsNull)
genericized method to see if first is null, if so then return second, else first.

Type Parameters:
T -
Parameters:
theValue - first input
defaultIfTheValueIsNull - second input
Returns:
the first if not null, second if no

defaultIfBlank

public static String defaultIfBlank(String string,
                                    String defaultStringIfBlank)
return the string or the other if the first is blank

Parameters:
string -
defaultStringIfBlank -
Returns:
the string or the default one

propertiesFromResourceName

public static Properties propertiesFromResourceName(String resourceName)
read properties from a resource, dont modify the properties returned since they are cached

Parameters:
resourceName -
Returns:
the properties

computeUrl

public static URL computeUrl(String resourceName,
                             boolean canBeNull)
compute a url of a resource

Parameters:
resourceName -
canBeNull - if cant be null, throw runtime
Returns:
the URL

classLoader

public static ClassLoader classLoader()
fast class loader

Returns:
the class loader

closeQuietly

public static void closeQuietly(Connection connection)
close a connection null safe and dont throw exception

Parameters:
connection -

closeQuietly

public static void closeQuietly(InputStream input)
Unconditionally close an InputStream. Equivalent to InputStream.close(), except any exceptions will be ignored.

Parameters:
input - A (possibly null) InputStream

closeQuietly

public static void closeQuietly(OutputStream output)
Unconditionally close an OutputStream. Equivalent to OutputStream.close(), except any exceptions will be ignored.

Parameters:
output - A (possibly null) OutputStream

closeQuietly

public static void closeQuietly(Reader input)
Unconditionally close an Reader. Equivalent to Reader.close(), except any exceptions will be ignored.

Parameters:
input - A (possibly null) Reader

closeQuietly

public static void closeQuietly(ResultSet resultSet)
close a resultSet null safe and dont throw exception

Parameters:
resultSet -

closeQuietly

public static void closeQuietly(org.hsqldb.Session session)
close a session null safe and dont throw exception

Parameters:
session -

closeQuietly

public static void closeQuietly(Statement statement)
close a statement null safe and dont throw exception

Parameters:
statement -

closeQuietly

public static void closeQuietly(Writer writer)
close a writer quietly

Parameters:
writer -

fileFromResourceName

public static File fileFromResourceName(String resourceName)
get a file name from a resource name

Parameters:
resourceName - is the classpath location
Returns:
the file path on the system

fileCanonicalPath

public static String fileCanonicalPath(File file)
get canonical path of file

Parameters:
file -
Returns:
the path

substituteExpressionLanguage

public static String substituteExpressionLanguage(String stringToParse,
                                                  Map<String,Object> variableMap)
substitute an EL for objects

Parameters:
stringToParse -
variableMap -
Returns:
the string

assertion

public static void assertion(boolean isTrue,
                             String reason)
If false, throw an assertException, and give a reason

Parameters:
isTrue -
reason -