All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.evrythng.thng.resource.model.utils.ObjectUtils Maven / Gradle / Ivy

There is a newer version: 1.33
Show newest version
/*
 * (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 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