Example usage for org.apache.commons.lang.builder EqualsBuilder isEquals

List of usage examples for org.apache.commons.lang.builder EqualsBuilder isEquals

Introduction

In this page you can find the example usage for org.apache.commons.lang.builder EqualsBuilder isEquals.

Prototype

boolean isEquals

To view the source code for org.apache.commons.lang.builder EqualsBuilder isEquals.

Click Source Link

Document

If the fields tested are equals.

Usage

From source file:com.sonatype.security.ldap.api.DeepEqualsBuilder.java

private static void reflectionAppend(Object lhs, Object rhs, Class clazz, EqualsBuilder builder,
        boolean useTransients, String[] excludeFields) {
    while (clazz.getSuperclass() != null) {

        Field[] fields = clazz.getDeclaredFields();
        List excludedFieldList = excludeFields != null ? Arrays.asList(excludeFields) : Collections.EMPTY_LIST;
        AccessibleObject.setAccessible(fields, true);
        for (int i = 0; i < fields.length && builder.isEquals(); i++) {
            Field f = fields[i];//from  w  w w.  j  a  va 2 s. c  o  m
            if (!excludedFieldList.contains(f.getName()) && (f.getName().indexOf('$') == -1)
                    && (useTransients || !Modifier.isTransient(f.getModifiers()))
                    && (!Modifier.isStatic(f.getModifiers()))) {
                try {
                    Object lhsChild = f.get(lhs);
                    Object rhsChild = f.get(rhs);
                    Class testClass = getTestClass(lhsChild, rhsChild);
                    boolean hasEqualsMethod = classHasEqualsMethod(testClass);

                    if (testClass != null && !hasEqualsMethod) {
                        reflectionAppend(lhsChild, rhsChild, testClass, builder, useTransients, excludeFields);
                    } else {
                        builder.append(lhsChild, rhsChild);
                    }
                } catch (IllegalAccessException e) {
                    // this can't happen. Would get a Security exception instead
                    // throw a runtime exception in case the impossible happens.
                    throw new InternalError("Unexpected IllegalAccessException");
                }
            }
        }

        // now for the parent
        clazz = clazz.getSuperclass();
        reflectionAppend(lhs, rhs, clazz, builder, useTransients, excludeFields);
    }
}

From source file:com.google.code.simplestuff.bean.SimpleBean.java

/**
 * Compare two bean basing the comparison only on the {@link BusinessField}
 * annotated fields.// w  ww  .  j  ava  2 s .c  om
 * 
 * @param firstBean First bean to compare.
 * @param secondBean Second bean to compare.
 * @return The equals result.
 * @throws IllegalArgumentException If one of the beans compared is not an
 *         instance of a {@link BusinessObject} annotated class.
 */
public static boolean equals(Object firstBean, Object secondBean) {

    // null + !null = false
    // null + null = true
    if ((firstBean == null) || (secondBean == null)) {
        if ((firstBean == null) && (secondBean == null)) {
            return true;
        } else {
            return false;
        }
    }

    final BusinessObjectDescriptor firstBusinessObjectInfo = BusinessObjectContext
            .getBusinessObjectDescriptor(firstBean.getClass());
    final BusinessObjectDescriptor secondBusinessObjectInfo = BusinessObjectContext
            .getBusinessObjectDescriptor(secondBean.getClass());

    // We don't need here a not null check since by contract the
    // getBusinessObjectDescriptor method always returns an abject.

    // All this conditions are to support the case in which
    // SimpleBean.equals is used in not Business Object. Than the rules are:
    // !BO.equals(!BO) = The objects are equals if one of them is assignable
    // to the other (the or is used for the respect the symmetric rule)
    // !BO.eqauls(BO) = The equals of the !BO is used.
    // BO.equals(!BO) = The equals of the !BO is used.
    if (firstBusinessObjectInfo.getNearestBusinessObjectClass() == null) {
        if (secondBusinessObjectInfo.getNearestBusinessObjectClass() == null) {
            return firstBean.getClass().isAssignableFrom(secondBean.getClass())
                    || secondBean.getClass().isAssignableFrom(firstBean.getClass());
        } else {
            return firstBean.equals(secondBean);
        }
    } else if (secondBusinessObjectInfo.getNearestBusinessObjectClass() == null) {
        return secondBean.equals(firstBean);
    }

    // TODO: Revise this code in order to make it more readable...
    // If one of the two bean has the class with Business relevance then
    // we need to compare the lowest hierarchical annotated classes of
    // the two beans.
    if ((firstBusinessObjectInfo.isClassToBeConsideredInComparison()
            || secondBusinessObjectInfo.isClassToBeConsideredInComparison())
            && (!firstBusinessObjectInfo.getNearestBusinessObjectClass()
                    .equals(secondBusinessObjectInfo.getNearestBusinessObjectClass()))) {
        // If the comparison fails than we can already return false.
        return false;
    }

    // Then we continue with the annotated fields, first checking
    // if the two objects contain the same annotated fields. A paranoid
    // comparison (both sides) is done only if the two objects are not on
    // the same class in order to handle tricky cases.
    final boolean performParanoidComparison = false;
    if (!compareAnnotatedFieldsByName(firstBusinessObjectInfo.getAnnotatedFields(),
            secondBusinessObjectInfo.getAnnotatedFields(), performParanoidComparison)) {
        // If the comparison fails than we can already return false.
        return false;
    }

    // Then we continue with the values of the annotated fields.
    Collection<Field> firstBeanAnnotatedFields = firstBusinessObjectInfo.getAnnotatedFields();

    for (Field field : firstBeanAnnotatedFields) {

        final BusinessField fieldAnnotation = field.getAnnotation(BusinessField.class);
        // Since the cycle is on the annotated Field we are sure that
        // fieldAnnotation will always be not null.
        if (fieldAnnotation.includeInEquals()) {

            Object firstBeanFieldValue = null;
            Object secondBeanFieldValue = null;

            try {
                firstBeanFieldValue = PropertyUtils.getProperty(firstBean, field.getName());
                // Also in this case, since before of the cycle we
                // compare the annotated fields, we can be sure that the
                // field exists.
                secondBeanFieldValue = PropertyUtils.getProperty(secondBean, field.getName());

                // If there were problems (like when we compare
                // different Business Object with different Business
                // Fields), then we return false.
            } catch (IllegalAccessException e) {
                if (log.isDebugEnabled()) {
                    log.debug("IllegalAccessException exception when comparing class "
                            + firstBean.getClass().toString() + " with class"
                            + secondBean.getClass().toString(), e);
                }
                return false;
            } catch (InvocationTargetException e) {
                if (log.isDebugEnabled()) {
                    log.debug("InvocationTargetException exceptionwhen comparing class "
                            + firstBean.getClass().toString() + " with class"
                            + secondBean.getClass().toString(), e);
                }
                return false;
            } catch (NoSuchMethodException e) {
                if (log.isDebugEnabled()) {
                    log.debug("NoSuchMethodException exception when comparing class "
                            + firstBean.getClass().toString() + " with class"
                            + secondBean.getClass().toString(), e);
                }
                return false;
            }

            // Some date implementations give not exact
            // comparison...
            if ((ClassUtils.isAssignable(field.getType(), Date.class))
                    || (ClassUtils.isAssignable(field.getType(), Calendar.class))) {

                if (firstBeanFieldValue != null) {
                    firstBeanFieldValue = DateUtils.round(firstBeanFieldValue, Calendar.MILLISECOND);
                }

                if (secondBeanFieldValue != null) {
                    secondBeanFieldValue = DateUtils.round(secondBeanFieldValue, Calendar.MILLISECOND);
                }

            }

            // We use always EqualsBuilder since we can get also
            // primitive arrays and they need ot be internally
            // compared.
            EqualsBuilder equalsBuilder = new EqualsBuilder();
            equalsBuilder.append(firstBeanFieldValue, secondBeanFieldValue);
            if (!equalsBuilder.isEquals()) {
                return false;
            } else {

                // If we are here the bean are both not null and
                // equals or both null (then equals)... the cycle
                // can
                // continue...
                continue;
            }

        }
    }

    // If we finally arrive here, then all the comparison were
    // successful and the two beans are equals.
    return true;

}

