forked from ppwozniak/confitura-jamjars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJamJars.java
35 lines (30 loc) · 1.19 KB
/
JamJars.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package pl.allegro.itm.confitura;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import static java.util.Comparator.comparing;
import static java.util.stream.Collector.of;
import static java.util.stream.Collectors.toList;
public class JamJars {
public List<JarBox> giveMeMyJars(List<Shelf> shelves, int jarsInBox) {
BiConsumer<LinkedList<List<Jar>>, Jar> accumulator = (lists, jar) -> {
if (lists.isEmpty() || lists.getLast().size() == jarsInBox) {
lists.add(new ArrayList<>());
}
lists.getLast().add(jar);
};
BinaryOperator<LinkedList<List<Jar>>> combiner = (lists1, lists2) -> {
throw new UnsupportedOperationException("Combine not supported");
};
return shelves.stream()
.flatMap(Shelf::getJarStream)
.filter(Jar::isFresh)
.sorted(comparing(Jar::flavor).reversed())
.collect(of(LinkedList::new, accumulator, combiner))
.stream()
.map(JarBox::new)
.collect(toList());
}
}