WeakLogic { public static <T> Comparator<T> weakComparator(Comparator<? super T> comparator) { return (T x, T y) -> { if ((x == null) && (y != null)) { return -1; } else if (x == null) { return 0; } else if (y == null) { return 1; } else { return comparator.compare(x, y); } }; } private  WeakLogic(); static Comparator<T> weakComparator(Comparator<? super T> comparator); static Comparator<T> weakComparator(); static BinaryOperator<T> weakBinaryOperator(BinaryOperator<T> op); static UnaryOperator<T> weakUnaryOperator(UnaryOperator<T> op); static Function<F, T> weakFunction(Function<F, T> f); static boolean weakEquals(T x, T y);  }
@Test public void testWeakComparator() { Comparator<Integer> cmp = (x, y) -> x - y; Comparator<Integer> w = weakComparator(cmp); assertTrue(w.compare(0, 1) < 0); assertTrue(w.compare(1, 0) > 0); assertSame(w.compare(1, 1), 0); assertTrue(w.compare(null, 1) < 0); assertTrue(w.compare(1, null) > 0); assertSame(w.compare(null, null), 0); w = weakComparator(); assertTrue(w.compare(0, 1) < 0); assertTrue(w.compare(1, 0) > 0); assertSame(w.compare(1, 1), 0); assertTrue(w.compare(null, 1) < 0); assertTrue(w.compare(1, null) > 0); assertSame(w.compare(null, null), 0); cmp = weakComparator(); int result; result = cmp.compare(null, null); assertEquals(result, 0); result = cmp.compare(null, 1); assertEquals(result, -1); result = cmp.compare(1, 2); assertEquals(result, -1); result = cmp.compare(1, null); assertEquals(result, 1); }