Java7のObjectsクラス

Java7 には Objects クラスが追加される予定になっています*1
Arrays や Collections みたいな、ユーテリティクラスですね。
ざっと調べてみました (といってもAPIを引用&翻訳*2しただけですが・・・)。

http://download.java.net/jdk7/docs/api/java/util/Objects.html

static boolean
equals(Object a, Object b)

Returns true if the arguments are equal to each other and false otherwise. Consequently, if both arguments are null, true is returned and if exactly one argument is null, false is returned. Otherwise, equality is determined by using the equals method of the first argument.
両方の引数が同じならば true, そうでなければ false を返します。そのため、両方の引数が null ならば true を返し、どちらかの引数が null ならば false を返します。そうでなければ、同じかどうかは最初の引数の equals メソッドにより決まります。

static boolean
deepEquals(Object a, Object b)

Returns true if the arguments are deeply equal to each other and false otherwise. Two null values are deeply equal. If both arguments are arrays, the algorithm in Arrays.deepEquals is used to determine equality. Otherwise, equality is determined by using the equals method of the first argument.
引数が、もう片方と「深層で等価」な場合は true, そうでなければ false を返します。両方の値が null なら、「深層で等価」と判断します。もし両方の引数が配列なら、Arrays.deepEquals のアルゴリズムを使用して等価かどうかを判断します。そうでなければ、等価かどうかは第一引数の equals メソッドを使用して判定します。

static int
hashCode(Object o)

Returns the hash code of a non-null argument and 0 for a null argument
null でなければオブジェクトのハッシュコードを、null ならば 0を返します。

static int
hash(Object... values)

Generates a hash code for a sequence of input values. The hash code is generated as if all the input values were placed into an array, and that array were hashed by calling Arrays.hashCode(Object[]).

This method is useful for implementing Object.hashCode() on objects containing multiple fields. For example, if an object that has three fields, x, y, and z, one could write:

     @Override public int hashCode() {
         return Objects.hash(x, y, z);
     }

Warning: When a single object reference is supplied, the returned value does not equal the hash code of that object reference. This value can be computed by calling hashCode(Object).
引数で与えられた値をもとにハッシュコードを生成します。ハッシュコードは、引数で与えられた値を配列に配置し、その配列を元に Arrays.hashCode(Object[]) を呼び出して生成します。

このメソッドは、複数のフィールドを含むオブジェクトの Object.hashCode)を実装する場合に便利です。たとえば、3つのフィールドを持つオブジェクトは、x、y、zは、このように書くことができます:
(中略)
警告:単独のオブジェクトが与えられた場合、返す値はオブジェクトのハッシュコードと等価ではありません。この値は、hashCode(Object) を呼び出すことによって計算することができます。

static String
toString(Object o)

Returns the result of calling toString for a non-null argument and "null" for a null argument
null でなければ toString を呼んだ結果を、null ならば "null" を返します。

static String
toString(Object o, String nullDefault)

Returns the result of calling toString on the first argument if the first argument is not null and returns the second argument otherwise.
第一引数が null でなければ第一引数の toString を呼んだ結果を、そうでなければ第二引数を返します。

static int
compare(T a, T b, Comparator c)

Returns 0 if the arguments are identical and c.compare(a, b) otherwise. Consequently, if both arguments are null 0 is returned.

Note that if one of the arguments is null, a NullPointerException may or may not be thrown depending on what ordering policy, if any, the Comparator chooses to have for null values.
引数が同じものであれば 0 を、そうでなければ c.compare(a, b) を返します。
備考:いずれかの引数が null であれば、ソートポリシーによっては NullPointerException をスローするかもしれないし、しないかもしれない。もしかすると、コンパレータが null の扱いを選ぶかもしれない。

static T
nonNull(T obj)

Checks that the specified object reference is not null. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:

     public Foo(Bar bar) {
         this.bar = Objects.nonNull(bar);
     }

指定されたオブジェクトが null でないことを確認します。このメソッドは、主に以下に示すようなメソッドやコンストラクタのパラメータの検証を行うために設計されています:
(略)

static T
nonNull(T obj, String message)

Checks that the specified object reference is not null and throws a customized NullPointerException if it is. This method is designed primarily for doing parameter validation in methods and constructors with multiple parameters, as demonstrated below:

     public Foo(Bar bar, Baz baz) {
         this.bar = Objects.nonNull(bar, "bar must not be null");
         this.baz = Objects.nonNull(baz, "baz must not be null");
     }

指定されたオブジェクトが null でないことを確認し、null であればカスタマイズした NullPointerException をスローします。このメソッドは、主に以下に示すようなメソッドやコンストラクタの複数のパラメータの検証を行うために設計されています:
(略)


使えそう!

hash はコーディングを大幅に省略できるようになりそうです。
それに、結構気を使うところ(ハッシュコードはバグっても分かりづらい)なので、この実装で済むのはとてもありがたいです。
equals や toString は、Java7で見送られた Null-safe タイプ*3の代わりになりそうですね。
この程度であれば、保守的な環境*4でも使わせてもらえそうです。


ちなみに、最初にこのクラスを知った時は Object クラスと間違えそうと思いました。
ですが、改めて考えれば Object に static メソッドはないので、間違えることはないですね・・・。
(現に、ソースコード上では Collection と Collections を間違えないです)

*1:この記事の内容は、DRAFT b118 の内容をもとに書いています。

*2:いつものように、だいぶGoogle翻訳に頼っています。

*3:NULLを許容する演算子(?.)。「obj?.toString()」と書けば、「obj != null ? obj.toString() : null」と等価に動作する。

*4:自分は今のところないのですが、Javaの新しい文法をコーディング規約で禁止されるというのを聞いたことがあります。