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.indentityHashCode()
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 if (lhs.getClass() != rhs.getClass()) {
483            // Here when we compare different dimensions, for example: a boolean[][] to a boolean[]
484            this.setEquals(false);
485        }
486        // 'Switch' on type of array, to dispatch to the correct handler
487        // This handles multi dimensional arrays of the same depth
488        else if (lhs instanceof long[]) {
489            append((long[]) lhs, (long[]) rhs);
490        } else if (lhs instanceof int[]) {
491            append((int[]) lhs, (int[]) rhs);
492        } else if (lhs instanceof short[]) {
493            append((short[]) lhs, (short[]) rhs);
494        } else if (lhs instanceof char[]) {
495            append((char[]) lhs, (char[]) rhs);
496        } else if (lhs instanceof byte[]) {
497            append((byte[]) lhs, (byte[]) rhs);
498        } else if (lhs instanceof double[]) {
499            append((double[]) lhs, (double[]) rhs);
500        } else if (lhs instanceof float[]) {
501            append((float[]) lhs, (float[]) rhs);
502        } else if (lhs instanceof boolean[]) {
503            append((boolean[]) lhs, (boolean[]) rhs);
504        } else {
505            // Not an array of primitives
506            append((Object[]) lhs, (Object[]) rhs);
507        }
508        return this;
509    }
510
511    /**
512     * <p>
513     * Test if two <code>long</code> s are equal.
514     * </p>
515     *
516     * @param lhs
517     *                  the left hand <code>long</code>
518     * @param rhs
519     *                  the right hand <code>long</code>
520     * @return EqualsBuilder - used to chain calls.
521     */
522    public EqualsBuilder append(final long lhs, final long rhs) {
523        if (!isEquals) {
524            return this;
525        }
526        isEquals = lhs == rhs;
527        return this;
528    }
529
530    /**
531     * <p>Test if two <code>int</code>s are equal.</p>
532     *
533     * @param lhs  the left hand <code>int</code>
534     * @param rhs  the right hand <code>int</code>
535     * @return EqualsBuilder - used to chain calls.
536     */
537    public EqualsBuilder append(final int lhs, final int rhs) {
538        if (!isEquals) {
539            return this;
540        }
541        isEquals = lhs == rhs;
542        return this;
543    }
544
545    /**
546     * <p>Test if two <code>short</code>s are equal.</p>
547     *
548     * @param lhs  the left hand <code>short</code>
549     * @param rhs  the right hand <code>short</code>
550     * @return EqualsBuilder - used to chain calls.
551     */
552    public EqualsBuilder append(final short lhs, final short rhs) {
553        if (!isEquals) {
554            return this;
555        }
556        isEquals = lhs == rhs;
557        return this;
558    }
559
560    /**
561     * <p>Test if two <code>char</code>s are equal.</p>
562     *
563     * @param lhs  the left hand <code>char</code>
564     * @param rhs  the right hand <code>char</code>
565     * @return EqualsBuilder - used to chain calls.
566     */
567    public EqualsBuilder append(final char lhs, final char rhs) {
568        if (!isEquals) {
569            return this;
570        }
571        isEquals = lhs == rhs;
572        return this;
573    }
574
575    /**
576     * <p>Test if two <code>byte</code>s are equal.</p>
577     *
578     * @param lhs  the left hand <code>byte</code>
579     * @param rhs  the right hand <code>byte</code>
580     * @return EqualsBuilder - used to chain calls.
581     */
582    public EqualsBuilder append(final byte lhs, final byte rhs) {
583        if (!isEquals) {
584            return this;
585        }
586        isEquals = lhs == rhs;
587        return this;
588    }
589
590    /**
591     * <p>Test if two <code>double</code>s are equal by testing that the
592     * pattern of bits returned by <code>doubleToLong</code> are equal.</p>
593     *
594     * <p>This handles NaNs, Infinities, and <code>-0.0</code>.</p>
595     *
596     * <p>It is compatible with the hash code generated by
597     * <code>HashCodeBuilder</code>.</p>
598     *
599     * @param lhs  the left hand <code>double</code>
600     * @param rhs  the right hand <code>double</code>
601     * @return EqualsBuilder - used to chain calls.
602     */
603    public EqualsBuilder append(final double lhs, final double rhs) {
604        if (!isEquals) {
605            return this;
606        }
607        return append(Double.doubleToLongBits(lhs), Double.doubleToLongBits(rhs));
608    }
609
610    /**
611     * <p>Test if two <code>float</code>s are equal byt testing that the
612     * pattern of bits returned by doubleToLong are equal.</p>
613     *
614     * <p>This handles NaNs, Infinities, and <code>-0.0</code>.</p>
615     *
616     * <p>It is compatible with the hash code generated by
617     * <code>HashCodeBuilder</code>.</p>
618     *
619     * @param lhs  the left hand <code>float</code>
620     * @param rhs  the right hand <code>float</code>
621     * @return EqualsBuilder - used to chain calls.
622     */
623    public EqualsBuilder append(final float lhs, final float rhs) {
624        if (!isEquals) {
625            return this;
626        }
627        return append(Float.floatToIntBits(lhs), Float.floatToIntBits(rhs));
628    }
629
630    /**
631     * <p>Test if two <code>booleans</code>s are equal.</p>
632     *
633     * @param lhs  the left hand <code>boolean</code>
634     * @param rhs  the right hand <code>boolean</code>
635     * @return EqualsBuilder - used to chain calls.
636      */
637    public EqualsBuilder append(final boolean lhs, final boolean rhs) {
638        if (!isEquals) {
639            return this;
640        }
641        isEquals = lhs == rhs;
642        return this;
643    }
644
645    /**
646     * <p>Performs a deep comparison of two <code>Object</code> arrays.</p>
647     *
648     * <p>This also will be called for the top level of
649     * multi-dimensional, ragged, and multi-typed arrays.</p>
650     *
651     * @param lhs  the left hand <code>Object[]</code>
652     * @param rhs  the right hand <code>Object[]</code>
653     * @return EqualsBuilder - used to chain calls.
654     */
655    public EqualsBuilder append(final Object[] lhs, final Object[] rhs) {
656        if (!isEquals) {
657            return this;
658        }
659        if (lhs == rhs) {
660            return this;
661        }
662        if (lhs == null || rhs == null) {
663            this.setEquals(false);
664            return this;
665        }
666        if (lhs.length != rhs.length) {
667            this.setEquals(false);
668            return this;
669        }
670        for (int i = 0; i < lhs.length && isEquals; ++i) {
671            append(lhs[i], rhs[i]);
672        }
673        return this;
674    }
675
676    /**
677     * <p>Deep comparison of array of <code>long</code>. Length and all
678     * values are compared.</p>
679     *
680     * <p>The method {@link #append(long, long)} is used.</p>
681     *
682     * @param lhs  the left hand <code>long[]</code>
683     * @param rhs  the right hand <code>long[]</code>
684     * @return EqualsBuilder - used to chain calls.
685     */
686    public EqualsBuilder append(final long[] lhs, final long[] rhs) {
687        if (!isEquals) {
688            return this;
689        }
690        if (lhs == rhs) {
691            return this;
692        }
693        if (lhs == null || rhs == null) {
694            this.setEquals(false);
695            return this;
696        }
697        if (lhs.length != rhs.length) {
698            this.setEquals(false);
699            return this;
700        }
701        for (int i = 0; i < lhs.length && isEquals; ++i) {
702            append(lhs[i], rhs[i]);
703        }
704        return this;
705    }
706
707    /**
708     * <p>Deep comparison of array of <code>int</code>. Length and all
709     * values are compared.</p>
710     *
711     * <p>The method {@link #append(int, int)} is used.</p>
712     *
713     * @param lhs  the left hand <code>int[]</code>
714     * @param rhs  the right hand <code>int[]</code>
715     * @return EqualsBuilder - used to chain calls.
716     */
717    public EqualsBuilder append(final int[] lhs, final int[] rhs) {
718        if (!isEquals) {
719            return this;
720        }
721        if (lhs == rhs) {
722            return this;
723        }
724        if (lhs == null || rhs == null) {
725            this.setEquals(false);
726            return this;
727        }
728        if (lhs.length != rhs.length) {
729            this.setEquals(false);
730            return this;
731        }
732        for (int i = 0; i < lhs.length && isEquals; ++i) {
733            append(lhs[i], rhs[i]);
734        }
735        return this;
736    }
737
738    /**
739     * <p>Deep comparison of array of <code>short</code>. Length and all
740     * values are compared.</p>
741     *
742     * <p>The method {@link #append(short, short)} is used.</p>
743     *
744     * @param lhs  the left hand <code>short[]</code>
745     * @param rhs  the right hand <code>short[]</code>
746     * @return EqualsBuilder - used to chain calls.
747     */
748    public EqualsBuilder append(final short[] lhs, final short[] rhs) {
749        if (!isEquals) {
750            return this;
751        }
752        if (lhs == rhs) {
753            return this;
754        }
755        if (lhs == null || rhs == null) {
756            this.setEquals(false);
757            return this;
758        }
759        if (lhs.length != rhs.length) {
760            this.setEquals(false);
761            return this;
762        }
763        for (int i = 0; i < lhs.length && isEquals; ++i) {
764            append(lhs[i], rhs[i]);
765        }
766        return this;
767    }
768
769    /**
770     * <p>Deep comparison of array of <code>char</code>. Length and all
771     * values are compared.</p>
772     *
773     * <p>The method {@link #append(char, char)} is used.</p>
774     *
775     * @param lhs  the left hand <code>char[]</code>
776     * @param rhs  the right hand <code>char[]</code>
777     * @return EqualsBuilder - used to chain calls.
778     */
779    public EqualsBuilder append(final char[] lhs, final char[] rhs) {
780        if (!isEquals) {
781            return this;
782        }
783        if (lhs == rhs) {
784            return this;
785        }
786        if (lhs == null || rhs == null) {
787            this.setEquals(false);
788            return this;
789        }
790        if (lhs.length != rhs.length) {
791            this.setEquals(false);
792            return this;
793        }
794        for (int i = 0; i < lhs.length && isEquals; ++i) {
795            append(lhs[i], rhs[i]);
796        }
797        return this;
798    }
799
800    /**
801     * <p>Deep comparison of array of <code>byte</code>. Length and all
802     * values are compared.</p>
803     *
804     * <p>The method {@link #append(byte, byte)} is used.</p>
805     *
806     * @param lhs  the left hand <code>byte[]</code>
807     * @param rhs  the right hand <code>byte[]</code>
808     * @return EqualsBuilder - used to chain calls.
809     */
810    public EqualsBuilder append(final byte[] lhs, final byte[] rhs) {
811        if (!isEquals) {
812            return this;
813        }
814        if (lhs == rhs) {
815            return this;
816        }
817        if (lhs == null || rhs == null) {
818            this.setEquals(false);
819            return this;
820        }
821        if (lhs.length != rhs.length) {
822            this.setEquals(false);
823            return this;
824        }
825        for (int i = 0; i < lhs.length && isEquals; ++i) {
826            append(lhs[i], rhs[i]);
827        }
828        return this;
829    }
830
831    /**
832     * <p>Deep comparison of array of <code>double</code>. Length and all
833     * values are compared.</p>
834     *
835     * <p>The method {@link #append(double, double)} is used.</p>
836     *
837     * @param lhs  the left hand <code>double[]</code>
838     * @param rhs  the right hand <code>double[]</code>
839     * @return EqualsBuilder - used to chain calls.
840     */
841    public EqualsBuilder append(final double[] lhs, final double[] rhs) {
842        if (!isEquals) {
843            return this;
844        }
845        if (lhs == rhs) {
846            return this;
847        }
848        if (lhs == null || rhs == null) {
849            this.setEquals(false);
850            return this;
851        }
852        if (lhs.length != rhs.length) {
853            this.setEquals(false);
854            return this;
855        }
856        for (int i = 0; i < lhs.length && isEquals; ++i) {
857            append(lhs[i], rhs[i]);
858        }
859        return this;
860    }
861
862    /**
863     * <p>Deep comparison of array of <code>float</code>. Length and all
864     * values are compared.</p>
865     *
866     * <p>The method {@link #append(float, float)} is used.</p>
867     *
868     * @param lhs  the left hand <code>float[]</code>
869     * @param rhs  the right hand <code>float[]</code>
870     * @return EqualsBuilder - used to chain calls.
871     */
872    public EqualsBuilder append(final float[] lhs, final float[] rhs) {
873        if (!isEquals) {
874            return this;
875        }
876        if (lhs == rhs) {
877            return this;
878        }
879        if (lhs == null || rhs == null) {
880            this.setEquals(false);
881            return this;
882        }
883        if (lhs.length != rhs.length) {
884            this.setEquals(false);
885            return this;
886        }
887        for (int i = 0; i < lhs.length && isEquals; ++i) {
888            append(lhs[i], rhs[i]);
889        }
890        return this;
891    }
892
893    /**
894     * <p>Deep comparison of array of <code>boolean</code>. Length and all
895     * values are compared.</p>
896     *
897     * <p>The method {@link #append(boolean, boolean)} is used.</p>
898     *
899     * @param lhs  the left hand <code>boolean[]</code>
900     * @param rhs  the right hand <code>boolean[]</code>
901     * @return EqualsBuilder - used to chain calls.
902     */
903    public EqualsBuilder append(final boolean[] lhs, final boolean[] rhs) {
904        if (!isEquals) {
905            return this;
906        }
907        if (lhs == rhs) {
908            return this;
909        }
910        if (lhs == null || rhs == null) {
911            this.setEquals(false);
912            return this;
913        }
914        if (lhs.length != rhs.length) {
915            this.setEquals(false);
916            return this;
917        }
918        for (int i = 0; i < lhs.length && isEquals; ++i) {
919            append(lhs[i], rhs[i]);
920        }
921        return this;
922    }
923
924    /**
925     * <p>Returns <code>true</code> if the fields that have been checked
926     * are all equal.</p>
927     *
928     * @return boolean
929     */
930    public boolean isEquals() {
931        return this.isEquals;
932    }
933
934    /**
935     * <p>Returns <code>true</code> if the fields that have been checked
936     * are all equal.</p>
937     *
938     * @return <code>true</code> if all of the fields that have been checked
939     *         are equal, <code>false</code> otherwise.
940     *
941     * @since 3.0
942     */
943    @Override
944    public Boolean build() {
945        return Boolean.valueOf(isEquals());
946    }
947
948    /**
949     * Sets the <code>isEquals</code> value.
950     *
951     * @param isEquals The value to set.
952     * @since 2.1
953     */
954    protected void setEquals(final boolean isEquals) {
955        this.isEquals = isEquals;
956    }
957
958    /**
959     * Reset the EqualsBuilder so you can use the same object again
960     * @since 2.5
961     */
962    public void reset() {
963        this.isEquals = true;
964    }
965}