org.norther.tammi.acorn.type
Class Classes

java.lang.Object
  extended by org.norther.tammi.acorn.type.Classes

public abstract class Classes
extends Object

Converts between object classes.

Version:
$Id: Classes.java,v 1.38 2010-07-02 13:03:08 cvsimp Exp $
Author:
Ilkka Priha

Field Summary
static String ANY_DOMAIN
          The domain wildcard.
static String FILE_PATH_SPLITTER
          The class path separator expression.
static String GMT_TIME_ZONE
          The GMT time zone.
static String MBEAN_SUFFIX
          The MBean interface suffix.
static OrderedMap<String,TimeZone> TIME_ZONES
          The known time zones.
 
Constructor Summary
Classes()
           
 
Method Summary
static
<T> T
cast(Object object)
          Casts an unknown object to the specified parametrized type.
static Object getPropertyValue(Object bean, String field)
          Gets the value of the specified property.
static boolean isDateType(String className, boolean arrayp, Class<?>... loaded)
          Checks whether the specified class type is a date, i.e.
static boolean isLOBType(String className, boolean arrayp, Class<?>... loaded)
          Checks whether the specified class type is a LOB, i.e.
static boolean isLocalizedPath(String path)
          Checks if a path is a localized one.
static boolean isLocationType(String className, boolean arrayp, Class<?>... loaded)
          Checks whether the specified class type is a location, i.e.
static boolean isSubtypeOf(String className, Class<?> superType, boolean arrayp, Class<?>... loaded)
          Checks whether the specified class type is a subtype of or implements the interface of the specified super type.
static void mergePropertyValues(Object source, Object target, AttributeFilter... filter)
          Merges values of properties of two objects.
static void setPropertyValue(Object bean, String field, Object value)
          Sets the value of the specified property.
static String toBaseName(String name)
          Converts a localized name to its base name.
static
<T> Class<T>
toClass(String type, boolean... options)
          Converts a class or primitive type to the corresponding class by applying the default class loader.
static
<T> Class<T>
toClass(String type, ClassLoader loader, boolean... options)
          Converts a class or primitive type to the corresponding class by applying the specified class loader.
static Class<?>[] toClassHierarchy(Class<?> clazz)
          Converts the specified class to a class hierarchy of its implemented interfaces and extended superclasses.
static Class<?>[] toClassHierarchy(String className)
          Converts the named class to a class hierarchy of its implemented interfaces and extended superclasses.
static OrderedMap<Class<Enum<?>>,Enum<?>> toEnumConstants(String[] classNames)
          Converts the named classes to enum instances.
static
<L extends EventListener>
OrderedMap<Class<L>,L[]>
toEventListeners(String[] classNames)
          Converts the named classes to event listener instances.
static String toExtension(File file, String def)
          Converts a file to its extension.
static String toExtension(String path, String def)
          Converts a path to its extension.
static String toFileName(String path)
          Converts a file path to its file name.
static Locale toLocale(String str, String... pattern)
          Converts a string to a locale.
static String toLocaleName(String name)
          Converts a localized name to its corresponding locale name.
static String toLocalizedName(String name, Locale locale)
          Converts a base name to a localized name.
static String toLocalizedPath(String path, Locale locale)
          Converts a base path to a localized path.
static Class<?> toMBean(Class<?> clazz)
          Converts a class into its MBean interface.
static Locale toNamedLocale(String name)
          Converts a localized name to its corresponding locale.
static String toPackageName(String uri, String domain)
          Converts an URI path to a package name.
static Object[] toParameters(Object parameters, Object signature)
          Converts parameters into the corresponding parameter array.
static String toPathName(String path, boolean... includeSeparator)
          Converts a file path to its path name.
static PropertyDescriptor toPropertyDescriptor(Class<?> clazz, String field)
          Converts a field of a class into the corresponding property descriptor.
static String toQualifiedName(String name)
          Converts a base name to the corresponding qualified name.
static String toRelativePath(String path)
          Converts an absolute path to a relative one.
static String toSignature(Object parameter)
          Converts a parameter into the corresponding signature.
static String[] toSignature(Object parameters, Object signature)
          Converts parameters into the corresponding signature array.
static String toString(Locale locale, String... pattern)
          Converts a locale to a string.
