org.joda.beans
Class JodaBeanUtils

java.lang.Object
  extended by org.joda.beans.JodaBeanUtils

public final class JodaBeanUtils
extends Object

A set of utilities to assist when working with beans and properties.

Author:
Stephen Colebourne

Method Summary
static
<T extends Bean>
T
clone(T original)
           
static Class<?> collectionType(MetaProperty<?> prop, Class<?> targetClass)
          Extracts the collection content type as a Class from a meta-property.
static Class<?> collectionType(Property<?> prop)
          Extracts the collection content type as a Class from a property.
static Comparator<Bean> comparator(BeanQuery<?> query, boolean ascending)
          Obtains a comparator for the specified bean query.
static Comparator<Bean> comparatorAscending(BeanQuery<?> query)
          Obtains an ascending comparator for the specified bean query.
static Comparator<Bean> comparatorDescending(BeanQuery<?> query)
          Obtains an descending comparator for the specified bean query.
static boolean equal(Object obj1, Object obj2)
          Checks if two objects are equal handling null.
static int hashCode(double value)
          Returns a hash code for a double.
static int hashCode(float value)
          Returns a hash code for a float.
static int hashCode(long value)
          Returns a hash code for a long.
static int hashCode(Object obj)
          Returns a hash code for an object handling null.
static Class<?> mapKeyType(MetaProperty<?> prop, Class<?> targetClass)
          Extracts the map key type as a Class from a meta-property.
static Class<?> mapKeyType(Property<?> prop)
          Extracts the map key type as a Class from a meta-property.
static Class<?> mapValueType(MetaProperty<?> prop, Class<?> targetClass)
          Extracts the map key type as a Class from a meta-property.
static Class<?> mapValueType(Property<?> prop)
          Extracts the map key type as a Class from a meta-property.
static MetaBean metaBean(Class<?> cls)
          Gets the meta-bean given a class.
static void notEmpty(String value, String propertyName)
          Checks if the value is not empty, throwing an exception if it is.
static void notNull(Object value, String propertyName)
          Checks if the value is not null, throwing an exception if it is.
static boolean propertiesEqual(Bean bean1, Bean bean2)
          Checks if the two beans have the same set of properties.
static int propertiesHashCode(Bean bean)
          Returns a hash code based on the set of properties on a bean.
static String propertiesToString(Bean bean, String prefix)
          Returns a string describing the set of properties on a bean.
static void registerMetaBean(MetaBean metaBean)
          Registers a meta-bean.
static org.joda.convert.StringConvert stringConverter()
          Gets the standard string format converter.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

metaBean

public static MetaBean metaBean(Class<?> cls)
Gets the meta-bean given a class.

This only works for those beans that have registered their meta-beans. See registerMetaBean(MetaBean).

Parameters:
cls - the class to get the meta-bean for, not null
Returns:
the meta-bean, not null
Throws:
IllegalArgumentException - if unable to obtain the meta-bean

registerMetaBean

public static void registerMetaBean(MetaBean metaBean)
Registers a meta-bean.

This should be done for all beans in a static factory where possible. If the meta-bean is dynamic, this method should not be called.

Parameters:
metaBean - the meta-bean, not null
Throws:
IllegalArgumentException - if unable to register

stringConverter

public static org.joda.convert.StringConvert stringConverter()
Gets the standard string format converter.

This returns a singleton that may be mutated (holds a concurrent map). New conversions should be registered at program startup.

Returns:
the standard string converter, not null

equal

public static boolean equal(Object obj1,
                            Object obj2)
Checks if two objects are equal handling null.

Parameters:
obj1 - the first object, may be null
obj2 - the second object, may be null
Returns:
true if equal

hashCode

public static int hashCode(Object obj)
Returns a hash code for an object handling null.

Parameters:
obj - the object, may be null
Returns:
the hash code

hashCode

public static int hashCode(long value)
Returns a hash code for a long.

Parameters:
obj - the object, may be null
Returns:
the hash code

hashCode

public static int hashCode(float value)
Returns a hash code for a float.

Parameters:
obj - the object, may be null
Returns:
the hash code

hashCode

public static int hashCode(double value)
Returns a hash code for a double.

Parameters:
obj - the object, may be null
Returns:
the hash code

propertiesEqual

public static boolean propertiesEqual(Bean bean1,
                                      Bean bean2)
Checks if the two beans have the same set of properties.

This comparison checks that both beans have the same set of property names and that the value of each property name is also equal. It does not check the bean type, thus a FlexiBean may be equal to a DirectBean.

