Java Doc for CompareToBuilder.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 Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   org.apache.commons.lang.builder.CompareToBuilder

CompareToBuilder
public class CompareToBuilder (Code)
Assists in implementing java.lang.Comparable.compareTo(Object) methods. It is consistent with equals(Object) and hashcode() built with EqualsBuilder and HashCodeBuilder .

Two Objects that compare equal using equals(Object) should normally also compare equal using compareTo(Object).

All relevant fields should be included in the calculation of the comparison. Derived fields may be ignored. The same fields, in the same order, should be used in both compareTo(Object) and equals(Object).

To use this class write code as follows:

 public class MyClass {
 String field1;
 int field2;
 boolean field3;
 ...
 public int compareTo(Object o) {
 MyClass myClass = (MyClass) o;
 return new CompareToBuilder()
 .appendSuper(super.compareTo(o)
 .append(this.field1, myClass.field1)
 .append(this.field2, myClass.field2)
 .append(this.field3, myClass.field3)
 .toComparison();
 }
 }
 

Alternatively, there are CompareToBuilder.reflectionCompare(Object,Object) reflectionCompare methods that use reflection to determine the fields to append. Because fields can be private, reflectionCompare uses java.lang.reflect.AccessibleObject.setAccessible(boolean) to bypass normal access control checks. This will fail under a security manager, unless the appropriate permissions are set up correctly. It is also slower than appending explicitly.

A typical implementation of compareTo(Object) using reflectionCompare looks like:

 public int compareTo(Object o) {
 return CompareToBuilder.reflectionCompare(this, o);
 }
 

See Also:   java.lang.Comparable
See Also:   java.lang.Object.equals(Object)
See Also:   java.lang.Object.hashCode
See Also:   EqualsBuilder
See Also:   HashCodeBuilder
author:
   Steve Downey
author:
   Stephen Colebourne
author:
   Gary Gregory
author:
   Pete Gieser
since:
   1.0
version:
   $Id: CompareToBuilder.java 447139 2006-09-17 20:36:53Z bayard $



Constructor Summary
public  CompareToBuilder()
    

Constructor for CompareToBuilder.

Starts off assuming that the objects are equal.


Method Summary
public  CompareToBuilderappend(Object lhs, Object rhs)
    
public  CompareToBuilderappend(Object lhs, Object rhs, Comparator comparator)
    

Appends to the builder the comparison of two Objects.

  1. Check if lhs == rhs
  2. Check if either lhs or rhs is null, a null object is less than a non-null object
  3. Check the object contents

If lhs is an array, array comparison methods will be used.

public  CompareToBuilderappend(long lhs, long rhs)
     Appends to the builder the comparison of two longs.
public  CompareToBuilderappend(int lhs, int rhs)
     Appends to the builder the comparison of two ints.
public  CompareToBuilderappend(short lhs, short rhs)
     Appends to the builder the comparison of two shorts.
public  CompareToBuilderappend(char lhs, char rhs)
     Appends to the builder the comparison of two chars.
public  CompareToBuilderappend(byte lhs, byte rhs)
     Appends to the builder the comparison of two bytes.
public  CompareToBuilderappend(double lhs, double rhs)
    
public  CompareToBuilderappend(float lhs, float rhs)
    
public  CompareToBuilderappend(boolean lhs, boolean rhs)
     Appends to the builder the comparison of two booleanss.
public  CompareToBuilderappend(Object[] lhs, Object[] rhs)
    
public  CompareToBuilderappend(Object[] lhs, Object[] rhs, Comparator comparator)
    

Appends to the builder the deep comparison of two Object arrays.

  1. Check if arrays are the same using ==
  2. Check if for null, null is less than non-null
  3. Check array length, a short length array is less than a long length array
  4. Check array contents element by element using CompareToBuilder.append(Object,Object,Comparator)

This method will also will be called for the top level of multi-dimensional, ragged, and multi-typed arrays.


Parameters:
  lhs - left-hand array
Parameters:
  rhs - right-hand array
Parameters:
  comparator - Comparator to use to compare the array elements,null means to treat lhs elements as Comparable.
public  CompareToBuilderappend(long[] lhs, long[] rhs)
    
public  CompareToBuilderappend(int[] lhs, int[] rhs)
    
public  CompareToBuilderappend(short[] lhs, short[] rhs)
    
public  CompareToBuilderappend(char[] lhs, char[] rhs)
    
public  CompareToBuilderappend(byte[] lhs, byte[] rhs)
    
public  CompareToBuilderappend(double[] lhs, double[] rhs)
    
public  CompareToBuilderappend(float[] lhs, float[] rhs)
    
public  CompareToBuilderappend(boolean[] lhs, boolean[] rhs)
    
public  CompareToBuilderappendSuper(int superCompareTo)
    
public static  intreflectionCompare(Object lhs, Object rhs)
    

Compares two Objects via reflection.

Fields can be private, thus AccessibleObject.setAccessible is used to bypass normal access control checks.

public static  intreflectionCompare(Object lhs, Object rhs, boolean compareTransients)
    

Compares two Objects via reflection.

Fields can be private, thus AccessibleObject.setAccessible is used to bypass normal access control checks.

public static  intreflectionCompare(Object lhs, Object rhs, Collection excludeFields)
    

Compares two Objects via reflection.

Fields can be private, thus AccessibleObject.setAccessible is used to bypass normal access control checks.

public static  intreflectionCompare(Object lhs, Object rhs, String[] excludeFields)
    

Compares two Objects via reflection.

Fields can be private, thus AccessibleObject.setAccessible is used to bypass normal access control checks.

public static  intreflectionCompare(Object lhs, Object rhs, boolean compareTransients, Class reflectUpToClass)
    

Compares two Objects via reflection.

Fields can be private, thus AccessibleObject.setAccessible is used to bypass normal access control checks.

public static  intreflectionCompare(Object lhs, Object rhs, boolean compareTransients, Class reflectUpToClass, String[] excludeFields)
    

Compares two Objects via reflection.

Fields can be private, thus AccessibleObject.setAccessible is used to bypass normal access control checks.

public  inttoComparison()
     Returns a negative integer, a positive integer, or zero as the builder has judged the "left-hand" side as less than, greater than, or equal to the "right-hand" side.


Constructor Detail
CompareToBuilder
public CompareToBuilder()(Code)

Constructor for CompareToBuilder.

Starts off assuming that the objects are equal. Multiple calls are then made to the various append methods, followed by a call to CompareToBuilder.toComparison to get the result.





Method Detail
append
public CompareToBuilder append(Object lhs, Object rhs)(Code)

Appends to the builder the comparison of two Objects.

  1. Check if lhs == rhs
  2. Check if either lhs or rhs is null, a null object is less than a non-null object
  3. Check the object contents

lhs must either be an array or implement Comparable .


Parameters:
  lhs - left-hand object
Parameters:
  rhs - right-hand object this - used to chain append calls
throws:
  ClassCastException - if rhs is not assignment-compatiblewith lhs



append
public CompareToBuilder append(Object lhs, Object rhs, Comparator comparator)(Code)

Appends to the builder the comparison of two Objects.

  1. Check if lhs == rhs
  2. Check if either lhs or rhs is null, a null object is less than a non-null object
  3. Check the object contents

If lhs is an array, array comparison methods will be used. Otherwise comparator will be used to compare the objects. If comparator is null, lhs must implement Comparable instead.


Parameters:
  lhs - left-hand object
Parameters:
  rhs - right-hand object
Parameters:
  comparator - Comparator used to compare the objects,null means treat lhs as Comparable this - used to chain append calls
throws:
  ClassCastException - if rhs is not assignment-compatiblewith lhs
since:
   2.0



append
public CompareToBuilder append(long lhs, long rhs)(Code)
Appends to the builder the comparison of two longs.
Parameters:
  lhs - left-hand value
Parameters:
  rhs - right-hand value this - used to chain append calls



append
public CompareToBuilder append(int lhs, int rhs)(Code)
Appends to the builder the comparison of two ints.
Parameters:
  lhs - left-hand value
Parameters:
  rhs - right-hand value this - used to chain append calls



append
public CompareToBuilder append(short lhs, short rhs)(Code)
Appends to the builder the comparison of two shorts.
Parameters:
  lhs - left-hand value
Parameters:
  rhs - right-hand value this - used to chain append calls



append
public CompareToBuilder append(char lhs, char rhs)(Code)
Appends to the builder the comparison of two chars.
Parameters:
  lhs - left-hand value
Parameters:
  rhs - right-hand value this - used to chain append calls



append
public CompareToBuilder append(byte lhs, byte rhs)(Code)
Appends to the builder the comparison of two bytes.
Parameters:
  lhs - left-hand value
Parameters:
  rhs - right-hand value this - used to chain append calls



append
public CompareToBuilder append(double lhs, double rhs)(Code)

Appends to the builder the comparison of two doubles.

This handles NaNs, Infinities, and -0.0.

It is compatible with the hash code generated by HashCodeBuilder.


Parameters:
  lhs - left-hand value
Parameters:
  rhs - right-hand value this - used to chain append calls



append
public CompareToBuilder append(float lhs, float rhs)(Code)

Appends to the builder the comparison of two floats.

This handles NaNs, Infinities, and -0.0.

It is compatible with the hash code generated by HashCodeBuilder.


Parameters:
  lhs - left-hand value
Parameters:
  rhs - right-hand value this - used to chain append calls



append
public CompareToBuilder append(boolean lhs, boolean rhs)(Code)
Appends to the builder the comparison of two booleanss.
Parameters:
  lhs - left-hand value
Parameters:
  rhs - right-hand value this - used to chain append calls



append
public CompareToBuilder append(Object[] lhs, Object[] rhs)(Code)

Appends to the builder the deep comparison of two Object arrays.

  1. Check if arrays are the same using ==
  2. Check if for null, null is less than non-null
  3. Check array length, a short length array is less than a long length array
  4. Check array contents element by element using CompareToBuilder.append(Object,Object,Comparator)

This method will also will be called for the top level of multi-dimensional, ragged, and multi-typed arrays.


Parameters:
  lhs - left-hand array
Parameters:
  rhs - right-hand array this - used to chain append calls
throws:
  ClassCastException - if rhs is not assignment-compatiblewith lhs



append
public CompareToBuilder append(Object[] lhs, Object[] rhs, Comparator comparator)(Code)

Appends to the builder the deep comparison of two Object arrays.

  1. Check if arrays are the same using ==
  2. Check if for null, null is less than non-null
  3. Check array length, a short length array is less than a long length array
  4. Check array contents element by element using CompareToBuilder.append(Object,Object,Comparator)

This method will also will be called for the top level of multi-dimensional, ragged, and multi-typed arrays.


Parameters:
  lhs - left-hand array
Parameters:
  rhs - right-hand array
Parameters:
  comparator - Comparator to use to compare the array elements,null means to treat lhs elements as Comparable. this - used to chain append calls
throws:
  ClassCastException - if rhs is not assignment-compatiblewith lhs
since:
   2.0



append
public CompareToBuilder append(long[] lhs, long[] rhs)(Code)

Appends to the builder the deep comparison of two long arrays.

  1. Check if arrays are the same using ==
  2. Check if for null, null is less than non-null
  3. Check array length, a shorter length array is less than a longer length array
  4. Check array contents element by element using CompareToBuilder.append(long,long)

Parameters:
  lhs - left-hand array
Parameters:
  rhs - right-hand array this - used to chain append calls



append
public CompareToBuilder append(int[] lhs, int[] rhs)(Code)

Appends to the builder the deep comparison of two int arrays.

  1. Check if arrays are the same using ==
  2. Check if for null, null is less than non-null
  3. Check array length, a shorter length array is less than a longer length array
  4. Check array contents element by element using CompareToBuilder.append(int,int)

Parameters:
  lhs - left-hand array
Parameters:
  rhs - right-hand array this - used to chain append calls



append
public CompareToBuilder append(short[] lhs, short[] rhs)(Code)

Appends to the builder the deep comparison of two short arrays.

  1. Check if arrays are the same using ==
  2. Check if for null, null is less than non-null
  3. Check array length, a shorter length array is less than a longer length array
  4. Check array contents element by element using CompareToBuilder.append(short,short)

Parameters:
  lhs - left-hand array
Parameters:
  rhs - right-hand array this - used to chain append calls



append
public CompareToBuilder append(char[] lhs, char[] rhs)(Code)

Appends to the builder the deep comparison of two char arrays.

  1. Check if arrays are the same using ==
  2. Check if for null, null is less than non-null
  3. Check array length, a shorter length array is less than a longer length array
  4. Check array contents element by element using CompareToBuilder.append(char,char)

Parameters:
  lhs - left-hand array
Parameters:
  rhs - right-hand array this - used to chain append calls



append
public CompareToBuilder append(byte[] lhs, byte[] rhs)(Code)

Appends to the builder the deep comparison of two byte arrays.

  1. Check if arrays are the same using ==
  2. Check if for null, null is less than non-null
  3. Check array length, a shorter length array is less than a longer length array
  4. Check array contents element by element using CompareToBuilder.append(byte,byte)

Parameters:
  lhs - left-hand array
Parameters:
  rhs - right-hand array this - used to chain append calls



append
public CompareToBuilder append(double[] lhs, double[] rhs)(Code)

Appends to the builder the deep comparison of two double arrays.

  1. Check if arrays are the same using ==
  2. Check if for null, null is less than non-null
  3. Check array length, a shorter length array is less than a longer length array
  4. Check array contents element by element using CompareToBuilder.append(double,double)

Parameters:
  lhs - left-hand array
Parameters:
  rhs - right-hand array this - used to chain append calls



append
public CompareToBuilder append(float[] lhs, float[] rhs)(Code)

Appends to the builder the deep comparison of two float arrays.

  1. Check if arrays are the same using ==
  2. Check if for null, null is less than non-null
  3. Check array length, a shorter length array is less than a longer length array
  4. Check array contents element by element using CompareToBuilder.append(float,float)

Parameters:
  lhs - left-hand array
Parameters:
  rhs - right-hand array this - used to chain append calls



append
public CompareToBuilder append(boolean[] lhs, boolean[] rhs)(Code)

Appends to the builder the deep comparison of two boolean arrays.

  1. Check if arrays are the same using ==
  2. Check if for null, null is less than non-null
  3. Check array length, a shorter length array is less than a longer length array
  4. Check array contents element by element using CompareToBuilder.append(boolean,boolean)

Parameters:
  lhs - left-hand array
Parameters:
  rhs - right-hand array this - used to chain append calls



appendSuper
public CompareToBuilder appendSuper(int superCompareTo)(Code)

Appends to the builder the compareTo(Object) result of the superclass.


Parameters:
  superCompareTo - result of calling super.compareTo(Object) this - used to chain append calls
since:
   2.0



reflectionCompare
public static int reflectionCompare(Object lhs, Object rhs)(Code)

Compares two Objects via reflection.

Fields can be private, thus AccessibleObject.setAccessible is used to bypass normal access control checks. This will fail under a security manager unless the appropriate permissions are set.

  • Static fields will not be compared
  • Transient members will be not be compared, as they are likely derived fields
  • Superclass fields will be compared

If both lhs and rhs are null, they are considered equal.


Parameters:
  lhs - left-hand object
Parameters:
  rhs - right-hand object a negative integer, zero, or a positive integer as lhsis less than, equal to, or greater than rhs
throws:
  NullPointerException - if either (but not both) parameters arenull
throws:
  ClassCastException - if rhs is not assignment-compatiblewith lhs



reflectionCompare
public static int reflectionCompare(Object lhs, Object rhs, boolean compareTransients)(Code)

Compares two Objects via reflection.

Fields can be private, thus AccessibleObject.setAccessible is used to bypass normal access control checks. This will fail under a security manager unless the appropriate permissions are set.

  • Static fields will not be compared
  • If compareTransients is true, compares transient members. Otherwise ignores them, as they are likely derived fields.
  • Superclass fields will be compared

If both lhs and rhs are null, they are considered equal.


Parameters:
  lhs - left-hand object
Parameters:
  rhs - right-hand object
Parameters:
  compareTransients - whether to compare transient fields a negative integer, zero, or a positive integer as lhsis less than, equal to, or greater than rhs
throws:
  NullPointerException - if either lhs or rhs(but not both) is null
throws:
  ClassCastException - if rhs is not assignment-compatiblewith lhs



reflectionCompare
public static int reflectionCompare(Object lhs, Object rhs, Collection excludeFields)(Code)

Compares two Objects via reflection.

Fields can be private, thus AccessibleObject.setAccessible is used to bypass normal access control checks. This will fail under a security manager unless the appropriate permissions are set.

  • Static fields will not be compared
  • If compareTransients is true, compares transient members. Otherwise ignores them, as they are likely derived fields.
  • Superclass fields will be compared

If both lhs and rhs are null, they are considered equal.


Parameters:
  lhs - left-hand object
Parameters:
  rhs - right-hand object
Parameters:
  excludeFields - Collection of String fields to exclude a negative integer, zero, or a positive integer as lhsis less than, equal to, or greater than rhs
throws:
  NullPointerException - if either lhs or rhs(but not both) is null
throws:
  ClassCastException - if rhs is not assignment-compatiblewith lhs
since:
   2.2



reflectionCompare
public static int reflectionCompare(Object lhs, Object rhs, String[] excludeFields)(Code)

Compares two Objects via reflection.

Fields can be private, thus AccessibleObject.setAccessible is used to bypass normal access control checks. This will fail under a security manager unless the appropriate permissions are set.

  • Static fields will not be compared
  • If compareTransients is true, compares transient members. Otherwise ignores them, as they are likely derived fields.
  • Superclass fields will be compared

If both lhs and rhs are null, they are considered equal.


Parameters:
  lhs - left-hand object
Parameters:
  rhs - right-hand object
Parameters:
  excludeFields - array of fields to exclude a negative integer, zero, or a positive integer as lhsis less than, equal to, or greater than rhs
throws:
  NullPointerException - if either lhs or rhs(but not both) is null
throws:
  ClassCastException - if rhs is not assignment-compatiblewith lhs
since:
   2.2



reflectionCompare
public static int reflectionCompare(Object lhs, Object rhs, boolean compareTransients, Class reflectUpToClass)(Code)

Compares two Objects via reflection.

Fields can be private, thus AccessibleObject.setAccessible is used to bypass normal access control checks. This will fail under a security manager unless the appropriate permissions are set.

  • Static fields will not be compared
  • If the compareTransients is true, compares transient members. Otherwise ignores them, as they are likely derived fields.
  • Compares superclass fields up to and including reflectUpToClass. If reflectUpToClass is null, compares all superclass fields.

If both lhs and rhs are null, they are considered equal.


Parameters:
  lhs - left-hand object
Parameters:
  rhs - right-hand object
Parameters:
  compareTransients - whether to compare transient fields
Parameters:
  reflectUpToClass - last superclass for which fields are compared a negative integer, zero, or a positive integer as lhsis less than, equal to, or greater than rhs
throws:
  NullPointerException - if either lhs or rhs(but not both) is null
throws:
  ClassCastException - if rhs is not assignment-compatiblewith lhs
since:
   2.0



reflectionCompare
public static int reflectionCompare(Object lhs, Object rhs, boolean compareTransients, Class reflectUpToClass, String[] excludeFields)(Code)

Compares two Objects via reflection.

Fields can be private, thus AccessibleObject.setAccessible is used to bypass normal access control checks. This will fail under a security manager unless the appropriate permissions are set.

  • Static fields will not be compared
  • If the compareTransients is true, compares transient members. Otherwise ignores them, as they are likely derived fields.
  • Compares superclass fields up to and including reflectUpToClass. If reflectUpToClass is null, compares all superclass fields.

If both lhs and rhs are null, they are considered equal.


Parameters:
  lhs - left-hand object
Parameters:
  rhs - right-hand object
Parameters:
  compareTransients - whether to compare transient fields
Parameters:
  reflectUpToClass - last superclass for which fields are compared
Parameters:
  excludeFields - fields to exclude a negative integer, zero, or a positive integer as lhsis less than, equal to, or greater than rhs
throws:
  NullPointerException - if either lhs or rhs(but not both) is null
throws:
  ClassCastException - if rhs is not assignment-compatiblewith lhs
since:
   2.2



toComparison
public int toComparison()(Code)
Returns a negative integer, a positive integer, or zero as the builder has judged the "left-hand" side as less than, greater than, or equal to the "right-hand" side. final comparison result



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.