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