Pomocná třída Objects

Tomáš Pitner, Radek Ošlejšek, Marek Šabo, Jakub Čecháček

Pomocná třída Objects

  • Třída dostupná od Javy 8

  • Nabízí praktické statické metody obecně využitelné pro všechny objekty:

    • kalkulace hash-kódů (hash, hashCode),

    • test/vynucení nenulovosti (requireNonNull),

    • hluboké porovnání (deepEquals)

Porovnání objektů

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 deepEquals(Object a, Object b)

Returns true if the arguments are deeply equal to each other and false otherwise.

static boolean equals(Object a, Object b)

Returns true if the arguments are equal to each other and false otherwise.

Vynucení nenulovosti

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 requireNonNull(T obj, Supplier<String> messageSupplier)

Checks that the specified object reference is not null and throws a customized NullPointerException if it is.

Metody toString

  • zbavuje nás nutnosti testu na nulovost odkazu

  • kratší náhrada o == null ? "null" : o.toString()

    static String toString(Object o)

    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.