Java tutorial
//package com.java2s; public class Main { /** * This method is a helper for classes to implement {@link java.lang.Object#equals(java.lang.Object)} * The method checks whether the two arguments are both null or both not null and * whether they are of the same class * @param obj1 first object to compare * @param obj2 second object to compare * @return true if both objects are null or both are not null * and if both are of the same class if not null * false otherwise * * source from: https://github.com/apache/pig/blob/89c2e8e76c68d0d0abe6a36b4e08ddc56979796f/src/org/apache/pig/impl/util/Utils.java */ public static boolean checkNullAndClass(Object obj1, Object obj2) { if (checkNullEquals(obj1, obj2, false)) { if (obj1 != null) { return obj1.getClass() == obj2.getClass(); } else { return true; // both obj1 and obj2 should be null } } else { return false; } } /** * This method is a helper for classes to implement {@link java.lang.Object#equals(java.lang.Object)} * checks if two objects are equals - two levels of checks are * made - first if both are null or not null. If either is null, * check is made whether both are null. * If both are non null, equality also is checked if so indicated * @param obj1 first object to be compared * @param obj2 second object to be compared * @param checkEquality flag to indicate whether object equality should * be checked if obj1 and obj2 are non-null * @return true if the two objects are equal * false otherwise * * source from: https://github.com/apache/pig/blob/89c2e8e76c68d0d0abe6a36b4e08ddc56979796f/src/org/apache/pig/impl/util/Utils.java */ public static boolean checkNullEquals(Object obj1, Object obj2, boolean checkEquality) { if (obj1 == null || obj2 == null) { return obj1 == obj2; } if (checkEquality) { if (!obj1.equals(obj2)) { return false; } } return true; } }