cn.keayuan.util.ObjectUtils Maven / Gradle / Ivy
The newest version!
package cn.keayuan.util;
import java.util.Comparator;
import java.util.Objects;
/**
* Object Utils
*/
public class ObjectUtils {
private ObjectUtils() {
throw new AssertionError();
}
/**
* compare two object
*
* @return
* - if both are null, return true
* - return actual.{@link Object#equals(Object)}
*
*/
public static boolean equals(Object a, Object b) {
return a == b || (a != null && a.equals(b));
}
/**
* null Object to empty string
*
* nullStrToEmpty(null) = "";
* nullStrToEmpty("") = "";
* nullStrToEmpty("aa") = "aa";
*
*
* @return -
*/
public static String nullStrToEmpty(Object str) {
return (str == null ? "" : str.toString());
}
public static int compare(T a, T b, Comparator super T> c) {
return (a == b) ? 0 : c.compare(a, b);
}
/**
* compare two object
*
* - About result
* - if v1 > v2, return 1
* - if v1 = v2, return 0
* - if v1 < v2, return -1
*
* About rule
*
* - if v1 is null, v2 is null, then return 0
* - if v1 is null, v2 is not null, then return -1
* - if v1 is not null, v2 is null, then return 1
* - return v1.{@link Comparable#compareTo(Object)}
*
*/
public static > int compare(V v1, V v2) {
return v1 == null ? (v2 == null ? 0 : -1) : (v2 == null ? 1 : v1.compareTo(v2));
}
/**
* 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:
*
* public Foo(Bar bar) {
* this.bar = Objects.requireNonNull(bar);
* }
*
*
* @param obj the object reference to check for nullity
* @param the type of the reference
* @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@code null}
*/
public static 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:
*
* 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");
* }
*
*
* @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 the type of the reference
* @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@code null}
*/
public static T requireNonNull(T obj, String message) {
if (obj == null) throw new NullPointerException(message);
return obj;
}
/**
* Returns {@code true} if the provided reference is {@code null} otherwise
* returns {@code false}.
*
* @param obj a reference to be checked against {@code null}
* @return {@code true} if the provided reference is {@code null} otherwise
* {@code false}
*/
public static boolean isNull(Object obj) {
return obj == null;
}
/**
* Returns {@code true} if the provided reference is non-{@code null}
* otherwise returns {@code false}.
*
* @param obj a reference to be checked against {@code null}
* @return {@code true} if the provided reference is non-{@code null}
* otherwise {@code false}
*/
public static boolean nonNull(Object obj) {
return obj != null;
}
public static int hashCode(Object o) {
return o != null ? o.hashCode() : 0;
}
}