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

The following examples show how to use com.google.common.collect.Ordering#explicit() . 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: TestViewChanges.java    From morf with Apache License 2.0 6 votes vote down vote up
/**
 * Ensure that case-infidelity of discovered views doesn't break us.
 */
@Test
public void testCaseCorrectionOfDropSet() {
  ViewChanges c = new ViewChanges(
    ImmutableSet.of(
      view("A", "B"),
      view("B")
    ),
    ImmutableSet.of(
      view("b")
    ),
    ImmutableSet.of(
      view("A"),
      view("B")
    ));

  assertEquals("Views to drop mismatch", ImmutableSet.of("A", "B"), nameSet(c.getViewsToDrop()));
  assertEquals("Views to deploy mismatch", ImmutableSet.of("A", "B"), nameSet(c.getViewsToDeploy()));

  Ordering<String> dropOrder = Ordering.explicit(nameList(c.getViewsToDrop()));
  assertTrue("Must drop A before B", dropOrder.compare("A", "B") < 0);

  Ordering<String> deployOrder = Ordering.explicit(nameList(c.getViewsToDeploy()));
  assertTrue("Must deploy B before A", deployOrder.compare("B", "A") < 0);
}
 
Example 2
Source File: TestViewChanges.java    From morf with Apache License 2.0 6 votes vote down vote up
/**
 * Check we can expand the set of drops and deploys.
 */
@Test
public void testDroppingAlsoDeployingAlso() {
  ViewChanges c = new ViewChanges(
    ImmutableSet.of(
      view("A", "B"),
      view("B"),
      view("D")
    ),
    ImmutableSet.of(
      view("B")
    ),
    ImmutableSet.of(
      view("A"),
      view("B")
    )).droppingAlso(ImmutableSet.of(view("C"))).deployingAlso(ImmutableSet.of(view("D")));

  assertEquals("Views to drop mismatch", ImmutableSet.of("A", "B", "C"), nameSet(c.getViewsToDrop()));
  assertEquals("Views to deploy mismatch", ImmutableSet.of("A", "B", "D"), nameSet(c.getViewsToDeploy()));

  Ordering<String> dropOrder = Ordering.explicit(nameList(c.getViewsToDrop()));
  assertTrue("Must drop A before B", dropOrder.compare("A", "B") < 0);

  Ordering<String> deployOrder = Ordering.explicit(nameList(c.getViewsToDeploy()));
  assertTrue("Must deploy B before A", deployOrder.compare("B", "A") < 0);
}
 
Example 3
Source File: SortEnum.java    From levelup-java-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void create_drop_down() {

	Ordering<Fruit> byFruitAlphabetical = Ordering.explicit(Fruit.APPLE,
			Fruit.BANANAS, Fruit.CHERRIES, Fruit.ORANGE);

	List<Fruit> dropDown = byFruitAlphabetical.sortedCopy(Arrays
			.asList(Fruit.values()));

	logger.info(dropDown);

	assertThat(
			dropDown,
			contains(Fruit.APPLE, Fruit.BANANAS, Fruit.CHERRIES,
					Fruit.ORANGE));
}
 
Example 4
Source File: SortEnum.java    From levelup-java-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void sort_enum_with_guava() {

	Ordering<Fruit> byFruitAlphabetical = Ordering.explicit(Fruit.APPLE,
			Fruit.BANANAS, Fruit.CHERRIES, Fruit.ORANGE);

	List<Fruit> fruitAlphabetical = byFruitAlphabetical
			.sortedCopy(RANDOM_FRUIT);

	logger.info(fruitAlphabetical);

	assertThat(
			fruitAlphabetical,
			contains(Fruit.APPLE, Fruit.BANANAS, Fruit.CHERRIES,
					Fruit.CHERRIES, Fruit.ORANGE, Fruit.ORANGE));

}
 
Example 5
Source File: TestViewChanges.java    From morf with Apache License 2.0 5 votes vote down vote up
/**
 * Test a simple configuration.
 */
@Test
public void testSimple() {
  ViewChanges c = new ViewChanges(
    ImmutableSet.of(
      view("A", "B", "C"),
      view("B", "C", "D"),
      view("C"),
      view("D")
    ),
    ImmutableSet.of(
      view("B")
    ),
    ImmutableSet.of(
      view("A"),
      view("B")
    ));

  assertEquals("Views to drop mismatch", ImmutableSet.of("B", "A"), nameSet(c.getViewsToDrop()));
  assertEquals("Views to deploy mismatch", ImmutableSet.of("A", "B"), nameSet(c.getViewsToDeploy()));

  Ordering<String> dropOrder = Ordering.explicit(nameList(c.getViewsToDrop()));
  assertTrue("Must drop A before B", dropOrder.compare("A", "B") < 0);

  Ordering<String> deployOrder = Ordering.explicit(nameList(c.getViewsToDeploy()));
  assertTrue("Must deploy B before A", deployOrder.compare("B", "A") < 0);
}
 