static String[] toStrings(Locale locale, String... variants)
          Tokenizes a locale and variants and returns the tokens in an array.
static TimeZone toTimeZone(Locale locale)
          Converts a locale to a time zone.
static TimeZone toTimeZone(String id)
          Converts a time zone id or a locale variant to a time zone.
static String[] toTypeHierarchy(Class<?> clazz)
          Converts the specified class to a type hierarchy of its implemented interfaces and extended superclasses.
static String[] toTypeHierarchy(String className)
          Converts the named class to a type hierarchy of its implemented interfaces and extended superclasses.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ANY_DOMAIN

public static final String ANY_DOMAIN
The domain wildcard.

See Also:
Constant Field Values

MBEAN_SUFFIX

public static final String MBEAN_SUFFIX
The MBean interface suffix.

See Also:
Constant Field Values

GMT_TIME_ZONE

public static final String GMT_TIME_ZONE
The GMT time zone.

See Also:
Constant Field Values

FILE_PATH_SPLITTER

public static final String FILE_PATH_SPLITTER
The class path separator expression.


TIME_ZONES

public static final OrderedMap<String,TimeZone> TIME_ZONES
The known time zones.

Constructor Detail

Classes

public Classes()
Method Detail

cast

public static <T> T cast(Object object)
Casts an unknown object to the specified parametrized type.

Type Parameters:
T - the parametrized type.
Parameters:
object - the unknown object.
Returns:
the parametrized object.

toClass

public static <T> Class<T> toClass(String type,
                                   boolean... options)
                        throws ClassNotFoundException
Converts a class or primitive type to the corresponding class by applying the default class loader.

Type Parameters:
T - the class to load.
Parameters:
type - the type to convert.
options - to discard array dimensions and to prefer object types.
Returns:
the corresponding class.
Throws:
ClassNotFoundException - if not found.

toClass

public static <T> Class<T> toClass(String type,
                                   ClassLoader loader,
                                   boolean... options)
                        throws ClassNotFoundException
Converts a class or primitive type to the corresponding class by applying the specified class loader.

Type Parameters:
T - the class to load.
Parameters:
type - the type to convert.
loader - the class loader to apply.
options - to discard array dimensions and to prefer object types.
Returns:
the corresponding class.
Throws:
ClassNotFoundException - if not found.

toClassHierarchy

public static Class<?>[] toClassHierarchy(Class<?> clazz)
Converts the specified class to a class hierarchy of its implemented interfaces and extended superclasses. The specified class is at index 0. Primitive types are converted to the corresponding object types and array types to component types.

Parameters:
clazz - the class to convert.
Returns:
an array of interfaces and superclasses.

toClassHierarchy

public static Class<?>[] toClassHierarchy(String className)
Converts the named class to a class hierarchy of its implemented interfaces and extended superclasses. The named class is at index 0. Primitive types are converted to the corresponding object types and array types to component types.

Parameters:
className - the named class to convert.
Returns:
an array of interfaces and superclasses.

toTypeHierarchy

public static String[] toTypeHierarchy(Class<?> clazz)
Converts the specified class to a type hierarchy of its implemented interfaces and extended superclasses. The specified class is at index 0. Primitive types are converted to the corresponding object types and array types to component types.

Parameters:
clazz - the class to convert.
Returns:
an array of interface and superclass names.

toTypeHierarchy

public static String[] toTypeHierarchy(String className)
Converts the named class to a type hierarchy of its implemented interfaces and extended superclasses. The named class is at index 0. Primitive types are converted to the corresponding object types and array types to component types.

Parameters:
className - the named class to convert.
Returns:
an array of interface and superclass names.

isSubtypeOf

public static boolean isSubtypeOf(String className,
                                  Class<?> superType,
                                  boolean arrayp,
                                  Class<?>... loaded)
Checks whether the specified class type is a subtype of or implements the interface of the specified super type.

Parameters:
className - the class type to check.
superType - the super type.
arrayp - true if an array type is accepted.
loaded - an optionally loaded class.
Returns:
true for a location type, otherwise false.

isDateType

public static boolean isDateType(String className,
                                 boolean arrayp,
                                 Class<?>... loaded)
Checks whether the specified class type is a date, i.e. a subclass of java.util.Date.

Parameters:
className - the class type to check.
arrayp - true if an array type is accepted.
loaded - an optionally loaded class.
Returns:
true for a date type, otherwise false.

isLocationType

public static boolean isLocationType(String className,
                                     boolean arrayp,
                                     Class<?>... loaded)
Checks whether the specified class type is a location, i.e. a subclass of org.norther.tammi.acorn.lang.Location.

Parameters:
className - the class type to check.
arrayp - true if an array type is accepted.
loaded - an optionally loaded class.
Returns:
true for a location type, otherwise false.

isLOBType

public static boolean isLOBType(String className,
                                boolean arrayp,
                                Class<?>... loaded)
Checks whether the specified class type is a LOB, i.e. a subclass of java.sql.Blob or java.sql.Clob.

Parameters:
className - the class type to check.
arrayp - true if an array type is accepted.
loaded - an optionally loaded class.
Returns:
true for a location type, otherwise false.

toFileName

public static String toFileName(String path)
Converts a file path to its file name.

Parameters:
path - the file path.
Returns:
the file name.

toPathName

public static String toPathName(String path,
                                boolean... includeSeparator)
Converts a file path to its path name.

Parameters:
path - the file path.
includeSeparator - true to inlude the separator at the end.
Returns:
the path name or null.

toString

public static String toString(Locale locale,
                              String... pattern)
Converts a locale to a string. A plain variant without a language or country is accepted.

Parameters:
locale - the locale.
pattern - an optional pattern.
Returns:
the converted string or null.

toLocale

public static Locale toLocale(String str,
                              String... pattern)
Converts a string to a locale. A null string, an empty string and an invalid string returns null.

Note that the conversion is case insensitive.

Parameters:
str - the locale string.
pattern - an optional pattern.
Returns:
the converted locale or null.

toLocaleName

public static String toLocaleName(String name)
Converts a localized name to its corresponding locale name. The name can be a path, qualified class name or plain name.

Parameters:
name - the localized name.
Returns:
the locale name or null.

toNamedLocale

public static Locale toNamedLocale(String name)
Converts a localized name to its corresponding locale. The name can be a path, qualified class name or plain name.

Parameters:
name - the localized name.
Returns:
the named locale or null.

toStrings

public static String[] toStrings(Locale locale,
                                 String... variants)
Tokenizes a locale and variants and returns the tokens in an array.

Parameters:
locale - the locale to tokenize.
variants - optional variants.
Returns:
an array of tokens.

toBaseName

public static String toBaseName(String name)
Converts a localized name to its base name. The name can be a path, qualified class name or plain name.

Parameters:
name - the localized name.
Returns:
the corresponding base name.

toLocalizedName

public static String toLocalizedName(String name,
                                     Locale locale)
Converts a base name to a localized name.

Note that if the name already contains a locale component, it is returned unmodified.

Parameters:
name - the base name.
locale - the locale.
Returns:
the localized name.

isLocalizedPath

public static boolean isLocalizedPath(String path)
Checks if a path is a localized one.

Parameters:
path - the base name.
Returns:
true if localized, false otherwise.

toLocalizedPath

public static String toLocalizedPath(String path,
                                     Locale locale)
Converts a base path to a localized path.

Note that if the path already contains a locale component, it is returned unmodified.

Parameters:
path - the base name.
locale - the locale.
Returns:
the localized path.

toRelativePath

public static String toRelativePath(String path)
Converts an absolute path to a relative one.

Parameters:
path - the base name.
Returns:
the localized path.

toTimeZone

public static TimeZone toTimeZone(Locale locale)
Converts a locale to a time zone. The time zone must be defined as a variant of the locale.

Parameters:
locale - the locale.
Returns:
the time zone or null.

toTimeZone

public static TimeZone toTimeZone(String id)
Converts a time zone id or a locale variant to a time zone.

Parameters:
id - the id.
Returns:
the time zone or null.

toQualifiedName

public static String toQualifiedName(String name)
Converts a base name to the corresponding qualified name.

Parameters:
name - the base name.
Returns:
the corresponding qualified name.

toPackageName

public static String toPackageName(String uri,
                                   String domain)
Converts an URI path to a package name.

Parameters:
uri - the URI path to convert.
domain - the default domain to replace '*'.
Returns:
the corresponding package name or null if not valid.

toExtension

