Webwarp Util Bean

General

This project provides functionality for comparing and sorting beans. 

Class Overview

Utility Classes

Bean Comparator implementation

This package realizes a configurable comparator implementation for comparing beans of the same class.

Reflection sorter

This package realizes a reflection based comparator implementation for usage in collections.

Examples

Getting different property values

You can extract the differing properties of two beans by using the BeanComparator and registering a corresponding property change listener instance. For each differing property a corresponding property change event is created.

PropertyChangedListener listener = <yourListener>; BeanPropertiesComparator comparator = new BeanPropertiesComparator(); comparator.compareBeans(<aBean>, <anotherBean>, listener);

Configuring a BeanPropertyComparator

The bean comparator can additionally configured for only comparing properties with given types, e.g.:

comparator.setClassesToCompare(new Class[] { boolean.class, String.class, Number.class, Date.class });

Additionally properties can also explicitely being ecluded from the comparison by:

comparator.setPropertiesToIgnore(new String[] { "description", "dob", "externalID" });

If you set the recursive flag to TRUE thet also non trivial properties are recursively included into the bean comparison.
comparator.setRecursive(true);

Finally you can define what property value comparator implementation must be used for a given property type (for non default types you must do this).

PropertyValueComparator propertyValueComparator = new UpperCaseStringComparatorImpl(); comparator.addPropertyValueComparator(String.class, propertyValueComparator);

Sorting using the ReflectionComparator

Implementing a comparator for usage on a collection of arbitrary beans can be implemented very simple. Just create a ReflectorComparator instance and set the properties to be compared.

ReflectionComparator comparator = new ReflectionComparator("name", ComparedProperty.SORT_ASCENDING); Collections.sort(sortedBeans, comparator);

You can add additional properties to be compared as follows:

comparator.addComparedProperty("id", ComparedProperty.SORT_DESCENDING);

You can add additional properties to be compared as follows:

comparator.addComparedProperty("id", ComparedProperty.SORT_DESCENDING);

Webwarp Util