@Test public void shouldFailWithNonComparableTypes() { final ImmutablePair<Object, Object> pair1 = new ImmutablePair<>(new Object(), new Object()); final ImmutablePair<Object, Object> pair2 = new ImmutablePair<>(new Object(), new Object()); try { pair1.compareTo(pair2); fail("Pairs should not be comparable"); } catch (IllegalArgumentException e) { assertThat(e).hasMessageContaining("Please provide comparable elements"); } }
@Override @SuppressWarnings("unchecked") public int compareTo(ImmutablePair<L, R> o) { if (o == null) { throw new IllegalArgumentException("Pair to compare to must not be null"); } try { int leftComparison = compare((Comparable<L>) getLeft(), (Comparable<L>) o.getLeft()); return leftComparison == 0 ? compare((Comparable<R>) getRight(), (Comparable<R>) o.getRight()) : leftComparison; } catch (ClassCastException cce) { throw new IllegalArgumentException("Please provide comparable elements", cce); } }
ImmutablePair implements Entry<L, R>, Serializable, Comparable<ImmutablePair<L, R>> { @Override @SuppressWarnings("unchecked") public int compareTo(ImmutablePair<L, R> o) { if (o == null) { throw new IllegalArgumentException("Pair to compare to must not be null"); } try { int leftComparison = compare((Comparable<L>) getLeft(), (Comparable<L>) o.getLeft()); return leftComparison == 0 ? compare((Comparable<R>) getRight(), (Comparable<R>) o.getRight()) : leftComparison; } catch (ClassCastException cce) { throw new IllegalArgumentException("Please provide comparable elements", cce); } } }
ImmutablePair implements Entry<L, R>, Serializable, Comparable<ImmutablePair<L, R>> { @Override @SuppressWarnings("unchecked") public int compareTo(ImmutablePair<L, R> o) { if (o == null) { throw new IllegalArgumentException("Pair to compare to must not be null"); } try { int leftComparison = compare((Comparable<L>) getLeft(), (Comparable<L>) o.getLeft()); return leftComparison == 0 ? compare((Comparable<R>) getRight(), (Comparable<R>) o.getRight()) : leftComparison; } catch (ClassCastException cce) { throw new IllegalArgumentException("Please provide comparable elements", cce); } } ImmutablePair(L left, R right); }
ImmutablePair implements Entry<L, R>, Serializable, Comparable<ImmutablePair<L, R>> { @Override @SuppressWarnings("unchecked") public int compareTo(ImmutablePair<L, R> o) { if (o == null) { throw new IllegalArgumentException("Pair to compare to must not be null"); } try { int leftComparison = compare((Comparable<L>) getLeft(), (Comparable<L>) o.getLeft()); return leftComparison == 0 ? compare((Comparable<R>) getRight(), (Comparable<R>) o.getRight()) : leftComparison; } catch (ClassCastException cce) { throw new IllegalArgumentException("Please provide comparable elements", cce); } } ImmutablePair(L left, R right); L getLeft(); R getRight(); @Override final L getKey(); @Override R getValue(); @Override R setValue(R value); @Override @SuppressWarnings("unchecked") int compareTo(ImmutablePair<L, R> o); @Override boolean equals(Object obj); @Override int hashCode(); @Override String toString(); }
ImmutablePair implements Entry<L, R>, Serializable, Comparable<ImmutablePair<L, R>> { @Override @SuppressWarnings("unchecked") public int compareTo(ImmutablePair<L, R> o) { if (o == null) { throw new IllegalArgumentException("Pair to compare to must not be null"); } try { int leftComparison = compare((Comparable<L>) getLeft(), (Comparable<L>) o.getLeft()); return leftComparison == 0 ? compare((Comparable<R>) getRight(), (Comparable<R>) o.getRight()) : leftComparison; } catch (ClassCastException cce) { throw new IllegalArgumentException("Please provide comparable elements", cce); } } ImmutablePair(L left, R right); L getLeft(); R getRight(); @Override final L getKey(); @Override R getValue(); @Override R setValue(R value); @Override @SuppressWarnings("unchecked") int compareTo(ImmutablePair<L, R> o); @Override boolean equals(Object obj); @Override int hashCode(); @Override String toString();  }