net.sf.doolin.util
Class Utils

java.lang.Object
  extended by net.sf.doolin.util.Utils

public class Utils
extends Object

Set of utility methods.

Author:
Damien Coraboeuf

Field Summary
static String ISO_DATE_FORMAT
          ISO format
static PropertyResolver propertyResolver
          Property resolver
 
Method Summary
static
<T> List<T>
asList(Collection<T> set)
          Get a collection has a list.
static String asPropertyPath(String... propertyName)
          Gets a property path from a list of properties
static Object callMethod(Object target, String methodName, Object... parameters)
          Calls a method using introspection.
static
<T extends Serializable>
T
clone(T o)
          Duplicates an object using serialization.
static
<S,T> void
convert(Collection<? extends S> sourceCollection, Collection<T> targetCollection, com.google.common.base.Function<S,T> converter)
          Converts a collection into another using a converter.
static
<S,T> void
convert(Collection<? extends S> sourceCollection, Collection<T> targetCollection, ItemConverter<S,T> converter)
          Deprecated. Use convert(Collection, Collection, Function) instead
static
<S,T> List<T>
convertToList(Collection<? extends S> sourceCollection, com.google.common.base.Function<S,T> function)
          Converts a collection to a list using a function.
static
<S,T> List<T>
convertToList(Collection<? extends S> sourceCollection, ItemConverter<S,T> converter)
          Deprecated. Use Lists.transform(List, com.google.common.base.Function) instead.
static
<S,T> List<T>
convertToList(Collection<? extends S> sourceCollection, String property)
          Converts a collection to a list using a PropertyItemConverter.
static
<S,T> Set<T>
convertToSet(Collection<? extends S> sourceCollection, com.google.common.base.Function<S,T> function)
          Converts a collection to a set using a function.
static
<S,T> Set<T>
convertToSet(Collection<? extends S> sourceCollection, ItemConverter<S,T> converter)
          Deprecated. Use convertToSet(Collection, Function) instead
static void copyProperties(Object destination, Object origin)
          Copy properties from one bean to the other
static Class<?> forClass(String className)
          Protected call to Class.forName
static Date fromISOFormat(String value)
          Parses a date as ISO 8601
static
<A extends Annotation>
A
getAnnotation(Class<?> oClass, Class<A> aClass)
          Get an annotation from the class or from its parent classes
static
<A extends Annotation>
A
getAnnotation(Class<A> annotationClass, Object bean)
          Gets an annotation for a type.
static Object getConstant(Class<?> type, String name)
          Gets the value for a constant.
static List<Object> getProperties(Collection<?> items, String property)
          List of properties for a set of objects
static Object getProperty(Object bean, String property)
          Gets a property from a bean
static
<A extends Annotation>
A
getPropertyAnnotation(Class<A> annotationClass, Object bean, String propertyName)
          Gets an annotation for a property.
static PropertyResolver getPropertyResolver()
           
static URL getResource(Class<?> referenceClass, String path)
          Gets a resource URL from a reference class and a resource path.
static
<T> T
newInstance(Class<T> clazz)
          Creates a new instance
static
<T> T
newInstance(Class<T> clazz, Object parameter)
          Creates a new instance with a unique parameter in the constructor
static Object newInstance(String className)
          Creates a new instance of a class given its name.
static Object newInstance(String className, ParameterSet params)
          Creates a new instance of a class given its name and a set of properties
static Map<String,String> parseQuery(String query)
          Parses a query
static Map<String,String> parseQuery(URL url)
          Parses a URL query
static String readTextResource(Class<?> referenceClass, String path, String encoding)
          Reads a text resource from a reference class and a resource path.
static void setProperties(Object o, ParameterSet params)
          Set the properties of an object
static void setProperty(Object bean, String property, Object value)
          Sets a property on a bean
static void setPropertyResolver(PropertyResolver propertyResolver)
           
static String simpleToString(Object o, String nullString)
           
static String toISOFormat(Date date)
          Formats a date as ISO 8601 format
static Map<String,String> toMap(String[] strings)
          Converts a sequence of strings to a map.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ISO_DATE_FORMAT

public static final String ISO_DATE_FORMAT
ISO format

See Also:
Constant Field Values

propertyResolver

public static PropertyResolver propertyResolver
Property resolver

Method Detail

asList

public static <T> List<T> asList(Collection<T> set)
Get a collection has a list. If set is null, returns an empty list.

Type Parameters:
T - Type of items in the set
Parameters:
set - Incoming collection
Returns:
Resulting list

asPropertyPath

public static String asPropertyPath(String... propertyName)
Gets a property path from a list of properties

