java基础-java.util下的Objects类
Objects 类包含了常用的操作object的方法。这些方法包括计算hash code、返回string 、比较对象等方法。
package java.util;
/**
* This class consists of {@code static} utility methods for operating
* on objects. These utilities include {@code null}-safe or {@code
* null}-tolerant methods for computing the hash code of an object,
* returning a string for an object, and comparing two objects.
*
* @since 1.7
*/
//不可改变类
public final class Objects {
private Objects() {
throw new AssertionError("No java.util.Objects instances for you!");
}
//如果ab都为null返回true,任意一个为null ,返回false。
//如果其中一个非空,则比较引用是否相同
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}
//深度比较
public static boolean deepEquals(Object a, Object b) {
if (a == b)//如果
return true;
else if (a == null || b == null)
return false;
else
return Arrays.deepEquals0(a, b);//进行深度比较
}
//返回该object的hashcode值。null的话返回0
public static int hashCode(Object o) {
return o != null ? o.hashCode() : 0;
}
/**
* 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 {@link
* Arrays#hashCode(Object[])}.
*
* <p>This method is useful for implementing {@link
* Object#hashCode()} on objects containing multiple fields. For
* example, if an object that has three fields, {@code x}, {@code
* y}, and {@code z}, one could write:
*
* <blockquote><pre>
* @Override public int hashCode() {
* return Objects.hash(x, y, z);
* }
* </pre></blockquote>
*
* <b>Warning: When a single object reference is supplied, the returned
* value does not equal the hash code of that object reference.</b> This
* value can be computed by calling {@link #hashCode(Object)}.
*
* @param values the values to be hashed
* @return a hash value of the sequence of input values
* @see Arrays#hashCode(Object[])
* @see List#hashCode
*/
//
public static int hash(Object... values) {
return Arrays.hashCode(values);
}
//返回非空的string
public static String toString(Object o) {
return String.valueOf(o);
}
//返回该object的字符串或者给定的默认空值
public static String toString(Object o, String nullDefault) {
return (o != null) ? o.toString() : nullDefault;
}
//比较
public static <T> int compare(T a, T b, Comparator<? super T> c) {
return (a == b) ? 0 : c.compare(a, b);
}
/**
* Checks that the specified(规定的) object reference is not {@code null}. This
* method is designed primarily(主要) for doing parameter validation in methods
* and constructors, as demonstrated below(证明如下):
* <blockquote><pre>
* public Foo(Bar bar) {
* this.bar = Objects.requireNonNull(bar);
* }
* </pre></blockquote>
*
* @param obj the object reference to check for nullity
* @param <T> the type of the reference
* @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@code null}
*/
public static <T> T requireNonNull(T obj) {
if (obj == null)
throw new NullPointerException();
return obj;
}
/**
* Checks that the specified object reference is not {@code null} and
* throws a customized {@link NullPointerException} if it is. This method
* is designed primarily for doing parameter validation in methods and
* constructors with multiple parameters, as demonstrated below:
* <blockquote><pre>
* public Foo(Bar bar, Baz baz) {
* this.bar = Objects.requireNonNull(bar, "bar must not be null");
* this.baz = Objects.requireNonNull(baz, "baz must not be null");
* }
* </pre></blockquote>
*
* @param obj the object reference to check for nullity
* @param message detail message to be used in the event that a {@code
* NullPointerException} is thrown
* @param <T> the type of the reference
* @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@code null}
*/
//构造方法中使用,验证非空,并提供提示信息
public static <T> T requireNonNull(T obj, String message) {
if (obj == null)
throw new NullPointerException(message);
return obj;
}
}