jfun.yan.util
Class Utils

java.lang.Object
  extended by jfun.yan.util.Utils

public class Utils
extends java.lang.Object

Common utility class that provides some utility functions.

Author:
Ben Yu Nov 14, 2005 9:57:35 PM

Constructor Summary
Utils()
           
 
Method Summary
static Component asComponent(Binder binder)
          Convert a Binder object's bind() method to a Component that expects one parameter and instantiates the component instance by calling the return value of the bind() method.
static java.util.List asList(java.lang.Object arr)
          Use an array as a list.
static java.util.List createList(java.lang.Class impltype, int capacity)
          Create a java.util.List instance.
static java.util.Map createMap(java.lang.Class impltype, int capacity)
          Create a java.util.Map instance.
static java.lang.Object createPreallocatedCollection(java.lang.Class type)
          To instantiate a collection instance.
static java.util.Set createSet(java.lang.Class impltype, int capacity)
          Create a java.util.Set instance.
static java.lang.Class getObjType(java.lang.Object arg, java.lang.Class nulltype)
          Get the type of an object.
static java.lang.String getObjTypeName(java.lang.Object arg, java.lang.String nullname)
          Get the type name of an object.
static void injectProperties(BeanType btype, java.lang.Object obj, java.util.Set props, Dependency dep)
          Inject property values into a bean.
static void injectProperty(BeanType btype, java.lang.Object obj, java.lang.String name, Dependency dep)
          Inject a property value.
static boolean isCompatible(java.lang.Class type, Component c)
          Checkes to see if a component may return a value that's an instance of a type.
static java.util.Properties loadResourceProperties(ResourceLoader loader, java.lang.String resource)
          To read a property file from a class loader into a Properties object.
static BeanType toBeanType(java.lang.Class type)
          Get the BeanType for a class.
static java.util.HashSet toSet(java.lang.Object[] keys, java.lang.String item_name)
          Convert an array of objects to a Set.
static java.lang.String toString(java.lang.Class[] param_types)
          Convert an array of parameter types to a string in the form of "(type1, type2, type3)".
static void verifyProperties(BeanType btype, java.util.Set props, Dependency dep)
          Verify that a set of properties can be resolved.
static YanException wrapInstantiationException(java.lang.Throwable e)
          Wrap an exception thrown when instantiating components.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Utils

public Utils()
Method Detail

toString

public static java.lang.String toString(java.lang.Class[] param_types)
Convert an array of parameter types to a string in the form of "(type1, type2, type3)". Array types are printed in the human readable form.

Parameters:
param_types - the parameter types.
Returns:
the string representation.

isCompatible

public static boolean isCompatible(java.lang.Class type,
                                   Component c)
Checkes to see if a component may return a value that's an instance of a type.

Parameters:
type - the type.
c - the component.
Returns:
true if the component may return a value that's an instance of the type.

createSet

public static java.util.Set createSet(java.lang.Class impltype,
                                      int capacity)
                               throws java.lang.IllegalAccessException,
                                      java.lang.InstantiationException
Create a java.util.Set instance.

Parameters:
impltype - the actual implementation class. If it is null or java.util.Set, java.util.HashSet is used.
capacity - the initial capacity.
Returns:
the Set instance.
Throws:
java.lang.IllegalAccessException - when the constructor is not public.
java.lang.InstantiationException - when the constructor fails.

createMap

public static java.util.Map createMap(java.lang.Class impltype,
                                      int capacity)
                               throws java.lang.IllegalAccessException,
                                      java.lang.InstantiationException
Create a java.util.Map instance.

Parameters:
impltype - the actual implementation class. If it is null or java.util.Map, java.util.HashMap is used.
capacity - the initial capacity.
Returns:
the Map instance.
Throws:
java.lang.IllegalAccessException - when the constructor is not public.
java.lang.InstantiationException - when the constructor fails.

createList

public static java.util.List createList(java.lang.Class impltype,
                                        int capacity)
                                 throws java.lang.IllegalAccessException,
                                        java.lang.InstantiationException
Create a java.util.List instance.

Parameters:
impltype - the implementation class. If it is null or java.util.List, java.util.ArrayList is used.
capacity - the initial capacity.
Returns:
the List instance.
Throws:
java.lang.IllegalAccessException - when the constructor is not public.
java.lang.InstantiationException - when the constructor fails.

createPreallocatedCollection

public static java.lang.Object createPreallocatedCollection(java.lang.Class type)
                                                     throws java.lang.IllegalAccessException,
                                                            java.lang.InstantiationException
To instantiate a collection instance.

The default constructor of the type is called.

Parameters:
type - the type of the collection.
Returns:
the instance.
Throws:
java.lang.IllegalAccessException - when the constructor is not public.
java.lang.InstantiationException - when the constructor fails.

asComponent

public static Component asComponent(Binder binder)
Convert a Binder object's bind() method to a Component that expects one parameter and instantiates the component instance by calling the return value of the bind() method.

Parameters:
binder - the Binder object.
Returns:
the Component.

asList

public static java.util.List asList(java.lang.Object arr)
Use an array as a list.

Parameters:
arr - the array object.
Returns:
the List object.

wrapInstantiationException

public static YanException wrapInstantiationException(java.lang.Throwable e)
Wrap an exception thrown when instantiating components. Error is thrown out directly without being wrapped.

Parameters:
e - the exception.
Returns:
the wrapped exception.

injectProperty

public static void injectProperty(BeanType btype,
                                  java.lang.Object obj,
                                  java.lang.String name,
                                  Dependency dep)
Inject a property value.

Parameters:
btype - the BeanType.
obj - the object to inject property into.
name - the property name.
dep - the dependency to resolve property value.

toBeanType

public static BeanType toBeanType(java.lang.Class type)
Get the BeanType for a class.

Parameters:
type - the class.
Returns:
the BeanType object. Or null if the type is null.

injectProperties

public static void injectProperties(BeanType btype,
                                    java.lang.Object obj,
                                    java.util.Set props,
                                    Dependency dep)
Inject property values into a bean.

Parameters:
btype - the BeanType.
obj - the bean object.
props - the property names.
dep - the dependency object to resolve property values.

verifyProperties

public static void verifyProperties(BeanType btype,
                                    java.util.Set props,
                                    Dependency dep)
Verify that a set of properties can be resolved.

Parameters:
btype - the BeanType object.
props - the set of property names.
dep - the dependency.

toSet

public static java.util.HashSet toSet(java.lang.Object[] keys,
                                      java.lang.String item_name)
Convert an array of objects to a Set. No duplicate is allowed.

Parameters:
keys - the array of objects.
item_name - the item name in the error message when duplicate is found.
Returns:
the Set object.

loadResourceProperties

public static java.util.Properties loadResourceProperties(ResourceLoader loader,
                                                          java.lang.String resource)
                                                   throws java.io.IOException
To read a property file from a class loader into a Properties object.

Parameters:
loader - the ResourceLoader used to load resource.
resource - the resource name.
Returns:
the Properties object.
Throws:
java.io.IOException - when loading fails.

getObjTypeName

public static java.lang.String getObjTypeName(java.lang.Object arg,
                                              java.lang.String nullname)
Get the type name of an object.

Parameters:
arg - the object.
nullname - the default name if the object is null.
Returns:
the name.

getObjType

public static java.lang.Class getObjType(java.lang.Object arg,
                                         java.lang.Class nulltype)
Get the type of an object.

Parameters:
arg - the object.
nulltype - the default type if the object is null.
Returns:
the type.