com.squeakysand.commons.lang.EqualsHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of squeakysand-commons Show documentation
Show all versions of squeakysand-commons Show documentation
Classes, interfaces and enums that assist with everyday Java development tasks.
The newest version!
/*
* Copyright 2010-2012 Craig S. Dickson (http://craigsdickson.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squeakysand.commons.lang;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.squeakysand.commons.beans.BeanHelper;
import com.squeakysand.commons.beans.BeanHelperException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Helper class that makes writing good equals() methods easier, it can also be used outside of an equals
* method where the equality of two objects needs to be determined. Arguments to any method that are not primitive can
* be null and will be handled correctly - this includes the ability to pass null where a
* primitive array is expected.
*
* @see java.util.Arrays
*/
public final class EqualsHelper {
private static final Logger LOG = LoggerFactory.getLogger(EqualsHelper.class);
private EqualsHelper() {
}
/**
* Tests the equality of two boolean values.
*
* @param b1 left side of equals operation.
* @param b2 right side of equals operation.
* @return true if they are equal and false otherwise.
*/
public static boolean equals(boolean b1, boolean b2) {
return b1 == b2;
}
/**
* Tests the equality of two arrays of boolean values.
*
* @param b1 left side of equals operation, can be null.
* @param b2 right side of equals operation, can be null.
* @return true if they are equal and false otherwise.
*/
public static boolean equals(boolean[] b1, boolean[] b2) {
return Arrays.equals(b1, b2);
}
/**
* Tests the equality of two long values.
*
* @param l1 left side of equals operation.
* @param l2 right side of equals operation.
* @return true if they are equal and false otherwise.
*/
public static boolean equals(long l1, long l2) {
return l1 == l2;
}
/**
* Tests the equality of two arrays of long values.
*
* @param l1 left side of equals operation, can be null.
* @param l2 right side of equals operation, can be null.
* @return true if they are equal and false otherwise.
*/
public static boolean equals(long[] l1, long[] l2) {
return Arrays.equals(l1, l2);
}
/**
* Tests the equality of two float values.
*
* @param f1 left side of equals operation.
* @param f2 right side of equals operation.
* @return true if they are equal and false otherwise.
*/
public static boolean equals(float f1, float f2) {
return f1 == f2;
}
/**
* Tests the equality of two arrays of float values.
*
* @param f1 left side of equals operation, can be null.
* @param f2 right side of equals operation, can be null.
* @return true if they are equal and false otherwise.
*/
public static boolean equals(float[] f1, float[] f2) {
return Arrays.equals(f1, f2);
}
/**
* Tests the equality of two double values.
*
* @param d1 left side of equals operation.
* @param d2 right side of equals operation.
* @return true if they are equal and false otherwise.
*/
public static boolean equals(double d1, double d2) {
return d1 == d2;
}
/**
* Tests the equality of two arrays of double values.
*
* @param d1 left side of equals operation, can be null.
* @param d2 right side of equals operation, can be null.
* @return true if they are equal and false otherwise.
*/
public static boolean equals(double[] d1, double[] d2) {
return Arrays.equals(d1, d2);
}
/**
* Tests the equality of two int values.
*
* @param i1 left side of equals operation.
* @param i2 right side of equals operation.
* @return true if they are equal and false otherwise.
*/
public static boolean equals(int i1, int i2) {
return i1 == i2;
}
/**
* Tests the equality of two arrays of int values.
*
* @param i1 left side of equals operation, can be null.
* @param i2 right side of equals operation, can be null.
* @return true if they are equal and false otherwise.
*/
public static boolean equals(int[] i1, int[] i2) {
return Arrays.equals(i1, i2);
}
/**
* Tests the equality of two arrays of byte values.
*
* @param b1 left side of equals operation, can be null.
* @param b2 right side of equals operation, can be null.
* @return true if they are equal and false otherwise.
*/
public static boolean equals(byte[] b1, byte[] b2) {
return Arrays.equals(b1, b2);
}
/**
* Tests the equality of two arrays of char values.
*
* @param c1 left side of equals operation, can be null.
* @param c2 right side of equals operation, can be null.
* @return true if they are equal and false otherwise.
*/
public static boolean equals(char[] c1, char[] c2) {
return Arrays.equals(c1, c2);
}
/**
* Tests the equality of two arrays of short values.
*
* @param s1 left side of equals operation, can be null.
* @param s2 right side of equals operation, can be null.
* @return true if they are equal and false otherwise.
*/
public static boolean equals(short[] s1, short[] s2) {
return Arrays.equals(s1, s2);
}
/**
* Tests the equality of two Objects.
*
* WARNING : If both objects passed to this method are non-null values, it will simply return the result
* of calling o1.equals(o2). This will cause an infinite recursion loop to be created if
* o1 uses this class to implement it's equals method.
*
* @param o1 left side of equals operation, can be null.
* @param o2 right side of equals operation, can be null.
* @return true if they are equal and false otherwise. Two null values will
* be considered equal.
*/
public static boolean equals(Object o1, Object o2) {
boolean result = false;
if ((o1 == null) && (o2 == null)) {
result = true;
} else if ((o1 != null) && (o2 != null)) {
result = o1.equals(o2);
}
return result;
}
/**
* Tests the equality of two arrays of Objects.
*
* @param o1 left side of equals operation, can be null.
* @param o2 right side of equals operation, can be null.
* @return true if they are equal and false otherwise.
* @see java.util.Arrays#equals(Object[], Object[])
*/
public static boolean equals(Object[] o1, Object[] o2) {
return Arrays.equals(o1, o2);
}
/**
* Tests the equality of two {@link java.util.Map} objects. The Map implementation classes are not
* considered in the decision, only the contents of the Maps are tested.
*
* @param m1 left side of equals operation, can be null.
* @param m2 right side of equals operation, can be null.
* @return true if both Maps are null, or both Maps have the same number of entries and
* the values for each key in both Maps are equal; false otherwise.
* @see java.util.Map#equals(Object)
* @param a K object.
* @param a V object.
*/
public static boolean equals(Map m1, Map m2) {
boolean result = true;
// make sure they are not both null first
if ((m1 != null) || (m2 != null)) {
// now check one is not null while the other isn't
if (((m1 == null) && (m2 != null)) || ((m1 != null) && (m2 == null))) {
result = false;
} else {
// the definition of Map.equals() is sufficient to use here
result = m1.equals(m2);
}
}
return result;
}
/**
* Compares 2 Lists for equality. The logic constrained by {@link List#equals(java.lang.Object)} is good enough,
* this method just takes care of handling null arguments.
*
* @param DOCUMENT ME!
* @param l1 DOCUMENT ME!
* @param l2 DOCUMENT ME!
* @return compares 2 Lists for equality.
*/
public static boolean equals(List l1, List l2) {
boolean result = false;
if (l1 != null) {
result = l1.equals(l2);
}
return result;
}
/**
* DOCUMENT ME!
*
* @param DOCUMENT ME!
* @param s1 DOCUMENT ME!
* @param s2 DOCUMENT ME!
* @return DOCUMENT ME!
*/
public static boolean equals(Set s1, Set s2) {
boolean result = false;
if (s1 != null) {
result = s1.equals(s2);
}
return result;
}
/**
* Treats 2 objects as JavaBeans and compares the bean properties of both for equivalence. The implementation class
* of each bean is not considered as part of the decision.
*
* @param bean1 left side of equals operation, can be null.
* @param bean2 right side of equals operation, can be null.
* @return true if both beans are null, or if both beans have the same number of readable
* properties, the names of those properties are equal and the values of those properties are equal;
* false otherwise.
*/
public static boolean equalsAsJavaBean(Object bean1, Object bean2) {
boolean result = true;
// make sure they are not both null first
if ((bean1 != null) || (bean2 != null)) {
// now check one is not null while the other isn't
if (((bean1 == null) && (bean2 != null)) || ((bean1 != null) && (bean2 == null))) {
result = false;
} else {
try {
Map o1properties = BeanHelper.getPropertyValues(bean1, true);
Map o2properties = BeanHelper.getPropertyValues(bean2, true);
result = equals(o1properties, o2properties);
} catch (BeanHelperException e) {
LOG.error(e.getMessage(), e);
result = false;
}
}
}
return result;
}
public static boolean equalsAsJavaBean(Object bean1, Object bean2, String... propertyNameFilter) {
boolean result = true;
// make sure they are not both null first
if ((bean1 != null) || (bean2 != null)) {
// now check one is not null while the other isn't
if (((bean1 == null) && (bean2 != null)) || ((bean1 != null) && (bean2 == null))) {
result = false;
} else {
try {
Map o1properties = BeanHelper.getPropertyValues(bean1, true, propertyNameFilter);
Map o2properties = BeanHelper.getPropertyValues(bean2, true, propertyNameFilter);
result = equals(o1properties, o2properties);
} catch (BeanHelperException e) {
LOG.error(e.getMessage(), e);
result = false;
}
}
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy