org.apache.commons.lang.builder
クラス CompareToBuilder

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

public class CompareToBuilder
extends Object

Assists in implementing 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 is a method reflectionCompare that uses reflection to determine the fields to append. Because fields can be private, reflectionCompare uses 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);
 }
 

導入されたバージョン:
1.0
バージョン:
$Id: CompareToBuilder.java,v 1.1.1.1 2004/02/13 10:02:06 hioki Exp $
作成者:
Steve Downey, Stephen Colebourne, Gary Gregory, Pete Gieser
関連項目:
Comparable, Object.equals(Object), Object.hashCode(), EqualsBuilder, HashCodeBuilder

コンストラクタの概要
CompareToBuilder()
          Constructor for CompareToBuilder.
 
メソッドの概要
 CompareToBuilder append(boolean[] lhs, boolean[] rhs)
          Appends to the builder the deep comparison of two boolean arrays.
 CompareToBuilder append(boolean lhs, boolean rhs)
          Appends to the builder the comparison of two booleanss.
 CompareToBuilder append(byte[] lhs, byte[] rhs)
          Appends to the builder the deep comparison of two byte arrays.
 CompareToBuilder append(byte lhs, byte rhs)
          Appends to the builder the comparison of two bytes.
 CompareToBuilder append(char[] lhs, char[] rhs)
          Appends to the builder the deep comparison of two char arrays.
 CompareToBuilder append(char lhs, char rhs)
          Appends to the builder the comparison of two chars.
 CompareToBuilder append(double[] lhs, double[] rhs)
          Appends to the builder the deep comparison of two double arrays.
 CompareToBuilder append(double lhs, double rhs)
          Appends to the builder the comparison of two doubles.
 CompareToBuilder append(float[] lhs, float[] rhs)
          Appends to the builder the deep comparison of two float arrays.
 CompareToBuilder append(float lhs, float rhs)
          Appends to the builder the comparison of two floats.
 CompareToBuilder append(int[] lhs, int[] rhs)
          Appends to the builder the deep comparison of two int arrays.
 CompareToBuilder append(int lhs, int rhs)
          Appends to the builder the comparison of two ints.
 CompareToBuilder append(long[] lhs, long[] rhs)
          Appends to the builder the deep comparison of two long arrays.
 CompareToBuilder append(long lhs, long rhs)
          Appends to the builder the comparison of two longs.
 CompareToBuilder append(Object[] lhs, Object[] rhs)
          Appends to the builder the deep comparison of two Object arrays.
 CompareToBuilder append(Object[] lhs, Object[] rhs, Comparator comparator)
          Appends to the builder the deep comparison of two Object arrays.
 CompareToBuilder append(Object lhs, Object rhs)
          Appends to the builder the comparison of two Objects.
 CompareToBuilder append(Object lhs, Object rhs, Comparator comparator)
          Appends to the builder the comparison of two Objects.
 CompareToBuilder append(short[] lhs, short[] rhs)
          Appends to the builder the deep comparison of two short arrays.
 CompareToBuilder append(short lhs, short rhs)
          Appends to the builder the comparison of two shorts.
 CompareToBuilder appendSuper(int superCompareTo)
          Appends to the builder the compareTo(Object) result of the superclass.
static int reflectionCompare(Object lhs, Object rhs)
          Compares two Objects via reflection.
static int reflectionCompare(Object lhs, Object rhs, boolean compareTransients)
          Compares two Objects via reflection.
static int reflectionCompare(Object lhs, Object rhs, boolean compareTransients, Class reflectUpToClass)
          Compares two Objects via reflection.
 int toComparison()
          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.
 
クラス java.lang.Object から継承したメソッド
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

コンストラクタの詳細

CompareToBuilder

public CompareToBuilder()

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 toComparison() to get the result.

メソッドの詳細

reflectionCompare

public static int reflectionCompare(Object lhs,
                                    Object rhs)

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.

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

パラメータ:
lhs - left-hand object
rhs - right-hand object
戻り値:
a negative integer, zero, or a positive integer as lhs is less than, equal to, or greater than rhs
例外:
NullPointerException - if either (but not both) parameters are null
ClassCastException - if rhs is not assignment-compatible with lhs

reflectionCompare

