001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.lang3.builder;
018
019import java.lang.reflect.AccessibleObject;
020import java.lang.reflect.Field;
021import java.lang.reflect.Modifier;
022import java.util.Collection;
023import java.util.HashSet;
024import java.util.Set;
025
026import org.apache.commons.lang3.ArrayUtils;
027import org.apache.commons.lang3.tuple.Pair;
028
029/**
030 * <p>Assists in implementing {@link Object#equals(Object)} methods.</p>
031 *
032 * <p> This class provides methods to build a good equals method for any
033 * class. It follows rules laid out in
034 * <a href="http://www.oracle.com/technetwork/java/effectivejava-136174.html">Effective Java</a>
035 * , by Joshua Bloch. In particular the rule for comparing <code>doubles</code>,
036 * <code>floats</code>, and arrays can be tricky. Also, making sure that
037 * <code>equals()</code> and <code>hashCode()</code> are consistent can be
038 * difficult.</p>
039 *
040 * <p>Two Objects that compare as equals must generate the same hash code,
041 * but two Objects with the same hash code do not have to be equal.</p>
042 *
043 * <p>All relevant fields should be included in the calculation of equals.
044 * Derived fields may be ignored. In particular, any field used in
045 * generating a hash code must be used in the equals method, and vice
046 * versa.</p>
047 *
048 * <p>Typical use for the code is as follows:</p>
049 * <pre>
050 * public boolean equals(Object obj) {
051 *   if (obj == null) { return false; }
052 *   if (obj == this) { return true; }
053 *   if (obj.getClass() != getClass()) {
054 *     return false;
055 *   }
056 *   MyClass rhs = (MyClass) obj;
057 *   return new EqualsBuilder()
058 *                 .appendSuper(super.equals(obj))
059 *                 .append(field1, rhs.field1)
060 *                 .append(field2, rhs.field2)
061 *                 .append(field3, rhs.field3)
062 *                 .isEquals();
063 *  }
064 * </pre>
065 *
066 * <p> Alternatively, there is a method that uses reflection to determine
067 * the fields to test. Because these fields are usually private, the method,
068 * <code>reflectionEquals</code>, uses <code>AccessibleObject.setAccessible</code> to
069 * change the visibility of the fields. This will fail under a security
070 * manager, unless the appropriate permissions are set up correctly. It is
071 * also slower than testing explicitly.  Non-primitive fields are compared using 
072 * <code>equals()</code>.</p>
073 *
074 * <p> A typical invocation for this method would look like:</p>
075 * <pre>
076 * public boolean equals(Object obj) {
077 *   return EqualsBuilder.reflectionEquals(this, obj);
078 * }
079 * </pre>
080 * 
081 * <p>The {@link EqualsExclude} annotation can be used to exclude fields from being
082 * used by the <code>reflectionEquals</code> methods.</p>
083 *
084 * @since 1.0
085 */
086public class EqualsBuilder implements Builder<Boolean> {
087
088    /**
089     * <p>
090     * A registry of objects used by reflection methods to detect cyclical object references and avoid infinite loops.
091     * </p>
092     *
093     * @since 3.0
094     */
095    private static final ThreadLocal<Set<Pair<IDKey, IDKey>>> REGISTRY = new ThreadLocal<Set<Pair<IDKey, IDKey>>>();
096
097    /*
098     * NOTE: we cannot store the actual objects in a HashSet, as that would use the very hashCode()
099     * 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    private static void register(final Object lhs, final Object rhs) {
175        Set<Pair<IDKey, IDKey>> registry = getRegistry();
176        if (registry == null) {
177            registry = new HashSet<Pair<IDKey, IDKey>>();
178            REGISTRY.set(registry);
179        }
180        final Pair<IDKey, IDKey> pair = getRegisterPair(lhs, rhs);
181        registry.add(pair);
182    }
183
184    /**
185     * <p>
186     * Unregisters the given object pair.
187     * </p>
188     *
189     * <p>
190     * Used by the reflection methods to avoid infinite loops.
191     *
192     * @param lhs <code>this</code> object to unregister
193     * @param rhs the other object to unregister
194     * @since 3.0
195     */
196    private static void unregister(final Object lhs, final Object rhs) {
197        Set<Pair<IDKey, IDKey>> registry = getRegistry();
198        if (registry != null) {
199            final Pair<IDKey, IDKey> pair = getRegisterPair(lhs, rhs);
200            registry.remove(pair);
201            if (registry.isEmpty()) {
202                REGISTRY.remove();
203            }
204        }
205    }
206
207    /**
208     * If the fields tested are equals.
209     * The default value is <code>true</code>.
210     */
211    private boolean isEquals = true;
212
213    /**
214     * <p>Constructor for EqualsBuilder.</p>
215     *
216     * <p>Starts off assuming that equals is <code>true</code>.</p>
217     * @see Object#equals(Object)
218     */
219    public EqualsBuilder() {
220        // do nothing for now.
221    }
222
223    //-------------------------------------------------------------------------
224
225    /**
226     * <p>This method uses reflection to determine if the two <code>Object</code>s
227     * are equal.</p>
228     *
229     * <p>It uses <code>AccessibleObject.setAccessible</code> to gain access to private
230     * fields. This means that it will throw a security exception if run under
231     * a security manager, if the permissions are not set up correctly. It is also
232     * not as efficient as testing explicitly. Non-primitive fields are compared using 
233     * <code>equals()</code>.</p>
234     * 
235     * <p>Transient members will be not be tested, as they are likely derived
236     * fields, and not part of the value of the Object.</p>
237     *
238     * <p>Static fields will not be tested. Superclass fields will be included.</p>
239     *
240     * @param lhs  <code>this</code> object
241     * @param rhs  the other object
242     * @param excludeFields  Collection of String field names to exclude from testing
243     * @return <code>true</code> if the two Objects have tested equals.
244     * 
245     * @see EqualsExclude
246     */
247    public static boolean reflectionEquals(final Object lhs, final Object rhs, final Collection<String> excludeFields) {
248        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. Non-primitive fields are compared using 
259     * <code>equals()</code>.</p>
260     *
261     * <p>Transient members will be not be tested, as they are likely derived
262     * fields, and not part of the value of the Object.</p>
263     *
264     * <p>Static fields will not be tested. Superclass fields will be included.</p>
265     *
266     * @param lhs  <code>this</code> object
267     * @param rhs  the other object
268     * @param excludeFields  array of field names to exclude from testing
269     * @return <code>true</code> if the two Objects have tested equals.
270     * 
271     * @see EqualsExclude
272     */
273    public static boolean reflectionEquals(final Object lhs, final Object rhs, final String... excludeFields) {
274        return reflectionEquals(lhs, rhs, false, null, excludeFields);
275    }
276
277    /**
278     * <p>This method uses reflection to determine if the two <code>Object</code>s
279     * are equal.</p>
280     *
281     * <p>It uses <code>AccessibleObject.setAccessible</code> to gain access to private
282     * fields. This means that it will throw a security exception if run under
283     * a security manager, if the permissions are not set up correctly. It is also
284     * not as efficient as testing explicitly. Non-primitive fields are compared using 
285     * <code>equals()</code>.</p>
286     *
287     * <p>If the TestTransients parameter is set to <code>true</code>, transient
288     * members will be tested, otherwise they are ignored, as they are likely
289     * derived fields, and not part of the value of the <code>Object</code>.</p>
290     *
291     * <p>Static fields will not be tested. Superclass fields will be included.</p>
292     *
293     * @param lhs  <code>this</code> object
294     * @param rhs  the other object
295     * @param testTransients  whether to include transient fields
296     * @return <code>true</code> if the two Objects have tested equals.
297     * 
298     * @see EqualsExclude
299     */
300    public static boolean reflectionEquals(final Object lhs, final Object rhs, final boolean testTransients) {
301        return reflectionEquals(lhs, rhs, testTransients, null);
302    }
303
304    /**
305     * <p>This method uses reflection to determine if the two <code>Object</code>s
306     * are equal.</p>
307     *
308     * <p>It uses <code>AccessibleObject.setAccessible</code> to gain access to private
309     * fields. This means that it will throw a security exception if run under
310     * a security manager, if the permissions are not set up correctly. It is also
311     * not as efficient as testing explicitly. Non-primitive fields are compared using 
312     * <code>equals()</code>.</p>
313     *
314     * <p>If the testTransients parameter is set to <code>true</code>, transient
315     * members will be tested, otherwise they are ignored, as they are likely
316     * derived fields, and not part of the value of the <code>Object</code>.</p>
317     *
318     * <p>Static fields will not be included. Superclass fields will be appended
319     * up to and including the specified superclass. A null superclass is treated
320     * as java.lang.Object.</p>
321     *
322     * @param lhs  <code>this</code> object
323     * @param rhs  the other object
324     * @param testTransients  whether to include transient fields
325     * @param reflectUpToClass  the superclass to reflect up to (inclusive),
326     *  may be <code>null</code>
327     * @param excludeFields  array of field names to exclude from testing
328     * @return <code>true</code> if the two Objects have tested equals.
329     * 
330     * @see EqualsExclude
331     * @since 2.0
332     */
333    public static boolean reflectionEquals(final Object lhs, final Object rhs, final boolean testTransients, final Class<?> reflectUpToClass,
334            final String... excludeFields) {
335        if (lhs == rhs) {
336            return true;
337        }
338        if (lhs == null || rhs == null) {
339            return false;
340        }
341        // Find the leaf class since there may be transients in the leaf
342        // class or in classes between the leaf and root.
343        // If we are not testing transients or a subclass has no ivars,
344        // then a subclass can test equals to a superclass.
345        final Class<?> lhsClass = lhs.getClass();
346        final Class<?> rhsClass = rhs.getClass();
347        Class<?> testClass;
348        if (lhsClass.isInstance(rhs)) {
349            testClass = lhsClass;
350            if (!rhsClass.isInstance(lhs)) {
351                // rhsClass is a subclass of lhsClass
352                testClass = rhsClass;
353            }
354        } else if (rhsClass.isInstance(lhs)) {
355            testClass = rhsClass;
356            if (!lhsClass.isInstance(rhs)) {
357                // lhsClass is a subclass of rhsClass
358                testClass = lhsClass;
359            }
360        } else {
361            // The two classes are not related.
362            return false;
363        }
364        final EqualsBuilder equalsBuilder = new EqualsBuilder();
365        try {
366            if (testClass.isArray()) {
367                equalsBuilder.append(lhs, rhs);
368            } else {
369                reflectionAppend(lhs, rhs, testClass, equalsBuilder, testTransients, excludeFields);
370                while (testClass.getSuperclass() != null && testClass != reflectUpToClass) {
371                    testClass = testClass.getSuperclass();
372                    reflectionAppend(lhs, rhs, testClass, equalsBuilder, testTransients, excludeFields);
373                }
374            }
375        } catch (final IllegalArgumentException e) {
376            // In this case, we tried to test a subclass vs. a superclass and
377            // the subclass has ivars or the ivars are transient and
378            // we are testing transients.
379            // If a subclass has ivars that we are trying to test them, we get an
380            // exception and we know that the objects are not equal.
381            return false;
382        }
383        return equalsBuilder.isEquals();
384    }
385
386    /**
387     * <p>Appends the fields and values defined by the given object of the
388     * given Class.</p>
389     *
390     * @param lhs  the left hand object
391     * @param rhs  the right hand object
392     * @param clazz  the class to append details of
393     * @param builder  the builder to append to
394     * @param useTransients  whether to test transient fields
395     * @param excludeFields  array of field names to exclude from testing
396     */
397    private static void reflectionAppend(
398        final Object lhs,
399        final Object rhs,
400        final Class<?> clazz,
401        final EqualsBuilder builder,
402        final boolean useTransients,
403        final String[] excludeFields) {
404
405        if (isRegistered(lhs, rhs)) {
406            return;
407        }
408
409        try {
410            register(lhs, rhs);
411            final Field[] fields = clazz.getDeclaredFields();
412            AccessibleObject.setAccessible(fields, true);
413            for (int i = 0; i < fields.length && builder.isEquals; i++) {
414                final Field f = fields[i];
415                if (!ArrayUtils.contains(excludeFields, f.getName())
416                    && !f.getName().contains("$")
417                    && (useTransients || !Modifier.isTransient(f.getModifiers()))
418                    && !Modifier.isStatic(f.getModifiers())
419                    && !f.isAnnotationPresent(EqualsExclude.class)) {
420                    try {
421                        builder.append(f.get(lhs), f.get(rhs));
422                    } catch (final IllegalAccessException e) {
423                        //this can't happen. Would get a Security exception instead
424                        //throw a runtime exception in case the impossible happens.
425                        throw new InternalError("Unexpected IllegalAccessException");
426                    }
427                }
428            }
429        } finally {
430            unregister(lhs, rhs);
431        }
432    }
433
434    //-------------------------------------------------------------------------
435
436    /**
437     * <p>Adds the result of <code>super.equals()</code> to this builder.</p>
438     *
439     * @param superEquals  the result of calling <code>super.equals()</code>
440     * @return EqualsBuilder - used to chain calls.
441     * @since 2.0
442     */
443    public EqualsBuilder appendSuper(final boolean superEquals) {
444        if (!isEquals) {
445            return this;
446        }
447        isEquals = superEquals;
448        return this;
449    }
450
451    //-------------------------------------------------------------------------
452
453    /**
454     * <p>Test if two <code>Object</code>s are equal using their
455     * <code>equals</code> method.</p>
456     *
457     * @param lhs  the left hand object
458     * @param rhs  the right hand object
459     * @return EqualsBuilder - used to chain calls.
460     */
461    public EqualsBuilder append(final Object lhs, final Object rhs) {
462        if (!isEquals) {
463            return this;
464        }
465        if (lhs == rhs) {
466            return this;
467        }
468        if (lhs == null || rhs == null) {
469            this.setEquals(false);
470            return this;
471        }
472        final Class<?> lhsClass = lhs.getClass();
473        if (!lhsClass.isArray()) {
474            // The simple case, not an array, just test the element
475            isEquals = lhs.equals(rhs);
476        } else {
477            // factor out array case in order to keep method small enough
478            // to be inlined
479            appendArray(lhs, rhs);
480        }
481        return this;
482    }
483
484    /**
485     * <p>Test if an <code>Object</code> is equal to an array.</p>
486     *
487     * @param lhs  the left hand object, an array
488     * @param rhs  the right hand object
489     */
490    private void appendArray(final Object lhs, final Object rhs) {
491        if (lhs.getClass() != rhs.getClass()) {
492            // Here when we compare different dimensions, for example: a boolean[][] to a boolean[]
493            this.setEquals(false);
494        }
495        // 'Switch' on type of array, to dispatch to the correct handler
496        // This handles multi dimensional arrays of the same depth
497        else if (lhs instanceof long[]) {
498            append((long[]) lhs, (long[]) rhs);
499        } else if (lhs instanceof int[]) {
500            append((int[]) lhs, (int[]) rhs);
501        } else if (lhs instanceof short[]) {
502            append((short[]) lhs, (short[]) rhs);
503        } else if (lhs instanceof char[]) {
504            append((char[]) lhs, (char[]) rhs);
505        } else if (lhs instanceof byte[]) {
506            append((byte[]) lhs, (byte[]) rhs);
507        } else if (lhs instanceof double[]) {
508            append((double[]) lhs, (double[]) rhs);
509        } else if (lhs instanceof float[]) {
510            append((float[]) lhs, (float[]) rhs);
511        } else if (lhs instanceof boolean[]) {
512            append((boolean[]) lhs, (boolean[]) rhs);
513        } else {
514            // Not an array of primitives
515            append((Object[]) lhs, (Object[]) rhs);
516        }
517    }
518
519    /**
520     * <p>
521     * Test if two <code>long</code> s are equal.
522     * </p>
523     *
524     * @param lhs
525     *                  the left hand <code>long</code>
526     * @param rhs
527     *                  the right hand <code>long</code>
528     * @return EqualsBuilder - used to chain calls.
529     */
530    public EqualsBuilder append(final long lhs, final long rhs) {
531        if (!isEquals) {
532            return this;
533        }
534        isEquals = lhs == rhs;
535        return this;
536    }
537
538    /**
539     * <p>Test if two <code>int</code>s are equal.</p>
540     *
541     * @param lhs  the left hand <code>int</code>
542     * @param rhs  the right hand <code>int</code>
543     * @return EqualsBuilder - used to chain calls.
544     */
545    public EqualsBuilder append(final int lhs, final int rhs) {
546        if (!isEquals) {
547            return this;
548        }
549        isEquals = lhs == rhs;
550        return this;
551    }
552
553    /**
554     * <p>Test if two <code>short</code>s are equal.</p>
555     *
556     * @param lhs  the left hand <code>short</code>
557     * @param rhs  the right hand <code>short</code>
558     * @return EqualsBuilder - used to chain calls.
559     */
560    public EqualsBuilder append(final short lhs, final short rhs) {
561        if (!isEquals) {
562            return this;
563        }
564        isEquals = lhs == rhs;
565        return this;
566    }
567
568    /**
569     * <p>Test if two <code>char</code>s are equal.</p>
570     *
571     * @param lhs  the left hand <code>char</code>
572     * @param rhs  the right hand <code>char</code>
573     * @return EqualsBuilder - used to chain calls.
574     */
575    public EqualsBuilder append(final char lhs, final char rhs) {
576        if (!isEquals) {
577            return this;
578        }
579        isEquals = lhs == rhs;
580        return this;
581    }
582
583    /**
584     * <p>Test if two <code>byte</code>s are equal.</p>
585     *
586     * @param lhs  the left hand <code>byte</code>
587     * @param rhs  the right hand <code>byte</code>
588     * @return EqualsBuilder - used to chain calls.
589     */
590    public EqualsBuilder append(final byte lhs, final byte rhs) {
591        if (!isEquals) {
592            return this;
593        }
594        isEquals = lhs == rhs;
595        return this;
596    }
597
598    /**
599     * <p>Test if two <code>double</code>s are equal by testing that the
600     * pattern of bits returned by <code>doubleToLong</code> are equal.</p>
601     *
602     * <p>This handles NaNs, Infinities, and <code>-0.0</code>.</p>
603     *
604     * <p>It is compatible with the hash code generated by
605     * <code>HashCodeBuilder</code>.</p>
606     *
607     * @param lhs  the left hand <code>double</code>
608     * @param rhs  the right hand <code>double</code>
609     * @return EqualsBuilder - used to chain calls.
610     */
611    public EqualsBuilder append(final double lhs, final double rhs) {
612        if (!isEquals) {
613            return this;
614        }
615        return append(Double.doubleToLongBits(lhs), Double.doubleToLongBits(rhs));
616    }
617
618    /**
619     * <p>Test if two <code>float</code>s are equal byt testing that the
620     * pattern of bits returned by doubleToLong are equal.</p>
621     *
622     * <p>This handles NaNs, Infinities, and <code>-0.0</code>.</p>
623     *
624     * <p>It is compatible with the hash code generated by
625     * <code>HashCodeBuilder</code>.</p>
626     *
627     * @param lhs  the left hand <code>float</code>
628     * @param rhs  the right hand <code>float</code>
629     * @return EqualsBuilder - used to chain calls.
630     */
631    public EqualsBuilder append(final float lhs, final float rhs) {
632        if (!isEquals) {
633            return this;
634        }
635        return append(Float.floatToIntBits(lhs), Float.floatToIntBits(rhs));
636    }
637
638    /**
639     * <p>Test if two <code>booleans</code>s are equal.</p>
640     *
641     * @param lhs  the left hand <code>boolean</code>
642     * @param rhs  the right hand <code>boolean</code>
643     * @return EqualsBuilder - used to chain calls.
644      */
645    public EqualsBuilder append(final boolean lhs, final boolean rhs) {
646        if (!isEquals) {
647            return this;
648        }
649        isEquals = lhs == rhs;
650        return this;
651    }
652
653    /**
654     * <p>Performs a deep comparison of two <code>Object</code> arrays.</p>
655     *
656     * <p>This also will be called for the top level of
657     * multi-dimensional, ragged, and multi-typed arrays.</p>
658     *
659     * @param lhs  the left hand <code>Object[]</code>
660     * @param rhs  the right hand <code>Object[]</code>
661     * @return EqualsBuilder - used to chain calls.
662     */
663    public EqualsBuilder append(final Object[] lhs, final Object[] rhs) {
664        if (!isEquals) {
665            return this;
666        }
667        if (lhs == rhs) {
668            return this;
669        }
670        if (lhs == null || rhs == null) {
671            this.setEquals(false);
672            return this;
673        }
674        if (lhs.length != rhs.length) {
675            this.setEquals(false);
676            return this;
677        }
678        for (int i = 0; i < lhs.length && isEquals; ++i) {
679            append(lhs[i], rhs[i]);
680        }
681        return this;
682    }
683
684    /**
685     * <p>Deep comparison of array of <code>long</code>. Length and all
686     * values are compared.</p>
687     *
688     * <p>The method {@link #append(long, long)} is used.</p>
689     *
690     * @param lhs  the left hand <code>long[]</code>
691     * @param rhs  the right hand <code>long[]</code>
692     * @return EqualsBuilder - used to chain calls.
693     */
694    public EqualsBuilder append(final long[] lhs, final long[] rhs) {
695        if (!isEquals) {
696            return this;
697        }
698        if (lhs == rhs) {
699            return this;
700        }
701        if (lhs == null || rhs == null) {
702            this.setEquals(false);
703            return this;
704        }
705        if (lhs.length != rhs.length) {
706            this.setEquals(false);
707            return this;
708        }
709        for (int i = 0; i < lhs.length && isEquals; ++i) {
710            append(lhs[i], rhs[i]);
711        }
712        return this;
713    }
714
715    /**
716     * <p>Deep comparison of array of <code>int</code>. Length and all
717     * values are compared.</p>
718     *
719     * <p>The method {@link #append(int, int)} is used.</p>
720     *
721     * @param lhs  the left hand <code>int[]</code>
722     * @param rhs  the right hand <code>int[]</code>
723     * @return EqualsBuilder - used to chain calls.
724     */
725    public EqualsBuilder append(final int[] lhs, final int[] rhs) {
726        if (!isEquals) {
727            return this;
728        }
729        if (lhs == rhs) {
730            return this;
731        }
732        if (lhs == null || rhs == null) {
733            this.setEquals(false);
734            return this;
735        }
736        if (lhs.length != rhs.length) {
737            this.setEquals(false);
738            return this;
739        }
740        for (int i = 0; i < lhs.length && isEquals; ++i) {
741            append(lhs[i], rhs[i]);
742        }
743        return this;
744    }
745
746    /**
747     * <p>Deep comparison of array of <code>short</code>. Length and all
748     * values are compared.</p>
749     *
750     * <p>The method {@link #append(short, short)} is used.</p>
751     *
752     * @param lhs  the left hand <code>short[]</code>
753     * @param rhs  the right hand <code>short[]</code>
754     * @return EqualsBuilder - used to chain calls.
755     */
756    public EqualsBuilder append(final short[] lhs, final short[] rhs) {
757        if (!isEquals) {
758            return this;
759        }
760        if (lhs == rhs) {
761            return this;
762        }
763        if (lhs == null || rhs == null) {
764            this.setEquals(false);
765            return this;
766        }
767        if (lhs.length != rhs.length) {
768            this.setEquals(false);
769            return this;
770        }
771        for (int i = 0; i < lhs.length && isEquals; ++i) {
772            append(lhs[i], rhs[i]);
773        }
774        return this;
775    }
776
777    /**
778     * <p>Deep comparison of array of <code>char</code>. Length and all
779     * values are compared.</p>
780     *
781     * <p>The method {@link #append(char, char)} is used.</p>
782     *
783     * @param lhs  the left hand <code>char[]</code>
784     * @param rhs  the right hand <code>char[]</code>
785     * @return EqualsBuilder - used to chain calls.
786     */
787    public EqualsBuilder append(final char[] lhs, final char[] rhs) {
788        if (!isEquals) {
789            return this;
790        }
791        if (lhs == rhs) {
792            return this;
793        }
794        if (lhs == null || rhs == null) {
795            this.setEquals(false);
796            return this;
797        }
798        if (lhs.length != rhs.length) {
799            this.setEquals(false);
800            return this;
801        }
802        for (int i = 0; i < lhs.length && isEquals; ++i) {
803            append(lhs[i], rhs[i]);
804        }
805        return this;
806    }
807
808    /**
809     * <p>Deep comparison of array of <code>byte</code>. Length and all
810     * values are compared.</p>
811     *
812     * <p>The method {@link #append(byte, byte)} is used.</p>
813     *
814     * @param lhs  the left hand <code>byte[]</code>
815     * @param rhs  the right hand <code>byte[]</code>
816     * @return EqualsBuilder - used to chain calls.
817     */
818    public EqualsBuilder append(final byte[] lhs, final byte[] rhs) {
819        if (!isEquals) {
820            return this;
821        }
822        if (lhs == rhs) {
823            return this;
824        }
825        if (lhs == null || rhs == null) {
826            this.setEquals(false);
827            return this;
828        }
829        if (lhs.length != rhs.length) {
830            this.setEquals(false);
831            return this;
832        }
833        for (int i = 0; i < lhs.length && isEquals; ++i) {
834            append(lhs[i], rhs[i]);
835        }
836        return this;
837    }
838
839    /**
840     * <p>Deep comparison of array of <code>double</code>. Length and all
841     * values are compared.</p>
842     *
843     * <p>The method {@link #append(double, double)} is used.</p>
844     *
845     * @param lhs  the left hand <code>double[]</code>
846     * @param rhs  the right hand <code>double[]</code>
847     * @return EqualsBuilder - used to chain calls.
848     */
849    public EqualsBuilder append(final double[] lhs, final double[] rhs) {
850        if (!isEquals) {
851            return this;
852        }
853        if (lhs == rhs) {
854            return this;
855        }
856        if (lhs == null || rhs == null) {
857            this.setEquals(false);
858            return this;
859        }
860        if (lhs.length != rhs.length) {
861            this.setEquals(false);
862            return this;
863        }
864        for (int i = 0; i < lhs.length && isEquals; ++i) {
865            append(lhs[i], rhs[i]);
866        }
867        return this;
868    }
869
870    /**
871     * <p>Deep comparison of array of <code>float</code>. Length and all
872     * values are compared.</p>
873     *
874     * <p>The method {@link #append(float, float)} is used.</p>
875     *
876     * @param lhs  the left hand <code>float[]</code>
877     * @param rhs  the right hand <code>float[]</code>
878     * @return EqualsBuilder - used to chain calls.
879     */
880    public EqualsBuilder append(final float[] lhs, final float[] rhs) {
881        if (!isEquals) {
882            return this;
883        }
884        if (lhs == rhs) {
885            return this;
886        }
887        if (lhs == null || rhs == null) {
888            this.setEquals(false);
889            return this;
890        }
891        if (lhs.length != rhs.length) {
892            this.setEquals(false);
893            return this;
894        }
895        for (int i = 0; i < lhs.length && isEquals; ++i) {
896            append(lhs[i], rhs[i]);
897        }
898        return this;
899    }
900
901    /**
902     * <p>Deep comparison of array of <code>boolean</code>. Length and all
903     * values are compared.</p>
904     *
905     * <p>The method {@link #append(boolean, boolean)} is used.</p>
906     *
907     * @param lhs  the left hand <code>boolean[]</code>
908     * @param rhs  the right hand <code>boolean[]</code>
909     * @return EqualsBuilder - used to chain calls.
910     */
911    public EqualsBuilder append(final boolean[] lhs, final boolean[] rhs) {
912        if (!isEquals) {
913            return this;
914        }
915        if (lhs == rhs) {
916            return this;
917        }
918        if (lhs == null || rhs == null) {
919            this.setEquals(false);
920            return this;
921        }
922        if (lhs.length != rhs.length) {
923            this.setEquals(false);
924            return this;
925        }
926        for (int i = 0; i < lhs.length && isEquals; ++i) {
927            append(lhs[i], rhs[i]);
928        }
929        return this;
930    }
931
932    /**
933     * <p>Returns <code>true</code> if the fields that have been checked
934     * are all equal.</p>
935     *
936     * @return boolean
937     */
938    public boolean isEquals() {
939        return this.isEquals;
940    }
941
942    /**
943     * <p>Returns <code>true</code> if the fields that have been checked
944     * are all equal.</p>
945     *
946     * @return <code>true</code> if all of the fields that have been checked
947     *         are equal, <code>false</code> otherwise.
948     *
949     * @since 3.0
950     */
951    @Override
952    public Boolean build() {
953        return Boolean.valueOf(isEquals());
954    }
955
956    /**
957     * Sets the <code>isEquals</code> value.
958     *
959     * @param isEquals The value to set.
960     * @since 2.1
961     */
962    protected void setEquals(final boolean isEquals) {
963        this.isEquals = isEquals;
964    }
965
966    /**
967     * Reset the EqualsBuilder so you can use the same object again
968     * @since 2.5
969     */
970    public void reset() {
971        this.isEquals = true;
972    }
973}