Class JodaBeanUtils


  • public final class JodaBeanUtils
    extends Object
    A set of utilities to assist when working with beans and properties.
    • Method Detail

      • metaBean

        @Deprecated
        public static MetaBean metaBean​(Class<?> cls)
        Deprecated.
        Obtains a meta-bean from a Class.

        This will return a meta-bean if it has been registered, or if the class implements DynamicBean and has a no-args constructor. Note that the common case where the meta-bean is registered by a static initializer is handled.

        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

        @Deprecated
        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
      • equal

        public static boolean equal​(float val1,
                                    float val2)
        Checks if two floats are equal based on identity.

        This performs the same check as Float.equals(Object).

        Parameters:
        val1 - the first value, may be null
        val2 - the second value, may be null
        Returns:
        true if equal
      • equalWithTolerance

        public static boolean equalWithTolerance​(float val1,
                                                 float val2,
                                                 double tolerance)
        Checks if two floats are equal within the specified tolerance.

        Two NaN values are equal. Positive and negative infinity are only equal with themselves. Otherwise, the difference between the values is compared to the tolerance.

        Parameters:
        val1 - the first value, may be null
        val2 - the second value, may be null
        tolerance - the tolerance used to compare equal
        Returns:
        true if equal
      • equal

        public static boolean equal​(double val1,
                                    double val2)
        Checks if two doubles are equal based on identity.

        This performs the same check as Double.equals(Object).

        Parameters:
        val1 - the first value, may be null
        val2 - the second value, may be null
        Returns:
        true if equal
      • equalWithTolerance

        public static boolean equalWithTolerance​(double val1,
                                                 double val2,
                                                 double tolerance)
        Checks if two doubles are equal within the specified tolerance.

        Two NaN values are equal. Positive and negative infinity are only equal with themselves. Otherwise, the difference between the values is compared to the tolerance. The tolerance is expected to be a finite value, not NaN or infinity.

        Parameters:
        val1 - the first value, may be null
        val2 - the second value, may be null
        tolerance - the tolerance used to compare equal
        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​(boolean value)
        Returns a hash code for a boolean.
        Parameters:
        value - the value to convert to a hash code
        Returns:
        the hash code
      • hashCode

        public static int hashCode​(int value)
        Returns a hash code for an int.
        Parameters:
        value - the value to convert to a hash code
        Returns:
        the hash code
      • hashCode

        public static int hashCode​(long value)
        Returns a hash code for a long.
        Parameters:
        value - the value to convert to a hash code
        Returns:
        the hash code
      • hashCode

        public static int hashCode​(float value)
        Returns a hash code for a float.
        Parameters:
        value - the value to convert to a hash code
        Returns:
        the hash code
      • hashCode

        public static int hashCode​(double value)
        Returns a hash code for a double.
        Parameters:
        value - the value to convert to a hash code
        Returns:
        the hash code
      • toString

        public static String toString​(Object obj)
        Returns the toString value handling arrays.
        Parameters:
        obj - the object, may be null
        Returns:
        the string, not null
      • 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
      • flatten

        public static Map<String,​Object> flatten​(Bean bean)
        Flattens a bean to a Map.

        The returned map will contain all the properties from the bean with their actual values.

        Parameters:
        bean - the bean to generate a string for, not null
        Returns:
        the bean as a map, not null
      • copy

        public static <T extends BeanBeanBuilder<T> copy​(Bean sourceBean,
                                                           Class<T> destType)
        Copies properties from a bean to a different bean type.

        This copies each non-null property value from the source bean to the destination builder provided that the destination builder supports the property name and the type is compatible.

        Type Parameters:
        T - the type of the bean to create
        Parameters:
        sourceBean - the bean to copy from, not null
        destType - the type of the destination bean, not null
        Returns:
        the copied bean as a builder
        Throws:
        RuntimeException - if unable to copy a property
      • copyInto

        public static <T extends BeanBeanBuilder<T> copyInto​(Bean sourceBean,
                                                               MetaBean destMeta,
                                                               BeanBuilder<T> destBuilder)
        Copies properties from a bean to a builder, which may be for a different type.

        This copies each non-null property value from the source bean to the destination builder provided that the destination builder supports the property name and the type is compatible.

        Type Parameters:
        T - the type of the bean to create
        Parameters:
        sourceBean - the bean to copy from, not null
        destMeta - the meta bean of the destination, not null
        destBuilder - the builder to populate, not null
        Returns:
        the updated builder
        Throws:
        RuntimeException - if unable to copy a property
      • clone

        public static <T extends Bean> T clone​(T original)
        Clones a bean.

        This performs a deep clone. There is no protection against cycles in the object graph beyond StackOverflowError.

        Type Parameters:
        T - the type of the bean
        Parameters:
        original - the original bean to clone, null returns null
        Returns:
        the cloned bean, null if null input
      • cloneAlways

        public static <T extends Bean> T cloneAlways​(T original)
        Clones a bean always.

        This performs a deep clone. There is no protection against cycles in the object graph beyond StackOverflowError. This differs from Object.clone() in that immutable beans are also cloned.

        Type Parameters:
        T - the type of the bean
        Parameters:
        original - the original bean to clone, not null
        Returns:
        the cloned bean, not null
      • 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
      • notBlank

        public static void notBlank​(String value,
                                    String propertyName)
        Checks if the value is not blank, throwing an exception if it is.

        Validate that the specified argument is not null and has at least one non-whitespace character.

        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
      • 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
      • notEmpty

        public static void notEmpty​(Collection<?> value,
                                    String propertyName)
        Checks if the collection 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
      • notEmpty

        public static void notEmpty​(Map<?,​?> value,
                                    String propertyName)
        Checks if the map 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 or type has no generic parameters
      • 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 or type has no generic parameters
      • collectionTypeTypes

        public static List<Class<?>> collectionTypeTypes​(MetaProperty<?> prop,
                                                         Class<?> targetClass)
        Extracts the map value type generic type parameters as a Class from a meta-property.

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

        This is used when the collection generic parameter is a map or collection.

        Parameters:
        prop - the property to examine, not null
        targetClass - the target type to evaluate against, not null
        Returns:
        the collection content type generic parameters, empty if unable to determine, no nulls
      • 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 or type has no generic parameters
      • 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 or type has no generic parameters
      • mapValueType

        public static Class<?> mapValueType​(Property<?> prop)
        Extracts the map value type as a Class from a meta-property.
        Parameters:
        prop - the property to examine, not null
        Returns:
        the map value type, null if unable to determine or type has no generic parameters
      • mapValueType

        public static Class<?> mapValueType​(MetaProperty<?> prop,
                                            Class<?> targetClass)
        Extracts the map value 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 value type, null if unable to determine or type has no generic parameters
      • mapValueTypeTypes

        public static List<Class<?>> mapValueTypeTypes​(MetaProperty<?> prop,
                                                       Class<?> targetClass)
        Extracts the map value type generic type parameters as a Class from a meta-property.

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

        This is used when the map value generic parameter is a map or collection.

        Parameters:
        prop - the property to examine, not null
        targetClass - the target type to evaluate against, not null
        Returns:
        the map value type generic parameters, empty if unable to determine, no nulls
      • extractTypeClass

        public static Class<?> extractTypeClass​(MetaProperty<?> prop,
                                                Class<?> targetClass,
                                                int size,
                                                int index)
        Low-level method to extract generic type information.
        Parameters:
        prop - the property to examine, not null
        targetClass - the target type to evaluate against, not null
        size - the number of generic parameters expected
        index - the index of the generic parameter
        Returns:
        the type, null if unable to determine or type has no generic parameters
      • equalIgnoring

        public static boolean equalIgnoring​(Bean bean1,
                                            Bean bean2,
                                            MetaProperty<?>... properties)
        Checks if two beans are equal ignoring one or more properties.

        This version of equalIgnoring only checks properties at the top level. For example, if a Person bean contains an Address bean then only properties on the Person bean will be checked against the ignore list.

        Parameters:
        bean1 - the first bean, not null
        bean2 - the second bean, not null
        properties - the properties to ignore, not null
        Returns:
        true if equal
        Throws:
        IllegalArgumentException - if inputs are null
      • beanIterator

        public static Iterator<Bean> beanIterator​(Bean bean)
        Returns an iterator over all the beans contained within the bean.

        The iterator is a depth-first traversal of the beans within the specified bean. The first returned bean is the specified bean. Beans within collections will be returned.

        A cycle in the bean structure will cause an infinite loop.

        Parameters:
        bean - the bean to iterate over, not null
        Returns:
        the iterator, not null
      • chain

        public static <P> Function<Bean,​P> chain​(MetaProperty<? extends Bean> mp1,
                                                       MetaProperty<P> mp2)
        Chains two meta-properties together.

        The resulting function takes an instance of a bean, queries using the first meta-property, then queries the result using the second meta-property. If the first returns null, the result will be null.

        Type Parameters:
        P - the type of the result of the chain
        Parameters:
        mp1 - the first meta-property, not null
        mp2 - the second meta-property, not null
        Returns:
        the chain function, not null
      • chain

        public static <P> Function<Bean,​P> chain​(Function<Bean,​? extends Bean> fn1,
                                                       MetaProperty<P> mp2)
        Chains a function to a meta-property.

        The resulting function takes an instance of a bean, queries using the first function, then queries the result using the second meta-property. If the first returns null, the result will be null.

        Type Parameters:
        P - the type of the result of the chain
        Parameters:
        fn1 - the first meta-property, not null
        mp2 - the second meta-property, not null
        Returns:
        the chain function, not null
      • comparator

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

        The result of the query must be Comparable.

        To use this with a meta-property append ::get to the meta-property, for example Address.meta().street()::get.

        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​(Function<Bean,​?> 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​(Function<Bean,​?> 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