KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > builder > CompareToBuilder


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang.builder;
17
18 import java.lang.reflect.AccessibleObject JavaDoc;
19 import java.lang.reflect.Field JavaDoc;
20 import java.lang.reflect.Modifier JavaDoc;
21 import java.util.Comparator JavaDoc;
22
23 import org.apache.commons.lang.math.NumberUtils;
24
25 /**
26  * Assists in implementing {@link java.lang.Comparable#compareTo(Object)} methods.
27  *
28  * It is consistent with <code>equals(Object)</code> and
29  * <code>hashcode()</code> built with {@link EqualsBuilder} and
30  * {@link HashCodeBuilder}.</p>
31  *
32  * <p>Two Objects that compare equal using <code>equals(Object)</code> should normally
33  * also compare equal using <code>compareTo(Object)</code>.</p>
34  *
35  * <p>All relevant fields should be included in the calculation of the
36  * comparison. Derived fields may be ignored. The same fields, in the same
37  * order, should be used in both <code>compareTo(Object)</code> and
38  * <code>equals(Object)</code>.</p>
39  *
40  * <p>To use this class write code as follows:</p>
41  *
42  * <pre>
43  * public class MyClass {
44  * String field1;
45  * int field2;
46  * boolean field3;
47  *
48  * ...
49  *
50  * public int compareTo(Object o) {
51  * MyClass myClass = (MyClass) o;
52  * return new CompareToBuilder()
53  * .appendSuper(super.compareTo(o)
54  * .append(this.field1, myClass.field1)
55  * .append(this.field2, myClass.field2)
56  * .append(this.field3, myClass.field3)
57  * .toComparison();
58  * }
59  * }
60  * </pre>
61  *
62  * <p>Alternatively, there is are {@link #reflectionCompare reflectionCompare} method that uses
63  * reflection to determine the fields to append. Because fields can be private,
64  * <code>reflectionCompare</code> uses {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} to
65  * bypass normal access control checks. This will fail under a security manager,
66  * unless the appropriate permissions are set up correctly. It is also
67  * slower than appending explicitly.</p>
68  *
69  * <p>A typical implementation of <code>compareTo(Object)</code> using
70  * <code>reflectionCompare</code> looks like:</p>
71
72  * <pre>
73  * public int compareTo(Object o) {
74  * return CompareToBuilder.reflectionCompare(this, o);
75  * }
76  * </pre>
77  *
78  * @see java.lang.Comparable
79  * @see java.lang.Object#equals(Object)
80  * @see java.lang.Object#hashCode()
81  * @see EqualsBuilder
82  * @see HashCodeBuilder
83  * @author <a HREF="mailto:steve.downey@netfolio.com">Steve Downey</a>
84  * @author Stephen Colebourne
85  * @author Gary Gregory
86  * @author Pete Gieser
87  * @since 1.0
88  * @version $Id: CompareToBuilder.java 161243 2005-04-14 04:30:28Z ggregory $
89  */

