com.evrythng.thng.resource.model.utils.ObjectUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of thng-resource-model Show documentation
Show all versions of thng-resource-model Show documentation
Models for REST resources representations.
/*
* (c) Copyright 2012 EVRYTHNG Ltd London / Zurich
* www.evrythng.com
*/
package com.evrythng.thng.resource.model.utils;
import java.util.Comparator;
/**
* Provides utility method for objects.
*/
public class ObjectUtils {
public static boolean nullSafeEquals(final Object a, final Object b) {
if (a == null && b == null) {
return true;
}
return !(a == null || b == null) && a.equals(b);
}
/**
* NOTE: {@code null} is treated as the smallest value.
*/
public static int nullSafeCompare(final T a, final T b, final Comparator super T> comp) {
if (a == null && b == null) {
return 0;
}
if (a == null) {
return -1;
}
if (b == null) {
return 1;
}
return normalizeCompareResult(comp.compare(a, b));
}
/**
* NOTE: {@code null} is treated as the smallest value.
*/
public static > int nullSafeCompare(final T a, final T b) {
if (a == null && b == null) {
return 0;
}
if (a == null) {
return -1;
}
if (b == null) {
return 1;
}
return normalizeCompareResult(a.compareTo(b));
}
private static int normalizeCompareResult(final int x) {
return x > 0 ? 1 : x < 0 ? -1 : 0;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy