Example usage for java.lang Object equals

List of usage examples for java.lang Object equals

Introduction

In this page you can find the example usage for java.lang Object equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Indicates whether some other object is "equal to" this one.

Usage

From source file:ObjectUtils.java

/**
 * <p>Compares two objects for equality, where either one or both
 * objects may be <code>null</code>.</p>
 *
 * <pre>/*  w ww .  ja  v  a 2  s  .c o  m*/
 * ObjectUtils.equals(null, null)                  = true
 * ObjectUtils.equals(null, "")                    = false
 * ObjectUtils.equals("", null)                    = false
 * ObjectUtils.equals("", "")                      = true
 * ObjectUtils.equals(Boolean.TRUE, null)          = false
 * ObjectUtils.equals(Boolean.TRUE, "true")        = false
 * ObjectUtils.equals(Boolean.TRUE, Boolean.TRUE)  = true
 * ObjectUtils.equals(Boolean.TRUE, Boolean.FALSE) = false
 * </pre>
 *
 * @param object1  the first object, may be <code>null</code>
 * @param object2  the second object, may be <code>null</code>
 * @return <code>true</code> if the values of both objects are the same
 */
public static boolean equals(Object object1, Object object2) {
    if (object1 == object2) {
        return true;
    }
    if ((object1 == null) || (object2 == null)) {
        return false;
    }
    return object1.equals(object2);
}

From source file:com.britesnow.snow.util.ObjectUtil.java

/**
 * Safe equal methods for two object. If both are null, return true, otherwise, if only one is null, return false,
 * otherwise, if they match return true if not false.
 * //from www  .jav  a2 s . c  o m
 * @param obj1
 * @param obj2
 * @return
 */
public static boolean equal(Object obj1, Object obj2) {
    if (obj1 == null && obj2 == null) {
        return true;
    }
    // if we are hear, one of the obj must be not null, so, if one null, return false
    if (obj1 == null || obj2 == null) {
        return false;
    }

    return obj1.equals(obj2);
}

From source file:com.p5solutions.core.utils.Comparison.java

/**
 * Checks if is equal./*from  w  ww  . j a v a 2  s. c o  m*/
 * 
 * @param a
 *          the a
 * @param b
 *          the b
 * @return true, if is equal
 */
public static boolean isEqual(Object a, Object b) {
    if (isNull(a) && isNull(b)) {
        return true;
    }

    if (!isNull(a)) {
        return a.equals(b);
    } else if (!isNull(b)) {
        return b.equals(a);
    }

    return false;
}

From source file:com.icesoft.faces.util.CoreUtils.java

public static boolean objectsEqual(Object ob1, Object ob2) {
    if (ob1 == null && ob2 == null) {
        return true;
    }//from  ww w. j a  va  2 s  .  com
    if (ob1 == null || ob2 == null) {
        return false;
    }
    return ob1.equals(ob2);
}

From source file:de.bayern.gdi.services.Service.java

private static boolean equals(Object a, Object b) {
    if (a == null && b == null) {
        return true;
    }//ww  w .jav a2  s .c o m
    if (a == null || b == null) {
        return false;
    }
    return a.equals(b);
}

From source file:com.linkedin.pinot.controller.helix.core.UAutoRebalancer.java

public static boolean isTheSameMapping(Map oldMapping, Map nMapping) {
    if (oldMapping.size() != nMapping.size())
        return false;
    Map<String, Object> total = new HashedMap(oldMapping);
    total.putAll(nMapping);//  ww w . ja va 2 s .com
    if (total.size() != oldMapping.size())
        return false;
    Set<Boolean> result = new HashSet<Boolean>();
    for (Object oKey : oldMapping.keySet()) {
        Object oValue = oldMapping.get(oKey);
        if (oValue instanceof String) {
            result.add(oValue.equals(nMapping.get(oValue)));
        } else if (oValue instanceof Map) {
            result.add(isTheSameMapping((Map) oValue, (Map) nMapping.get(oKey)));
        }
    }
    if (result.size() != 1)
        return false;
    return true;
}

From source file:com.haulmont.chile.core.model.utils.InstanceUtils.java

/**
 * Used by {@link AbstractInstance} to check whether property value has been changed.
 *
 * @param a an object/*from ww  w.  j  av a  2  s .  co m*/
 * @param b  an object
 * @return true if a equals to b, but in case of a is {@link AbstractInstance} returns true only if a are the same instance as b
 */
public static boolean propertyValueEquals(Object a, Object b) {
    if (a == b) {
        return true;
    }
    if (a instanceof AbstractInstance) {
        return false;
    }
    return a != null && a.equals(b);
}

From source file:com.link_intersystems.lang.Assert.java

