Skip to content

Commit 95d5ff9

Browse files
committed
Update Iterables.
1 parent 313b99c commit 95d5ff9

File tree

3 files changed

+272
-12
lines changed

3 files changed

+272
-12
lines changed

README.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,14 @@ public static <T> Function<Iterable<T>, Double> toAverage(ToIntFunction<T> trans
11881188
public static <T> Function<Iterable<T>, Double> toAverage(ToLongFunction<T> transform) { ... }
11891189
public static <T> Function<Iterable<T>, Double> toAverage(ToDoubleFunction<T> transform) { ... }
11901190

1191+
public static <T> Function<Iterable<T>, Integer> toMinimum(ToIntFunction<T> transform) { ... }
1192+
public static <T> Function<Iterable<T>, Long> toMinimum(ToLongFunction<T> transform) { ... }
1193+
public static <T> Function<Iterable<T>, Double> toMinimum(ToDoubleFunction<T> transform) { ... }
1194+
1195+
public static <T> Function<Iterable<T>, Integer> toMaximum(ToIntFunction<T> transform) { ... }
1196+
public static <T> Function<Iterable<T>, Long> toMaximum(ToLongFunction<T> transform) { ... }
1197+
public static <T> Function<Iterable<T>, Double> toMaximum(ToDoubleFunction<T> transform) { ... }
1198+
11911199
public static <T extends Comparable<? super T>> Function<Iterable<T>, T> toMinimum() { ... }
11921200
public static <T extends Comparable<? super T>> Function<Iterable<T>, T> toMaximum() { ... }
11931201
```
@@ -1200,24 +1208,12 @@ var values = listOf(1, 2, 3, 4, 5);
12001208
var result = map(values, toSum(Integer::intValue)); // 15
12011209
```
12021210

1203-
```java
1204-
var values = listOf(1, 2, 3, 4, 5);
1205-
1206-
var result = map(values, toAverage(Integer::intValue)); // 3.0
1207-
```
1208-
12091211
```java
12101212
var values = listOf("a", "b", "c", "d", "e");
12111213

12121214
var result = map(values, toMinimum()); // a
12131215
```
12141216

1215-
```java
1216-
var values = listOf("a", "b", "c", "d", "e");
1217-
1218-
var result = map(values, toMaximum()); // e
1219-
```
1220-
12211217
Finally, `Iterables` provides this method, which coerces a value to a given type:
12221218

12231219
```java

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

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,192 @@ public static <T> Function<Iterable<T>, Double> toAverage(ToDoubleFunction<T> tr
396396
};
397397
}
398398

399+
/**
400+
* Returns a function that calculates a minimum.
401+
*
402+
* @param <T>
403+
* The element type.
404+
*
405+
* @param transform
406+
* The transform function.
407+
*
408+
* @return
409+
* The reduction function.
410+
*/
411+
public static <T> Function<Iterable<T>, Integer> toMinimum(ToIntFunction<T> transform) {
412+
if (transform == null) {
413+
throw new IllegalArgumentException();
414+
}
415+
416+
return iterable -> {
417+
var minimum = Integer.MAX_VALUE;
418+
var n = 0;
419+
420+
for (var element : iterable) {
421+
minimum = Math.min(transform.applyAsInt(element), minimum);
422+
423+
n++;
424+
}
425+
426+
return n > 0 ? minimum : null;
427+
};
428+
}
429+
430+
/**
431+
* Returns a function that calculates a minimum.
432+
*
433+
* @param <T>
434+
* The element type.
435+
*
436+
* @param transform
437+
* The transform function.
438+
*
439+
* @return
440+
* The reduction function.
441+
*/
442+
public static <T> Function<Iterable<T>, Long> toMinimum(ToLongFunction<T> transform) {
443+
if (transform == null) {
444+
throw new IllegalArgumentException();
445+
}
446+
447+
return iterable -> {
448+
var minimum = Long.MAX_VALUE;
449+
var n = 0;
450+
451+
for (var element : iterable) {
452+
minimum = Math.min(transform.applyAsLong(element), minimum);
453+
454+
n++;
455+
}
456+
457+
return n > 0 ? minimum : null;
458+
};
459+
}
460+
461+
/**
462+
* Returns a function that calculates a minimum.
463+
*
464+
* @param <T>
465+
* The element type.
466+
*
467+
* @param transform
468+
* The transform function.
469+
*
470+
* @return
471+
* The reduction function.
472+
*/
473+
public static <T> Function<Iterable<T>, Double> toMinimum(ToDoubleFunction<T> transform) {
474+
if (transform == null) {
475+
throw new IllegalArgumentException();
476+
}
477+
478+
return iterable -> {
479+
var minimum = Double.POSITIVE_INFINITY;
480+
var n = 0;
481+
482+
for (var element : iterable) {
483+
minimum = Math.min(transform.applyAsDouble(element), minimum);
484+
485+
n++;
486+
}
487+
488+
return n > 0 ? minimum : null;
489+
};
490+
}
491+
492+
/**
493+
* Returns a function that calculates a maximum.
494+
*
495+
* @param <T>
496+
* The element type.
497+
*
498+
* @param transform
499+
* The transform function.
500+
*
501+
* @return
502+
* The reduction function.
503+
*/
504+
public static <T> Function<Iterable<T>, Integer> toMaximum(ToIntFunction<T> transform) {
505+
if (transform == null) {
506+
throw new IllegalArgumentException();
507+
}
508+
509+
return iterable -> {
510+
var maximum = Integer.MIN_VALUE;
511+
var n = 0;
512+
513+
for (var element : iterable) {
514+
maximum = Math.max(transform.applyAsInt(element), maximum);
515+
516+
n++;
517+
}
518+
519+
return n > 0 ? maximum : null;
520+
};
521+
}
522+
523+
/**
524+
* Returns a function that calculates a maximum.
525+
*
526+
* @param <T>
527+
* The element type.
528+
*
529+
* @param transform
530+
* The transform function.
531+
*
532+
* @return
533+
* The reduction function.
534+
*/
535+
public static <T> Function<Iterable<T>, Long> toMaximum(ToLongFunction<T> transform) {
536+
if (transform == null) {
537+
throw new IllegalArgumentException();
538+
}
539+
540+
return iterable -> {
541+
var maximum = Long.MIN_VALUE;
542+
var n = 0;
543+
544+
for (var element : iterable) {
545+
maximum = Math.max(transform.applyAsLong(element), maximum);
546+
547+
n++;
548+
}
549+
550+
return n > 0 ? maximum : null;
551+
};
552+
}
553+
554+
/**
555+
* Returns a function that calculates a maximum.
556+
*
557+
* @param <T>
558+
* The element type.
559+
*
560+
* @param transform
561+
* The transform function.
562+
*
563+
* @return
564+
* The reduction function.
565+
*/
566+
public static <T> Function<Iterable<T>, Double> toMaximum(ToDoubleFunction<T> transform) {
567+
if (transform == null) {
568+
throw new IllegalArgumentException();
569+
}
570+
571+
return iterable -> {
572+
var maximum = Double.NEGATIVE_INFINITY;
573+
var n = 0;
574+
575+
for (var element : iterable) {
576+
maximum = Math.max(transform.applyAsDouble(element), maximum);
577+
578+
n++;
579+
}
580+
581+
return n > 0 ? maximum : null;
582+
};
583+
}
584+
399585
/**
400586
* Returns a function that calculates a minimum.
401587
*

kilo-client/src/test/java/org/httprpc/kilo/util/IterablesTest.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,84 @@ public void testToAverageDouble() {
162162
assertEquals(Double.NaN, map(emptyListOf(Integer.class), toAverage(Integer::doubleValue)));
163163
}
164164

165+
@Test
166+
public void testToMinimumInt() {
167+
var values = listOf(1, 2, 3, 4, 5);
168+
169+
var result = map(values, toMinimum(Integer::intValue)); // 1
170+
171+
assertEquals(1, result);
172+
173+
assertEquals(result, values.stream().mapToInt(Integer::intValue).min().orElseThrow());
174+
175+
assertNull(map(emptyListOf(Integer.class), toMinimum(Integer::intValue)));
176+
}
177+
178+
@Test
179+
public void testToMinimumLong() {
180+
var values = listOf(1, 2, 3, 4, 5);
181+
182+
var result = map(values, toMinimum(Integer::longValue)); // 1L
183+
184+
assertEquals(1L, result);
185+
186+
assertEquals(result, values.stream().mapToLong(Integer::longValue).min().orElseThrow());
187+
188+
assertNull(map(emptyListOf(Integer.class), toMinimum(Integer::longValue)));
189+
}
190+
191+
@Test
192+
public void testToMinimumDouble() {
193+
var values = listOf(1, 2, 3, 4, 5);
194+
195+
var result = map(values, toMinimum(Integer::doubleValue)); // 1.0
196+
197+
assertEquals(1.0, result);
198+
199+
assertEquals(result, values.stream().mapToDouble(Integer::doubleValue).min().orElseThrow());
200+
201+
assertNull(map(emptyListOf(Integer.class), toMinimum(Integer::doubleValue)));
202+
}
203+
204+
@Test
205+
public void testToMaximumInt() {
206+
var values = listOf(1, 2, 3, 4, 5);
207+
208+
var result = map(values, toMaximum(Integer::intValue)); // 5
209+
210+
assertEquals(5, result);
211+
212+
assertEquals(result, values.stream().mapToInt(Integer::intValue).max().orElseThrow());
213+
214+
assertNull(map(emptyListOf(Integer.class), toMaximum(Integer::intValue)));
215+
}
216+
217+
@Test
218+
public void testToMaximumLong() {
219+
var values = listOf(1, 2, 3, 4, 5);
220+
221+
var result = map(values, toMaximum(Integer::longValue)); // 5L
222+
223+
assertEquals(5L, result);
224+
225+
assertEquals(result, values.stream().mapToLong(Integer::longValue).max().orElseThrow());
226+
227+
assertNull(map(emptyListOf(Integer.class), toMaximum(Integer::longValue)));
228+
}
229+
230+
@Test
231+
public void testToMaximumDouble() {
232+
var values = listOf(1, 2, 3, 4, 5);
233+
234+
var result = map(values, toMaximum(Integer::doubleValue)); // 5.0
235+
236+
assertEquals(5.0, result);
237+
238+
assertEquals(result, values.stream().mapToDouble(Integer::doubleValue).max().orElseThrow());
239+
240+
assertNull(map(emptyListOf(Integer.class), toMaximum(Integer::doubleValue)));
241+
}
242+
165243
@Test
166244
public void testToMinimum() {
167245
var values = listOf("a", "b", "c", "d", "e");

0 commit comments

Comments
 (0)