Java Code Examples for java.util.Comparator#reversed()

The following examples show how to use java.util.Comparator#reversed() . 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: PropertyComparatorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testChainedComparatorsReversed() {
	Comparator<Dog> c = (new PropertyComparator<Dog>("lastName", false, true)).
			thenComparing(new PropertyComparator<>("firstName", false, true));

	Dog dog1 = new Dog();
	dog1.setFirstName("macy");
	dog1.setLastName("grayspots");

	Dog dog2 = new Dog();
	dog2.setFirstName("biscuit");
	dog2.setLastName("grayspots");

	assertTrue(c.compare(dog1, dog2) > 0);
	c = c.reversed();
	assertTrue(c.compare(dog1, dog2) < 0);
}
 
Example 2
Source File: FileResolver.java    From jpa2ddl with Apache License 2.0 6 votes vote down vote up
static List<Path> resolveExistingMigrations(File migrationsDir, boolean reversed, boolean onlySchemaMigrations) {
	if (!migrationsDir.exists()) {
		migrationsDir.mkdirs();
	}

	File[] files = migrationsDir.listFiles();

	if (files == null) {
		return Collections.emptyList();
	}

	Comparator<Path> pathComparator = Comparator.comparingLong(FileResolver::compareVersionedMigrations);
	if (reversed) {
		pathComparator = pathComparator.reversed();
	}
	return Arrays.stream(files)
			.map(File::toPath)
			.filter(path -> !onlySchemaMigrations || SCHEMA_FILENAME_PATTERN.matcher(path.getFileName().toString()).matches())
			.sorted(pathComparator)
			.collect(Collectors.toList());
}
 
Example 3
Source File: TorrentDumper.java    From mldht with Mozilla Public License 2.0 6 votes vote down vote up
void scrubActive() {
	
	// as long as there are young connections it means some fraction of the fetch tasks dies quickly
	// we're fine with other ones taking longer as long as that's the case
	long youngConnections = activeTasks.values().stream().filter(t -> t.attemptedCount() < 5).count();
	
	if(youngConnections > 15 || activeCount.get() < maxFetches() * 0.9)
		return;
	
	
	Comparator<Map.Entry<FetchTask, Integer>> comp = Map.Entry.comparingByValue();
	comp = comp.reversed();
	
	activeTasks.values().stream().map(t -> new AbstractMap.SimpleEntry<>(t, t.attemptedCount())).filter(e -> e.getValue() > 70).sorted(comp).limit(10).forEachOrdered(e -> {
		e.getKey().stop();
	});
}
 
Example 4
Source File: SortOption.java    From business with Mozilla Public License 2.0 6 votes vote down vote up
private <T> Comparator<T> buildComparator(SortedAttribute sortedAttribute) {
    final String[] parts = sortedAttribute.getAttribute().split(ATTRIBUTE_PATH_PATTERN);
    Comparator<T> comparator = (t1, t2) -> {
        Object val1 = t1;
        Object val2 = t2;
        for (String part : parts) {
            val1 = accessValue(val1, part);
            val2 = accessValue(val2, part);
        }
        return ensureComparable(val1).compareTo(val2);
    };
    if (sortedAttribute.getDirection() == Direction.DESCENDING) {
        return comparator.reversed();
    } else {
        return comparator;
    }
}
 
Example 5
Source File: PropertyComparatorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testChainedComparatorsReversed() {
	Comparator<Dog> c = (new PropertyComparator<Dog>("lastName", false, true)).
			thenComparing(new PropertyComparator<>("firstName", false, true));

	Dog dog1 = new Dog();
	dog1.setFirstName("macy");
	dog1.setLastName("grayspots");

	Dog dog2 = new Dog();
	dog2.setFirstName("biscuit");
	dog2.setLastName("grayspots");

	assertTrue(c.compare(dog1, dog2) > 0);
	c = c.reversed();
	assertTrue(c.compare(dog1, dog2) < 0);
}
 