From source file:com.vladmihalcea.hibernate.model.store.Company.java

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }/*from ww w .j a  v a  2s.  c  om*/
    if (!(obj instanceof Company)) {
        return false;
    }
    Company that = (Company) obj;
    EqualsBuilder eb = new EqualsBuilder();
    eb.append(name, that.name);
    return eb.isEquals();
}

From source file:com.alibaba.cobar.manager.util.Tuple.java

@Override
public boolean equals(Object obj) {
    if (obj == this)
        return true;
    if (!(obj instanceof Tuple))
        return false;
    Tuple that = (Tuple) obj;/*  w w w .  j av  a 2  s  .c o m*/
    EqualsBuilder builder = new EqualsBuilder();
    builder.append(this.objects, that.objects);
    return builder.isEquals();
}

From source file:com.vladmihalcea.hibernate.model.store.Importer.java

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }/*from   w  w  w .j  a  v  a2 s . com*/
    if (!(obj instanceof Importer)) {
        return false;
    }
    Importer that = (Importer) obj;
    EqualsBuilder eb = new EqualsBuilder();
    eb.append(name, that.getName());
    return eb.isEquals();
}

From source file:com.vladmihalcea.hibernate.model.store.SubVersion.java

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }// w w  w  .j a v a2s  .com
    if (!(obj instanceof SubVersion)) {
        return false;
    }
    SubVersion that = (SubVersion) obj;
    EqualsBuilder eb = new EqualsBuilder();
    eb.append(getCode(), that.getCode());
    return eb.isEquals();
}

From source file:com.vladmihalcea.hibernate.model.store.WarehouseProductInfo.java

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }//from   w  w  w.  j ava 2s .c om
    if (!(obj instanceof WarehouseProductInfo)) {
        return false;
    }
    WarehouseProductInfo that = (WarehouseProductInfo) obj;
    EqualsBuilder eb = new EqualsBuilder();
    eb.append(product, that.getProduct());
    return eb.isEquals();
}

From source file:com.vladmihalcea.hibernate.model.baglist.BagLeaf.java

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }// w w  w  .  j  a  v  a2s .com
    if (!(obj instanceof BagLeaf)) {
        return false;
    }
    BagLeaf that = (BagLeaf) obj;
    EqualsBuilder eb = new EqualsBuilder();
    eb.append(index, that.index);
    return eb.isEquals();
}

From source file:ch.systemsx.cisd.openbis.generic.shared.dto.InvalidationPE.java

@Override
public final boolean equals(final Object obj) {
    if (obj == this) {
        return true;
    }//from   w ww  . j  a v a  2 s. c  o  m
    if (obj instanceof InvalidationPE == false) {
        return false;
    }
    final InvalidationPE that = (InvalidationPE) obj;
    final EqualsBuilder builder = new EqualsBuilder();
    builder.append(id, that.id);
    return builder.isEquals();
}

From source file:ch.systemsx.cisd.openbis.generic.shared.dto.Code.java

@Override
public boolean equals(final Object obj) {
    if (obj == this) {
        return true;
    }//from  w w w .  ja v  a2  s . c o m
    if (obj instanceof Code<?> == false) {
        return false;
    }
    final Code<?> that = (Code<?>) obj;
    final EqualsBuilder builder = new EqualsBuilder();
    builder.append(that.code, code);
    return builder.isEquals();
}