Example usage for org.apache.commons.beanutils BeanComparator compare

List of usage examples for org.apache.commons.beanutils BeanComparator compare

Introduction

In this page you can find the example usage for org.apache.commons.beanutils BeanComparator compare.

Prototype

public int compare(Object o1, Object o2) 

Source Link

Document

Compare two JavaBeans by their shared property.

Usage

From source file:net.sourceforge.jaulp.io.SerializedObjectUtils.java

/**
 * Gets the changed data.// w  w w.j a v a 2 s . c o m
 *
 * @param sourceOjbect
 *            the source ojbect
 * @param objectToCompare
 *            the object to compare
 * @return the changed data
 * @throws IllegalAccessException
 *             the illegal access exception
 * @throws InvocationTargetException
 *             the invocation target exception
 * @throws NoSuchMethodException
 *             the no such method exception
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List<SerializedChangedAttributeResult> getChangedData(Object sourceOjbect, Object objectToCompare)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    Map beanDescription = BeanUtils.describe(sourceOjbect);
    beanDescription.remove("class");
    Map clonedBeanDescription = BeanUtils.describe(objectToCompare);
    clonedBeanDescription.remove("class");
    List<SerializedChangedAttributeResult> changedData = new ArrayList<>();
    for (Object key : beanDescription.keySet()) {
        BeanComparator comparator = new BeanComparator(key.toString());
        if (comparator.compare(sourceOjbect, objectToCompare) != 0) {
            Object sourceAttribute = beanDescription.get(key);
            Object changedAttribute = clonedBeanDescription.get(key);
            changedData.add(new SerializedChangedAttributeResult(key, sourceAttribute, changedAttribute));
        }
    }
    return changedData;
}

From source file:net.sourceforge.jaulp.io.SerializedObjectUtils.java

/**
 * Compares the given two objects and gets the changed data.
 *
 * @param sourceOjbect//  w  w  w .  ja va 2 s. com
 *            the source ojbect
 * @param objectToCompare
 *            the object to compare
 * @return the changed data
 * @throws IllegalAccessException
 *             the illegal access exception
 * @throws InvocationTargetException
 *             the invocation target exception
 * @throws NoSuchMethodException
 *             the no such method exception
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Map<Object, SerializedChangedAttributeResult> getChangedDataMap(Object sourceOjbect,
        Object objectToCompare)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    Map beanDescription = BeanUtils.describe(sourceOjbect);
    beanDescription.remove("class");
    Map clonedBeanDescription = BeanUtils.describe(objectToCompare);
    clonedBeanDescription.remove("class");
    Map<Object, SerializedChangedAttributeResult> changedData = new HashMap<>();
    for (Object key : beanDescription.keySet()) {
        BeanComparator comparator = new BeanComparator(key.toString());
        if (comparator.compare(sourceOjbect, objectToCompare) != 0) {
            Object sourceAttribute = beanDescription.get(key);
            Object changedAttribute = clonedBeanDescription.get(key);
            changedData.put(key, new SerializedChangedAttributeResult(key, sourceAttribute, changedAttribute));
        }
    }
    return changedData;
}

From source file:de.alpharogroup.io.SerializedObjectExtensions.java

/**
 * Gets the changed data./*from   w  ww.j  a va  2 s .  co  m*/
 *
 * @param sourceOjbect
 *            the source ojbect
 * @param objectToCompare
 *            the object to compare
 * @return the changed data
 * @throws IllegalAccessException
 *             the illegal access exception
 * @throws InvocationTargetException
 *             the invocation target exception
 * @throws NoSuchMethodException
 *             the no such method exception
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List<SerializedChangedAttributeResult> getChangedData(final Object sourceOjbect,
        final Object objectToCompare)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    final Map beanDescription = BeanUtils.describe(sourceOjbect);
    beanDescription.remove("class");
    final Map clonedBeanDescription = BeanUtils.describe(objectToCompare);
    clonedBeanDescription.remove("class");
    final List<SerializedChangedAttributeResult> changedData = new ArrayList<>();
    for (final Object key : beanDescription.keySet()) {
        final BeanComparator comparator = new BeanComparator(key.toString());
        if (comparator.compare(sourceOjbect, objectToCompare) != 0) {
            final Object sourceAttribute = beanDescription.get(key);
            final Object changedAttribute = clonedBeanDescription.get(key);
            changedData.add(new SerializedChangedAttributeResult(key, sourceAttribute, changedAttribute));
        }
    }
    return changedData;
}

From source file:de.alpharogroup.io.SerializedObjectExtensions.java

/**
 * Compares the given two objects and gets the changed data.
 *
 * @param sourceOjbect//from  w ww. j a  va  2  s.  co m
 *            the source ojbect
 * @param objectToCompare
 *            the object to compare
 * @return the changed data
 * @throws IllegalAccessException
 *             the illegal access exception
 * @throws InvocationTargetException
 *             the invocation target exception
 * @throws NoSuchMethodException
 *             the no such method exception
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Map<Object, SerializedChangedAttributeResult> getChangedDataMap(final Object sourceOjbect,
        final Object objectToCompare)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    final Map beanDescription = BeanUtils.describe(sourceOjbect);
    beanDescription.remove("class");
    final Map clonedBeanDescription = BeanUtils.describe(objectToCompare);
    clonedBeanDescription.remove("class");
    final Map<Object, SerializedChangedAttributeResult> changedData = new HashMap<>();
    for (final Object key : beanDescription.keySet()) {
        final BeanComparator comparator = new BeanComparator(key.toString());
        if (comparator.compare(sourceOjbect, objectToCompare) != 0) {
            final Object sourceAttribute = beanDescription.get(key);
            final Object changedAttribute = clonedBeanDescription.get(key);
            changedData.put(key, new SerializedChangedAttributeResult(key, sourceAttribute, changedAttribute));
        }
    }
    return changedData;
}

From source file:com.nec.nsgui.taglib.nssorttab.ListSTModel.java

public int compare(Object o1, Object o2) {

    if (o1 == null && o2 == null)
        return 0;

    if (o1 == null) {
        return isAscend ? -1 : 1;
    } else if (o2 == null) {
        return isAscend ? 1 : -1;
    }//from   w w  w.j  a va2s.c  om

    int result = 0; //the result of compare
    int size = sortData.size(); //the size of sortData 

    for (int i = 0; i < size && result == 0; i += 2) {
        String colName = (String) sortData.get(i);
        Object prop1 = null, prop2 = null;
        try {
            prop1 = PropertyUtils.getProperty(o1, colName);
            prop2 = PropertyUtils.getProperty(o2, colName);
        } catch (Exception e) {
        }

        if (prop1 == null && prop2 == null) {
            return 0;
        }
        if (prop1 == null) {
            result = -1;
        } else if (prop2 == null) {
            result = 1;
        } else {
            Comparator comparator = (Comparator) sortData.get(i + 1);
            BeanComparator beanComp;

            if (comparator != null) {
                beanComp = new BeanComparator(colName, comparator);
            } else {
                beanComp = new BeanComparator(colName);
            }

            result = beanComp.compare(o1, o2);
        }
        if (i == 0 && !isAscend) {
            result = -result;
        }
    }

    return result;
}