1717import org .httprpc .kilo .beans .BeanAdapter ;
1818
1919import java .util .ArrayList ;
20+ import java .util .Comparator ;
2021import java .util .Iterator ;
2122import java .util .LinkedHashMap ;
2223import java .util .List ;
@@ -561,11 +562,44 @@ public static <T> Function<Iterable<T>, Double> toMaximum(ToDoubleFunction<T> tr
561562 * The reduction function.
562563 */
563564 public static <T extends Comparable <? super T >> Function <Iterable <T >, T > toMinimum () {
565+ return toMinimum (Comparator .naturalOrder ());
566+ }
567+
568+ /**
569+ * Returns a function that calculates a maximum.
570+ *
571+ * @param <T>
572+ * The element type.
573+ *
574+ * @return
575+ * The reduction function.
576+ */
577+ public static <T extends Comparable <? super T >> Function <Iterable <T >, T > toMaximum () {
578+ return toMaximum (Comparator .naturalOrder ());
579+ }
580+
581+ /**
582+ * Returns a function that calculates a minimum.
583+ *
584+ * @param <T>
585+ * The element type.
586+ *
587+ * @param comparator
588+ * The comparator.
589+ *
590+ * @return
591+ * The reduction function.
592+ */
593+ public static <T > Function <Iterable <T >, T > toMinimum (Comparator <? super T > comparator ) {
594+ if (comparator == null ) {
595+ throw new IllegalArgumentException ();
596+ }
597+
564598 return iterable -> {
565599 T minimum = null ;
566600
567601 for (var element : iterable ) {
568- if (minimum == null || element . compareTo ( minimum ) < 0 ) {
602+ if (minimum == null || comparator . compare ( element , minimum ) < 0 ) {
569603 minimum = element ;
570604 }
571605 }
@@ -580,15 +614,22 @@ public static <T extends Comparable<? super T>> Function<Iterable<T>, T> toMinim
580614 * @param <T>
581615 * The element type.
582616 *
617+ * @param comparator
618+ * The comparator.
619+ *
583620 * @return
584621 * The reduction function.
585622 */
586- public static <T extends Comparable <? super T >> Function <Iterable <T >, T > toMaximum () {
623+ public static <T > Function <Iterable <T >, T > toMaximum (Comparator <? super T > comparator ) {
624+ if (comparator == null ) {
625+ throw new IllegalArgumentException ();
626+ }
627+
587628 return iterable -> {
588629 T maximum = null ;
589630
590631 for (var element : iterable ) {
591- if (maximum == null || element . compareTo ( maximum ) > 0 ) {
632+ if (maximum == null || comparator . compare ( element , maximum ) > 0 ) {
592633 maximum = element ;
593634 }
594635 }
0 commit comments