类 Objects

java.lang.Object
com.alibaba.nacos.common.utils.Objects

public class Objects extends Object
Objects utils.
作者:
liaochuntao
  • 构造器概要

    构造器
    构造器
    说明
     
  • 方法概要

    修饰符和类型
    方法
    说明
    static <T> int
    compare​(T a, T b, Comparator<? super T> c)
    Returns 0 if the arguments are identical and c.compare(a, b) otherwise.
    static boolean
    equals​(Object a, Object b)
    Returns true if the arguments are equal to each other and false otherwise.
    static int
    hash​(Object... values)
    Generates a hash code for a sequence of input values.
    static int
    Returns the hash code of a non-null argument and 0 for a null argument.
    static boolean
    isNull​(Object obj)
    Returns true if the provided reference is null otherwise returns false.
    static boolean
    nonNull​(Object obj)
    Returns true if the provided reference is non-null otherwise returns false.
    static <T> T
    requireNonNull​(T obj)
    Checks that the specified object reference is not null.
    static <T> T
    requireNonNull​(T obj, String message)
    Checks that the specified object reference is not null and throws a customized NullPointerException if it is.
    static <T> T
    requireNonNullElse​(T obj, T defaultObj)
    Returns the first argument if it is non-null and otherwise returns the non-null second argument.
    static String
    Returns the result of calling toString for a non-null argument and "null" for a null argument.
    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.

    从类继承的方法 java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 构造器详细资料

    • Objects

      public Objects()
  • 方法详细资料

    • equals

      public 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.
      参数:
      a - an object
      b - an object to be compared with a for equality
      返回:
      true if the arguments are equal to each other and false otherwise
      另请参阅:
      Object.equals(Object)
    • hashCode

      public static int hashCode(Object o)
      Returns the hash code of a non-null argument and 0 for a null argument.
      参数:
      o - an object
      返回:
      the hash code of a non-null argument and 0 for a null argument
      另请参阅:
      Object.hashCode()
    • hash

      public 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).
      参数:
      values - the values to be hashed
      返回:
      a hash value of the sequence of input values
      另请参阅:
      Arrays.hashCode(Object[]), List.hashCode()
    • toString

      public static String toString(Object o)
      Returns the result of calling toString for a non-null argument and "null" for a null argument.
      参数:
      o - an object
      返回:
      the result of calling toString for a non-null argument and "null" for a null argument
      另请参阅:
      Object.toString(), String.valueOf(Object)
    • toString

      public 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.
      参数:
      o - an object
      nullDefault - string to return if the first argument is null
      返回:
      the result of calling toString on the first argument if it is not null and the second argument otherwise.
    • compare

      public static <T> int compare(T a, T b, Comparator<? super T> 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.

      类型参数:
      T - the type of the objects being compared
      参数:
      a - an object
      b - an object to be compared with a
      c - the Comparator to 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 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.requireNonNull(bar);
       }
       
      类型参数:
      T - the type of the reference
      参数:
      obj - the object reference to check for nullity
      返回:
      obj if not null
      抛出:
      NullPointerException - if obj is null
    • requireNonNull

      public static <T> T requireNonNull(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.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 nullity
      message - detail message to be used in the event that a NullPointerException is thrown
      返回:
      obj if not null
      抛出:
      NullPointerException - if obj is null
    • isNull

      public static boolean isNull(Object obj)
      Returns true if the provided reference is null otherwise returns false.
      参数:
      obj - a reference to be checked against null
      返回:
      true if the provided reference is null otherwise false
      从以下版本开始:
      1.8
    • nonNull

      public static boolean nonNull(Object obj)
      Returns true if the provided reference is non-null otherwise returns false.
      参数:
      obj - a reference to be checked against null
      返回:
      true if the provided reference is non-null otherwise false
      从以下版本开始:
      1.8
    • requireNonNullElse

      public static <T> T requireNonNullElse(T obj, T defaultObj)
      Returns the first argument if it is non-null and otherwise returns the non-null second argument.
      类型参数:
      T - the type of the reference
      参数:
      obj - an object
      defaultObj - a non-null object to return if the first argument is null
      返回:
      the first argument if it is non-null and otherwise the second argument if it is non-null
      抛出:
      NullPointerException - if both obj is null and defaultObj is null
      从以下版本开始:
      9