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

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

public class CompareToBuilder
extends Object

CompareTo を生成する処理です。

CompareTo generation routines.

このクラスはより良い compareTo() メソッドをクラスに実装するためのメソッドを提供します。 これは EqualsBuilder と HashCodeBuilder によって実装された equalshashcode と調和的です。

This class provides methods to build a good compareTo() method for any class. It is consistent with the equals and hashcode built with EqualsBuilder and HashCodeBuilder.

equals メソッドで等しいと判定された2つのオブジェクトは、compareTo でも等しいと判定されるべきです。

Two object that compare equal using equals should compare equals using compareTo.

全ての関連のあるフィールドを比較を行う際の計算に含めるべきです。 取得されたフィールドは無視されるかもしれません(訳注 毎回登録した全てのフィールドを計算するわけではないと言う意味?)。 使用されるフィールドとその順番は compareToequals で同じであるべきです。

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 and equals.

一般的な使用方法を以下に示します:

Typical use for the code is as follows:
  public int comapareTo(Object o) {
    MyClass rhs = (MyClass) o;
    return new CompareToBuilder()
                 .append(field1, rhs.field1)
                 .append(field2, rhs.field2)
                 .appendb(field3, rhs.field3)
                 .toComparison();
  }
 

もう一つの方法として、リフレクションを使用してフィールドを比較するメソッドがあります。 普通、対象となるフィールドは private であるため、この reflectionCompare メソッドは フィールドのアクセス制限を変更するために Field.setAccessible を使用します。 これは適切なパーミッションが設定されていない限り、セキュリティマネージャが稼動している状態では行うことができません。 また、この方法は明らかに低速です。

Alternatively, there is a method that uses reflection to determine the fields to test. Because these fields are usually private, the method, reflectionCompare, uses Field.setAccessible to change the visibility of the fields. This will fail under a security manager, unless the appropriate permissions are set. It is also slower than testing explicitly.

このメソッドの一般的な使用方法は以下の様になります:

A typical invocation for this method would look like:
 public int compareTo(Object o) {
   return CompareToBuilder.reflectionCompare(this, obj);
 }
 

バージョン:
$Id: CompareToBuilder.java,v 1.1.1.1 2004/02/13 10:02:04 hioki Exp $
作成者:
Steve Downey, Stephen Colebourne
翻訳者:
日置 聡
翻訳状況:
初稿(校正者募集中)
翻訳更新日:
2003/08/18

フィールドの概要
private  int comparison
          テストされたフィールドが等しいかどうか。
 
コンストラクタの概要
CompareToBuilder()
          CompareToBuilder のコンストラクタです。
 
メソッドの概要
 CompareToBuilder append(boolean[] lhs, boolean[] rhs)
          boolean の配列の長さと含まれる全ての値を比較します。
 CompareToBuilder append(boolean lhs, boolean rhs)
          2つの boolean の値の大きさ(<、>、==)を評価します。
 CompareToBuilder append(byte[] lhs, byte[] rhs)
          byte の配列の長さと含まれる全ての値を比較します。
 CompareToBuilder append(byte lhs, byte rhs)
          2つの byte の値の大きさ(<、>、==)を評価します。
 CompareToBuilder append(char[] lhs, char[] rhs)
          char の配列の長さと含まれる全ての値を比較します。
 CompareToBuilder append(char lhs, char rhs)
          2つの char の値の大きさ(<、>、==)を評価します。
 CompareToBuilder append(double[] lhs, double[] rhs)
          double の配列の長さと含まれる全ての値を比較します。
 CompareToBuilder append(double lhs, double rhs)
          2つの double の値の大きさ(<、>、==)を評価します。
 CompareToBuilder append(float[] lhs, float[] rhs)
          float の配列の長さと含まれる全ての値を比較します。
 CompareToBuilder append(float lhs, float rhs)
          2つの float の値の大きさ(<、>、==)を評価します。
 CompareToBuilder append(int[] lhs, int[] rhs)
          int の配列の長さと含まれる全ての値を比較します。
 CompareToBuilder append(int lhs, int rhs)
          2つの int の値の大きさ(<、>、==)を評価します。
 CompareToBuilder append(long[] lhs, long[] rhs)
          long の配列の長さと含まれる全ての値を比較します。
 CompareToBuilder append(long lhs, long rhs)
          2つの long の値の大きさ(<、>、==)を評価します。
 CompareToBuilder append(Object[] lhs, Object[] rhs)
          2つのオブジェクトの配列の内容を掘り下げて比較を行います。
 CompareToBuilder append(Object lhs, Object rhs)
          2つの Object が等しいかどうかを各 compareTo メソッドを使用して評価します。
 CompareToBuilder append(short[] lhs, short[] rhs)
          short の配列の長さと含まれる全ての値を比較します。
 CompareToBuilder append(short lhs, short rhs)
          2つの short の値の大きさ(<、>、==)を評価します。
