Pair { public static <T1> Comparator<Pair<T1, ?>> lhsComparator(final Comparator<T1> comparator) { return new Comparator<Pair<T1, ?>>() { @Override public int compare(Pair<T1, ?> o1, Pair<T1, ?> o2) { return comparator.compare(o1.lhs, o2.lhs); } }; } Pair(T1 lhs, T2 rhs); static Pair<T1, T2> of(T1 lhs, T2 rhs); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); static Function<Pair<T1, T2>, T1> lhsFn(); static Function<Pair<T1, T2>, T2> rhsFn(); static Comparator<Pair<T1, ?>> lhsComparator(final Comparator<T1> comparator); final T1 lhs; final T2 rhs; }
@Test public void testLhsComparator() { Comparator comp = new Comparator<String>() { @Override public int compare(String s, String t1) { if (s.equals(t1)) { return 0; } else { return 1; } } @Override public boolean equals(Object o) { return false; } }; Pair pair = new Pair("lhs", "rhs"); Pair compare1 = new Pair<>("foo", "bar"); Pair compare2 = new Pair<>("baz", "baz"); Assert.assertEquals(0, pair.lhsComparator(comp).compare(compare1, compare1)); Assert.assertEquals(1, pair.lhsComparator(comp).compare(compare1, compare2)); }