类 Objects
java.lang.Object
com.alibaba.nacos.common.utils.Objects
Objects utils.
- 作者:
- liaochuntao
-
构造器概要
构造器 -
方法概要
修饰符和类型方法说明static <T> intcompare(T a, T b, Comparator<? super T> c)Returns 0 if the arguments are identical andc.compare(a, b)otherwise.static booleanReturnstrueif the arguments are equal to each other andfalseotherwise.static intGenerates a hash code for a sequence of input values.static intReturns the hash code of a non-nullargument and 0 for anullargument.static booleanReturnstrueif the provided reference isnullotherwise returnsfalse.static booleanReturnstrueif the provided reference is non-nullotherwise returnsfalse.static <T> TrequireNonNull(T obj)Checks that the specified object reference is notnull.static <T> TrequireNonNull(T obj, String message)Checks that the specified object reference is notnulland throws a customizedNullPointerExceptionif it is.static <T> TrequireNonNullElse(T obj, T defaultObj)Returns the first argument if it is non-nulland otherwise returns the non-nullsecond argument.static StringReturns the result of callingtoStringfor a non-nullargument and"null"for anullargument.static StringReturns the result of callingtoStringon the first argument if the first argument is notnulland returns the second argument otherwise.
-
构造器详细资料
-
Objects
public Objects()
-
-
方法详细资料
-
equals
Returnstrueif the arguments are equal to each other andfalseotherwise. Consequently, if both arguments arenull,trueis returned and if exactly one argument isnull,falseis returned. Otherwise, equality is determined by using theequalsmethod of the first argument.- 参数:
a- an objectb- an object to be compared withafor equality- 返回:
trueif the arguments are equal to each other andfalseotherwise- 另请参阅:
Object.equals(Object)
-
hashCode
Returns the hash code of a non-nullargument and 0 for anullargument.- 参数:
o- an object- 返回:
- the hash code of a non-
nullargument and 0 for anullargument - 另请参阅:
Object.hashCode()
-
hash
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 callingArrays.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, andz, one could write:
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@Override public int hashCode() { return Objects.hash(x, y, z); }hashCode(Object).- 参数:
values- the values to be hashed- 返回:
- a hash value of the sequence of input values
- 另请参阅:
Arrays.hashCode(Object[]),List.hashCode()
-
toString
Returns the result of callingtoStringfor a non-nullargument and"null"for anullargument.- 参数:
o- an object- 返回:
- the result of calling
toStringfor a non-nullargument and"null"for anullargument - 另请参阅:
Object.toString(),String.valueOf(Object)
-
toString
Returns the result of callingtoStringon the first argument if the first argument is notnulland returns the second argument otherwise.- 参数:
o- an objectnullDefault- string to return if the first argument isnull- 返回:
- the result of calling
toStringon the first argument if it is notnulland the second argument otherwise.
-
compare
Returns 0 if the arguments are identical andc.compare(a, b)otherwise. Consequently, if both arguments arenull0 is returned.Note that if one of the arguments is
null, aNullPointerExceptionmay or may not be thrown depending on what ordering policy, if any, theComparatorchooses to have fornullvalues.- 类型参数:
T- the type of the objects being compared- 参数:
a- an objectb- an object to be compared withac- theComparatorto compare the first two arguments- 返回:
- 0 if the arguments are identical and
c.compare(a, b)otherwise. - 另请参阅:
Comparable,Comparator
-
requireNonNull
public static <T> T requireNonNull(T obj)Checks that the specified object reference is notnull. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(Bar bar) { this.bar = Objects.requireNonNull(bar); }- 类型参数:
T- the type of the reference- 参数:
obj- the object reference to check for nullity- 返回:
objif notnull- 抛出:
NullPointerException- ifobjisnull
-
requireNonNull
Checks that the specified object reference is notnulland throws a customizedNullPointerExceptionif 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.requireNonNull(bar, "bar must not be null"); this.baz = Objects.requireNonNull(baz, "baz must not be null"); }- 类型参数:
T- the type of the reference- 参数:
obj- the object reference to check for nullitymessage- detail message to be used in the event that aNullPointerExceptionis thrown- 返回:
objif notnull- 抛出:
NullPointerException- ifobjisnull
-
isNull
Returnstrueif the provided reference isnullotherwise returnsfalse.- 参数:
obj- a reference to be checked againstnull- 返回:
trueif the provided reference isnullotherwisefalse- 从以下版本开始:
- 1.8
-
nonNull
Returnstrueif the provided reference is non-nullotherwise returnsfalse.- 参数:
obj- a reference to be checked againstnull- 返回:
trueif the provided reference is non-nullotherwisefalse- 从以下版本开始:
- 1.8
-
requireNonNullElse
public static <T> T requireNonNullElse(T obj, T defaultObj)Returns the first argument if it is non-nulland otherwise returns the non-nullsecond argument.- 类型参数:
T- the type of the reference- 参数:
obj- an objectdefaultObj- a non-nullobject to return if the first argument isnull- 返回:
- the first argument if it is non-
nulland otherwise the second argument if it is non-null - 抛出:
NullPointerException- if bothobjis null anddefaultObjisnull- 从以下版本开始:
- 9
-