static int reflectionCompare(Object lhs, Object rhs)
          このメソッドは指定された2つのオブジェクトを測定するためにリフレクションを使用します。
static int reflectionCompare(Object lhs, Object rhs, boolean testTransients)
          このメソッドは指定された2つのオブジェクトを測定するためにリフレクションを使用します。
 int toComparison()
          より小さい場合にはマイナスのint値、大きい場合にはプラスのint値、等しい場合には0を返します。
 
クラス java.lang.Object から継承したメソッド
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

フィールドの詳細

comparison

private int comparison
テストされたフィールドが等しいかどうか。
If the fields tested are equals.

コンストラクタの詳細

CompareToBuilder

public CompareToBuilder()
CompareToBuilder のコンストラクタです。 初期状態では(2つの)オブジェクトは等しいとみなされます。
Constructor for CompareToBuilder. Starts off assuming that the objects are equal.

関連項目:
Object.Object()
メソッドの詳細

reflectionCompare

public static int reflectionCompare(Object lhs,
                                    Object rhs)
このメソッドは指定された2つのオブジェクトを測定するためにリフレクションを使用します。
This method uses reflection to determine the ordering between two objects.

このメソッドは private フィールドの値を取得するために Field.setAccessible を使用します。 これはセキュリティマネージャが稼動していて、適切なパーミッションが設定されていない場合、 セキュリティ例外が投げられることを意味します。 このメソッドは明らかに非効率です。 transient メンバーは、おそらくフィールドの値から取得されたものでオブジェクトの値とはならないと考え、 処理の対象としません。 static フィールドは処理の対象にはなりません。

It uses Field.setAccessible to gain access to private fields. This means that it will throw a security exception if run under a security manger, if the permissions are not set up. It is also not as efficient as testing explicitly. Transient members will be not be tested, as they are likely derived fields, and not part of the value of the object. Static fields will not be tested.

パラメータ:
lhs - 左側
Left Hand Side
rhs - 右側
Right Hand Side
戻り値:
このオブジェクトの方が小さい場合マイナスの値、等しい場合0、大きい場合プラスの値
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
例外:
NullPointerException - どちらか片方の引数が null の場合
if either (but not both) parameter is null
ClassCastException - 指定されたオブジェクト型が自身の比較を行えない場合
if the specified object's type prevents it from being compared to this Object.

reflectionCompare

public static int reflectionCompare(Object lhs,
                                    Object rhs,
                                    boolean testTransients)
このメソッドは指定された2つのオブジェクトを測定するためにリフレクションを使用します。
This method uses reflection to determine if the two object are equal.

このメソッドは private フィールドの値を取得するために Field.setAccessible を使用します。 これはセキュリティマネージャが稼動していて、適切なパーミッションが設定されていない場合、 セキュリティ例外が投げられることを意味します。 このメソッドは明らかに非効率です。 testTransients 引数に true が設定された場合 transient メンバーを処理の対象とし、 false が設定された場合にはフィールドの値から取得されたものでオブジェクトの値とはならないとみなし、 transient メンバーを無視します。 static フィールドは処理の対象にはなりません。