public static int reflectionCompare(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. This will fail under a security manager unless the appropriate permissions are set.

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

パラメータ:
lhs - left-hand object
rhs - right-hand object
compareTransients - whether to compare transient fields
戻り値:
a negative integer, zero, or a positive integer as lhs is less than, equal to, or greater than rhs
例外:
NullPointerException - if either lhs or rhs (but not both) is null
ClassCastException - if rhs is not assignment-compatible with lhs

reflectionCompare

public static int reflectionCompare(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. This will fail under a security manager unless the appropriate permissions are set.

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

パラメータ:
lhs - left-hand object
rhs - right-hand object
compareTransients - whether to compare transient fields
reflectUpToClass - last superclass for which fields are compared
戻り値:
a negative integer, zero, or a positive integer as lhs is less than, equal to, or greater than rhs
例外:
NullPointerException - if either lhs or rhs (but not both) is null
ClassCastException - if rhs is not assignment-compatible with lhs
導入されたバージョン:
2.0

appendSuper

public CompareToBuilder appendSuper(int superCompareTo)

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

パラメータ:
superCompareTo - result of calling super.compareTo(Object)
戻り値:
this - used to chain append calls
導入されたバージョン:
2.0

append

public CompareToBuilder append(Object lhs,
                               Object rhs)

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.

パラメータ:
lhs - left-hand object
rhs - right-hand object
戻り値:
this - used to chain append calls
例外:
ClassCastException - if rhs is not assignment-compatible with lhs

append

public CompareToBuilder append(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. Otherwise comparator will be used to compare the objects. If comparator is null, lhs must implement Comparable instead.

パラメータ:
lhs - left-hand object
rhs - right-hand object
comparator - Comparator used to compare the objects, null means treat lhs as Comparable
戻り値:
this - used to chain append calls
例外:
ClassCastException - if rhs is not assignment-compatible with lhs
導入されたバージョン:
2.0

append

public CompareToBuilder append(long lhs,
                               long rhs)
Appends to the builder the comparison of two longs.

パラメータ:
lhs - left-hand value
rhs - right-hand value
戻り値:
this - used to chain append calls

append

public CompareToBuilder append(int lhs,
                               int rhs)
Appends to the builder the comparison of two ints.

パラメータ:
lhs - left-hand value
rhs - right-hand value
戻り値:
this - used to chain append calls

append

public CompareToBuilder append(short lhs,
                               short rhs)
Appends to the builder the comparison of two shorts.

パラメータ:
lhs - left-hand value
rhs - right-hand value
戻り値:
this - used to chain append calls

append

public CompareToBuilder append(char lhs,
                               char rhs)
Appends to the builder the comparison of two chars.

パラメータ:
lhs - left-hand value
rhs - right-hand value
戻り値:
this - used to chain append calls

append

public CompareToBuilder append(byte lhs,
                               byte rhs)
Appends to the builder the comparison of two bytes.

パラメータ:
lhs - left-hand value
rhs - right-hand value
戻り値:
this - used to chain append calls

append

public CompareToBuilder append(double lhs,
                               double rhs)

Appends to the builder the comparison of two doubles.

This handles NaNs, Infinties, and -0.0.

It is compatible with the hash code generated by HashCodeBuilder.

パラメータ:
lhs - left-hand value
rhs - right-hand value
戻り値:
this - used to chain append calls

append

public CompareToBuilder append(float lhs,
                               float rhs)

Appends to the builder the comparison of two floats.

This handles NaNs, Infinties, and -0.0.

It is compatible with the hash code generated by HashCodeBuilder.

パラメータ:
lhs - left-hand value
rhs - right-hand value
戻り値:
this - used to chain append calls

append

public CompareToBuilder append(boolean lhs,
                               boolean rhs)
Appends to the builder the comparison of two booleanss.

パラメータ:
lhs - left-hand value
rhs - right-hand value
戻り値:
this - used to chain append calls

append

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

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 append(Object, Object, Comparator)

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

パラメータ:
lhs - left-hand array
rhs - right-hand array
戻り値:
this - used to chain append calls
例外:
ClassCastException - if rhs is not assignment-compatible with lhs

append

public CompareToBuilder append(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 append(Object, Object, Comparator)

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

パラメータ:
lhs - left-hand array
rhs - right-hand array
comparator - Comparator to use to compare the array elements, null means to treat lhs elements as Comparable.
戻り値:
this - used to chain append calls
例外:
ClassCastException - if rhs is not assignment-compatible with lhs
導入されたバージョン:
2.0

append

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

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 append(long, long)

パラメータ:
lhs - left-hand array
rhs - right-hand array
戻り値:
this - used to chain append calls

append

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

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 append(int, int)

パラメータ:
lhs - left-hand array
rhs - right-hand array
戻り値:
this - used to chain append calls

append

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

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 append(short, short)

パラメータ:
lhs - left-hand array
rhs - right-hand array
戻り値:
this - used to chain append calls

append

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

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 append(char, char)

パラメータ:
lhs - left-hand array
rhs - right-hand array
戻り値:
this - used to chain append calls

append

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

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 append(byte, byte)

パラメータ:
lhs - left-hand array
rhs - right-hand array
戻り値:
this - used to chain append calls

append

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

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 append(double, double)

パラメータ:
lhs - left-hand array
rhs - right-hand array
戻り値:
this - used to chain append calls

append

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

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 append(float, float)

パラメータ:
lhs - left-hand array
rhs - right-hand array
戻り値:
this - used to chain append calls

append

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

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 append(boolean, boolean)

パラメータ:
lhs - left-hand array
rhs - right-hand array
戻り値:
this - used to chain append calls

toComparison

public int toComparison()
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


このドキュメントは、Ja-Jakartaにより訳されました。 コメントがある場合は report@jajakarta.orgまでお願いします。
Translated into Japanese by jajakarta.org. The original page is here.
Copyright (c) 2002-2003 - Apache Software Foundation