@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void shouldNeedComparable() { Comparator c = new ComparableComparator(); Object o1 = new Object(); Object o2 = new Object(); thrown.expect(ClassCastException.class); c.compare(o1, o2); }
@Override public int compare(Integer o1, Integer o2) { assertThat(o1, instanceOf(Integer.class)); assertThat(o2, instanceOf(Integer.class)); this.called = true; return super.compare(o1, o2); };
@Test public void testComparableComparator() { Comparator<String> c = new ComparableComparator<>(); String s1 = "abc"; String s2 = "cde"; assertTrue(c.compare(s1, s2) < 0); }
/** * Compares the two given objects. Expects both of them to be {@link Comparable} instances. * * @see ComparableComparator#compare(Object, Object) */ public int compare(Object o1, Object o2) { if (Number.class.isInstance(o1) && Number.class.isInstance(o2)) { return compareNumbers((Number) o1, (Number) o2); } return super.compare(o1, o2); }
@Test public void shouldGetMapEntryValues() throws Exception { ArrayList<Entry<String, Integer>> list = createReverseOrderMapEntryList(); Comparator<Map.Entry<String, Integer>> comparator = ConvertingComparator.mapEntryValues(new ComparableComparator<Integer>()); Collections.sort(list, comparator); assertThat(list.get(0).getValue(), is(1)); }
@Test public void shouldGetMapEntryKeys() throws Exception { ArrayList<Entry<String, Integer>> list = createReverseOrderMapEntryList(); Comparator<Map.Entry<String, Integer>> comparator = ConvertingComparator.mapEntryKeys(new ComparableComparator<String>()); Collections.sort(list, comparator); assertThat(list.get(0).getKey(), is("a")); }
/** * Create a NullSafeComparator that sorts <code>null</code> based on * the provided flag, working on Comparables. * <p>When comparing two non-null objects, their Comparable implementation * will be used: this means that non-null elements (that this Comparator * will be applied to) need to implement Comparable. * <p>As a convenience, you can use the default shared instances: * <code>NullSafeComparator.NULLS_LOW</code> and * <code>NullSafeComparator.NULLS_HIGH</code>. * @param nullsLow whether to treat nulls lower or higher than non-null objects * @see java.lang.Comparable * @see #NULLS_LOW * @see #NULLS_HIGH */ @SuppressWarnings("unchecked") private NullSafeComparator(boolean nullsLow) { this.nonNullComparator = new ComparableComparator(); this.nullsLow = nullsLow; }
@Override protected Comparator<MappingInformation> getMappingComparator(Message<?> message) { return new ComparableComparator<>(); }
@Override protected Comparator<MappingInformation> getMappingComparator(Message<?> message) { return new ComparableComparator<>(); }
/** * Create a NullSafeComparator that sorts <code>null</code> based on * the provided flag, working on Comparables. * <p>When comparing two non-null objects, their Comparable implementation * will be used: this means that non-null elements (that this Comparator * will be applied to) need to implement Comparable. * <p>As a convenience, you can use the default shared instances: * <code>NullSafeComparator.NULLS_LOW</code> and * <code>NullSafeComparator.NULLS_HIGH</code>. * @param nullsLow whether to treat nulls lower or higher than non-null objects * @see java.lang.Comparable * @see #NULLS_LOW * @see #NULLS_HIGH */ private NullSafeComparator(boolean nullsLow) { this(new ComparableComparator(), nullsLow); }
new PagingRequest(-1, 1), new ComparableComparator<Integer>()); fail("Invalid page data."); new PagingRequest(1, -1), new ComparableComparator<Integer>()); fail("Invalid page data.");
new PagingRequest(skip, pageSize), new ComparableComparator<Integer>()); List<Integer> actualPage = actualResults.getPage();