It uses Field.setAccessible to gain access to private fields. This means that it will throw a security exception if run under a security manger, if the permissions are not set up. It is also not as efficient as testing explicitly. If the TestTransients parameter is set to true, transient members will be tested, otherwise they are ignored, as they are likely derived fields, and not part of the value of the object. Static fields will not be tested.

パラメータ:
lhs - 左側
Left Hand Side
rhs - 右側
Right Hand Side
testTransients - transient フィールドを含めるかどうか
whether to include transient fields
戻り値:
このオブジェクトの方が小さい場合マイナスの値、等しい場合0、大きい場合プラスの値
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
例外:
NullPointerException - どちらか片方の引数が null の場合
if either (but not both) parameter is null
ClassCastException - 指定されたオブジェクト型が自身の比較を行えない場合
if the specified object's type prevents it from being compared to this Object.

append

public CompareToBuilder append(Object lhs,
                               Object rhs)
2つの Object が等しいかどうかを各 compareTo メソッドを使用して評価します。 もし渡されたオブジェクトが配列だった場合には該当する比較を行います。
Test if two Objects are equal using either the compareTo method, or native comparison if the Objects are actually arrays.

オブジェクトは Comparable である必要があります。 そうでなかった場合、このメソッドは ClassCastException を投げます。

The objects must be Comparable. If they are not, the method will throw a ClassCastException.

パラメータ:
lhs - 左側
Left Hand Side
rhs - 右側
Right Hand Side
戻り値:
連結してコールするために使用される CompareToBuilder
CompareToBuilder - used to chain calls.
例外:
NullPointerException - どちらか片方の引数が null の場合
if either (but not both) parameter is null
ClassCastException - 指定されたオブジェクト型が自身の比較を行えない場合
if the specified object's type prevents it from being compared to this Object.

append

public CompareToBuilder append(long lhs,
                               long rhs)
2つの long の値の大きさ(<、>、==)を評価します。
Test if two longs are <, > or ==.

パラメータ:
lhs - 左側
Left Hand Side
rhs - 右側
Right Hand Side
戻り値:
連結してコールするために使用される CompareToBuilder
CompareToBuilder - used to chain calls.

append

public CompareToBuilder append(int lhs,
                               int rhs)
2つの int の値の大きさ(<、>、==)を評価します。
Test if two ints are <, > or ==.

パラメータ:
lhs - 左側
Left Hand Side
rhs - 右側
Right Hand Side
戻り値:
連結してコールするために使用される CompareToBuilder
CompareToBuilder - used to chain calls.

append

public CompareToBuilder append(short lhs,
                               short rhs)
2つの short の値の大きさ(<、>、==)を評価します。
Test if two shorts are <, > or ==.

パラメータ:
lhs - 左側
Left Hand Side
rhs - 右側
Right Hand Side
戻り値:
連結してコールするために使用される CompareToBuilder
CompareToBuilder - used to chain calls.

append

public CompareToBuilder append(char lhs,
                               char rhs)
2つの char の値の大きさ(<、>、==)を評価します。
Test if two chars are <, > or ==.

パラメータ:
lhs - 左側
Left Hand Side
rhs - 右側
Right Hand Side
戻り値:
連結してコールするために使用される CompareToBuilder
CompareToBuilder - used to chain calls.

append

public CompareToBuilder append(byte lhs,
                               byte rhs)
2つの byte の値の大きさ(<、>、==)を評価します。
Test if two bytes are <, > or ==.

パラメータ:
lhs - 左側
Left Hand Side
rhs - 右側
Right Hand Side
戻り値:
連結してコールするために使用される CompareToBuilder
CompareToBuilder - used to chain calls.

