ComparatorUtils { public static <T extends Comparable<T>> int nullSafeCompare(T t1, T t2) { if (t1 == null) { return t2 == null ? 0 : -1; } else if (t2 == null) { return 1; } else { return t1.compareTo(t2); } } private  ComparatorUtils(); static int nullSafeCompare(T t1, T t2); static boolean nullSafeEquals(T t1, T t2);  }
@Test public void testNullSafeCompare() throws Exception { assertTrue(nullSafeCompare("a", "b") < 0); assertTrue(nullSafeCompare("b", "a") > 0); assertTrue(nullSafeCompare("a", "a") == 0); assertTrue(nullSafeCompare("", "a") < 0); assertTrue(nullSafeCompare("a", "") > 0); assertTrue(nullSafeCompare("", "") == 0); assertTrue(nullSafeCompare(null, "a") < 0); assertTrue(nullSafeCompare("a", null) > 0); assertTrue(nullSafeCompare((String) null, null) == 0); assertTrue(nullSafeCompare(null, "") < 0); assertTrue(nullSafeCompare("", null) > 0); }