Coverage Report - org.apache.commons.lang3.builder.EqualsBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
EqualsBuilder
91%
240/262
83%
195/234
5,882
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.lang3.builder;
 18  
 
 19  
 import java.lang.reflect.AccessibleObject;
 20  
 import java.lang.reflect.Field;
 21  
 import java.lang.reflect.Modifier;
 22  
 import java.util.Collection;
 23  
 import java.util.HashSet;
 24  
 import java.util.Set;
 25  
 
 26  
 import org.apache.commons.lang3.ArrayUtils;
 27  
 import org.apache.commons.lang3.tuple.Pair;
 28  
 
 29  
 /**
 30  
  * <p>Assists in implementing {@link Object#equals(Object)} methods.</p>
 31  
  *
 32  
  * <p> This class provides methods to build a good equals method for any
 33  
  * class. It follows rules laid out in
 34  
  * <a href="http://www.oracle.com/technetwork/java/effectivejava-136174.html">Effective Java</a>
 35  
  * , by Joshua Bloch. In particular the rule for comparing <code>doubles</code>,
 36  
  * <code>floats</code>, and arrays can be tricky. Also, making sure that
 37  
  * <code>equals()</code> and <code>hashCode()</code> are consistent can be
 38  
  * difficult.</p>
 39  
  *
 40  
  * <p>Two Objects that compare as equals must generate the same hash code,
 41  
  * but two Objects with the same hash code do not have to be equal.</p>
 42  
  *
 43  
  * <p>All relevant fields should be included in the calculation of equals.
 44  
  * Derived fields may be ignored. In particular, any field used in
 45  
  * generating a hash code must be used in the equals method, and vice
 46  
  * versa.</p>
 47  
  *
 48  
  * <p>Typical use for the code is as follows:</p>
 49  
  * <pre>
 50  
  * public boolean equals(Object obj) {
 51  
  *   if (obj == null) { return false; }
 52  
  *   if (obj == this) { return true; }
 53  
  *   if (obj.getClass() != getClass()) {
 54  
  *     return false;
 55  
  *   }
 56  
  *   MyClass rhs = (MyClass) obj;
 57  
  *   return new EqualsBuilder()
 58  
  *                 .appendSuper(super.equals(obj))
 59  
  *                 .append(field1, rhs.field1)
 60  
  *                 .append(field2, rhs.field2)
 61  
  *                 .append(field3, rhs.field3)
 62  
  *                 .isEquals();
 63  
  *  }
 64  
  * </pre>
 65  
  *
 66  
  * <p> Alternatively, there is a method that uses reflection to determine
 67  
  * the fields to test. Because these fields are usually private, the method,
 68  
  * <code>reflectionEquals</code>, uses <code>AccessibleObject.setAccessible</code> to
 69  
  * change the visibility of the fields. This will fail under a security
 70  
  * manager, unless the appropriate permissions are set up correctly. It is
 71  
  * also slower than testing explicitly.</p>
 72  
  *
 73  
  * <p> A typical invocation for this method would look like:</p>
 74  
  * <pre>
 75  
  * public boolean equals(Object obj) {
 76  
  *   return EqualsBuilder.reflectionEquals(this, obj);
 77  
  * }
 78  
  * </pre>
 79  
  *
 80  
  * @since 1.0
 81  
  * @version $Id: EqualsBuilder.java 1535757 2013-10-25 15:07:13Z mbenson $
 82  
  */
 83  0
 public class EqualsBuilder implements Builder<Boolean> {
 84  
 
 85  
     /**
 86  
      * <p>
 87  
      * A registry of objects used by reflection methods to detect cyclical object references and avoid infinite loops.
 88  
      * </p>
 89  
      *
 90  
      * @since 3.0
 91  
      */
 92  1
     private static final ThreadLocal<Set<Pair<IDKey, IDKey>>> REGISTRY = new ThreadLocal<Set<Pair<IDKey, IDKey>>>();
 93  
 
 94  
     /*
 95  
      * NOTE: we cannot store the actual objects in a HashSet, as that would use the very hashCode()
 96  
      * we are in the process of calculating.
 97  
      *
 98  
      * So we generate a one-to-one mapping from the original object to a new object.
 99  
      *
 100  
      * Now HashSet uses equals() to determine if two elements with the same hashcode really
 101  
      * are equal, so we also need to ensure that the replacement objects are only equal
 102  
      * if the original objects are identical.
 103  
      *
 104  
      * The original implementation (2.4 and before) used the System.indentityHashCode()
 105  
      * method - however this is not guaranteed to generate unique ids (e.g. LANG-459)
 106  
      *
 107  
      * We now use the IDKey helper class (adapted from org.apache.axis.utils.IDKey)
 108  
      * to disambiguate the duplicate ids.
 109  
      */
 110  
 
 111  
     /**
 112  
      * <p>
 113  
      * Returns the registry of object pairs being traversed by the reflection
 114  
      * methods in the current thread.
 115  
      * </p>
 116  
      *
 117  
      * @return Set the registry of objects being traversed
 118  
      * @since 3.0
 119  
      */
 120  
     static Set<Pair<IDKey, IDKey>> getRegistry() {
 121  1759
         return REGISTRY.get();
 122  
     }
 123  
 
 124  
     /**
 125  
      * <p>
 126  
      * Converters value pair into a register pair.
 127  
      * </p>
 128  
      *
 129  
      * @param lhs <code>this</code> object
 130  
      * @param rhs the other object
 131  
      *
 132  
      * @return the pair
 133  
      */
 134  
     static Pair<IDKey, IDKey> getRegisterPair(final Object lhs, final Object rhs) {
 135  1056
         final IDKey left = new IDKey(lhs);
 136  1056
         final IDKey right = new IDKey(rhs);
 137  1056
         return Pair.of(left, right);
 138  
     }
 139  
 
 140  
     /**
 141  
      * <p>
 142  
      * Returns <code>true</code> if the registry contains the given object pair.
 143  
      * Used by the reflection methods to avoid infinite loops.
 144  
      * Objects might be swapped therefore a check is needed if the object pair
 145  
      * is registered in given or swapped order.
 146  
      * </p>
 147  
      *
 148  
      * @param lhs <code>this</code> object to lookup in registry
 149  
      * @param rhs the other object to lookup on registry
 150  
      * @return boolean <code>true</code> if the registry contains the given object.
 151  
      * @since 3.0
 152  
      */
 153  
     static boolean isRegistered(final Object lhs, final Object rhs) {
 154  356
         final Set<Pair<IDKey, IDKey>> registry = getRegistry();
 155  356
         final Pair<IDKey, IDKey> pair = getRegisterPair(lhs, rhs);
 156  356
         final Pair<IDKey, IDKey> swappedPair = Pair.of(pair.getLeft(), pair.getRight());
 157  
 
 158  356
         return registry != null
 159  
                 && (registry.contains(pair) || registry.contains(swappedPair));
 160  
     }
 161  
 
 162  
     /**
 163  
      * <p>
 164  
      * Registers the given object pair.
 165  
      * Used by the reflection methods to avoid infinite loops.
 166  
      * </p>
 167  
      *
 168  
      * @param lhs <code>this</code> object to register
 169  
      * @param rhs the other object to register
 170  
      */
 171  
     static void register(final Object lhs, final Object rhs) {
 172  350
         synchronized (EqualsBuilder.class) {
 173  350
             if (getRegistry() == null) {
 174  344
                 REGISTRY.set(new HashSet<Pair<IDKey, IDKey>>());
 175  
             }
 176  350
         }
 177  
 
 178  350
         final Set<Pair<IDKey, IDKey>> registry = getRegistry();
 179  350
         final Pair<IDKey, IDKey> pair = getRegisterPair(lhs, rhs);
 180  350
         registry.add(pair);
 181  350
     }
 182  
 
 183  
     /**
 184  
      * <p>
 185  
      * Unregisters the given object pair.
 186  
      * </p>
 187  
      *
 188  
      * <p>
 189  
      * Used by the reflection methods to avoid infinite loops.
 190  
      *
 191  
      * @param lhs <code>this</code> object to unregister
 192  
      * @param rhs the other object to unregister
 193  
      * @since 3.0
 194  
      */
 195  
     static void unregister(final Object lhs, final Object rhs) {
 196  350
         Set<Pair<IDKey, IDKey>> registry = getRegistry();
 197  350
         if (registry != null) {
 198  350
             final Pair<IDKey, IDKey> pair = getRegisterPair(lhs, rhs);
 199  350
             registry.remove(pair);
 200  350
             synchronized (EqualsBuilder.class) {
 201  
                 //read again
 202  350
                 registry = getRegistry();
 203  350
                 if (registry != null && registry.isEmpty()) {
 204  344
                     REGISTRY.remove();
 205  
                 }
 206  350
             }
 207  
         }
 208  350
     }
 209  
 
 210  
     /**
 211  
      * If the fields tested are equals.
 212  
      * The default value is <code>true</code>.
 213  
      */
 214  466
     private boolean isEquals = true;
 215  
 
 216  
     /**
 217  
      * <p>Constructor for EqualsBuilder.</p>
 218  
      *
 219  
      * <p>Starts off assuming that equals is <code>true</code>.</p>
 220  
      * @see Object#equals(Object)
 221  
      */
 222  466
     public EqualsBuilder() {
 223  
         // do nothing for now.
 224  466
     }
 225  
 
 226  
     //-------------------------------------------------------------------------
 227  
 
 228  
     /**
 229  
      * <p>This method uses reflection to determine if the two <code>Object</code>s
 230  
      * are equal.</p>
 231  
      *
 232  
      * <p>It uses <code>AccessibleObject.setAccessible</code> to gain access to private
 233  
      * fields. This means that it will throw a security exception if run under
 234  
      * a security manager, if the permissions are not set up correctly. It is also
 235  
      * not as efficient as testing explicitly.</p>
 236  
      *
 237  
      * <p>Transient members will be not be tested, as they are likely derived
 238  
      * fields, and not part of the value of the Object.</p>
 239  
      *
 240  
      * <p>Static fields will not be tested. Superclass fields will be included.</p>
 241  
      *
 242  
      * @param lhs  <code>this</code> object
 243  
      * @param rhs  the other object
 244  
      * @param excludeFields  Collection of String field names to exclude from testing
 245  
      * @return <code>true</code> if the two Objects have tested equals.
 246  
      */
 247  
     public static boolean reflectionEquals(final Object lhs, final Object rhs, final Collection<String> excludeFields) {
 248  0
         return reflectionEquals(lhs, rhs, ReflectionToStringBuilder.toNoNullStringArray(excludeFields));
 249  
     }
 250  
 
 251  
     /**
 252  
      * <p>This method uses reflection to determine if the two <code>Object</code>s
 253  
      * are equal.</p>
 254  
      *
 255  
      * <p>It uses <code>AccessibleObject.setAccessible</code> to gain access to private
 256  
      * fields. This means that it will throw a security exception if run under
 257  
      * a security manager, if the permissions are not set up correctly. It is also
 258  
      * not as efficient as testing explicitly.</p>
 259  
      *
 260  
      * <p>Transient members will be not be tested, as they are likely derived
 261  
      * fields, and not part of the value of the Object.</p>
 262  
      *
 263  
      * <p>Static fields will not be tested. Superclass fields will be included.</p>
 264  
      *
 265  
      * @param lhs  <code>this</code> object
 266  
      * @param rhs  the other object
 267  
      * @param excludeFields  array of field names to exclude from testing
 268  
      * @return <code>true</code> if the two Objects have tested equals.
 269  
      */
 270  
     public static boolean reflectionEquals(final Object lhs, final Object rhs, final String... excludeFields) {
 271  35
         return reflectionEquals(lhs, rhs, false, null, excludeFields);
 272  
     }
 273  
 
 274  
     /**
 275  
      * <p>This method uses reflection to determine if the two <code>Object</code>s
 276  
      * are equal.</p>
 277  
      *
 278  
      * <p>It uses <code>AccessibleObject.setAccessible</code> to gain access to private
 279  
      * fields. This means that it will throw a security exception if run under
 280  
      * a security manager, if the permissions are not set up correctly. It is also
 281  
      * not as efficient as testing explicitly.</p>
 282  
      *
 283  
      * <p>If the TestTransients parameter is set to <code>true</code>, transient
 284  
      * members will be tested, otherwise they are ignored, as they are likely
 285  
      * derived fields, and not part of the value of the <code>Object</code>.</p>
 286  
      *
 287  
      * <p>Static fields will not be tested. Superclass fields will be included.</p>
 288  
      *
 289  
      * @param lhs  <code>this</code> object
 290  
      * @param rhs  the other object
 291  
      * @param testTransients  whether to include transient fields
 292  
      * @return <code>true</code> if the two Objects have tested equals.
 293  
      */
 294  
     public static boolean reflectionEquals(final Object lhs, final Object rhs, final boolean testTransients) {
 295  155
         return reflectionEquals(lhs, rhs, testTransients, null);
 296  
     }
 297  
 
 298  
     /**
 299  
      * <p>This method uses reflection to determine if the two <code>Object</code>s
 300  
      * are equal.</p>
 301  
      *
 302  
      * <p>It uses <code>AccessibleObject.setAccessible</code> to gain access to private
 303  
      * fields. This means that it will throw a security exception if run under
 304  
      * a security manager, if the permissions are not set up correctly. It is also
 305  
      * not as efficient as testing explicitly.</p>
 306  
      *
 307  
      * <p>If the testTransients parameter is set to <code>true</code>, transient
 308  
      * members will be tested, otherwise they are ignored, as they are likely
 309  
      * derived fields, and not part of the value of the <code>Object</code>.</p>
 310  
      *
 311  
      * <p>Static fields will not be included. Superclass fields will be appended
 312  
      * up to and including the specified superclass. A null superclass is treated
 313  
      * as java.lang.Object.</p>
 314  
      *
 315  
      * @param lhs  <code>this</code> object
 316  
      * @param rhs  the other object
 317  
      * @param testTransients  whether to include transient fields
 318  
      * @param reflectUpToClass  the superclass to reflect up to (inclusive),
 319  
      *  may be <code>null</code>
 320  
      * @param excludeFields  array of field names to exclude from testing
 321  
      * @return <code>true</code> if the two Objects have tested equals.
 322  
      * @since 2.0
 323  
      */
 324  
     public static boolean reflectionEquals(final Object lhs, final Object rhs, final boolean testTransients, final Class<?> reflectUpToClass,
 325  
             final String... excludeFields) {
 326  190
         if (lhs == rhs) {
 327  24
             return true;
 328  
         }
 329  166
         if (lhs == null || rhs == null) {
 330  18
             return false;
 331  
         }
 332  
         // Find the leaf class since there may be transients in the leaf
 333  
         // class or in classes between the leaf and root.
 334  
         // If we are not testing transients or a subclass has no ivars,
 335  
         // then a subclass can test equals to a superclass.
 336  148
         final Class<?> lhsClass = lhs.getClass();
 337  148
         final Class<?> rhsClass = rhs.getClass();
 338  
         Class<?> testClass;
 339  148
         if (lhsClass.isInstance(rhs)) {
 340  131
             testClass = lhsClass;
 341  131
             if (!rhsClass.isInstance(lhs)) {
 342  
                 // rhsClass is a subclass of lhsClass
 343  16
                 testClass = rhsClass;
 344  
             }
 345  17
         } else if (rhsClass.isInstance(lhs)) {
 346  14
             testClass = rhsClass;
 347  14
             if (!lhsClass.isInstance(rhs)) {
 348  
                 // lhsClass is a subclass of rhsClass
 349  14
                 testClass = lhsClass;
 350  
             }
 351  
         } else {
 352  
             // The two classes are not related.
 353  3
             return false;
 354  
         }
 355  145
         final EqualsBuilder equalsBuilder = new EqualsBuilder();
 356  
         try {
 357  145
             if (testClass.isArray()) {
 358  4
                 equalsBuilder.append(lhs, rhs);
 359  
             } else {
 360  141
                 reflectionAppend(lhs, rhs, testClass, equalsBuilder, testTransients, excludeFields);
 361  348
                 while (testClass.getSuperclass() != null && testClass != reflectUpToClass) {
 362  215
                     testClass = testClass.getSuperclass();
 363  215
                     reflectionAppend(lhs, rhs, testClass, equalsBuilder, testTransients, excludeFields);
 364  
                 }
 365  
             }
 366  8
         } catch (final IllegalArgumentException e) {
 367  
             // In this case, we tried to test a subclass vs. a superclass and
 368  
             // the subclass has ivars or the ivars are transient and
 369  
             // we are testing transients.
 370  
             // If a subclass has ivars that we are trying to test them, we get an
 371  
             // exception and we know that the objects are not equal.
 372  8
             return false;
 373  137
         }
 374  137
         return equalsBuilder.isEquals();
 375  
     }
 376  
 
 377  
     /**
 378  
      * <p>Appends the fields and values defined by the given object of the
 379  
      * given Class.</p>
 380  
      *
 381  
      * @param lhs  the left hand object
 382  
      * @param rhs  the right hand object
 383  
      * @param clazz  the class to append details of
 384  
      * @param builder  the builder to append to
 385  
      * @param useTransients  whether to test transient fields
 386  
      * @param excludeFields  array of field names to exclude from testing
 387  
      */
 388  
     private static void reflectionAppend(
 389  
         final Object lhs,
 390  
         final Object rhs,
 391  
         final Class<?> clazz,
 392  
         final EqualsBuilder builder,
 393  
         final boolean useTransients,
 394  
         final String[] excludeFields) {
 395  
 
 396  356
         if (isRegistered(lhs, rhs)) {
 397  6
             return;
 398  
         }
 399  
 
 400  
         try {
 401  350
             register(lhs, rhs);
 402  350
             final Field[] fields = clazz.getDeclaredFields();
 403  350
             AccessibleObject.setAccessible(fields, true);
 404  824
             for (int i = 0; i < fields.length && builder.isEquals; i++) {
 405  482
                 final Field f = fields[i];
 406  482
                 if (!ArrayUtils.contains(excludeFields, f.getName())
 407  
                     && (f.getName().indexOf('$') == -1)
 408  
                     && (useTransients || !Modifier.isTransient(f.getModifiers()))
 409  
                     && (!Modifier.isStatic(f.getModifiers()))) {
 410  
                     try {
 411  417
                         builder.append(f.get(lhs), f.get(rhs));
 412  0
                     } catch (final IllegalAccessException e) {
 413  
                         //this can't happen. Would get a Security exception instead
 414  
                         //throw a runtime exception in case the impossible happens.
 415  0
                         throw new InternalError("Unexpected IllegalAccessException");
 416  409
                     }
 417  
                 }
 418  
             }
 419  
         } finally {
 420  350
             unregister(lhs, rhs);
 421  342
         }
 422  342
     }
 423  
 
 424  
     //-------------------------------------------------------------------------
 425  
 
 426  
     /**
 427  
      * <p>Adds the result of <code>super.equals()</code> to this builder.</p>
 428  
      *
 429  
      * @param superEquals  the result of calling <code>super.equals()</code>
 430  
      * @return EqualsBuilder - used to chain calls.
 431  
      * @since 2.0
 432  
      */
 433  
     public EqualsBuilder appendSuper(final boolean superEquals) {
 434  4
         if (isEquals == false) {
 435  0
             return this;
 436  
         }
 437  4
         isEquals = superEquals;
 438  4
         return this;
 439  
     }
 440  
 
 441  
     //-------------------------------------------------------------------------
 442  
 
 443  
     /**
 444  
      * <p>Test if two <code>Object</code>s are equal using their
 445  
      * <code>equals</code> method.</p>
 446  
      *
 447  
      * @param lhs  the left hand object
 448  
      * @param rhs  the right hand object
 449  
      * @return EqualsBuilder - used to chain calls.
 450  
      */
 451  
     public EqualsBuilder append(final Object lhs, final Object rhs) {
 452  812
         if (isEquals == false) {
 453  2
             return this;
 454  
         }
 455  810
         if (lhs == rhs) {
 456  92
             return this;
 457  
         }
 458  718
         if (lhs == null || rhs == null) {
 459  8
             this.setEquals(false);
 460  8
             return this;
 461  
         }
 462  710
         final Class<?> lhsClass = lhs.getClass();
 463  710
         if (!lhsClass.isArray()) {
 464  
             // The simple case, not an array, just test the element
 465  341
             isEquals = lhs.equals(rhs);
 466  369
         } else if (lhs.getClass() != rhs.getClass()) {
 467  
             // Here when we compare different dimensions, for example: a boolean[][] to a boolean[]
 468  20
             this.setEquals(false);
 469  
         }
 470  
         // 'Switch' on type of array, to dispatch to the correct handler
 471  
         // This handles multi dimensional arrays of the same depth
 472  349
         else if (lhs instanceof long[]) {
 473  38
             append((long[]) lhs, (long[]) rhs);
 474  311
         } else if (lhs instanceof int[]) {
 475  32
             append((int[]) lhs, (int[]) rhs);
 476  279
         } else if (lhs instanceof short[]) {
 477  28
             append((short[]) lhs, (short[]) rhs);
 478  251
         } else if (lhs instanceof char[]) {
 479  41
             append((char[]) lhs, (char[]) rhs);
 480  210
         } else if (lhs instanceof byte[]) {
 481  40
             append((byte[]) lhs, (byte[]) rhs);
 482  170
         } else if (lhs instanceof double[]) {
 483  38
             append((double[]) lhs, (double[]) rhs);
 484  132
         } else if (lhs instanceof float[]) {
 485  34
             append((float[]) lhs, (float[]) rhs);
 486  98
         } else if (lhs instanceof boolean[]) {
 487  42
             append((boolean[]) lhs, (boolean[]) rhs);
 488  
         } else {
 489  
             // Not an array of primitives
 490  56
             append((Object[]) lhs, (Object[]) rhs);
 491  
         }
 492  710
         return this;
 493  
     }
 494  
 
 495  
     /**
 496  
      * <p>
 497  
      * Test if two <code>long</code> s are equal.
 498  
      * </p>
 499  
      *
 500  
      * @param lhs
 501  
      *                  the left hand <code>long</code>
 502  
      * @param rhs
 503  
      *                  the right hand <code>long</code>
 504  
      * @return EqualsBuilder - used to chain calls.
 505  
      */
 506  
     public EqualsBuilder append(final long lhs, final long rhs) {
 507  178
         if (isEquals == false) {
 508  0
             return this;
 509  
         }
 510  178
         isEquals = (lhs == rhs);
 511  178
         return this;
 512  
     }
 513  
 
 514  
     /**
 515  
      * <p>Test if two <code>int</code>s are equal.</p>
 516  
      *
 517  
      * @param lhs  the left hand <code>int</code>
 518  
      * @param rhs  the right hand <code>int</code>
 519  
      * @return EqualsBuilder - used to chain calls.
 520  
      */
 521  
     public EqualsBuilder append(final int lhs, final int rhs) {
 522  159
         if (isEquals == false) {
 523  0
             return this;
 524  
         }
 525  159
         isEquals = (lhs == rhs);
 526  159
         return this;
 527  
     }
 528  
 
 529  
     /**
 530  
      * <p>Test if two <code>short</code>s are equal.</p>
 531  
      *
 532  
      * @param lhs  the left hand <code>short</code>
 533  
      * @param rhs  the right hand <code>short</code>
 534  
      * @return EqualsBuilder - used to chain calls.
 535  
      */
 536  
     public EqualsBuilder append(final short lhs, final short rhs) {
 537  73
         if (isEquals == false) {
 538  0
             return this;
 539  
         }
 540  73
         isEquals = (lhs == rhs);
 541  73
         return this;
 542  
     }
 543  
 
 544  
     /**
 545  
      * <p>Test if two <code>char</code>s are equal.</p>
 546  
      *
 547  
      * @param lhs  the left hand <code>char</code>
 548  
      * @param rhs  the right hand <code>char</code>
 549  
      * @return EqualsBuilder - used to chain calls.
 550  
      */
 551  
     public EqualsBuilder append(final char lhs, final char rhs) {
 552  86
         if (isEquals == false) {
 553  0
             return this;
 554  
         }
 555  86
         isEquals = (lhs == rhs);
 556  86
         return this;
 557  
     }
 558  
 
 559  
     /**
 560  
      * <p>Test if two <code>byte</code>s are equal.</p>
 561  
      *
 562  
      * @param lhs  the left hand <code>byte</code>
 563  
      * @param rhs  the right hand <code>byte</code>
 564  
      * @return EqualsBuilder - used to chain calls.
 565  
      */
 566  
     public EqualsBuilder append(final byte lhs, final byte rhs) {
 567  85
         if (isEquals == false) {
 568  0
             return this;
 569  
         }
 570  85
         isEquals = (lhs == rhs);
 571  85
         return this;
 572  
     }
 573  
 
 574  
     /**
 575  
      * <p>Test if two <code>double</code>s are equal by testing that the
 576  
      * pattern of bits returned by <code>doubleToLong</code> are equal.</p>
 577  
      *
 578  
      * <p>This handles NaNs, Infinities, and <code>-0.0</code>.</p>
 579  
      *
 580  
      * <p>It is compatible with the hash code generated by
 581  
      * <code>HashCodeBuilder</code>.</p>
 582  
      *
 583  
      * @param lhs  the left hand <code>double</code>
 584  
      * @param rhs  the right hand <code>double</code>
 585  
      * @return EqualsBuilder - used to chain calls.
 586  
      */
 587  
     public EqualsBuilder append(final double lhs, final double rhs) {
 588  87
         if (isEquals == false) {
 589  0
             return this;
 590  
         }
 591  87
         return append(Double.doubleToLongBits(lhs), Double.doubleToLongBits(rhs));
 592  
     }
 593  
 
 594  
     /**
 595  
      * <p>Test if two <code>float</code>s are equal byt testing that the
 596  
      * pattern of bits returned by doubleToLong are equal.</p>
 597  
      *
 598  
      * <p>This handles NaNs, Infinities, and <code>-0.0</code>.</p>
 599  
      *
 600  
      * <p>It is compatible with the hash code generated by
 601  
      * <code>HashCodeBuilder</code>.</p>
 602  
      *
 603  
      * @param lhs  the left hand <code>float</code>
 604  
      * @param rhs  the right hand <code>float</code>
 605  
      * @return EqualsBuilder - used to chain calls.
 606  
      */
 607  
     public EqualsBuilder append(final float lhs, final float rhs) {
 608  82
         if (isEquals == false) {
 609  0
             return this;
 610  
         }
 611  82
         return append(Float.floatToIntBits(lhs), Float.floatToIntBits(rhs));
 612  
     }
 613  
 
 614  
     /**
 615  
      * <p>Test if two <code>booleans</code>s are equal.</p>
 616  
      *
 617  
      * @param lhs  the left hand <code>boolean</code>
 618  
      * @param rhs  the right hand <code>boolean</code>
 619  
      * @return EqualsBuilder - used to chain calls.
 620  
       */
 621  
     public EqualsBuilder append(final boolean lhs, final boolean rhs) {
 622  87
         if (isEquals == false) {
 623  0
             return this;
 624  
         }
 625  87
         isEquals = (lhs == rhs);
 626  87
         return this;
 627  
     }
 628  
 
 629  
     /**
 630  
      * <p>Performs a deep comparison of two <code>Object</code> arrays.</p>
 631  
      *
 632  
      * <p>This also will be called for the top level of
 633  
      * multi-dimensional, ragged, and multi-typed arrays.</p>
 634  
      *
 635  
      * @param lhs  the left hand <code>Object[]</code>
 636  
      * @param rhs  the right hand <code>Object[]</code>
 637  
      * @return EqualsBuilder - used to chain calls.
 638  
      */
 639  
     public EqualsBuilder append(final Object[] lhs, final Object[] rhs) {
 640  100
         if (isEquals == false) {
 641  0
             return this;
 642  
         }
 643  100
         if (lhs == rhs) {
 644  15
             return this;
 645  
         }
 646  85
         if (lhs == null || rhs == null) {
 647  1
             this.setEquals(false);
 648  1
             return this;
 649  
         }
 650  84
         if (lhs.length != rhs.length) {
 651  1
             this.setEquals(false);
 652  1
             return this;
 653  
         }
 654  269
         for (int i = 0; i < lhs.length && isEquals; ++i) {
 655  186
             append(lhs[i], rhs[i]);
 656  
         }
 657  83
         return this;
 658  
     }
 659  
 
 660  
     /**
 661  
      * <p>Deep comparison of array of <code>long</code>. Length and all
 662  
      * values are compared.</p>
 663  
      *
 664  
      * <p>The method {@link #append(long, long)} is used.</p>
 665  
      *
 666  
      * @param lhs  the left hand <code>long[]</code>
 667  
      * @param rhs  the right hand <code>long[]</code>
 668  
      * @return EqualsBuilder - used to chain calls.
 669  
      */
 670  
     public EqualsBuilder append(final long[] lhs, final long[] rhs) {
 671  43
         if (isEquals == false) {
 672  0
             return this;
 673  
         }
 674  43
         if (lhs == rhs) {
 675  2
             return this;
 676  
         }
 677  41
         if (lhs == null || rhs == null) {
 678  1
             this.setEquals(false);
 679  1
             return this;
 680  
         }
 681  40
         if (lhs.length != rhs.length) {
 682  1
             this.setEquals(false);
 683  1
             return this;
 684  
         }
 685  128
         for (int i = 0; i < lhs.length && isEquals; ++i) {
 686  89
             append(lhs[i], rhs[i]);
 687  
         }
 688  39
         return this;
 689  
     }
 690  
 
 691  
     /**
 692  
      * <p>Deep comparison of array of <code>int</code>. Length and all
 693  
      * values are compared.</p>
 694  
      *
 695  
      * <p>The method {@link #append(int, int)} is used.</p>
 696  
      *
 697  
      * @param lhs  the left hand <code>int[]</code>
 698  
      * @param rhs  the right hand <code>int[]</code>
 699  
      * @return EqualsBuilder - used to chain calls.
 700  
      */
 701  
     public EqualsBuilder append(final int[] lhs, final int[] rhs) {
 702  37
         if (isEquals == false) {
 703  0
             return this;
 704  
         }
 705  37
         if (lhs == rhs) {
 706  2
             return this;
 707  
         }
 708  35
         if (lhs == null || rhs == null) {
 709  1
             this.setEquals(false);
 710  1
             return this;
 711  
         }
 712  34
         if (lhs.length != rhs.length) {
 713  1
             this.setEquals(false);
 714  1
             return this;
 715  
         }
 716  108
         for (int i = 0; i < lhs.length && isEquals; ++i) {
 717  75
             append(lhs[i], rhs[i]);
 718  
         }
 719  33
         return this;
 720  
     }
 721  
 
 722  
     /**
 723  
      * <p>Deep comparison of array of <code>short</code>. Length and all
 724  
      * values are compared.</p>
 725  
      *
 726  
      * <p>The method {@link #append(short, short)} is used.</p>
 727  
      *
 728  
      * @param lhs  the left hand <code>short[]</code>
 729  
      * @param rhs  the right hand <code>short[]</code>
 730  
      * @return EqualsBuilder - used to chain calls.
 731  
      */
 732  
     public EqualsBuilder append(final short[] lhs, final short[] rhs) {
 733  33
         if (isEquals == false) {
 734  0
             return this;
 735  
         }
 736  33
         if (lhs == rhs) {
 737  2
             return this;
 738  
         }
 739  31
         if (lhs == null || rhs == null) {
 740  1
             this.setEquals(false);
 741  1
             return this;
 742  
         }
 743  30
         if (lhs.length != rhs.length) {
 744  1
             this.setEquals(false);
 745  1
             return this;
 746  
         }
 747  100
         for (int i = 0; i < lhs.length && isEquals; ++i) {
 748  71
             append(lhs[i], rhs[i]);
 749  
         }
 750  29
         return this;
 751  
     }
 752  
 
 753  
     /**
 754  
      * <p>Deep comparison of array of <code>char</code>. Length and all
 755  
      * values are compared.</p>
 756  
      *
 757  
      * <p>The method {@link #append(char, char)} is used.</p>
 758  
      *
 759  
      * @param lhs  the left hand <code>char[]</code>
 760  
      * @param rhs  the right hand <code>char[]</code>
 761  
      * @return EqualsBuilder - used to chain calls.
 762  
      */
 763  
     public EqualsBuilder append(final char[] lhs, final char[] rhs) {
 764  46
         if (isEquals == false) {
 765  0
             return this;
 766  
         }
 767  46
         if (lhs == rhs) {
 768  2
             return this;
 769  
         }
 770  44
         if (lhs == null || rhs == null) {
 771  1
             this.setEquals(false);
 772  1
             return this;
 773  
         }
 774  43
         if (lhs.length != rhs.length) {
 775  1
             this.setEquals(false);
 776  1
             return this;
 777  
         }
 778  126
         for (int i = 0; i < lhs.length && isEquals; ++i) {
 779  84
             append(lhs[i], rhs[i]);
 780  
         }
 781  42
         return this;
 782  
     }
 783  
 
 784  
     /**
 785  
      * <p>Deep comparison of array of <code>byte</code>. Length and all
 786  
      * values are compared.</p>
 787  
      *
 788  
      * <p>The method {@link #append(byte, byte)} is used.</p>
 789  
      *
 790  
      * @param lhs  the left hand <code>byte[]</code>
 791  
      * @param rhs  the right hand <code>byte[]</code>
 792  
      * @return EqualsBuilder - used to chain calls.
 793  
      */
 794  
     public EqualsBuilder append(final byte[] lhs, final byte[] rhs) {
 795  45
         if (isEquals == false) {
 796  0
             return this;
 797  
         }
 798  45
         if (lhs == rhs) {
 799  2
             return this;
 800  
         }
 801  43
         if (lhs == null || rhs == null) {
 802  1
             this.setEquals(false);
 803  1
             return this;
 804  
         }
 805  42
         if (lhs.length != rhs.length) {
 806  1
             this.setEquals(false);
 807  1
             return this;
 808  
         }
 809  124
         for (int i = 0; i < lhs.length && isEquals; ++i) {
 810  83
             append(lhs[i], rhs[i]);
 811  
         }
 812  41
         return this;
 813  
     }
 814  
 
 815  
     /**
 816  
      * <p>Deep comparison of array of <code>double</code>. Length and all
 817  
      * values are compared.</p>
 818  
      *
 819  
      * <p>The method {@link #append(double, double)} is used.</p>
 820  
      *
 821  
      * @param lhs  the left hand <code>double[]</code>
 822  
      * @param rhs  the right hand <code>double[]</code>
 823  
      * @return EqualsBuilder - used to chain calls.
 824  
      */
 825  
     public EqualsBuilder append(final double[] lhs, final double[] rhs) {
 826  43
         if (isEquals == false) {
 827  0
             return this;
 828  
         }
 829  43
         if (lhs == rhs) {
 830  2
             return this;
 831  
         }
 832  41
         if (lhs == null || rhs == null) {
 833  1
             this.setEquals(false);
 834  1
             return this;
 835  
         }
 836  40
         if (lhs.length != rhs.length) {
 837  1
             this.setEquals(false);
 838  1
             return this;
 839  
         }
 840  121
         for (int i = 0; i < lhs.length && isEquals; ++i) {
 841  82
             append(lhs[i], rhs[i]);
 842  
         }
 843  39
         return this;
 844  
     }
 845  
 
 846  
     /**
 847  
      * <p>Deep comparison of array of <code>float</code>. Length and all
 848  
      * values are compared.</p>
 849  
      *
 850  
      * <p>The method {@link #append(float, float)} is used.</p>
 851  
      *
 852  
      * @param lhs  the left hand <code>float[]</code>
 853  
      * @param rhs  the right hand <code>float[]</code>
 854  
      * @return EqualsBuilder - used to chain calls.
 855  
      */
 856  
     public EqualsBuilder append(final float[] lhs, final float[] rhs) {
 857  39
         if (isEquals == false) {
 858  0
             return this;
 859  
         }
 860  39
         if (lhs == rhs) {
 861  2
             return this;
 862  
         }
 863  37
         if (lhs == null || rhs == null) {
 864  1
             this.setEquals(false);
 865  1
             return this;
 866  
         }
 867  36
         if (lhs.length != rhs.length) {
 868  1
             this.setEquals(false);
 869  1
             return this;
 870  
         }
 871  112
         for (int i = 0; i < lhs.length && isEquals; ++i) {
 872  77
             append(lhs[i], rhs[i]);
 873  
         }
 874  35
         return this;
 875  
     }
 876  
 
 877  
     /**
 878  
      * <p>Deep comparison of array of <code>boolean</code>. Length and all
 879  
      * values are compared.</p>
 880  
      *
 881  
      * <p>The method {@link #append(boolean, boolean)} is used.</p>
 882  
      *
 883  
      * @param lhs  the left hand <code>boolean[]</code>
 884  
      * @param rhs  the right hand <code>boolean[]</code>
 885  
      * @return EqualsBuilder - used to chain calls.
 886  
      */
 887  
     public EqualsBuilder append(final boolean[] lhs, final boolean[] rhs) {
 888  47
         if (isEquals == false) {
 889  0
             return this;
 890  
         }
 891  47
         if (lhs == rhs) {
 892  2
             return this;
 893  
         }
 894  45
         if (lhs == null || rhs == null) {
 895  1
             this.setEquals(false);
 896  1
             return this;
 897  
         }
 898  44
         if (lhs.length != rhs.length) {
 899  1
             this.setEquals(false);
 900  1
             return this;
 901  
         }
 902  128
         for (int i = 0; i < lhs.length && isEquals; ++i) {
 903  85
             append(lhs[i], rhs[i]);
 904  
         }
 905  43
         return this;
 906  
     }
 907  
 
 908  
     /**
 909  
      * <p>Returns <code>true</code> if the fields that have been checked
 910  
      * are all equal.</p>
 911  
      *
 912  
      * @return boolean
 913  
      */
 914  
     public boolean isEquals() {
 915  461
         return this.isEquals;
 916  
     }
 917  
 
 918  
     /**
 919  
      * <p>Returns <code>true</code> if the fields that have been checked
 920  
      * are all equal.</p>
 921  
      *
 922  
      * @return <code>true</code> if all of the fields that have been checked
 923  
      *         are equal, <code>false</code> otherwise.
 924  
      *
 925  
      * @since 3.0
 926  
      */
 927  
     @Override
 928  
     public Boolean build() {
 929  7
         return Boolean.valueOf(isEquals());
 930  
     }
 931  
 
 932  
     /**
 933  
      * Sets the <code>isEquals</code> value.
 934  
      *
 935  
      * @param isEquals The value to set.
 936  
      * @since 2.1
 937  
      */
 938  
     protected void setEquals(final boolean isEquals) {
 939  49
         this.isEquals = isEquals;
 940  49
     }
 941  
 
 942  
     /**
 943  
      * Reset the EqualsBuilder so you can use the same object again
 944  
      * @since 2.5
 945  
      */
 946  
     public void reset() {
 947  1
         this.isEquals = true;
 948  1
     }
 949  
 }