Example 6
Source File: BasicTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void testNullsFirst() {
    Comparator<String> strcmp = Comparator.nullsFirst(Comparator.naturalOrder());
    Comparator<People> cmp = Comparator.comparing(People::getLastName, strcmp)
                                       .thenComparing(People::getFirstName, strcmp);
    // Mary.null vs Mary.Cook - solve by last name
    assertComparison(cmp, people[6], people[5]);
    // John.null vs Mary.null - solve by first name
    assertComparison(cmp, people[7], people[6]);

    // More than one thenComparing
    strcmp = Comparator.nullsFirst(Comparator.comparingInt(String::length)
                                             .thenComparing(String.CASE_INSENSITIVE_ORDER));
    assertComparison(strcmp, null, "abc");
    assertComparison(strcmp, "ab", "abc");
    assertComparison(strcmp, "abc", "def");
    assertEquals(0, strcmp.compare("abc", "ABC"));

    // Ensure reverse still handle null properly
    Comparator<String> strcmp2 = strcmp.reversed().thenComparing(Comparator.naturalOrder());
    assertComparison(strcmp2, "abc", null);
    assertComparison(strcmp2, "abc", "ab");
    assertComparison(strcmp2, "def", "abc");
    assertComparison(strcmp2, "ABC", "abc");

    // Considering non-null values to be equal
    Comparator<String> blind = Comparator.nullsFirst(null);
    assertComparison(blind, null, "abc");
    assertEquals(0, blind.compare("abc", "def"));
    // reverse still consider non-null values to be equal
    strcmp = blind.reversed();
    assertComparison(strcmp, "abc", null);
    assertEquals(0, strcmp.compare("abc", "def"));
    // chain with another comparator to compare non-nulls
    strcmp = blind.thenComparing(Comparator.naturalOrder());
    assertComparison(strcmp, null, "abc");
    assertComparison(strcmp, "abc", "def");
}
 
Example 7
Source File: BasicTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public void testReverseComparator() {
    Comparator<String> cmpr = Comparator.reverseOrder();
    Comparator<String> cmp = cmpr.reversed();

    assertEquals(cmp.reversed(), cmpr);
    assertEquals(0, cmp.compare("a", "a"));
    assertEquals(0, cmpr.compare("a", "a"));
    assertTrue(cmp.compare("a", "b") < 0);
    assertTrue(cmpr.compare("a", "b") > 0);
    assertTrue(cmp.compare("b", "a") > 0);
    assertTrue(cmpr.compare("b", "a") < 0);
}
 
Example 8
Source File: BasicTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void testReverseComparator2() {
    Comparator<String> cmp = (s1, s2) -> s1.length() - s2.length();
    Comparator<String> cmpr = cmp.reversed();

    assertEquals(cmpr.reversed(), cmp);
    assertEquals(0, cmp.compare("abc", "def"));
    assertEquals(0, cmpr.compare("abc", "def"));
    assertTrue(cmp.compare("abcd", "def") > 0);
    assertTrue(cmpr.compare("abcd", "def") < 0);
    assertTrue(cmp.compare("abc", "defg") < 0);
    assertTrue(cmpr.compare("abc", "defg") > 0);
}
 
Example 9
Source File: BasicTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void testReverseComparator() {
    Comparator<String> cmpr = Comparator.reverseOrder();
    Comparator<String> cmp = cmpr.reversed();

    assertEquals(cmp.reversed(), cmpr);
    assertEquals(0, cmp.compare("a", "a"));
    assertEquals(0, cmpr.compare("a", "a"));
    assertTrue(cmp.compare("a", "b") < 0);
    assertTrue(cmpr.compare("a", "b") > 0);
    assertTrue(cmp.compare("b", "a") > 0);
    assertTrue(cmpr.compare("b", "a") < 0);
}
 
Example 10
Source File: BasicTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public void testNullsLast() {
    Comparator<String> strcmp = Comparator.nullsLast(Comparator.naturalOrder());
    Comparator<People> cmp = Comparator.comparing(People::getLastName, strcmp)
                                       .thenComparing(People::getFirstName, strcmp);
    // Mary.null vs Mary.Cook - solve by last name
    assertComparison(cmp, people[5], people[6]);
    // John.null vs Mary.null - solve by first name
    assertComparison(cmp, people[7], people[6]);

    // More than one thenComparing
    strcmp = Comparator.nullsLast(Comparator.comparingInt(String::length)
                                            .thenComparing(String.CASE_INSENSITIVE_ORDER));
    assertComparison(strcmp, "abc", null);
    assertComparison(strcmp, "ab", "abc");
    assertComparison(strcmp, "abc", "def");

    // Ensure reverse still handle null properly
    Comparator<String> strcmp2 = strcmp.reversed().thenComparing(Comparator.naturalOrder());
    assertComparison(strcmp2, null, "abc");
    assertComparison(strcmp2, "abc", "ab");
    assertComparison(strcmp2, "def", "abc");
    assertComparison(strcmp2, "ABC", "abc");

    // Considering non-null values to be equal
    Comparator<String> blind = Comparator.nullsLast(null);
    assertComparison(blind, "abc", null);
    assertEquals(0, blind.compare("abc", "def"));
    // reverse still consider non-null values to be equal
    strcmp = blind.reversed();
    assertComparison(strcmp, null, "abc");
    assertEquals(0, strcmp.compare("abc", "def"));
    // chain with another comparator to compare non-nulls
    strcmp = blind.thenComparing(Comparator.naturalOrder());
    assertComparison(strcmp, "abc", null);
    assertComparison(strcmp, "abc", "def");
}
 
Example 11
Source File: BasicTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public void testReverseComparator2() {
    Comparator<String> cmp = (s1, s2) -> s1.length() - s2.length();
    Comparator<String> cmpr = cmp.reversed();

    assertEquals(cmpr.reversed(), cmp);
    assertEquals(0, cmp.compare("abc", "def"));
    assertEquals(0, cmpr.compare("abc", "def"));
    assertTrue(cmp.compare("abcd", "def") > 0);
    assertTrue(cmpr.compare("abcd", "def") < 0);
    assertTrue(cmp.compare("abc", "defg") < 0);
    assertTrue(cmpr.compare("abc", "defg") > 0);
}
 
Example 12
Source File: BasicTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public void testReverseComparator() {
    Comparator<String> cmpr = Comparator.reverseOrder();
    Comparator<String> cmp = cmpr.reversed();

    assertEquals(cmp.reversed(), cmpr);
    assertEquals(0, cmp.compare("a", "a"));
    assertEquals(0, cmpr.compare("a", "a"));
    assertTrue(cmp.compare("a", "b") < 0);
    assertTrue(cmpr.compare("a", "b") > 0);
    assertTrue(cmp.compare("b", "a") > 0);
    assertTrue(cmpr.compare("b", "a") < 0);
}
 
Example 13
Source File: CurrencyList.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
private Comparator<CurrencyListItem> getComparator() {
    Comparator<CurrencyListItem> result;
    if (preferences.isSortMarketCurrenciesNumerically()) {
        Comparator<CurrencyListItem> byCount = Comparator.comparingInt(left -> left.numTrades);
        result = byCount.reversed();
    } else {
        result = Comparator.comparing(item -> item.tradeCurrency);
    }
    return result;
}
 
Example 14
Source File: BasicTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testReverseComparator() {
    Comparator<String> cmpr = Comparator.reverseOrder();
    Comparator<String> cmp = cmpr.reversed();

    assertEquals(cmp.reversed(), cmpr);
    assertEquals(0, cmp.compare("a", "a"));
    assertEquals(0, cmpr.compare("a", "a"));
    assertTrue(cmp.compare("a", "b") < 0);
    assertTrue(cmpr.compare("a", "b") > 0);
    assertTrue(cmp.compare("b", "a") > 0);
    assertTrue(cmpr.compare("b", "a") < 0);
}
 
Example 15
Source File: BasicTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void testNullsFirst() {
    Comparator<String> strcmp = Comparator.nullsFirst(Comparator.naturalOrder());
    Comparator<People> cmp = Comparator.comparing(People::getLastName, strcmp)
                                       .thenComparing(People::getFirstName, strcmp);
    // Mary.null vs Mary.Cook - solve by last name
    assertComparison(cmp, people[6], people[5]);
    // John.null vs Mary.null - solve by first name
    assertComparison(cmp, people[7], people[6]);

    // More than one thenComparing
    strcmp = Comparator.nullsFirst(Comparator.comparingInt(String::length)
                                             .thenComparing(String.CASE_INSENSITIVE_ORDER));
    assertComparison(strcmp, null, "abc");
    assertComparison(strcmp, "ab", "abc");
    assertComparison(strcmp, "abc", "def");
    assertEquals(0, strcmp.compare("abc", "ABC"));

    // Ensure reverse still handle null properly
    Comparator<String> strcmp2 = strcmp.reversed().thenComparing(Comparator.naturalOrder());
    assertComparison(strcmp2, "abc", null);
    assertComparison(strcmp2, "abc", "ab");
    assertComparison(strcmp2, "def", "abc");
    assertComparison(strcmp2, "ABC", "abc");

    // Considering non-null values to be equal
    Comparator<String> blind = Comparator.nullsFirst(null);
    assertComparison(blind, null, "abc");
    assertEquals(0, blind.compare("abc", "def"));
    // reverse still consider non-null values to be equal
    strcmp = blind.reversed();
    assertComparison(strcmp, "abc", null);
    assertEquals(0, strcmp.compare("abc", "def"));
    // chain with another comparator to compare non-nulls
    strcmp = blind.thenComparing(Comparator.naturalOrder());
    assertComparison(strcmp, null, "abc");
    assertComparison(strcmp, "abc", "def");
}
 
Example 16
Source File: BasicTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void testNullsLast() {
    Comparator<String> strcmp = Comparator.nullsLast(Comparator.naturalOrder());
    Comparator<People> cmp = Comparator.comparing(People::getLastName, strcmp)
                                       .thenComparing(People::getFirstName, strcmp);
    // Mary.null vs Mary.Cook - solve by last name
    assertComparison(cmp, people[5], people[6]);
    // John.null vs Mary.null - solve by first name
    assertComparison(cmp, people[7], people[6]);

    // More than one thenComparing
    strcmp = Comparator.nullsLast(Comparator.comparingInt(String::length)
                                            .thenComparing(String.CASE_INSENSITIVE_ORDER));
    assertComparison(strcmp, "abc", null);
    assertComparison(strcmp, "ab", "abc");
    assertComparison(strcmp, "abc", "def");

    // Ensure reverse still handle null properly
    Comparator<String> strcmp2 = strcmp.reversed().thenComparing(Comparator.naturalOrder());
    assertComparison(strcmp2, null, "abc");
    assertComparison(strcmp2, "abc", "ab");
    assertComparison(strcmp2, "def", "abc");
    assertComparison(strcmp2, "ABC", "abc");

    // Considering non-null values to be equal
    Comparator<String> blind = Comparator.nullsLast(null);
    assertComparison(blind, "abc", null);
    assertEquals(0, blind.compare("abc", "def"));
    // reverse still consider non-null values to be equal
    strcmp = blind.reversed();
    assertComparison(strcmp, null, "abc");
    assertEquals(0, strcmp.compare("abc", "def"));
    // chain with another comparator to compare non-nulls
    strcmp = blind.thenComparing(Comparator.naturalOrder());
    assertComparison(strcmp, "abc", null);
    assertComparison(strcmp, "abc", "def");
}
 