Parameters:
propertyName - List of properties
Returns:
Property path

callMethod

public static Object callMethod(Object target,
                                String methodName,
                                Object... parameters)
Calls a method using introspection.

Parameters:
target - Object that holds the method to call
methodName - Method name
parameters - Parameters for the call
Returns:
Result of the call

clone

public static <T extends Serializable> T clone(T o)
Duplicates an object using serialization.

Type Parameters:
T - Type of the object
Parameters:
o - Object to close
Returns:
Cloned object
See Also:
SerializationUtils.clone(Serializable)

convert

public static <S,T> void convert(Collection<? extends S> sourceCollection,
                                 Collection<T> targetCollection,
                                 com.google.common.base.Function<S,T> converter)
Converts a collection into another using a converter.

Type Parameters:
S - Types in the source collection
T - Types in the target list
Parameters:
sourceCollection - Source collection
targetCollection - Target collection
converter - Converter

convert

@Deprecated
public static <S,T> void convert(Collection<? extends S> sourceCollection,
                                            Collection<T> targetCollection,
                                            ItemConverter<S,T> converter)
Deprecated. Use convert(Collection, Collection, Function) instead

Converts a collection into another using a converter.

Type Parameters:
S - Types in the source collection
T - Types in the target list
Parameters:
sourceCollection - Source collection
targetCollection - Target collection
converter - Converter

convertToList

public static <S,T> List<T> convertToList(Collection<? extends S> sourceCollection,
                                          com.google.common.base.Function<S,T> function)
Converts a collection to a list using a function.

Type Parameters:
S - Types in the source collection
T - Types in the target list
Parameters:
sourceCollection - Source collection
function - Transformation function
Returns:
Target list

convertToList

@Deprecated
public static <S,T> List<T> convertToList(Collection<? extends S> sourceCollection,
                                                     ItemConverter<S,T> converter)
Deprecated. Use Lists.transform(List, com.google.common.base.Function) instead.

Converts a collection to a list using a converter.

Type Parameters:
S - Types in the source collection
T - Types in the target list
Parameters:
sourceCollection - Source collection
converter - Converter
Returns:
Target list

convertToList

public static <S,T> List<T> convertToList(Collection<? extends S> sourceCollection,
                                          String property)
Converts a collection to a list using a PropertyItemConverter.

Type Parameters:
S - Types in the source collection
T - Types in the target list
Parameters:
sourceCollection - Source collection
property - Property to extract from each source item
Returns:
Target list

convertToSet

public static <S,T> Set<T> convertToSet(Collection<? extends S> sourceCollection,
                                        com.google.common.base.Function<S,T> function)
Converts a collection to a set using a function.

Type Parameters:
S - Types in the source collection
T - Types in the target set
Parameters:
sourceCollection - Source collection
function - Transformation function
Returns:
Target set

convertToSet

@Deprecated
public static <S,T> Set<T> convertToSet(Collection<? extends S> sourceCollection,
                                                   ItemConverter<S,T> converter)
Deprecated. Use convertToSet(Collection, Function) instead

Converts a collection to a set using a converter.

Type Parameters:
S - Types in the source collection
T - Types in the target set
Parameters:
sourceCollection - Source collection
converter - Converter
Returns:
Target set

copyProperties

public static void copyProperties(Object destination,
                                  Object origin)
Copy properties from one bean to the other

Parameters:
destination - Destination bean
origin - Origin bean

forClass

public static Class<?> forClass(String className)
Protected call to Class.forName

Parameters:
className - Class name to get the Class instance from
Returns:
Class instance

fromISOFormat

public static Date fromISOFormat(String value)
                          throws ParseException
Parses a date as ISO 8601

Parameters:
value - ISO8601 date
Returns:
Parsed date
Throws:
ParseException - If the value cannot be parsed

getAnnotation

public static <A extends Annotation> A getAnnotation(Class<?> oClass,
                                                     Class<A> aClass)
Get an annotation from the class or from its parent classes

Type Parameters:
A - Annotation type
Parameters:
oClass - Class to search annotation in.
aClass - Annotation to search
Returns:
Annotation or null if not found.

getAnnotation

public static <A extends Annotation> A getAnnotation(Class<A> annotationClass,
                                                     Object bean)
Gets an annotation for a type.

Type Parameters:
A - Type of annotation to look for
Parameters:
annotationClass - Annotation class
bean - Bean that contains the annotation
Returns:
Annotation instance or null if not defined

getConstant

public static Object getConstant(Class<?> type,
                                 String name)
Gets the value for a constant.