Example 6
Source File: TestViewChanges.java    From morf with Apache License 2.0 5 votes vote down vote up
/**
 * Test we're still prepared to drop views that we know nothing of.
 */
@Test
public void testObsoleted() {
  ViewChanges c = new ViewChanges(
    ImmutableSet.of(
      view("A", "B"),
      view("B", "C"),
      view("C")
    ),
    ImmutableSet.of(
      view("B"),
      view("X")
    ),
    ImmutableSet.of(
      view("A"),
      view("B")
    ));

  assertEquals("Views to drop mismatch", ImmutableSet.of("A", "B", "X"), nameSet(c.getViewsToDrop()));
  assertEquals("Views to deploy mismatch", ImmutableSet.of("A", "B"), nameSet(c.getViewsToDeploy()));

  Ordering<String> dropOrder = Ordering.explicit(nameList(c.getViewsToDrop()));
  assertTrue("Must drop A before B", dropOrder.compare("A", "B") < 0);

  Ordering<String> deployOrder = Ordering.explicit(nameList(c.getViewsToDeploy()));
  assertTrue("Must deploy B before A", deployOrder.compare("B", "A") < 0);
}
 
Example 7
Source File: DefaultDnsNameResolver.java    From armeria with Apache License 2.0 5 votes vote down vote up
public DefaultDnsNameResolver(DnsNameResolver delegate, EventLoop eventLoop, long queryTimeoutMillis) {
    this.delegate = requireNonNull(delegate, "delegate");
    this.eventLoop = requireNonNull(eventLoop, "eventLoop");
    checkArgument(queryTimeoutMillis >= 0, "queryTimeoutMillis: %s (expected: >= 0)", queryTimeoutMillis);
    this.queryTimeoutMillis = queryTimeoutMillis;

    if (delegate.resolvedAddressTypes() == ResolvedAddressTypes.IPV6_PREFERRED) {
        preferredOrder = Ordering.explicit(DnsRecordType.AAAA, DnsRecordType.A);
    } else {
        preferredOrder = Ordering.explicit(DnsRecordType.A, DnsRecordType.AAAA);
    }
}
 
Example 8
Source File: GuavaOrderingExamplesUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public final void givenCollectionIsSorted_whenUsingOrderingApiToCheckOrder_thenCheckCanBePerformed() {
    final List<String> toSort = Arrays.asList("zz", "aa", "b", "ccc");
    final Ordering<String> byLength = new OrderingByLenght();
    Collections.sort(toSort, byLength);

    final Ordering<String> expectedOrder = Ordering.explicit(Lists.newArrayList("b", "zz", "aa", "ccc"));
    assertTrue(expectedOrder.isOrdered(toSort));
}
 
Example 9
Source File: GuavaOrderingExamplesUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public final void whenSortingCollectionsOfStringsByLenght_thenCorrectlySorted() {
    final List<String> toSort = Arrays.asList("zz", "aa", "b", "ccc");
    final Ordering<String> byLength = new OrderingByLenght();

    Collections.sort(toSort, byLength);

    final Ordering<String> expectedOrder = Ordering.explicit(Lists.newArrayList("b", "zz", "aa", "ccc"));
    assertTrue(expectedOrder.isOrdered(toSort));
}
 
Example 10
Source File: GuavaOrderingExamplesUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public final void whenSortingCollectionsOfStringsByLenghtWithSecondaryNaturalOrdering_thenCorrectlySorted() {
    final List<String> toSort = Arrays.asList("zz", "aa", "b", "ccc");
    final Ordering<String> byLength = new OrderingByLenght();

    Collections.sort(toSort, byLength.compound(Ordering.natural()));

    final Ordering<String> expectedOrder = Ordering.explicit(Lists.newArrayList("b", "aa", "zz", "ccc"));
    assertTrue(expectedOrder.isOrdered(toSort));
}
 
Example 11
Source File: GuavaOrderingExamplesUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public final void givenUnorderdList_whenRetrievingSortedCopy_thenSorted() {
    final List<String> toSort = Arrays.asList("aa", "b", "ccc");
    final List<String> sortedCopy = new OrderingByLenght().sortedCopy(toSort);

    final Ordering<String> expectedOrder = Ordering.explicit(Lists.newArrayList("b", "aa", "ccc"));
    assertFalse(expectedOrder.isOrdered(toSort));
    assertTrue(expectedOrder.isOrdered(sortedCopy));
}
 
Example 12
Source File: GuavaOrderingExamplesUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public final void givenUnorderdList_whenUsingToStringForSortingObjects_thenSortedWithToString() {
    final List<Integer> toSort = Arrays.asList(1, 2, 11);
    Collections.sort(toSort, Ordering.usingToString());

    final Ordering<Integer> expectedOrder = Ordering.explicit(Lists.newArrayList(1, 11, 2));
    assertTrue(expectedOrder.isOrdered(toSort));
}