Example 17
Source File: BasicTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void testNullsFirst() {
    Comparator<String> strcmp = Comparator.nullsFirst(Comparator.naturalOrder());
    Comparator<People> cmp = Comparator.comparing(People::getLastName, strcmp)
                                       .thenComparing(People::getFirstName, strcmp);
    // Mary.null vs Mary.Cook - solve by last name
    assertComparison(cmp, people[6], people[5]);
    // John.null vs Mary.null - solve by first name
    assertComparison(cmp, people[7], people[6]);

    // More than one thenComparing
    strcmp = Comparator.nullsFirst(Comparator.comparingInt(String::length)
                                             .thenComparing(String.CASE_INSENSITIVE_ORDER));
    assertComparison(strcmp, null, "abc");
    assertComparison(strcmp, "ab", "abc");
    assertComparison(strcmp, "abc", "def");
    assertEquals(0, strcmp.compare("abc", "ABC"));

    // Ensure reverse still handle null properly
    Comparator<String> strcmp2 = strcmp.reversed().thenComparing(Comparator.naturalOrder());
    assertComparison(strcmp2, "abc", null);
    assertComparison(strcmp2, "abc", "ab");
    assertComparison(strcmp2, "def", "abc");
    assertComparison(strcmp2, "ABC", "abc");

    // Considering non-null values to be equal
    Comparator<String> blind = Comparator.nullsFirst(null);
    assertComparison(blind, null, "abc");
    assertEquals(0, blind.compare("abc", "def"));
    // reverse still consider non-null values to be equal
    strcmp = blind.reversed();
    assertComparison(strcmp, "abc", null);
    assertEquals(0, strcmp.compare("abc", "def"));
    // chain with another comparator to compare non-nulls
    strcmp = blind.thenComparing(Comparator.naturalOrder());
    assertComparison(strcmp, null, "abc");
    assertComparison(strcmp, "abc", "def");
}
 
Example 18
Source File: BasicTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void testNullsFirst() {
    Comparator<String> strcmp = Comparator.nullsFirst(Comparator.naturalOrder());
    Comparator<People> cmp = Comparator.comparing(People::getLastName, strcmp)
                                       .thenComparing(People::getFirstName, strcmp);
    // Mary.null vs Mary.Cook - solve by last name
    assertComparison(cmp, people[6], people[5]);
    // John.null vs Mary.null - solve by first name
    assertComparison(cmp, people[7], people[6]);

    // More than one thenComparing
    strcmp = Comparator.nullsFirst(Comparator.comparingInt(String::length)
                                             .thenComparing(String.CASE_INSENSITIVE_ORDER));
    assertComparison(strcmp, null, "abc");
    assertComparison(strcmp, "ab", "abc");
    assertComparison(strcmp, "abc", "def");
    assertEquals(0, strcmp.compare("abc", "ABC"));

    // Ensure reverse still handle null properly
    Comparator<String> strcmp2 = strcmp.reversed().thenComparing(Comparator.naturalOrder());
    assertComparison(strcmp2, "abc", null);
    assertComparison(strcmp2, "abc", "ab");
    assertComparison(strcmp2, "def", "abc");
    assertComparison(strcmp2, "ABC", "abc");

    // Considering non-null values to be equal
    Comparator<String> blind = Comparator.nullsFirst(null);
    assertComparison(blind, null, "abc");
    assertEquals(0, blind.compare("abc", "def"));
    // reverse still consider non-null values to be equal
    strcmp = blind.reversed();
    assertComparison(strcmp, "abc", null);
    assertEquals(0, strcmp.compare("abc", "def"));
    // chain with another comparator to compare non-nulls
    strcmp = blind.thenComparing(Comparator.naturalOrder());
    assertComparison(strcmp, null, "abc");
    assertComparison(strcmp, "abc", "def");
}
 
Example 19
Source File: BasicTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void testReverseComparator() {
    Comparator<String> cmpr = Comparator.reverseOrder();
    Comparator<String> cmp = cmpr.reversed();

    assertEquals(cmp.reversed(), cmpr);
    assertEquals(0, cmp.compare("a", "a"));
    assertEquals(0, cmpr.compare("a", "a"));
    assertTrue(cmp.compare("a", "b") < 0);
    assertTrue(cmpr.compare("a", "b") > 0);
    assertTrue(cmp.compare("b", "a") > 0);
    assertTrue(cmpr.compare("b", "a") < 0);
}
 
Example 20
Source File: BasicTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void testReverseComparator() {
    Comparator<String> cmpr = Comparator.reverseOrder();
    Comparator<String> cmp = cmpr.reversed();

    assertEquals(cmp.reversed(), cmpr);
    assertEquals(0, cmp.compare("a", "a"));
    assertEquals(0, cmpr.compare("a", "a"));
    assertTrue(cmp.compare("a", "b") < 0);
    assertTrue(cmpr.compare("a", "b") > 0);
    assertTrue(cmp.compare("b", "a") > 0);
    assertTrue(cmpr.compare("b", "a") < 0);
}