append

public CompareToBuilder append(double lhs,
                               double rhs)
2つの double の値の大きさ(<、>、==)を評価します。 これは NaN、無限大、-0.0 をハンドルします。 これは HashCodeBuilder によって生成されたハッシュコードと互換性を持ちます。
Test if two doubles are <, > or ==. This handles NaNs, Infinties, and -0.0. It is compatible with the hash code generated by HashCodeBuilder.

パラメータ:
lhs - 左側
Left Hand Side
rhs - 右側
Right Hand Side
戻り値:
連結してコールするために使用される CompareToBuilder
CompareToBuilder - used to chain calls.

append

public CompareToBuilder append(float lhs,
                               float rhs)
2つの float の値の大きさ(<、>、==)を評価します。 これは NaN、無限大、-0.0 をハンドルします。 これは HashCodeBuilder によって生成されたハッシュコードと互換性を持ちます。
Test if two doubles are <, > or ==. This handles NaNs, Infinties, and -0.0. It is compatible with the hash code generated by HashCodeBuilder.

パラメータ:
lhs - 左側
Left Hand Side
rhs - 右側
Right Hand Side
戻り値:
連結してコールするために使用される CompareToBuilder
CompareToBuilder - used to chain calls.

append

public CompareToBuilder append(boolean lhs,
                               boolean rhs)
2つの boolean の値の大きさ(<、>、==)を評価します。
Test if two booleans are <, > or ==.

パラメータ:
lhs - 左側
Left Hand Side
rhs - 右側
Right Hand Side
戻り値:
連結してコールするために使用される CompareToBuilder
CompareToBuilder - used to chain calls.

append

public CompareToBuilder append(Object[] lhs,
                               Object[] rhs)
2つのオブジェクトの配列の内容を掘り下げて比較を行います。 また、このメソッドは多次元配列、不完全な配列(訳注 ragged を無理やり訳しました)、複数の型の配列の比較の際にも呼ばれます。 もし2つの配列の長さが違って短い方の配列の内容が長い方の配列の(その長さまでの)内容と等しい場合、 長い方の配列が大きいと判断されます。 これは辞書または語彙の順番付けの方法です。
Performs a deep comparison of two object arrays. This also will be called for the top level of multi-dimensional, ragged, and multi-typed arrays. If two arrays are of different lengths, and all elements of the shorter array are equal to the elements in the longer array, the longer array is the greater. This is dictionary, or lexical, ordering.

パラメータ:
lhs - 左側
Left Hand Side
rhs - 右側
Right Hand Side
戻り値:
連結してコールするために使用される CompareToBuilder
CompareToBuilder - used to chain calls.
例外:
NullPointerException - どちらか片方の引数が null の場合
if either (but not both) parameter is null
ClassCastException - 指定され比較対照となるオブジェクトの型が異なる場合
if the specified object's type prevents it from being compared to this Object.

append

public CompareToBuilder append(long[] lhs,
                               long[] rhs)
long の配列の長さと含まれる全ての値を比較します。 このメソッドは append(long, long) を利用します。
Deep comparison of array of long Length and all values are compared. The method append(long, long) is used.

パラメータ:
lhs - 左側
Left Hand Side
rhs - 右側
Right Hand Side
戻り値:
連結してコールするために使用される CompareToBuilder
CompareToBuilder - used to chain calls.
例外:
NullPointerException - どちらか片方の引数が null の場合
if either (but not both) parameter is null

append

public CompareToBuilder append(int[] lhs,
                               int[] rhs)
int の配列の長さと含まれる全ての値を比較します。 このメソッドは append(int, int) を利用します。
Deep comparison of array of int Length and all values are compared. The method append(int, int) is used.

パラメータ:
lhs - 左側
Left Hand Side
rhs - 右側
Right Hand Side
戻り値:
連結してコールするために使用される CompareToBuilder
CompareToBuilder - used to chain calls.
例外:
NullPointerException - どちらか片方の引数が null の場合
if either (but not both) parameter is null

