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

The following examples show how to use java.util.Comparator#nullsFirst() . 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: ParquetTableMetadataUtils.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Returns {@link Comparator} instance considering specified {@code type}.
 *
 * @param type type of the column
 * @return {@link Comparator} instance
 */
public static Comparator getComparator(TypeProtos.MinorType type) {
  switch (type) {
    case INTERVALDAY:
    case INTERVAL:
    case INTERVALYEAR:
      return UNSIGNED_LEXICOGRAPHICAL_BINARY_COMPARATOR;
    case UINT1:
      return Comparator.nullsFirst(UnsignedBytes::compare);
    case UINT2:
    case UINT4:
      return Comparator.nullsFirst(Integer::compareUnsigned);
    case UINT8:
      return Comparator.nullsFirst(Long::compareUnsigned);
    default:
      return getNaturalNullsFirstComparator();
  }
}
 
Example 2
Source File: LimiterTest.java    From streamex with Apache License 2.0 6 votes vote down vote up
@Test
public void testLimiter() {
    Comparator<String> cmp = Comparator.nullsFirst(Comparator.comparingInt(String::length));
    exerciseLimiter("str", Arrays.asList("abc", "abgdc", "abd", "a", "fgssdfg", "sfsvsx", null, "wrffvs",
        "xcvbxvcb", "sffg", "abe", "adf", "abh"), cmp);
    for (int i : new int[] { 10, 100, 1000, 10000, 100000 }) {
        List<Integer> ascending = IntStream.range(0, i).boxed().collect(Collectors.toList());
        exerciseLimiter("asc, nat, " + i, ascending, Comparator.naturalOrder());
        exerciseLimiter("asc, dec, " + i, ascending, Comparator.comparingInt(x -> x / 10));
        List<Integer> descending = IntStream.range(0, i).mapToObj(x -> ~x).collect(Collectors.toList());
        exerciseLimiter("desc, nat, " + i, descending, Comparator.naturalOrder());
        exerciseLimiter("desc, dec, " + i, descending, Comparator.comparingInt(x -> x / 10));
        List<Integer> random = new Random(1).ints(i).boxed().collect(Collectors.toList());
        exerciseLimiter("rnd, nat, " + i, random, Comparator.naturalOrder());
        exerciseLimiter("rnd, dec, " + i, random, Comparator.comparingInt(x -> x / 10));
        List<Integer> randomRange = new Random(1).ints(i, -1000, 1000).boxed().collect(Collectors.toList());
        exerciseLimiter("rnd2, nat, " + i, randomRange, Comparator.naturalOrder());
        exerciseLimiter("rnd2, dec, " + i, randomRange, Comparator.comparingInt(x -> x / 10));
    }
    List<Integer> list = IntStreamEx.range(100000).boxed().toList();
    exerciseLimiter("big", list, list, 50000, Comparator.naturalOrder()); 
    exerciseLimiter("big", list, list, 49999, Comparator.naturalOrder()); 
    exerciseLimiter("big", list, list, 10000, Comparator.naturalOrder());
    exerciseLimiter("big", list, list, Integer.MAX_VALUE / 3, Comparator.naturalOrder());
    exerciseLimiter("big", list, list, Integer.MAX_VALUE / 2, Comparator.naturalOrder());
}
 
Example 3
Source File: Sample.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public int compareTo(final Sample other) {

    int cmp = ID.compareTo(other.getID());

    if (0 == cmp) {
        // use a null-tolerant string comparator for the optional strings
        Comparator<String> compStr = Comparator.nullsFirst(Comparator.comparing(String::toString));

        cmp = compStr.compare(familyID, other.getFamilyID());
        if (0 == cmp) {
            cmp = compStr.compare(paternalID, other.getPaternalID());
        }
        if (0 == cmp) {
            cmp = compStr.compare(maternalID, other.getMaternalID());
        }
        if (0 == cmp) {
            cmp = gender.compareTo(other.getSex());
        }
        if (0 == cmp) {
            cmp = affection.compareTo(other.getAffection());
        }
    }
    return cmp;
}
 
Example 4
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 5
Source File: BasicTest.java    From openjdk-jdk8u-backup 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 6
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 7
Source File: OrderbyDocumentQueryTest.java    From azure-cosmosdb-java with MIT License 5 votes vote down vote up
@Test(groups = { "simple" }, timeOut = TIMEOUT * 10, dataProvider = "sortOrder")
public void queryDocumentsWithOrderByContinuationTokensString(String sortOrder) throws Exception {
    // Get Actual
    String query = String.format("SELECT * FROM c ORDER BY c.id %s", sortOrder);
    
    // Get Expected
    Comparator<String> order = sortOrder.equals("ASC")?Comparator.naturalOrder():Comparator.reverseOrder();
    Comparator<String> validatorComparator = Comparator.nullsFirst(order);
        
    List<String> expectedResourceIds = sortDocumentsAndCollectResourceIds("id", d -> d.getString("id"), validatorComparator);
    this.queryWithContinuationTokensAndPageSizes(query, new int[] { 1, 5, 10, 100 }, expectedResourceIds);
}
 
Example 8
Source File: BasicTest.java    From openjdk-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 9
Source File: BasicTest.java    From hottub 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 10
Source File: BasicTest.java    From jdk8u60 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 11
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 12
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 13
Source File: Java8ComparatorUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenNullsFirst_thenSortedByNameWithNullsFirst() {
    Comparator<Employee> employeeNameComparator = Comparator.comparing(Employee::getName);
    Comparator<Employee> employeeNameComparator_nullFirst = Comparator.nullsFirst(employeeNameComparator);
    Arrays.sort(employeesArrayWithNulls, employeeNameComparator_nullFirst);
    // System.out.println(Arrays.toString(employeesArrayWithNulls));
    assertTrue(Arrays.equals(employeesArrayWithNulls, sortedEmployeesArray_WithNullsFirst));
}
 
Example 14
Source File: ComparisonPredicate.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Creates decimal statistics where min and max values are re-created using given scale.
 *
 * @param statistics statistics that needs to be adjusted
 * @param scale adjustment scale
 * @return adjusted statistics
 */
@SuppressWarnings("unchecked")
private ColumnStatistics<C> adjustDecimalStatistics(ColumnStatistics<C> statistics, int scale) {
  BigInteger min = new BigDecimal((BigInteger) statistics.getStatistic(ColumnStatisticsKind.MIN_VALUE))
      .setScale(scale, RoundingMode.HALF_UP).unscaledValue();
  BigInteger max = new BigDecimal((BigInteger) statistics.getStatistic(ColumnStatisticsKind.MAX_VALUE))
      .setScale(scale, RoundingMode.HALF_UP).unscaledValue();

  return new StatisticsProvider.MinMaxStatistics(min, max, Comparator.nullsFirst(Comparator.naturalOrder()));
}
 
Example 15
Source File: RevisionComparator.java    From nifi-registry with Apache License 2.0 5 votes vote down vote up
@Override
public int compare(final Revision o1, final Revision o2) {
    final int entityComparison = o1.getEntityId().compareTo(o2.getEntityId());
    if (entityComparison != 0) {
        return entityComparison;
    }

    final Comparator<String> nullSafeStringComparator = Comparator.nullsFirst(String::compareTo);
    final int clientComparison = Objects.compare(o1.getClientId(), o2.getClientId(), nullSafeStringComparator);
    if (clientComparison != 0) {
        return clientComparison;
    }

    return o1.getVersion().compareTo(o2.getVersion());
}
 
Example 16
Source File: ComparatorTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public void testNullsFirst() {
    Comparator<Item> comparator = Comparator.nullsFirst(Comparator.naturalOrder());
    checkComparison(comparator, nullsFirstItems);
}
 
Example 17
Source File: StatisticsProvider.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public ColumnStatistics<String> visitQuotedStringConstant(ValueExpressions.QuotedString quotedString, Void value) {
  String binary = quotedString.getString();
  return new MinMaxStatistics<>(binary, binary, Comparator.nullsFirst(Comparator.naturalOrder()));
}
 
Example 18
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static <ELEMENTTYPE extends Comparable <? super ELEMENTTYPE>> CommonsTreeSet <ELEMENTTYPE> newSortedSet ()
{
  return new CommonsTreeSet <> (Comparator.nullsFirst (Comparator.naturalOrder ()));
}
 
Example 19
Source File: IComparator.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Nonnull
static Comparator <String> getComparatorCollating (@Nonnull final Collator aCollator)
{
  ValueEnforcer.notNull (aCollator, "Collator");
  return Comparator.nullsFirst (aCollator::compare);
}
 
Example 20
Source File: IHasErrorField.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Nonnull
static Comparator <IHasErrorField> getComparatorErrorFieldName ()
{
  return Comparator.nullsFirst (Comparator.comparing (IHasErrorField::getErrorFieldName));
}