-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdjacentNumberAverageCollector.java
58 lines (51 loc) · 1.36 KB
/
AdjacentNumberAverageCollector.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
public class AdjacentNumberAverageCollector<T extends Number> implements Collector<T, List<T>, List<T>> {
@Override
public Supplier<List<T>> supplier() {
return LinkedList<T>::new;
}
@Override
public BiConsumer<List<T>,T> accumulator() {
return List<T>::add;
}
@Override
public BinaryOperator<List<T>> combiner() {
return ( left, right ) -> { left.addAll(right); return left; };
}
@Override
public Function<List<T>,List<T>> finisher() {
return (numbers)->{
List<T> result=new LinkedList<T>();
Iterator<T> i=numbers.iterator();
if(i.hasNext()) {
T left=i.next();
if(i.hasNext()) {
T middle=i.next();
while(i.hasNext()) {
T right=i.next();
long average=(left.longValue()+right.longValue())/2L;
if(middle.longValue()==average) {
result.add(middle);
}
left=middle;
middle=right;
};
}
}
return result;
};
}
@Override
public Set<Characteristics> characteristics() {
return Collections.emptySet();
}
}