509 Objects.requireNonNull(keyExtractor);
510 return (Comparator<T> & Serializable)
511 (c1, c2) -> Long.compare(keyExtractor.applyAsLong(c1), keyExtractor.applyAsLong(c2));
512 }
513
514 /**
515 * Accepts a function that extracts a {@code double} sort key from a type
516 * {@code T}, and returns a {@code Comparator<T>} that compares by that
517 * sort key.
518 *
519 * <p>The returned comparator is serializable if the specified function
520 * is also serializable.
521 *
522 * @param <T> the type of element to be compared
523 * @param keyExtractor the function used to extract the double sort key
524 * @return a comparator that compares by an extracted key
525 * @see #comparing(Function)
526 * @throws NullPointerException if the argument is null
527 * @since 1.8
528 */
529 public static<T> Comparator<T> comparingDouble(ToDoubleFunction<? super T> keyExtractor) {
530 Objects.requireNonNull(keyExtractor);
531 return (Comparator<T> & Serializable)
532 (c1, c2) -> Double.compare(keyExtractor.applyAsDouble(c1), keyExtractor.applyAsDouble(c2));
533 }
534 }
|
509 Objects.requireNonNull(keyExtractor);
510 return (Comparator<T> & Serializable)
511 (c1, c2) -> Long.compare(keyExtractor.applyAsLong(c1), keyExtractor.applyAsLong(c2));
512 }
513
514 /**
515 * Accepts a function that extracts a {@code double} sort key from a type
516 * {@code T}, and returns a {@code Comparator<T>} that compares by that
517 * sort key.
518 *
519 * <p>The returned comparator is serializable if the specified function
520 * is also serializable.
521 *
522 * @param <T> the type of element to be compared
523 * @param keyExtractor the function used to extract the double sort key
524 * @return a comparator that compares by an extracted key
525 * @see #comparing(Function)
526 * @throws NullPointerException if the argument is null
527 * @since 1.8
528 */
529 public static <T> Comparator<T> comparingDouble(ToDoubleFunction<? super T> keyExtractor) {
530 Objects.requireNonNull(keyExtractor);
531 return (Comparator<T> & Serializable)
532 (c1, c2) -> Double.compare(keyExtractor.applyAsDouble(c1), keyExtractor.applyAsDouble(c2));
533 }
534
535 /**
536 * Returns a comparator that compares {@link CharSequence} objects,
537 * paying special attention to the decimal digit characters.
538 * For example, the following array was sorted using such comparator:
539 * {@code { "abc5", "abc10", "abc15", "xyz5", "xyz10"}}.
540 *
541 * <p>The returned comparator does the following to compare two char
542 * sequences: first, the common prefix of the sequences is skipped;
543 * then, if the sequences are different at the point lying inside or
544 * at the boundary of a subsequence consisting solely of decimal digits,
545 * then the corresponding numerical values are compared; otherwise,
546 * the char sequences are compared literally.
547 *
548 * <p>In the case the numeric values of two compared char sequences
549 * are equal, but their string representations have different number
550 * of leading zeroes, the comparator treats the number with more
551 * leading zeros as greater.
552 * E.g. {@code "abc 1" < "abc 01" < "abc 001"}.
553 *
554 * @implSpec The returned comparator uses
555 * {@link java.lang.Character#compare(char,char)} to compare pairs of
556 * characters of its arguments. The comparator uses
557 * {@link java.lang.Character#isDigit(char)} to test if the character
558 * represents a decimal digit. The comparator uses
559 * {@link java.lang.Character#digit(char, int)} with the second
560 * argument set to {@code 10} to determine the decimal numeric value
561 * of the character.
562 *
563 * @param <T> the type of elements to be compared
564 * @return a comparator that is to compare character sequences
565 *
566 * @since 10
567 */
568 @SuppressWarnings("unchecked")
569 public static <T extends CharSequence> Comparator<T> comparingNumerically() {
570 return (Comparator<T>)Comparators.NumericComparator.INSTANCE_LEADING_ZEROS_LAST_NATURAL;
571 }
572
573 /**
574 * Returns a comparator that compares {@link CharSequence} objects,
575 * paying special attention to the decimal digit characters.
576 * For example, the following array was sorted using such comparator:
577 * {@code { "abc5", "abc10", "abc15", "xyz5", "xyz10"}}.
578 *
579 * <p>The returned comparator does the following to compare two char
580 * sequences: first, the common prefix of the sequences is skipped;
581 * then, if the sequences are different at the point lying inside or
582 * at the boundary of a subsequence, consisting of decimal digits, the
583 * at the boundary of a subsequence consisting solely of decimal digits,
584 * then the corresponding numerical values are compared; otherwise,
585 * the char sequences are compared literally.
586 *
587 * <p>In the case the numeric values of two compared char sequences
588 * are equal, but their string representations have different number
589 * of leading zeroes, the comparator treats the number with more
590 * leading zeros as smaller.
591 * E.g. {@code "abc 001" < "abc 01" < "abc 1"}.
592 *
593 * @implSpec The returned comparator uses
594 * {@link java.lang.Character#compare(char,char)} to compare pairs of
595 * characters of its arguments. The comparator uses
596 * {@link java.lang.Character#isDigit(char)} to test if the character
597 * represents a decimal digit. The comparator uses
598 * {@link java.lang.Character#digit(char, int)} with the second
599 * argument set to {@code 10} to determine the decimal numeric value
600 * of the character.
601 *
602 * @param <T> the type of elements to be compared
603 * @return a comparator that is to compare character sequences
604 *
605 * @since 10
606 */
607 @SuppressWarnings("unchecked")
608 public static <T extends CharSequence> Comparator<T> comparingNumericallyLeadingZerosFirst() {
609 return (Comparator<T>)Comparators.NumericComparator.INSTANCE_LEADING_ZEROS_FIRST_NATURAL;
610 }
611 }
|