90 public class CompareToBuilder {
91     
92     /**
93      * Current state of the comparison as appended fields are checked.
94      */

95     private int comparison;
96
97     /**
98      * <p>Constructor for CompareToBuilder.</p>
99      *
100      * <p>Starts off assuming that the objects are equal. Multiple calls are
101      * then made to the various append methods, followed by a call to
102      * {@link #toComparison} to get the result.</p>
103      */

104     public CompareToBuilder() {
105         super();
106         comparison = 0;
107     }
108
109     //-----------------------------------------------------------------------
110
/**
111      * <p>Compares two <code>Object</code>s via reflection.</p>
112      *
113      * <p>Fields can be private, thus <code>AccessibleObject.setAccessible</code>
114      * is used to bypass normal access control checks. This will fail under a
115      * security manager unless the appropriate permissions are set.</p>
116      *
117      * <ul>
118      * <li>Static fields will not be compared</li>
119      * <li>Transient members will be not be compared, as they are likely derived
120      * fields</li>
121      * <li>Superclass fields will be compared</li>
122      * </ul>
123      *
124      * <p>If both <code>lhs</code> and <code>rhs</code> are <code>null</code>,
125      * they are considered equal.</p>
126      *
127      * @param lhs left-hand object
128      * @param rhs right-hand object
129      * @return a negative integer, zero, or a positive integer as <code>lhs</code>
130      * is less than, equal to, or greater than <code>rhs</code>
131      * @throws NullPointerException if either (but not both) parameters are
132      * <code>null</code>
133      * @throws ClassCastException if <code>rhs</code> is not assignment-compatible
134      * with <code>lhs</code>
135      */

136     public static int reflectionCompare(Object JavaDoc lhs, Object JavaDoc rhs) {
137         return reflectionCompare(lhs, rhs, false, null);
138     }
139
140     /**
141      * <p>Compares two <code>Object</code>s via reflection.</p>
142      *
143      * <p>Fields can be private, thus <code>AccessibleObject.setAccessible</code>
144      * is used to bypass normal access control checks. This will fail under a
145      * security manager unless the appropriate permissions are set.</p>
146      *
147      * <ul>
148      * <li>Static fields will not be compared</li>
149      * <li>If <code>compareTransients</code> is <code>true</code>,
150      * compares transient members. Otherwise ignores them, as they
151      * are likely derived fields.</li>
152      * <li>Superclass fields will be compared</li>
153      * </ul>
154      *
155      * <p>If both <code>lhs</code> and <code>rhs</code> are <code>null</code>,
156      * they are considered equal.</p>
157      *
158      * @param lhs left-hand object
159      * @param rhs right-hand object
160      * @param compareTransients whether to compare transient fields
161      * @return a negative integer, zero, or a positive integer as <code>lhs</code>
162      * is less than, equal to, or greater than <code>rhs</code>
163      * @throws NullPointerException if either <code>lhs</code> or <code>rhs</code>
164      * (but not both) is <code>null</code>
165      * @throws ClassCastException if <code>rhs</code> is not assignment-compatible
166      * with <code>lhs</code>
167      */

168     public static int reflectionCompare(Object JavaDoc lhs, Object JavaDoc rhs, boolean compareTransients) {
169         return reflectionCompare(lhs, rhs, compareTransients, null);
170     }
171
172     /**
173      * <p>Compares two <code>Object</code>s via reflection.</p>
174      *
175      * <p>Fields can be private, thus <code>AccessibleObject.setAccessible</code>
176      * is used to bypass normal access control checks. This will fail under a
177      * security manager unless the appropriate permissions are set.</p>
178      *
179      * <ul>
180      * <li>Static fields will not be compared</li>
181      * <li>If the <code>compareTransients</code> is <code>true</code>,
182      * compares transient members. Otherwise ignores them, as they
183      * are likely derived fields.</li>
184      * <li>Compares superclass fields up to and including <code>reflectUpToClass</code>.
185      * If <code>reflectUpToClass</code> is <code>null</code>, compares all superclass fields.</li>
186      * </ul>
187      *
188      * <p>If both <code>lhs</code> and <code>rhs</code> are <code>null</code>,
189      * they are considered equal.</p>
190      *
191      * @param lhs left-hand object
192      * @param rhs right-hand object
193      * @param compareTransients whether to compare transient fields
194      * @param reflectUpToClass last superclass for which fields are compared
195      * @return a negative integer, zero, or a positive integer as <code>lhs</code>
196      * is less than, equal to, or greater than <code>rhs</code>
197      * @throws NullPointerException if either <code>lhs</code> or <code>rhs</code>
198      * (but not both) is <code>null</code>
199      * @throws ClassCastException if <code>rhs</code> is not assignment-compatible
200      * with <code>lhs</code>
201      * @since 2.0
202      */

203     public static int reflectionCompare(Object JavaDoc lhs, Object JavaDoc rhs, boolean compareTransients, Class JavaDoc reflectUpToClass) {
204         if (lhs == rhs) {
205             return 0;
206         }
207         if (lhs == null || rhs == null) {
208             throw new NullPointerException JavaDoc();
209         }
210         Class JavaDoc lhsClazz = lhs.getClass();
211         if (!lhsClazz.isInstance(rhs)) {
212             throw new ClassCastException JavaDoc();
213         }
214         CompareToBuilder compareToBuilder = new CompareToBuilder();
215         reflectionAppend(lhs, rhs, lhsClazz, compareToBuilder, compareTransients);
216         while (lhsClazz.getSuperclass() != null && lhsClazz != reflectUpToClass) {
217             lhsClazz = lhsClazz.getSuperclass();
218             reflectionAppend(lhs, rhs, lhsClazz, compareToBuilder, compareTransients);
219         }
220         return compareToBuilder.toComparison();
221     }
222
223     /**
224      * <p>Appends to <code>builder</code> the comparison of <code>lhs</code>
225      * to <code>rhs</code> using the fields defined in <code>clazz</code>.</p>
226      *
227      * @param lhs left-hand object
228      * @param rhs right-hand object
229      * @param clazz <code>Class</code> that defines fields to be compared
230      * @param builder <code>CompareToBuilder</code> to append to
231      * @param useTransients whether to compare transient fields
232      */

233     private static void reflectionAppend(
234         Object JavaDoc lhs,
235         Object JavaDoc rhs,
236         Class JavaDoc clazz,
237         CompareToBuilder builder,
238         boolean useTransients) {
239         
240         Field JavaDoc[] fields = clazz.getDeclaredFields();
241         AccessibleObject.setAccessible(fields, true);
242         for (int i = 0; i < fields.length && builder.comparison == 0; i++) {
243             Field JavaDoc f = fields[i];
244             if ((f.getName().indexOf('$') == -1)
245                 && (useTransients || !Modifier.isTransient(f.getModifiers()))
246                 && (!Modifier.isStatic(f.getModifiers()))) {
247                 try {
248                     builder.append(f.get(lhs), f.get(rhs));
249                 } catch (IllegalAccessException JavaDoc e) {
250                     // This can't happen. Would get a Security exception instead.
251
// Throw a runtime exception in case the impossible happens.
252
throw new InternalError JavaDoc("Unexpected IllegalAccessException");
253                 }
254             }
255         }
256     }
257
258     //-----------------------------------------------------------------------
259
/**
260      * <p>Appends to the <code>builder</code> the <code>compareTo(Object)</code>
261      * result of the superclass.</p>
262      *
263      * @param superCompareTo result of calling <code>super.compareTo(Object)</code>
264      * @return this - used to chain append calls
265      * @since 2.0
266      */

267     public CompareToBuilder appendSuper(int superCompareTo) {
268         if (comparison != 0) {
269             return this;
270         }
271         comparison = superCompareTo;
272         return this;
273     }
274     
275     //-----------------------------------------------------------------------
276
/**
277      * <p>Appends to the <code>builder</code> the comparison of
278      * two <code>Object</code>s.</p>
279      *
280      * <ol>
281      * <li>Check if <code>lhs == rhs</code></li>
282      * <li>Check if either <code>lhs</code> or <code>rhs</code> is <code>null</code>,
283      * a <code>null</code> object is less than a non-<code>null</code> object</li>
284      * <li>Check the object contents</li>
285      * </ol>
286      *
287      * <p><code>lhs</code> must either be an array or implement {@link Comparable}.</p>
288      *
289      * @param lhs left-hand object
290      * @param rhs right-hand object
291      * @return this - used to chain append calls
292      * @throws ClassCastException if <code>rhs</code> is not assignment-compatible
293      * with <code>lhs</code>
294      */

295     public CompareToBuilder append(Object JavaDoc lhs, Object JavaDoc rhs) {
296         return append(lhs, rhs, null);
297     }
298
299     /**
300      * <p>Appends to the <code>builder</code> the comparison of
301      * two <code>Object</code>s.</p>
302      *
303      * <ol>
304      * <li>Check if <code>lhs == rhs</code></li>
305      * <li>Check if either <code>lhs</code> or <code>rhs</code> is <code>null</code>,
306      * a <code>null</code> object is less than a non-<code>null</code> object</li>
307      * <li>Check the object contents</li>
308      * </ol>
309      *
310      * <p>If <code>lhs</code> is an array, array comparison methods will be used.
311      * Otherwise <code>comparator</code> will be used to compare the objects.
312      * If <code>comparator</code> is <code>null</code>, <code>lhs</code> must
313      * implement {@link Comparable} instead.</p>
314      *
315      * @param lhs left-hand object
316      * @param rhs right-hand object
317      * @param comparator <code>Comparator</code> used to compare the objects,
318      * <code>null</code> means treat lhs as <code>Comparable</code>
319      * @return this - used to chain append calls
320      * @throws ClassCastException if <code>rhs</code> is not assignment-compatible
321      * with <code>lhs</code>
322      * @since 2.0
323      */

324     public CompareToBuilder append(Object JavaDoc lhs, Object JavaDoc rhs, Comparator JavaDoc comparator) {
325         if (comparison != 0) {
326             return this;
327         }
328         if (lhs == rhs) {
329             return this;
330         }
331         if (lhs == null) {
332             comparison = -1;
333             return this;
334         }
335         if (rhs == null) {
336             comparison = +1;
337             return this;
338         }
339         if (lhs.getClass().isArray()) {
340             // switch on type of array, to dispatch to the correct handler
341
// handles multi dimensional arrays
342
// throws a ClassCastException if rhs is not the correct array type
343
if (lhs instanceof long[]) {
344                 append((long[]) lhs, (long[]) rhs);
345             } else if (lhs instanceof int[]) {
346                 append((int[]) lhs, (int[]) rhs);
347             } else if (lhs instanceof short[]) {
348                 append((short[]) lhs, (short[]) rhs);
349             } else if (lhs instanceof char[]) {
350                 append((char[]) lhs, (char[]) rhs);
351             } else if (lhs instanceof byte[]) {
352                 append((byte[]) lhs, (byte[]) rhs);
353             } else if (lhs instanceof double[]) {
354                 append((double[]) lhs, (double[]) rhs);
355             } else if (lhs instanceof float[]) {
356                 append((float[]) lhs, (float[]) rhs);
357             } else if (lhs instanceof boolean[]) {
358                 append((boolean[]) lhs, (boolean[]) rhs);
359             } else {
360                 // not an array of primitives
361
// throws a ClassCastException if rhs is not an array
362
append((Object JavaDoc[]) lhs, (Object JavaDoc[]) rhs, comparator);
363             }
364         } else {
365             // the simple case, not an array, just test the element
366
if (comparator == null) {
367                 comparison = ((Comparable JavaDoc) lhs).compareTo(rhs);
368             } else {
369                 comparison = comparator.compare(lhs, rhs);
370             }
371         }
372         return this;
373     }
374
375     //-------------------------------------------------------------------------
376
/**
377      * Appends to the <code>builder</code> the comparison of
378      * two <code>long</code>s.
379      *
380      * @param lhs left-hand value
381      * @param rhs right-hand value
382      * @return this - used to chain append calls
383      */

384     public CompareToBuilder append(long lhs, long rhs) {
385         if (comparison != 0) {
386             return this;
387         }
388         comparison = ((lhs < rhs) ? -1 : ((lhs > rhs) ? 1 : 0));
389         return this;
390     }
391
392     /**
393      * Appends to the <code>builder</code> the comparison of
394      * two <code>int</code>s.
395      *
396      * @param lhs left-hand value
397      * @param rhs right-hand value
398      * @return this - used to chain append calls
399      */

400     public CompareToBuilder append(int lhs, int rhs) {
401         if (comparison != 0) {
402             return this;
403         }
404         comparison = ((lhs < rhs) ? -1 : ((lhs > rhs) ? 1 : 0));
405         return this;
406     }
407
408     /**
409      * Appends to the <code>builder</code> the comparison of
410      * two <code>short</code>s.
411      *
412      * @param lhs left-hand value
413      * @param rhs right-hand value
414      * @return this - used to chain append calls
415      */

416     public CompareToBuilder append(short lhs, short rhs) {
417         if (comparison != 0) {
418             return this;
419         }
420         comparison = ((lhs < rhs) ? -1 : ((lhs > rhs) ? 1 : 0));
421         return this;
422     }
423
424     /**
425      * Appends to the <code>builder</code> the comparison of
426      * two <code>char</code>s.
427      *
428      * @param lhs left-hand value
429      * @param rhs right-hand value
430      * @return this - used to chain append calls
431      */

432     public CompareToBuilder append(char lhs, char rhs) {
433         if (comparison != 0) {
434             return this;
435         }
436         comparison = ((lhs < rhs) ? -1 : ((lhs > rhs) ? 1 : 0));
437         return this;
438     }
439
440     /**
441      * Appends to the <code>builder</code> the comparison of
442      * two <code>byte</code>s.
443      *
444      * @param lhs left-hand value
445      * @param rhs right-hand value
446      * @return this - used to chain append calls
447      */

448     public CompareToBuilder append(byte lhs, byte rhs) {
449         if (comparison != 0) {
450             return this;
451         }
452         comparison = ((lhs < rhs) ? -1 : ((lhs > rhs) ? 1 : 0));
453         return this;
454     }
455
456     /**
457      * <p>Appends to the <code>builder</code> the comparison of
458      * two <code>double</code>s.</p>
459      *
460      * <p>This handles NaNs, Infinities, and <code>-0.0</code>.</p>
461      *
462      * <p>It is compatible with the hash code generated by
463      * <code>HashCodeBuilder</code>.</p>
464      *
465      * @param lhs left-hand value
466      * @param rhs right-hand value
467      * @return this - used to chain append calls
468      */

469     public CompareToBuilder append(double lhs, double rhs) {
470         if (comparison != 0) {
471             return this;
472         }
473         comparison = NumberUtils.compare(lhs, rhs);
474         return this;
475     }
476
477     /**
478      * <p>Appends to the <code>builder</code> the comparison of
479      * two <code>float</code>s.</p>
480      *
481      * <p>This handles NaNs, Infinities, and <code>-0.0</code>.</p>
482      *
483      * <p>It is compatible with the hash code generated by
484      * <code>HashCodeBuilder</code>.</p>
485      *
486      * @param lhs left-hand value
487      * @param rhs right-hand value
488      * @return this - used to chain append calls
489      */

490     public CompareToBuilder append(float lhs, float rhs) {
491         if (comparison != 0) {
492             return this;
493         }
494         comparison = NumberUtils.compare(lhs, rhs);
495         return this;
496     }
497
498     /**
499      * Appends to the <code>builder</code> the comparison of
500      * two <code>booleans</code>s.
501      *
502      * @param lhs left-hand value
503      * @param rhs right-hand value
504      * @return this - used to chain append calls
505       */

506     public CompareToBuilder append(boolean lhs, boolean rhs) {
507         if (comparison != 0) {
508             return this;
509         }
510         if (lhs == rhs) {
511             return this;
512         }
513         if (lhs == false) {
514             comparison = -1;
515         } else {
516             comparison = +1;
517         }
518         return this;
519     }
520
521     //-----------------------------------------------------------------------
522
/**
523      * <p>Appends to the <code>builder</code> the deep comparison of
524      * two <code>Object</code> arrays.</p>
525      *
526      * <ol>
527      * <li>Check if arrays are the same using <code>==</code></li>
528      * <li>Check if for <code>null</code>, <code>null</code> is less than non-<code>null</code></li>
529      * <li>Check array length, a short length array is less than a long length array</li>
530      * <li>Check array contents element by element using {@link #append(Object, Object, Comparator)}</li>
531      * </ol>
532      *
533      * <p>This method will also will be called for the top level of multi-dimensional,
534      * ragged, and multi-typed arrays.</p>
535      *
536      * @param lhs left-hand array
537      * @param rhs right-hand array
538      * @return this - used to chain append calls
539      * @throws ClassCastException if <code>rhs</code> is not assignment-compatible
540      * with <code>lhs</code>
541      */

542     public CompareToBuilder append(Object JavaDoc[] lhs, Object JavaDoc[] rhs) {
543         return append(lhs, rhs, null);
544     }
545     
546     /**
547      * <p>Appends to the <code>builder</code> the deep comparison of
548      * two <code>Object</code> arrays.</p>
549      *
550      * <ol>
551      * <li>Check if arrays are the same using <code>==</code></li>
552      * <li>Check if for <code>null</code>, <code>null</code> is less than non-<code>null</code></li>
553      * <li>Check array length, a short length array is less than a long length array</li>
554      * <li>Check array contents element by element using {@link #append(Object, Object, Comparator)}</li>
555      * </ol>
556      *
557      * <p>This method will also will be called for the top level of multi-dimensional,
558      * ragged, and multi-typed arrays.</p>
559      *
560      * @param lhs left-hand array
561      * @param rhs right-hand array
562      * @param comparator <code>Comparator</code> to use to compare the array elements,
563      * <code>null</code> means to treat <code>lhs</code> elements as <code>Comparable</code>.
564      * @return this - used to chain append calls
565      * @throws ClassCastException if <code>rhs</code> is not assignment-compatible
566      * with <code>lhs</code>
567      * @since 2.0
568      */

569     public CompareToBuilder append(Object JavaDoc[] lhs, Object JavaDoc[] rhs, Comparator JavaDoc comparator) {
570         if (comparison != 0) {
571             return this;
572         }
573         if (lhs == rhs) {
574             return this;
575         }
576         if (lhs == null) {
577             comparison = -1;
578             return this;
579         }
580         if (rhs == null) {
581             comparison = +1;
582             return this;
583         }
584         if (lhs.length != rhs.length) {
585             comparison = (lhs.length < rhs.length) ? -1 : +1;
586             return this;
587         }
588         for (int i = 0; i < lhs.length && comparison == 0; i++) {
589             append(lhs[i], rhs[i], comparator);
590         }
591         return this;
592     }
593
594     /**
595      * <p>Appends to the <code>builder</code> the deep comparison of
596      * two <code>long</code> arrays.</p>
597      *
598      * <ol>
599      * <li>Check if arrays are the same using <code>==</code></li>
600      * <li>Check if for <code>null</code>, <code>null</code> is less than non-<code>null</code></li>
601      * <li>Check array length, a shorter length array is less than a longer length array</li>
602      * <li>Check array contents element by element using {@link #append(long, long)}</li>
603      * </ol>
604      *
605      * @param lhs left-hand array
606      * @param rhs right-hand array
607      * @return this - used to chain append calls
608      */

609     public CompareToBuilder append(long[] lhs, long[] rhs) {
610         if (comparison != 0) {
611             return this;
612         }
613         if (lhs == rhs) {
614             return this;
615         }
616         if (lhs == null) {
617             comparison = -1;
618             return this;
619         }
620         if (rhs == null) {
621             comparison = +1;
622             return this;
623         }
624         if (lhs.length != rhs.length) {
625             comparison = (lhs.length < rhs.length) ? -1 : +1;
626             return this;
627         }
628         for (int i = 0; i < lhs.length && comparison == 0; i++) {
629             append(lhs[i], rhs[i]);
630         }
631         return this;
632     }
633
634     /**
635      * <p>Appends to the <code>builder</code> the deep comparison of
636      * two <code>int</code> arrays.</p>
637      *
638      * <ol>
639      * <li>Check if arrays are the same using <code>==</code></li>
640      * <li>Check if for <code>null</code>, <code>null</code> is less than non-<code>null</code></li>
641      * <li>Check array length, a shorter length array is less than a longer length array</li>
642      * <li>Check array contents element by element using {@link #append(int, int)}</li>
643      * </ol>
644      *
645      * @param lhs left-hand array
646      * @param rhs right-hand array
647      * @return this - used to chain append calls
648      */

649     public CompareToBuilder append(int[] lhs, int[] rhs) {
650         if (comparison != 0) {
651             return this;
652         }
653         if (lhs == rhs) {
654             return this;
655         }
656         if (lhs == null) {
657             comparison = -1;
658             return this;
659         }
660         if (rhs == null) {
661             comparison = +1;
662             return this;
663         }
664         if (lhs.length != rhs.length) {
665             comparison = (lhs.length < rhs.length) ? -1 : +1;
666             return this;
667         }
668         for (int i = 0; i < lhs.length && comparison == 0; i++) {
669             append(lhs[i], rhs[i]);
670         }
671         return this;
672     }
673
674     /**
675      * <p>Appends to the <code>builder</code> the deep comparison of
676      * two <code>short</code> arrays.</p>
677      *
678      * <ol>
679      * <li>Check if arrays are the same using <code>==</code></li>
680      * <li>Check if for <code>null</code>, <code>null</code> is less than non-<code>null</code></li>
681      * <li>Check array length, a shorter length array is less than a longer length array</li>
682      * <li>Check array contents element by element using {@link #append(short, short)}</li>
683      * </ol>
684      *
685      * @param lhs left-hand array
686      * @param rhs right-hand array
687      * @return this - used to chain append calls
688      */

689     public CompareToBuilder append(short[] lhs, short[] rhs) {
690         if (comparison != 0) {
691             return this;
692         }
693         if (lhs == rhs) {
694             return this;
695         }
696         if (lhs == null) {
697             comparison = -1;
698             return this;
699         }
700         if (rhs == null) {
701             comparison = +1;
702             return this;
703         }
704         if (lhs.length != rhs.length) {
705             comparison = (lhs.length < rhs.length) ? -1 : +1;
706             return this;
707         }
708         for (int i = 0; i < lhs.length && comparison == 0; i++) {
709             append(lhs[i], rhs[i]);
710         }
711         return this;
712     }
713
714     /**
715      * <p>Appends to the <code>builder</code> the deep comparison of
716      * two <code>char</code> arrays.</p>
717      *
718      * <ol>
719      * <li>Check if arrays are the same using <code>==</code></li>
720      * <li>Check if for <code>null</code>, <code>null</code> is less than non-<code>null</code></li>
721      * <li>Check array length, a shorter length array is less than a longer length array</li>
722      * <li>Check array contents element by element using {@link #append(char, char)}</li>
723      * </ol>
724      *
725      * @param lhs left-hand array
726      * @param rhs right-hand array
727      * @return this - used to chain append calls
728      */

729     public CompareToBuilder append(char[] lhs, char[] rhs) {
730         if (comparison != 0) {
731             return this;
732         }
733         if (lhs == rhs) {
734             return this;
735         }
736         if (lhs == null) {
737             comparison = -1;
738             return this;
739         }
740         if (rhs == null) {
741             comparison = +1;
742             return this;
743         }
744         if (lhs.length != rhs.length) {
745             comparison = (lhs.length < rhs.length) ? -1 : +1;
746             return this;
747         }
748         for (int i = 0; i < lhs.length && comparison == 0; i++) {
749             append(lhs[i], rhs[i]);
750         }
751         return this;
752     }
753
754     /**
755      * <p>Appends to the <code>builder</code> the deep comparison of
756      * two <code>byte</code> arrays.</p>
757      *
758      * <ol>
759      * <li>Check if arrays are the same using <code>==</code></li>
760      * <li>Check if for <code>null</code>, <code>null</code> is less than non-<code>null</code></li>
761      * <li>Check array length, a shorter length array is less than a longer length array</li>
762      * <li>Check array contents element by element using {@link #append(byte, byte)}</li>
763      * </ol>
764      *
765      * @param lhs left-hand array
766      * @param rhs right-hand array
767      * @return this - used to chain append calls
768      */

769     public CompareToBuilder append(byte[] lhs, byte[] rhs) {
770         if (comparison != 0) {
771             return this;
772         }
773         if (lhs == rhs) {
774             return this;
775         }
776         if (lhs == null) {
777             comparison = -1;
778             return this;
779         }
780         if (rhs == null) {
781             comparison = +1;
782             return this;
783         }
784         if (lhs.length != rhs.length) {
785             comparison = (lhs.length < rhs.length) ? -1 : +1;
786             return this;
787         }
788         for (int i = 0; i < lhs.length && comparison == 0; i++) {
789             append(lhs[i], rhs[i]);
790         }
791         return this;
792     }
793
794     /**
795      * <p>Appends to the <code>builder</code> the deep comparison of
796      * two <code>double</code> arrays.</p>
797      *
798      * <ol>
799      * <li>Check if arrays are the same using <code>==</code></li>
800      * <li>Check if for <code>null</code>, <code>null</code> is less than non-<code>null</code></li>
801      * <li>Check array length, a shorter length array is less than a longer length array</li>
802      * <li>Check array contents element by element using {@link #append(double, double)}</li>
803      * </ol>
804      *
805      * @param lhs left-hand array
806      * @param rhs right-hand array
807      * @return this - used to chain append calls
808      */

809     public CompareToBuilder append(double[] lhs, double[] rhs) {
810         if (comparison != 0) {
811             return this;
812         }
813         if (lhs == rhs) {
814             return this;
815         }
816         if (lhs == null) {
817             comparison = -1;
818             return this;
819         }
820         if (rhs == null) {
821             comparison = +1;
822             return this;
823         }
824         if (lhs.length != rhs.length) {
825             comparison = (lhs.length < rhs.length) ? -1 : +1;
826             return this;
827         }
828         for (int i = 0; i < lhs.length && comparison == 0; i++) {
829             append(lhs[i], rhs[i]);
830         }
831         return this;
832     }
833
834     /**
835      * <p>Appends to the <code>builder</code> the deep comparison of
836      * two <code>float</code> arrays.</p>
837      *
838      * <ol>
839      * <li>Check if arrays are the same using <code>==</code></li>
840      * <li>Check if for <code>null</code>, <code>null</code> is less than non-<code>null</code></li>
841      * <li>Check array length, a shorter length array is less than a longer length array</li>
842      * <li>Check array contents element by element using {@link #append(float, float)}</li>
843      * </ol>
844      *
845      * @param lhs left-hand array
846      * @param rhs right-hand array
847      * @return this - used to chain append calls
848      */

849     public CompareToBuilder append(float[] lhs, float[] rhs) {
850         if (comparison != 0) {
851             return this;
852         }
853         if (lhs == rhs) {
854             return this;
855         }
856         if (lhs == null) {
857             comparison = -1;
858             return this;
859         }
860         if (rhs == null) {
861             comparison = +1;
862             return this;
863         }
864         if (lhs.length != rhs.length) {
865             comparison = (lhs.length < rhs.length) ? -1 : +1;
866             return this;
867         }
868         for (int i = 0; i < lhs.length && comparison == 0; i++) {
869             append(lhs[i], rhs[i]);
870         }
871         return this;
872     }
873
874     /**
875      * <p>Appends to the <code>builder</code> the deep comparison of
876      * two <code>boolean</code> arrays.</p>
877      *
878      * <ol>
879      * <li>Check if arrays are the same using <code>==</code></li>
880      * <li>Check if for <code>null</code>, <code>null</code> is less than non-<code>null</code></li>
881      * <li>Check array length, a shorter length array is less than a longer length array</li>
882      * <li>Check array contents element by element using {@link #append(boolean, boolean)}</li>
883      * </ol>
884      *
885      * @param lhs left-hand array
886      * @param rhs right-hand array
887      * @return this - used to chain append calls
888      */

889     public CompareToBuilder append(boolean[] lhs, boolean[] rhs) {
890         if (comparison != 0) {
891             return this;
892         }
893         if (lhs == rhs) {
894             return this;
895         }
896         if (lhs == null) {
897             comparison = -1;
898             return this;
899         }
900         if (rhs == null) {
901             comparison = +1;
902             return this;
903         }
904         if (lhs.length != rhs.length) {
905             comparison = (lhs.length < rhs.length) ? -1 : +1;
906             return this;
907         }
908         for (int i = 0; i < lhs.length && comparison == 0; i++) {
909             append(lhs[i], rhs[i]);
910         }
911         return this;
912     }
913
914     //-----------------------------------------------------------------------
915
/**
916      * Returns a negative integer, a positive integer, or zero as
917      * the <code>builder</code> has judged the "left-hand" side
918      * as less than, greater than, or equal to the "right-hand"
919      * side.
920      *
921      * @return final comparison result
922      */

923     public int toComparison() {
924         return comparison;
925     }
926
927 }
928
929
Popular Tags