-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathArrayCollectors.java
More file actions
127 lines (106 loc) · 3.36 KB
/
ArrayCollectors.java
File metadata and controls
127 lines (106 loc) · 3.36 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package javasabr.rlib.collections.array;
import static java.util.Collections.unmodifiableSet;
import java.util.EnumSet;
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;
import java.util.stream.Collector.Characteristics;
import lombok.experimental.UtilityClass;
/**
* Provides {@link Collector} implementations for collecting stream elements into {@link Array} instances.
*
* @since 10.0.0
*/
@UtilityClass
public class ArrayCollectors {
private static Set<Characteristics> CH_ID =
unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH));
private static Set<Characteristics> CH_ID_CONC = unmodifiableSet(EnumSet.of(
Characteristics.IDENTITY_FINISH,
Characteristics.CONCURRENT));
private static <T, M extends MutableArray<T>> Collector<T, M, Array<T>> collector(
Class<? super T> type,
Function<Class<? super T>, M> arrayFactory) {
return new Collector<>() {
private final Supplier<M> supplier = () -> arrayFactory.apply(type);
@Override
public Supplier<M> supplier() {
return supplier;
}
@Override
public BiConsumer<M, T> accumulator() {
return MutableArray::add;
}
@Override
public BinaryOperator<M> combiner() {
return (source, toAdd) -> {
source.addAll(toAdd);
return source;
};
}
@Override
public Function<M, Array<T>> finisher() {
return Array::copyOf;
}
@Override
public Set<Characteristics> characteristics() {
return CH_ID;
}
};
}
private static <T, A extends MutableArray<T>> Collector<T, A, A> mutableCollector(
Class<? super T> type,
Function<Class<? super T>, A> arrayFactory) {
return new Collector<>() {
private final Supplier<A> supplier = () -> arrayFactory.apply(type);
@Override
public Supplier<A> supplier() {
return supplier;
}
@Override
public BiConsumer<A, T> accumulator() {
return MutableArray::add;
}
@Override
public BinaryOperator<A> combiner() {
return (source, toAdd) -> {
source.addAll(toAdd);
return source;
};
}
@Override
public Function<A, A> finisher() {
return array -> array;
}
@Override
public Set<Characteristics> characteristics() {
return CH_ID;
}
};
}
/**
* Returns a collector that accumulates elements into a mutable array.
*
* @param <T> the type of elements
* @param type the component type of the array
* @return a collector that collects elements into a mutable array
* @since 10.0.0
*/
public static <T> Collector<T, MutableArray<T>, MutableArray<T>> toMutableArray(Class<? super T> type) {
return mutableCollector(type, ArrayFactory::mutableArray);
}
/**
* Returns a collector that accumulates elements into an immutable array.
*
* @param <T> the type of elements
* @param type the component type of the array
* @return a collector that collects elements into an immutable array
* @since 10.0.0
*/
public static <T> Collector<T, MutableArray<T>, Array<T>> toArray(Class<? super T> type) {
return collector(type, ArrayFactory::mutableArray);
}
}