Skip to content

Commit b16cdb8

Browse files
committed
Update Iterables.
1 parent 74ba4da commit b16cdb8

2 files changed

Lines changed: 47 additions & 3 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,9 @@ public static <T> Function<Iterable<T>, Double> toMaximum(ToDoubleFunction<T> tr
11991199
public static <T extends Comparable<? super T>> Function<Iterable<T>, T> toMinimum() { ... }
12001200
public static <T extends Comparable<? super T>> Function<Iterable<T>, T> toMaximum() { ... }
12011201

1202+
public static <T> Function<Iterable<T>, T> toMinimum(Comparator<? super T> comparator) { ... }
1203+
public static <T> Function<Iterable<T>, T> toMaximum(Comparator<? super T> comparator) { ... }
1204+
12021205
public static <T, K> Function<Iterable<T>, Map<K, List<T>>> groupingBy(Function<? super T, ? extends K> classifier) { ... }
12031206
```
12041207

kilo-client/src/main/java/org/httprpc/kilo/util/Iterables.java

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.httprpc.kilo.beans.BeanAdapter;
1818

1919
import java.util.ArrayList;
20+
import java.util.Comparator;
2021
import java.util.Iterator;
2122
import java.util.LinkedHashMap;
2223
import 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

Comments
 (0)