append

public CompareToBuilder append(short[] lhs,
                               short[] rhs)
short の配列の長さと含まれる全ての値を比較します。 このメソッドは append(short, short) を利用します。
Deep comparison of array of short Length and all values are compared. The method append(short, short) is used.

パラメータ:
lhs - 左側
Left Hand Side
rhs - 右側
Right Hand Side
戻り値:
連結してコールするために使用される CompareToBuilder
CompareToBuilder - used to chain calls.
例外:
NullPointerException - どちらか片方の引数が null の場合
if either (but not both) parameter is null

append

public CompareToBuilder append(char[] lhs,
                               char[] rhs)
char の配列の長さと含まれる全ての値を比較します。 このメソッドは append(char, char) を利用します。
Deep comparison of array of char Length and all values are compared. The method append(char, char) is used.

パラメータ:
lhs - 左側
Left Hand Side
rhs - 右側
Right Hand Side
戻り値:
連結してコールするために使用される CompareToBuilder
CompareToBuilder - used to chain calls.
例外:
NullPointerException - どちらか片方の引数が null の場合
if either (but not both) parameter is null

append

public CompareToBuilder append(byte[] lhs,
                               byte[] rhs)
byte の配列の長さと含まれる全ての値を比較します。 このメソッドは append(byte, byte) を利用します。
Deep comparison of array of byte Length and all values are compared. The method append(byte, byte) is used.

パラメータ:
lhs - 左側
Left Hand Side
rhs - 右側
Right Hand Side
戻り値:
連結してコールするために使用される CompareToBuilder
CompareToBuilder - used to chain calls.
例外:
NullPointerException - どちらか片方の引数が null の場合
if either (but not both) parameter is null

append

public CompareToBuilder append(double[] lhs,
                               double[] rhs)
double の配列の長さと含まれる全ての値を比較します。 このメソッドは append(double, double) を利用します。
Deep comparison of array of double Length and all values are compared. The method append(double, double) is used.

パラメータ:
lhs - 左側
Left Hand Side
rhs - 右側
Right Hand Side
戻り値:
連結してコールするために使用される CompareToBuilder
CompareToBuilder - used to chain calls.
例外:
NullPointerException - どちらか片方の引数が null の場合
if either (but not both) parameter is null

append

public CompareToBuilder append(float[] lhs,
                               float[] rhs)
float の配列の長さと含まれる全ての値を比較します。 このメソッドは append(float, float) を利用します。
Deep comparison of array of float Length and all values are compared. The method append(float, float) is used.

パラメータ:
lhs - 左側
Left Hand Side
rhs - 右側
Right Hand Side
戻り値:
連結してコールするために使用される CompareToBuilder
CompareToBuilder - used to chain calls.
例外:
NullPointerException - どちらか片方の引数が null の場合
if either (but not both) parameter is null

append

public CompareToBuilder append(boolean[] lhs,
                               boolean[] rhs)
boolean の配列の長さと含まれる全ての値を比較します。 このメソッドは append(boolean, boolean) を利用します。
Deep comparison of array of boolean Length and all values are compared. The method append(boolean, boolean) is used.

パラメータ:
lhs - 左側
Left Hand Side
rhs - 右側
Right Hand Side
戻り値:
連結してコールするために使用される CompareToBuilder
CompareToBuilder - used to chain calls.
例外:
NullPointerException - どちらか片方の引数が null の場合
if either (but not both) parameter is null

toComparison

public int toComparison()
より小さい場合にはマイナスのint値、大きい場合にはプラスのint値、等しい場合には0を返します。
Return a negative integer if the object is less than, a positive integer if the object is greater than, or 0 if the object is equal.

戻り値:
このオブジェクトの方が小さい場合マイナスの値、等しい場合0、大きい場合プラスの値
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.


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