/**
 * Assert that the value {@link Object} is equal to the expected
 * {@link Object} according to the {@link Object#equals(Object)}
 * specification.//from ww w  . j  av  a  2 s . c  o m
 *
 * @param name
 *            the name of the value.
 * @param expected
 *            the {@link Object} value that the value should be equal to.
 * @param value
 *            the {@link Object} value to ensure to be equal to the expected
 *            {@link Object}.
 * @since 1.2.0.0
 */
public static void equal(String name, Object expected, Object value) {
    if (expected == null && value == null) {
        return;
    }
    Object compare = expected;
    Object other = value;
    if (expected == null) {
        compare = value;
        other = expected;
    }
    if (!compare.equals(other)) {
        String exceptionMessage = String.format("%s was %s, but must be %s", name, value, expected);
        throw new IllegalArgumentException(exceptionMessage);
    }

}

From source file:Main.java

/**
 * Returns <code>true</code> iff all elements of {@code coll2} are also contained
 * in {@code coll1}. The cardinality of values in {@code coll2} is not taken into account,
 * which is the same behavior as {@link Collection#containsAll(Collection)}.
 * <p>/*from   w  w w  .j a v  a 2  s.com*/
 * In other words, this method returns <code>true</code> iff the
 * {@link #intersection} of <i>coll1</i> and <i>coll2</i> has the same cardinality as
 * the set of unique values from {@code coll2}. In case {@code coll2} is empty, {@code true}
 * will be returned.
 * <p>
 * This method is intended as a replacement for {@link Collection#containsAll(Collection)}
 * with a guaranteed runtime complexity of {@code O(n + m)}. Depending on the type of
 * {@link Collection} provided, this method will be much faster than calling
 * {@link Collection#containsAll(Collection)} instead, though this will come at the
 * cost of an additional space complexity O(n).
 *
 * @param coll1  the first collection, must not be null
 * @param coll2  the second collection, must not be null
 * @return <code>true</code> iff the intersection of the collections has the same cardinality
 *   as the set of unique elements from the second collection
 * @since 4.0
 */
public static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2) {
    if (coll2.isEmpty()) {
        return true;
    } else {
        final Iterator<?> it = coll1.iterator();
        final Set<Object> elementsAlreadySeen = new HashSet<Object>();
        for (final Object nextElement : coll2) {
            if (elementsAlreadySeen.contains(nextElement)) {
                continue;
            }

            boolean foundCurrentElement = false;
            while (it.hasNext()) {
                final Object p = it.next();
                elementsAlreadySeen.add(p);
                if (nextElement == null ? p == null : nextElement.equals(p)) {
                    foundCurrentElement = true;
                    break;
                }
            }

            if (foundCurrentElement) {
                continue;
            } else {
                return false;
            }
        }
        return true;
    }
}

From source file:org.codehaus.groovy.grails.plugins.searchable.compass.mapping.SearchableGrailsDomainClassCompassMappingUtils.java

/**
 * Get the mappable domain class properties
 * @param grailsDomainClass/*www  . j ava2  s.c  o  m*/
 * @param searchableValue
 * @param searchableGrailsDomainClasses
 * @param excludedProperties
 * @param domainClassPropertyMappingFactory
 * @return
 */
public static GrailsDomainClassProperty[] getMappableProperties(GrailsDomainClass grailsDomainClass,
        Object searchableValue, Collection searchableGrailsDomainClasses, final List excludedProperties,
        SearchableGrailsDomainClassPropertyMappingFactory domainClassPropertyMappingFactory) {
    boolean defaultExcludes = false;
    if (searchableValue instanceof Boolean) {
        if (searchableValue.equals(Boolean.FALSE)) {
            return null;
        }
        searchableValue = new HashMap() {
            {
                put("except", excludedProperties);
            }
        };
        defaultExcludes = true;
    }

    Class mappedClass = grailsDomainClass.getClazz();
    List properties = new ArrayList();
    for (int i = 0, max = grailsDomainClass.getProperties().length; i < max; i++) {
        GrailsDomainClassProperty property = grailsDomainClass.getProperties()[i];
        String propertyName = property.getName();
        if (propertyName.equals("id")) { // TODO refactor with specific id mapping
            continue;
        }
        if (!SearchableUtils.isIncludedProperty(propertyName, searchableValue)) {
            LOG.debug("Not mapping [" + ClassUtils.getShortName(mappedClass) + "." + propertyName
                    + "] because of "
                    + (defaultExcludes ? "default property exclusions" : "specified only/except rule"));
            continue;
        }

        if (domainClassPropertyMappingFactory.getGrailsDomainClassPropertyMapping(property,
                searchableGrailsDomainClasses) == null) {
            continue;
        }
        LOG.debug("Mapping [" + ClassUtils.getShortName(mappedClass) + "." + propertyName + "]");
        properties.add(property);
    }
    return (GrailsDomainClassProperty[]) properties.toArray(new GrailsDomainClassProperty[properties.size()]);
}