public static String toExtension(String path,
                                 String def)
Converts a path to its extension.

Parameters:
path - the path.
def - the default.
Returns:
the extension.

toExtension

public static String toExtension(File file,
                                 String def)
Converts a file to its extension.

Parameters:
file - the file.
def - the default.
Returns:
the extension.

toMBean

public static Class<?> toMBean(Class<?> clazz)
Converts a class into its MBean interface.

Parameters:
clazz - the class to convert.
Returns:
the MBean interface or null if not implemented.

toEventListeners

public static <L extends EventListener> OrderedMap<Class<L>,L[]> toEventListeners(String[] classNames)
                                                                                             throws ClassNotFoundException,
                                                                                                    InstantiationException,
                                                                                                    IllegalAccessException
Converts the named classes to event listener instances.

Type Parameters:
L - the event listener class.
Parameters:
classNames - the named classes to convert.
Returns:
a map of event listener interfaces and corresponding instances.
Throws:
ClassNotFoundException - for missing classes.
IllegalAccessException - for illegal access.
InstantiationException - for failed instantiation.

toEnumConstants

public static OrderedMap<Class<Enum<?>>,Enum<?>> toEnumConstants(String[] classNames)
                                                          throws ClassNotFoundException,
                                                                 InstantiationException,
                                                                 IllegalAccessException
Converts the named classes to enum instances.

Parameters:
classNames - the named classes to convert.
Returns:
a map of enum classes and corresponding instances.
Throws:
ClassNotFoundException - for missing classes.
IllegalAccessException - for illegal access.
InstantiationException - for failed instantiation.

toSignature

public static String toSignature(Object parameter)
Converts a parameter into the corresponding signature. Primitive types have precedence over the corresponding object types.

Parameters:
parameter - a parameter to convert.
Returns:
the converted signature.

toSignature

public static String[] toSignature(Object parameters,
                                   Object signature)
Converts parameters into the corresponding signature array. Parameters and signature may be an object and a string, an object array and a string array, or a list objects and a list of strings. A list of parameters without a signature is treated as a list of separate parameters. Primitive types have precedence over corresponding object types.

Parameters:
parameters - parameters to convert.
signature - an optional signature.
Returns:
the converted signature.

toParameters

public static Object[] toParameters(Object parameters,
                                    Object signature)
Converts parameters into the corresponding parameter array. Parameters and signature may be an object and a string, an object array and a string array, or a list objects and a list of strings. A list of parameters without a signature is treated as a list of separate parameters.

Parameters:
parameters - parameters to convert.
signature - an optional signature.
Returns:
the converted parameters.
Throws:
IllegalArgumentException - if parameters and signature are imcompatible with each other.

toPropertyDescriptor

public static PropertyDescriptor toPropertyDescriptor(Class<?> clazz,
                                                      String field)
Converts a field of a class into the corresponding property descriptor.

Parameters:
clazz - the class.
field - the field.
Returns:
the descriptor or null.

getPropertyValue

public static Object getPropertyValue(Object bean,
                                      String field)
                               throws IllegalAccessException,
                                      InvocationTargetException
Gets the value of the specified property.

Parameters:
bean - the bean.
field - the field.
Returns:
the value or null.
Throws:
IllegalAccessException - if inaccessible.
InvocationTargetException - if failed.
IllegalArgumentException - if not available.

setPropertyValue

public static void setPropertyValue(Object bean,
                                    String field,
                                    Object value)
                             throws IllegalAccessException,
                                    InvocationTargetException
Sets the value of the specified property.

Parameters:
bean - the bean.
field - the field.
value - the value.
Throws:
IllegalAccessException - if inaccessible.
InvocationTargetException - if failed.
IllegalArgumentException - if not available.

mergePropertyValues

public static void mergePropertyValues(Object source,
                                       Object target,
                                       AttributeFilter... filter)
                                throws IntrospectionException,
                                       IllegalAccessException,
                                       InvocationTargetException
Merges values of properties of two objects.

Parameters:
source - the source bean.
target - the target bean.
filter - an optional attribute filter.
Throws:
IllegalAccessException - if inaccessible.
IntrospectionException - if faulty.
InvocationTargetException - if failed.
IllegalArgumentException - if not available.


Copyright © 2004 The Norther Organization. All rights reserved.