Parameters:
type - Type that contains the constant
name - Name of the constant
Returns:
Constant value

getProperties

public static List<Object> getProperties(Collection<?> items,
                                         String property)
List of properties for a set of objects

Parameters:
items - List of objects to loop on
property - Property to get for each object
Returns:
List of all properties values

getProperty

public static Object getProperty(Object bean,
                                 String property)
Gets a property from a bean

Parameters:
bean - Bean
property - Property name
Returns:
Property value

getPropertyAnnotation

public static <A extends Annotation> A getPropertyAnnotation(Class<A> annotationClass,
                                                             Object bean,
                                                             String propertyName)
Gets an annotation for a property. The annotation may be associated with either the getter or the setter method of the property.

Type Parameters:
A - Type of annotation to look for
Parameters:
annotationClass - Annotation class
bean - Bean that contains the property
propertyName - Property name
Returns:
Annotation instance or null if not defined

getPropertyResolver

public static PropertyResolver getPropertyResolver()

getResource

public static URL getResource(Class<?> referenceClass,
                              String path)
                       throws net.sf.jstring.LocalizableException
Gets a resource URL from a reference class and a resource path. The resource URL is determined by computing the resource path from the reference class. If the reference class is null, the path is computed using the current class (i.e. the Utils class). If the URL cannot be accessed, a LocalizableException with code StringCodes.STRING_ERROR_RESOURCE_NOT_FOUND is thrown.

Parameters:
referenceClass - Reference for the resource path
path - Resource path from the reference class
Returns:
Resource URL
Throws:
net.sf.jstring.LocalizableException - If the URL cannot be found

newInstance

public static <T> T newInstance(Class<T> clazz)
Creates a new instance

Type Parameters:
T - Type of the class
Parameters:
clazz - Type of the instance to create
Returns:
Instance of the class

newInstance

public static <T> T newInstance(Class<T> clazz,
                                Object parameter)
Creates a new instance with a unique parameter in the constructor

Type Parameters:
T - Type of the class
Parameters:
clazz - Type of the instance to create
parameter - Parameter for the constructor
Returns:
Instance of the class

newInstance

public static Object newInstance(String className)
Creates a new instance of a class given its name.

Parameters:
className - Type name of the instance to create
Returns:
Instance of the given type

newInstance

public static Object newInstance(String className,
                                 ParameterSet params)
Creates a new instance of a class given its name and a set of properties

Parameters:
className - Type name of the instance to create
params - Parameters to set in the newly created instance
Returns:
Instance of the given type where properties have been set according to the given parameters
See Also:
setProperties(Object, ParameterSet), newInstance(String)

parseQuery

public static Map<String,String> parseQuery(String query)
Parses a query

Parameters:
query - Query to parse
Returns:
Extracted parameters. The returned map can be empty but never null.

parseQuery

public static Map<String,String> parseQuery(URL url)
Parses a URL query

Parameters:
url - URL to parse
Returns:
Extracted parameters. The returned map can be empty but never null.

readTextResource

public static String readTextResource(Class<?> referenceClass,
                                      String path,
                                      String encoding)
                               throws net.sf.jstring.LocalizableException
Reads a text resource from a reference class and a resource path. The resource URL is determined by computing the resource path from the reference class. If the reference class is null, the path is computed using the current class (i.e. the IOUtils class). If the resource cannot be accessed, a LocalizableException with code StringCodes.STRING_ERROR_RESOURCE_NOT_FOUND is thrown.

Parameters:
referenceClass - Reference for the resource path
path - Resource path from the reference class
encoding - Encoding of the target resource
Returns:
Resource content
Throws:
net.sf.jstring.LocalizableException - If the resource cannot be found

setProperties

public static void setProperties(Object o,
                                 ParameterSet params)
Set the properties of an object

Parameters:
o - Object to set
params - Parameters to set in the given object

setProperty

public static void setProperty(Object bean,
                               String property,
                               Object value)
Sets a property on a bean

Parameters:
bean - Bean
property - Property name
value - Property value

setPropertyResolver

public static void setPropertyResolver(PropertyResolver propertyResolver)

simpleToString

public static String simpleToString(Object o,
                                    String nullString)

toISOFormat

public static String toISOFormat(Date date)
Formats a date as ISO 8601 format

Parameters:
date - Date to format
Returns:
Formatted date

toMap

public static Map<String,String> toMap(String[] strings)
Converts a sequence of strings to a map. Each string must have the format : <key>=<value>.

Parameters:
strings - List of strings
Returns:
Corresponding map (not null)


Copyright © 2011. All Rights Reserved.