This comparison is usable with the propertiesHashCode(org.joda.beans.Bean) method. The result is the same as that if each bean was converted to a Map from name to value.

Parameters:
bean1 - the first bean to compare, not null
bean2 - the second bean to compare, not null
Returns:
true if equal

propertiesHashCode

public static int propertiesHashCode(Bean bean)
Returns a hash code based on the set of properties on a bean.

This hash code is usable with the propertiesEqual(org.joda.beans.Bean, org.joda.beans.Bean) method. The result is the same as that if each bean was converted to a Map from name to value.

Parameters:
bean - the bean to generate a hash code for, not null
Returns:
the hash code

propertiesToString

public static String propertiesToString(Bean bean,
                                        String prefix)
Returns a string describing the set of properties on a bean.

The result is the same as that if the bean was converted to a Map from name to value.

Parameters:
bean - the bean to generate a string for, not null
prefix - the prefix to use, null ignored
Returns:
the string form of the bean, not null

clone

public static <T extends Bean> T clone(T original)

notNull

public static void notNull(Object value,
                           String propertyName)
Checks if the value is not null, throwing an exception if it is.

Parameters:
value - the value to check, may be null
propertyName - the property name, should not be null
Throws:
IllegalArgumentException - if the value is null

notEmpty

public static void notEmpty(String value,
                            String propertyName)
Checks if the value is not empty, throwing an exception if it is.

Parameters:
value - the value to check, may be null
propertyName - the property name, should not be null
Throws:
IllegalArgumentException - if the value is null or empty

collectionType

public static Class<?> collectionType(Property<?> prop)
Extracts the collection content type as a Class from a property.

This method allows the resolution of generics in certain cases.

Parameters:
prop - the property to examine, not null
Returns:
the collection content type, null if unable to determine
Throws:
IllegalArgumentException - if the property is not a collection

collectionType

public static Class<?> collectionType(MetaProperty<?> prop,
                                      Class<?> targetClass)
Extracts the collection content type as a Class from a meta-property.

The target type is the type of the object, not the declaring type of the meta-property.

Parameters:
prop - the property to examine, not null
targetClass - the target type to evaluate against, not null
Returns:
the collection content type, null if unable to determine
Throws:
IllegalArgumentException - if the property is not a collection

mapKeyType

public static Class<?> mapKeyType(Property<?> prop)
Extracts the map key type as a Class from a meta-property.

Parameters:
prop - the property to examine, not null
Returns:
the map key type, null if unable to determine
Throws:
IllegalArgumentException - if the property is not a map

mapKeyType

public static Class<?> mapKeyType(MetaProperty<?> prop,
                                  Class<?> targetClass)
Extracts the map key type as a Class from a meta-property.

The target type is the type of the object, not the declaring type of the meta-property.

Parameters:
prop - the property to examine, not null
targetClass - the target type to evaluate against, not null
Returns:
the map key type, null if unable to determine
Throws:
IllegalArgumentException - if the property is not a map

mapValueType

public static Class<?> mapValueType(Property<?> prop)
Extracts the map key type as a Class from a meta-property.

Parameters:
prop - the property to examine, not null
Returns:
the map key type, null if unable to determine
Throws:
IllegalArgumentException - if the property is not a map

mapValueType

public static Class<?> mapValueType(MetaProperty<?> prop,
                                    Class<?> targetClass)
Extracts the map key type as a Class from a meta-property.

The target type is the type of the object, not the declaring type of the meta-property.

Parameters:
prop - the property to examine, not null
targetClass - the target type to evaluate against, not null
Returns:
the map key type, null if unable to determine
Throws:
IllegalArgumentException - if the property is not a map

comparator

public static Comparator<Bean> comparator(BeanQuery<?> query,
                                          boolean ascending)
Obtains a comparator for the specified bean query.

The result of the query must be Comparable.

Parameters:
query - the query to use, not null
ascending - true for ascending, false for descending
Returns:
the comparator, not null

comparatorAscending

public static Comparator<Bean> comparatorAscending(BeanQuery<?> query)
Obtains an ascending comparator for the specified bean query.

The result of the query must be Comparable.

Parameters:
query - the query to use, not null
Returns:
the comparator, not null

comparatorDescending

public static Comparator<Bean> comparatorDescending(BeanQuery<?> query)
Obtains an descending comparator for the specified bean query.

The result of the query must be Comparable.

Parameters:
query - the query to use, not null
Returns:
the comparator, not null


Copyright © 2007-2013 Joda.org. All Rights Reserved.