Java Code Examples for com.google.common.collect.Ordering#compare()

The following examples show how to use com.google.common.collect.Ordering#compare() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WorldSwitcherPanel.java    From plugins with GNU General Public License v3.0 5 votes vote down vote up
private int getCompareValue(WorldTableRow row1, WorldTableRow row2, Function<WorldTableRow, Comparable> compareByFn)
{
	Ordering<Comparable> ordering = Ordering.natural();
	if (!ascendingOrder)
	{
		ordering = ordering.reverse();
	}
	ordering = ordering.nullsLast();
	return ordering.compare(compareByFn.apply(row1), compareByFn.apply(row2));
}
 
Example 2
Source File: WorldSwitcherPanel.java    From runelite with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private int getCompareValue(WorldTableRow row1, WorldTableRow row2, Function<WorldTableRow, Comparable> compareByFn)
{
	Ordering<Comparable> ordering = Ordering.natural();
	if (!ascendingOrder)
	{
		ordering = ordering.reverse();
	}
	ordering = ordering.nullsLast();
	return ordering.compare(compareByFn.apply(row1), compareByFn.apply(row2));
}
 
Example 3
Source File: InstrumentationConfigOrderingTest.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@Test
public void testDifferentClassNames() {
    // given
    Ordering<InstrumentationConfig> ordering = new InstrumentationConfigOrdering();
    // when
    int compare = ordering.compare(left, right);
    // then
    assertThat(compare).isNegative();
}
 
Example 4
Source File: InstrumentationConfigOrderingTest.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@Test
public void testSameClassNames() {
    // given
    Ordering<InstrumentationConfig> ordering = new InstrumentationConfigOrdering();
    // when
    int compare = ordering.compare(left, right.toBuilder().setClassName("a").build());
    // then
    assertThat(compare).isPositive();
}
 
Example 5
Source File: InstrumentationConfigOrderingTest.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@Test
public void testSameClassAndMethodNames() {
    // given
    Ordering<InstrumentationConfig> ordering = new InstrumentationConfigOrdering();
    // when
    int compare = ordering.compare(left.toBuilder().setMethodName("m").build(),
            right.toBuilder().setClassName("a").build());
    // then
    assertThat(compare).isPositive();
}
 
Example 6
Source File: InstrumentationConfigOrderingTest.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@Test
public void testSameClassAndMethodNamesAndParamCount() {
    // given
    Ordering<InstrumentationConfig> ordering = new InstrumentationConfigOrdering();
    // when
    int compare =
            ordering.compare(left.toBuilder().setMethodName("m").build(), right.toBuilder()
                    .setClassName("a").addMethodParameterType("java.lang.Throwable").build());
    // then
    assertThat(compare).isNegative();
}
 
Example 7
Source File: InstrumentationConfigOrderingTest.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@Test
public void testSameEverything() {
    // given
    Ordering<InstrumentationConfig> ordering = new InstrumentationConfigOrdering();
    // when
    int compare = ordering.compare(left.toBuilder().setMethodName("m").build(), right
            .toBuilder().setClassName("a").addMethodParameterType("java.lang.String").build());
    // then
    assertThat(compare).isZero();
}
 
Example 8
Source File: TestingHelpers.java    From crate with Apache License 2.0 4 votes vote down vote up
public static <T, K extends Comparable> Matcher<Iterable<? extends T>> isSortedBy(final Function<T, K> extractSortingKeyFunction,
                                                                                  final boolean descending,
                                                                                  @Nullable final Boolean nullsFirst) {
    Ordering<K> ordering = Ordering.natural();
    if (descending) {
        ordering = ordering.reverse();
    }
    if (nullsFirst != null && nullsFirst) {
        ordering = ordering.nullsFirst();
    } else {
        ordering = ordering.nullsLast();
    }
    final Ordering<K> ord = ordering;

    return new TypeSafeDiagnosingMatcher<Iterable<? extends T>>() {
        @Override
        protected boolean matchesSafely(Iterable<? extends T> item, Description mismatchDescription) {
            K previous = null;
            int i = 0;
            for (T elem : item) {
                K current = extractSortingKeyFunction.apply(elem);
                if (previous != null) {
                    if (ord.compare(previous, current) > 0) {
                        mismatchDescription
                            .appendText("element ").appendValue(current)
                            .appendText(" at position ").appendValue(i)
                            .appendText(" is ")
                            .appendText(descending ? "bigger" : "smaller")
                            .appendText(" than previous element ")
                            .appendValue(previous);
                        return false;
                    }
                }
                i++;
                previous = current;
            }
            return true;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("expected iterable to be sorted ");
            if (descending) {
                description.appendText("in DESCENDING order");
            } else {
                description.appendText("in ASCENDING order");
            }
        }
    };
}