Source Code Cross Referenced for EqualsBuilder.java in  » Library » Apache-common-lang » org » apache » commons » lang » builder » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache common lang » org.apache.commons.lang.builder 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:         */
017:        package org.apache.commons.lang.builder;
018:
019:        import java.lang.reflect.AccessibleObject;
020:        import java.lang.reflect.Field;
021:        import java.lang.reflect.Modifier;
022:        import java.util.Arrays;
023:        import java.util.Collections;
024:        import java.util.Collection;
025:        import java.util.List;
026:
027:        /**
028:         * <p>Assists in implementing {@link Object#equals(Object)} methods.</p>
029:         *
030:         * <p> This class provides methods to build a good equals method for any
031:         * class. It follows rules laid out in
032:         * <a href="http://java.sun.com/docs/books/effective/index.html">Effective Java</a>
033:         * , by Joshua Bloch. In particular the rule for comparing <code>doubles</code>,
034:         * <code>floats</code>, and arrays can be tricky. Also, making sure that
035:         * <code>equals()</code> and <code>hashCode()</code> are consistent can be
036:         * difficult.</p>
037:         *
038:         * <p>Two Objects that compare as equals must generate the same hash code,
039:         * but two Objects with the same hash code do not have to be equal.</p>
040:         *
041:         * <p>All relevant fields should be included in the calculation of equals.
042:         * Derived fields may be ignored. In particular, any field used in
043:         * generating a hash code must be used in the equals method, and vice
044:         * versa.</p>
045:         *
046:         * <p>Typical use for the code is as follows:</p>
047:         * <pre>
048:         * public boolean equals(Object obj) {
049:         *   if (obj instanceof MyClass == false) {
050:         *     return false;
051:         *   }
052:         *   if (this == obj) {
053:         *     return true;
054:         *   }
055:         *   MyClass rhs = (MyClass) obj;
056:         *   return new EqualsBuilder()
057:         *                 .appendSuper(super.equals(obj))
058:         *                 .append(field1, rhs.field1)
059:         *                 .append(field2, rhs.field2)
060:         *                 .append(field3, rhs.field3)
061:         *                 .isEquals();
062:         *  }
063:         * </pre>
064:         *
065:         * <p> Alternatively, there is a method that uses reflection to determine
066:         * the fields to test. Because these fields are usually private, the method,
067:         * <code>reflectionEquals</code>, uses <code>AccessibleObject.setAccessible</code> to
068:         * change the visibility of the fields. This will fail under a security
069:         * manager, unless the appropriate permissions are set up correctly. It is
070:         * also slower than testing explicitly.</p>
071:         *
072:         * <p> A typical invocation for this method would look like:</p>
073:         * <pre>
074:         * public boolean equals(Object obj) {
075:         *   return EqualsBuilder.reflectionEquals(this, obj);
076:         * }
077:         * </pre>
078:         *
079:         * @author <a href="mailto:steve.downey@netfolio.com">Steve Downey</a>
080:         * @author Stephen Colebourne
081:         * @author Gary Gregory
082:         * @author Pete Gieser
083:         * @author Arun Mammen Thomas
084:         * @since 1.0
085:         * @version $Id: EqualsBuilder.java 437554 2006-08-28 06:21:41Z bayard $
086:         */
087:        public class EqualsBuilder {
088:
089:            /**
090:             * If the fields tested are equals.
091:             * The default value is <code>true</code>.
092:             */
093:            private boolean isEquals = true;
094:
095:            /**
096:             * <p>Constructor for EqualsBuilder.</p>
097:             *
098:             * <p>Starts off assuming that equals is <code>true</code>.</p>
099:             * @see Object#equals(Object)
100:             */
101:            public EqualsBuilder() {
102:                // do nothing for now.
103:            }
104:
105:            //-------------------------------------------------------------------------
106:
107:            /**
108:             * <p>This method uses reflection to determine if the two <code>Object</code>s
109:             * are equal.</p>
110:             *
111:             * <p>It uses <code>AccessibleObject.setAccessible</code> to gain access to private
112:             * fields. This means that it will throw a security exception if run under
113:             * a security manager, if the permissions are not set up correctly. It is also
114:             * not as efficient as testing explicitly.</p>
115:             *
116:             * <p>Transient members will be not be tested, as they are likely derived
117:             * fields, and not part of the value of the Object.</p>
118:             *
119:             * <p>Static fields will not be tested. Superclass fields will be included.</p>
120:             *
121:             * @param lhs  <code>this</code> object
122:             * @param rhs  the other object
123:             * @return <code>true</code> if the two Objects have tested equals.
124:             */
125:            public static boolean reflectionEquals(Object lhs, Object rhs) {
126:                return reflectionEquals(lhs, rhs, false, null, null);
127:            }
128:
129:            /**
130:             * <p>This method uses reflection to determine if the two <code>Object</code>s
131:             * are equal.</p>
132:             *
133:             * <p>It uses <code>AccessibleObject.setAccessible</code> to gain access to private
134:             * fields. This means that it will throw a security exception if run under
135:             * a security manager, if the permissions are not set up correctly. It is also
136:             * not as efficient as testing explicitly.</p>
137:             *
138:             * <p>Transient members will be not be tested, as they are likely derived
139:             * fields, and not part of the value of the Object.</p>
140:             *
141:             * <p>Static fields will not be tested. Superclass fields will be included.</p>
142:             *
143:             * @param lhs  <code>this</code> object
144:             * @param rhs  the other object
145:             * @param excludeFields  Collection of String field names to exclude from testing
146:             * @return <code>true</code> if the two Objects have tested equals.
147:             */
148:            public static boolean reflectionEquals(Object lhs, Object rhs,
149:                    Collection /*String*/excludeFields) {
150:                return reflectionEquals(lhs, rhs, ReflectionToStringBuilder
151:                        .toNoNullStringArray(excludeFields));
152:            }
153:
154:            /**
155:             * <p>This method uses reflection to determine if the two <code>Object</code>s
156:             * are equal.</p>
157:             *
158:             * <p>It uses <code>AccessibleObject.setAccessible</code> to gain access to private
159:             * fields. This means that it will throw a security exception if run under
160:             * a security manager, if the permissions are not set up correctly. It is also
161:             * not as efficient as testing explicitly.</p>
162:             *
163:             * <p>Transient members will be not be tested, as they are likely derived
164:             * fields, and not part of the value of the Object.</p>
165:             *
166:             * <p>Static fields will not be tested. Superclass fields will be included.</p>
167:             *
168:             * @param lhs  <code>this</code> object
169:             * @param rhs  the other object
170:             * @param excludeFields  array of field names to exclude from testing
171:             * @return <code>true</code> if the two Objects have tested equals.
172:             */
173:            public static boolean reflectionEquals(Object lhs, Object rhs,
174:                    String[] excludeFields) {
175:                return reflectionEquals(lhs, rhs, false, null, excludeFields);
176:            }
177:
178:            /**
179:             * <p>This method uses reflection to determine if the two <code>Object</code>s
180:             * are equal.</p>
181:             *
182:             * <p>It uses <code>AccessibleObject.setAccessible</code> to gain access to private
183:             * fields. This means that it will throw a security exception if run under
184:             * a security manager, if the permissions are not set up correctly. It is also
185:             * not as efficient as testing explicitly.</p>
186:             *
187:             * <p>If the TestTransients parameter is set to <code>true</code>, transient
188:             * members will be tested, otherwise they are ignored, as they are likely
189:             * derived fields, and not part of the value of the <code>Object</code>.</p>
190:             *
191:             * <p>Static fields will not be tested. Superclass fields will be included.</p>
192:             *
193:             * @param lhs  <code>this</code> object
194:             * @param rhs  the other object
195:             * @param testTransients  whether to include transient fields
196:             * @return <code>true</code> if the two Objects have tested equals.
197:             */
198:            public static boolean reflectionEquals(Object lhs, Object rhs,
199:                    boolean testTransients) {
200:                return reflectionEquals(lhs, rhs, testTransients, null, null);
201:            }
202:
203:            /**
204:             * <p>This method uses reflection to determine if the two <code>Object</code>s
205:             * are equal.</p>
206:             *
207:             * <p>It uses <code>AccessibleObject.setAccessible</code> to gain access to private
208:             * fields. This means that it will throw a security exception if run under
209:             * a security manager, if the permissions are not set up correctly. It is also
210:             * not as efficient as testing explicitly.</p>
211:             *
212:             * <p>If the testTransients parameter is set to <code>true</code>, transient
213:             * members will be tested, otherwise they are ignored, as they are likely
214:             * derived fields, and not part of the value of the <code>Object</code>.</p>
215:             *
216:             * <p>Static fields will not be included. Superclass fields will be appended
217:             * up to and including the specified superclass. A null superclass is treated
218:             * as java.lang.Object.</p>
219:             *
220:             * @param lhs  <code>this</code> object
221:             * @param rhs  the other object
222:             * @param testTransients  whether to include transient fields
223:             * @param reflectUpToClass  the superclass to reflect up to (inclusive),
224:             *  may be <code>null</code>
225:             * @return <code>true</code> if the two Objects have tested equals.
226:             * @since 2.0
227:             */
228:            public static boolean reflectionEquals(Object lhs, Object rhs,
229:                    boolean testTransients, Class reflectUpToClass) {
230:                return reflectionEquals(lhs, rhs, testTransients,
231:                        reflectUpToClass, null);
232:            }
233:
234:            /**
235:             * <p>This method uses reflection to determine if the two <code>Object</code>s
236:             * are equal.</p>
237:             *
238:             * <p>It uses <code>AccessibleObject.setAccessible</code> to gain access to private
239:             * fields. This means that it will throw a security exception if run under
240:             * a security manager, if the permissions are not set up correctly. It is also
241:             * not as efficient as testing explicitly.</p>
242:             *
243:             * <p>If the testTransients parameter is set to <code>true</code>, transient
244:             * members will be tested, otherwise they are ignored, as they are likely
245:             * derived fields, and not part of the value of the <code>Object</code>.</p>
246:             *
247:             * <p>Static fields will not be included. Superclass fields will be appended
248:             * up to and including the specified superclass. A null superclass is treated
249:             * as java.lang.Object.</p>
250:             *
251:             * @param lhs  <code>this</code> object
252:             * @param rhs  the other object
253:             * @param testTransients  whether to include transient fields
254:             * @param reflectUpToClass  the superclass to reflect up to (inclusive),
255:             *  may be <code>null</code>
256:             * @param excludeFields  array of field names to exclude from testing
257:             * @return <code>true</code> if the two Objects have tested equals.
258:             * @since 2.0
259:             */
260:            public static boolean reflectionEquals(Object lhs, Object rhs,
261:                    boolean testTransients, Class reflectUpToClass,
262:                    String[] excludeFields) {
263:                if (lhs == rhs) {
264:                    return true;
265:                }
266:                if (lhs == null || rhs == null) {
267:                    return false;
268:                }
269:                // Find the leaf class since there may be transients in the leaf 
270:                // class or in classes between the leaf and root.
271:                // If we are not testing transients or a subclass has no ivars, 
272:                // then a subclass can test equals to a superclass.
273:                Class lhsClass = lhs.getClass();
274:                Class rhsClass = rhs.getClass();
275:                Class testClass;
276:                if (lhsClass.isInstance(rhs)) {
277:                    testClass = lhsClass;
278:                    if (!rhsClass.isInstance(lhs)) {
279:                        // rhsClass is a subclass of lhsClass
280:                        testClass = rhsClass;
281:                    }
282:                } else if (rhsClass.isInstance(lhs)) {
283:                    testClass = rhsClass;
284:                    if (!lhsClass.isInstance(rhs)) {
285:                        // lhsClass is a subclass of rhsClass
286:                        testClass = lhsClass;
287:                    }
288:                } else {
289:                    // The two classes are not related.
290:                    return false;
291:                }
292:                EqualsBuilder equalsBuilder = new EqualsBuilder();
293:                try {
294:                    reflectionAppend(lhs, rhs, testClass, equalsBuilder,
295:                            testTransients, excludeFields);
296:                    while (testClass.getSuperclass() != null
297:                            && testClass != reflectUpToClass) {
298:                        testClass = testClass.getSuperclass();
299:                        reflectionAppend(lhs, rhs, testClass, equalsBuilder,
300:                                testTransients, excludeFields);
301:                    }
302:                } catch (IllegalArgumentException e) {
303:                    // In this case, we tried to test a subclass vs. a superclass and
304:                    // the subclass has ivars or the ivars are transient and 
305:                    // we are testing transients.
306:                    // If a subclass has ivars that we are trying to test them, we get an
307:                    // exception and we know that the objects are not equal.
308:                    return false;
309:                }
310:                return equalsBuilder.isEquals();
311:            }
312:
313:            /**
314:             * <p>Appends the fields and values defined by the given object of the
315:             * given Class.</p>
316:             * 
317:             * @param lhs  the left hand object
318:             * @param rhs  the right hand object
319:             * @param clazz  the class to append details of
320:             * @param builder  the builder to append to
321:             * @param useTransients  whether to test transient fields
322:             * @param excludeFields  array of field names to exclude from testing
323:             */
324:            private static void reflectionAppend(Object lhs, Object rhs,
325:                    Class clazz, EqualsBuilder builder, boolean useTransients,
326:                    String[] excludeFields) {
327:                Field[] fields = clazz.getDeclaredFields();
328:                List excludedFieldList = excludeFields != null ? Arrays
329:                        .asList(excludeFields) : Collections.EMPTY_LIST;
330:                AccessibleObject.setAccessible(fields, true);
331:                for (int i = 0; i < fields.length && builder.isEquals; i++) {
332:                    Field f = fields[i];
333:                    if (!excludedFieldList.contains(f.getName())
334:                            && (f.getName().indexOf('$') == -1)
335:                            && (useTransients || !Modifier.isTransient(f
336:                                    .getModifiers()))
337:                            && (!Modifier.isStatic(f.getModifiers()))) {
338:                        try {
339:                            builder.append(f.get(lhs), f.get(rhs));
340:                        } catch (IllegalAccessException e) {
341:                            //this can't happen. Would get a Security exception instead
342:                            //throw a runtime exception in case the impossible happens.
343:                            throw new InternalError(
344:                                    "Unexpected IllegalAccessException");
345:                        }
346:                    }
347:                }
348:            }
349:
350:            //-------------------------------------------------------------------------
351:
352:            /**
353:             * <p>Adds the result of <code>super.equals()</code> to this builder.</p>
354:             *
355:             * @param superEquals  the result of calling <code>super.equals()</code>
356:             * @return EqualsBuilder - used to chain calls.
357:             * @since 2.0
358:             */
359:            public EqualsBuilder appendSuper(boolean super Equals) {
360:                if (isEquals == false) {
361:                    return this ;
362:                }
363:                isEquals = super Equals;
364:                return this ;
365:            }
366:
367:            //-------------------------------------------------------------------------
368:
369:            /**
370:             * <p>Test if two <code>Object</code>s are equal using their
371:             * <code>equals</code> method.</p>
372:             *
373:             * @param lhs  the left hand object
374:             * @param rhs  the right hand object
375:             * @return EqualsBuilder - used to chain calls.
376:             */
377:            public EqualsBuilder append(Object lhs, Object rhs) {
378:                if (isEquals == false) {
379:                    return this ;
380:                }
381:                if (lhs == rhs) {
382:                    return this ;
383:                }
384:                if (lhs == null || rhs == null) {
385:                    this .setEquals(false);
386:                    return this ;
387:                }
388:                Class lhsClass = lhs.getClass();
389:                if (!lhsClass.isArray()) {
390:                    // The simple case, not an array, just test the element
391:                    isEquals = lhs.equals(rhs);
392:                } else if (lhs.getClass() != rhs.getClass()) {
393:                    // Here when we compare different dimensions, for example: a boolean[][] to a boolean[] 
394:                    this .setEquals(false);
395:                }
396:                // 'Switch' on type of array, to dispatch to the correct handler
397:                // This handles multi dimensional arrays of the same depth
398:                else if (lhs instanceof  long[]) {
399:                    append((long[]) lhs, (long[]) rhs);
400:                } else if (lhs instanceof  int[]) {
401:                    append((int[]) lhs, (int[]) rhs);
402:                } else if (lhs instanceof  short[]) {
403:                    append((short[]) lhs, (short[]) rhs);
404:                } else if (lhs instanceof  char[]) {
405:                    append((char[]) lhs, (char[]) rhs);
406:                } else if (lhs instanceof  byte[]) {
407:                    append((byte[]) lhs, (byte[]) rhs);
408:                } else if (lhs instanceof  double[]) {
409:                    append((double[]) lhs, (double[]) rhs);
410:                } else if (lhs instanceof  float[]) {
411:                    append((float[]) lhs, (float[]) rhs);
412:                } else if (lhs instanceof  boolean[]) {
413:                    append((boolean[]) lhs, (boolean[]) rhs);
414:                } else {
415:                    // Not an array of primitives
416:                    append((Object[]) lhs, (Object[]) rhs);
417:                }
418:                return this ;
419:            }
420:
421:            /**
422:             * <p>
423:             * Test if two <code>long</code> s are equal.
424:             * </p>
425:             * 
426:             * @param lhs
427:             *                  the left hand <code>long</code>
428:             * @param rhs
429:             *                  the right hand <code>long</code>
430:             * @return EqualsBuilder - used to chain calls.
431:             */
432:            public EqualsBuilder append(long lhs, long rhs) {
433:                if (isEquals == false) {
434:                    return this ;
435:                }
436:                isEquals = (lhs == rhs);
437:                return this ;
438:            }
439:
440:            /**
441:             * <p>Test if two <code>int</code>s are equal.</p>
442:             *
443:             * @param lhs  the left hand <code>int</code>
444:             * @param rhs  the right hand <code>int</code>
445:             * @return EqualsBuilder - used to chain calls.
446:             */
447:            public EqualsBuilder append(int lhs, int rhs) {
448:                if (isEquals == false) {
449:                    return this ;
450:                }
451:                isEquals = (lhs == rhs);
452:                return this ;
453:            }
454:
455:            /**
456:             * <p>Test if two <code>short</code>s are equal.</p>
457:             *
458:             * @param lhs  the left hand <code>short</code>
459:             * @param rhs  the right hand <code>short</code>
460:             * @return EqualsBuilder - used to chain calls.
461:             */
462:            public EqualsBuilder append(short lhs, short rhs) {
463:                if (isEquals == false) {
464:                    return this ;
465:                }
466:                isEquals = (lhs == rhs);
467:                return this ;
468:            }
469:
470:            /**
471:             * <p>Test if two <code>char</code>s are equal.</p>
472:             *
473:             * @param lhs  the left hand <code>char</code>
474:             * @param rhs  the right hand <code>char</code>
475:             * @return EqualsBuilder - used to chain calls.
476:             */
477:            public EqualsBuilder append(char lhs, char rhs) {
478:                if (isEquals == false) {
479:                    return this ;
480:                }
481:                isEquals = (lhs == rhs);
482:                return this ;
483:            }
484:
485:            /**
486:             * <p>Test if two <code>byte</code>s are equal.</p>
487:             *
488:             * @param lhs  the left hand <code>byte</code>
489:             * @param rhs  the right hand <code>byte</code>
490:             * @return EqualsBuilder - used to chain calls.
491:             */
492:            public EqualsBuilder append(byte lhs, byte rhs) {
493:                if (isEquals == false) {
494:                    return this ;
495:                }
496:                isEquals = (lhs == rhs);
497:                return this ;
498:            }
499:
500:            /**
501:             * <p>Test if two <code>double</code>s are equal by testing that the
502:             * pattern of bits returned by <code>doubleToLong</code> are equal.</p>
503:             *
504:             * <p>This handles NaNs, Infinities, and <code>-0.0</code>.</p>
505:             *
506:             * <p>It is compatible with the hash code generated by
507:             * <code>HashCodeBuilder</code>.</p>
508:             *
509:             * @param lhs  the left hand <code>double</code>
510:             * @param rhs  the right hand <code>double</code>
511:             * @return EqualsBuilder - used to chain calls.
512:             */
513:            public EqualsBuilder append(double lhs, double rhs) {
514:                if (isEquals == false) {
515:                    return this ;
516:                }
517:                return append(Double.doubleToLongBits(lhs), Double
518:                        .doubleToLongBits(rhs));
519:            }
520:
521:            /**
522:             * <p>Test if two <code>float</code>s are equal byt testing that the
523:             * pattern of bits returned by doubleToLong are equal.</p>
524:             *
525:             * <p>This handles NaNs, Infinities, and <code>-0.0</code>.</p>
526:             *
527:             * <p>It is compatible with the hash code generated by
528:             * <code>HashCodeBuilder</code>.</p>
529:             *
530:             * @param lhs  the left hand <code>float</code>
531:             * @param rhs  the right hand <code>float</code>
532:             * @return EqualsBuilder - used to chain calls.
533:             */
534:            public EqualsBuilder append(float lhs, float rhs) {
535:                if (isEquals == false) {
536:                    return this ;
537:                }
538:                return append(Float.floatToIntBits(lhs), Float
539:                        .floatToIntBits(rhs));
540:            }
541:
542:            /**
543:             * <p>Test if two <code>booleans</code>s are equal.</p>
544:             *
545:             * @param lhs  the left hand <code>boolean</code>
546:             * @param rhs  the right hand <code>boolean</code>
547:             * @return EqualsBuilder - used to chain calls.
548:             */
549:            public EqualsBuilder append(boolean lhs, boolean rhs) {
550:                if (isEquals == false) {
551:                    return this ;
552:                }
553:                isEquals = (lhs == rhs);
554:                return this ;
555:            }
556:
557:            /**
558:             * <p>Performs a deep comparison of two <code>Object</code> arrays.</p>
559:             *
560:             * <p>This also will be called for the top level of
561:             * multi-dimensional, ragged, and multi-typed arrays.</p>
562:             *
563:             * @param lhs  the left hand <code>Object[]</code>
564:             * @param rhs  the right hand <code>Object[]</code>
565:             * @return EqualsBuilder - used to chain calls.
566:             */
567:            public EqualsBuilder append(Object[] lhs, Object[] rhs) {
568:                if (isEquals == false) {
569:                    return this ;
570:                }
571:                if (lhs == rhs) {
572:                    return this ;
573:                }
574:                if (lhs == null || rhs == null) {
575:                    this .setEquals(false);
576:                    return this ;
577:                }
578:                if (lhs.length != rhs.length) {
579:                    this .setEquals(false);
580:                    return this ;
581:                }
582:                for (int i = 0; i < lhs.length && isEquals; ++i) {
583:                    append(lhs[i], rhs[i]);
584:                }
585:                return this ;
586:            }
587:
588:            /**
589:             * <p>Deep comparison of array of <code>long</code>. Length and all
590:             * values are compared.</p>
591:             *
592:             * <p>The method {@link #append(long, long)} is used.</p>
593:             *
594:             * @param lhs  the left hand <code>long[]</code>
595:             * @param rhs  the right hand <code>long[]</code>
596:             * @return EqualsBuilder - used to chain calls.
597:             */
598:            public EqualsBuilder append(long[] lhs, long[] rhs) {
599:                if (isEquals == false) {
600:                    return this ;
601:                }
602:                if (lhs == rhs) {
603:                    return this ;
604:                }
605:                if (lhs == null || rhs == null) {
606:                    this .setEquals(false);
607:                    return this ;
608:                }
609:                if (lhs.length != rhs.length) {
610:                    this .setEquals(false);
611:                    return this ;
612:                }
613:                for (int i = 0; i < lhs.length && isEquals; ++i) {
614:                    append(lhs[i], rhs[i]);
615:                }
616:                return this ;
617:            }
618:
619:            /**
620:             * <p>Deep comparison of array of <code>int</code>. Length and all
621:             * values are compared.</p>
622:             *
623:             * <p>The method {@link #append(int, int)} is used.</p>
624:             *
625:             * @param lhs  the left hand <code>int[]</code>
626:             * @param rhs  the right hand <code>int[]</code>
627:             * @return EqualsBuilder - used to chain calls.
628:             */
629:            public EqualsBuilder append(int[] lhs, int[] rhs) {
630:                if (isEquals == false) {
631:                    return this ;
632:                }
633:                if (lhs == rhs) {
634:                    return this ;
635:                }
636:                if (lhs == null || rhs == null) {
637:                    this .setEquals(false);
638:                    return this ;
639:                }
640:                if (lhs.length != rhs.length) {
641:                    this .setEquals(false);
642:                    return this ;
643:                }
644:                for (int i = 0; i < lhs.length && isEquals; ++i) {
645:                    append(lhs[i], rhs[i]);
646:                }
647:                return this ;
648:            }
649:
650:            /**
651:             * <p>Deep comparison of array of <code>short</code>. Length and all
652:             * values are compared.</p>
653:             *
654:             * <p>The method {@link #append(short, short)} is used.</p>
655:             *
656:             * @param lhs  the left hand <code>short[]</code>
657:             * @param rhs  the right hand <code>short[]</code>
658:             * @return EqualsBuilder - used to chain calls.
659:             */
660:            public EqualsBuilder append(short[] lhs, short[] rhs) {
661:                if (isEquals == false) {
662:                    return this ;
663:                }
664:                if (lhs == rhs) {
665:                    return this ;
666:                }
667:                if (lhs == null || rhs == null) {
668:                    this .setEquals(false);
669:                    return this ;
670:                }
671:                if (lhs.length != rhs.length) {
672:                    this .setEquals(false);
673:                    return this ;
674:                }
675:                for (int i = 0; i < lhs.length && isEquals; ++i) {
676:                    append(lhs[i], rhs[i]);
677:                }
678:                return this ;
679:            }
680:
681:            /**
682:             * <p>Deep comparison of array of <code>char</code>. Length and all
683:             * values are compared.</p>
684:             *
685:             * <p>The method {@link #append(char, char)} is used.</p>
686:             *
687:             * @param lhs  the left hand <code>char[]</code>
688:             * @param rhs  the right hand <code>char[]</code>
689:             * @return EqualsBuilder - used to chain calls.
690:             */
691:            public EqualsBuilder append(char[] lhs, char[] rhs) {
692:                if (isEquals == false) {
693:                    return this ;
694:                }
695:                if (lhs == rhs) {
696:                    return this ;
697:                }
698:                if (lhs == null || rhs == null) {
699:                    this .setEquals(false);
700:                    return this ;
701:                }
702:                if (lhs.length != rhs.length) {
703:                    this .setEquals(false);
704:                    return this ;
705:                }
706:                for (int i = 0; i < lhs.length && isEquals; ++i) {
707:                    append(lhs[i], rhs[i]);
708:                }
709:                return this ;
710:            }
711:
712:            /**
713:             * <p>Deep comparison of array of <code>byte</code>. Length and all
714:             * values are compared.</p>
715:             *
716:             * <p>The method {@link #append(byte, byte)} is used.</p>
717:             *
718:             * @param lhs  the left hand <code>byte[]</code>
719:             * @param rhs  the right hand <code>byte[]</code>
720:             * @return EqualsBuilder - used to chain calls.
721:             */
722:            public EqualsBuilder append(byte[] lhs, byte[] rhs) {
723:                if (isEquals == false) {
724:                    return this ;
725:                }
726:                if (lhs == rhs) {
727:                    return this ;
728:                }
729:                if (lhs == null || rhs == null) {
730:                    this .setEquals(false);
731:                    return this ;
732:                }
733:                if (lhs.length != rhs.length) {
734:                    this .setEquals(false);
735:                    return this ;
736:                }
737:                for (int i = 0; i < lhs.length && isEquals; ++i) {
738:                    append(lhs[i], rhs[i]);
739:                }
740:                return this ;
741:            }
742:
743:            /**
744:             * <p>Deep comparison of array of <code>double</code>. Length and all
745:             * values are compared.</p>
746:             *
747:             * <p>The method {@link #append(double, double)} is used.</p>
748:             *
749:             * @param lhs  the left hand <code>double[]</code>
750:             * @param rhs  the right hand <code>double[]</code>
751:             * @return EqualsBuilder - used to chain calls.
752:             */
753:            public EqualsBuilder append(double[] lhs, double[] rhs) {
754:                if (isEquals == false) {
755:                    return this ;
756:                }
757:                if (lhs == rhs) {
758:                    return this ;
759:                }
760:                if (lhs == null || rhs == null) {
761:                    this .setEquals(false);
762:                    return this ;
763:                }
764:                if (lhs.length != rhs.length) {
765:                    this .setEquals(false);
766:                    return this ;
767:                }
768:                for (int i = 0; i < lhs.length && isEquals; ++i) {
769:                    append(lhs[i], rhs[i]);
770:                }
771:                return this ;
772:            }
773:
774:            /**
775:             * <p>Deep comparison of array of <code>float</code>. Length and all
776:             * values are compared.</p>
777:             *
778:             * <p>The method {@link #append(float, float)} is used.</p>
779:             *
780:             * @param lhs  the left hand <code>float[]</code>
781:             * @param rhs  the right hand <code>float[]</code>
782:             * @return EqualsBuilder - used to chain calls.
783:             */
784:            public EqualsBuilder append(float[] lhs, float[] rhs) {
785:                if (isEquals == false) {
786:                    return this ;
787:                }
788:                if (lhs == rhs) {
789:                    return this ;
790:                }
791:                if (lhs == null || rhs == null) {
792:                    this .setEquals(false);
793:                    return this ;
794:                }
795:                if (lhs.length != rhs.length) {
796:                    this .setEquals(false);
797:                    return this ;
798:                }
799:                for (int i = 0; i < lhs.length && isEquals; ++i) {
800:                    append(lhs[i], rhs[i]);
801:                }
802:                return this ;
803:            }
804:
805:            /**
806:             * <p>Deep comparison of array of <code>boolean</code>. Length and all
807:             * values are compared.</p>
808:             *
809:             * <p>The method {@link #append(boolean, boolean)} is used.</p>
810:             *
811:             * @param lhs  the left hand <code>boolean[]</code>
812:             * @param rhs  the right hand <code>boolean[]</code>
813:             * @return EqualsBuilder - used to chain calls.
814:             */
815:            public EqualsBuilder append(boolean[] lhs, boolean[] rhs) {
816:                if (isEquals == false) {
817:                    return this ;
818:                }
819:                if (lhs == rhs) {
820:                    return this ;
821:                }
822:                if (lhs == null || rhs == null) {
823:                    this .setEquals(false);
824:                    return this ;
825:                }
826:                if (lhs.length != rhs.length) {
827:                    this .setEquals(false);
828:                    return this ;
829:                }
830:                for (int i = 0; i < lhs.length && isEquals; ++i) {
831:                    append(lhs[i], rhs[i]);
832:                }
833:                return this ;
834:            }
835:
836:            /**
837:             * <p>Returns <code>true</code> if the fields that have been checked
838:             * are all equal.</p>
839:             *
840:             * @return boolean
841:             */
842:            public boolean isEquals() {
843:                return this .isEquals;
844:            }
845:
846:            /**
847:             * Sets the <code>isEquals</code> value.
848:             * 
849:             * @param isEquals The value to set.
850:             * @since 2.1
851:             */
852:            protected void setEquals(boolean isEquals) {
853:                this.isEquals = isEquals;
854:            }
855:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.