1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
18
18
19
19
import java .lang .annotation .Annotation ;
20
20
import java .util .ArrayList ;
21
+ import java .util .Collection ;
21
22
import java .util .LinkedHashSet ;
22
- import java .util .List ;
23
23
import java .util .Set ;
24
24
import java .util .function .Function ;
25
25
import java .util .function .IntFunction ;
31
31
import org .springframework .util .MultiValueMap ;
32
32
33
33
/**
34
- * Collector implementations that provide various reduction operations for
34
+ * {@link Collector} implementations that provide various reduction operations for
35
35
* {@link MergedAnnotation} instances.
36
36
*
37
37
* @author Phillip Webb
38
+ * @author Sam Brannen
38
39
* @since 5.2
39
40
*/
40
41
public abstract class MergedAnnotationCollectors {
@@ -52,13 +53,16 @@ private MergedAnnotationCollectors() {
52
53
* Create a new {@link Collector} that accumulates merged annotations to a
53
54
* {@link LinkedHashSet} containing {@linkplain MergedAnnotation#synthesize()
54
55
* synthesized} versions.
56
+ * <p>The collector returned by this method is effectively equivalent to
57
+ * {@code Collectors.mapping(MergedAnnotation::synthesize, Collectors.toCollection(LinkedHashSet::new))}
58
+ * but avoids the creation of a composite collector.
55
59
* @param <A> the annotation type
56
60
* @return a {@link Collector} which collects and synthesizes the
57
61
* annotations into a {@link Set}
58
62
*/
59
63
public static <A extends Annotation > Collector <MergedAnnotation <A >, ?, Set <A >> toAnnotationSet () {
60
- return Collector .of (ArrayList < A > ::new , (list , annotation ) -> list .add (annotation .synthesize ()),
61
- MergedAnnotationCollectors ::addAll , LinkedHashSet :: new );
64
+ return Collector .of (LinkedHashSet ::new , (set , annotation ) -> set .add (annotation .synthesize ()),
65
+ MergedAnnotationCollectors ::combiner );
62
66
}
63
67
64
68
/**
@@ -90,14 +94,14 @@ private MergedAnnotationCollectors() {
90
94
IntFunction <R []> generator ) {
91
95
92
96
return Collector .of (ArrayList ::new , (list , annotation ) -> list .add (annotation .synthesize ()),
93
- MergedAnnotationCollectors ::addAll , list -> list .toArray (generator .apply (list .size ())));
97
+ MergedAnnotationCollectors ::combiner , list -> list .toArray (generator .apply (list .size ())));
94
98
}
95
99
96
100
/**
97
- * Create a new {@link Collector} that accumulates merged annotations to an
101
+ * Create a new {@link Collector} that accumulates merged annotations to a
98
102
* {@link MultiValueMap} with items {@linkplain MultiValueMap#add(Object, Object)
99
103
* added} from each merged annotation
100
- * {@link MergedAnnotation#asMap(Adapt...) as a map}.
104
+ * {@linkplain MergedAnnotation#asMap(Adapt...) as a map}.
101
105
* @param <A> the annotation type
102
106
* @param adaptations the adaptations that should be applied to the annotation values
103
107
* @return a {@link Collector} which collects and synthesizes the
@@ -111,13 +115,13 @@ private MergedAnnotationCollectors() {
111
115
}
112
116
113
117
/**
114
- * Create a new {@link Collector} that accumulates merged annotations to an
118
+ * Create a new {@link Collector} that accumulates merged annotations to a
115
119
* {@link MultiValueMap} with items {@linkplain MultiValueMap#add(Object, Object)
116
120
* added} from each merged annotation
117
- * {@link MergedAnnotation#asMap(Adapt...) as a map}.
121
+ * {@linkplain MergedAnnotation#asMap(Adapt...) as a map}.
118
122
* @param <A> the annotation type
119
- * @param adaptations the adaptations that should be applied to the annotation values
120
123
* @param finisher the finisher function for the new {@link MultiValueMap}
124
+ * @param adaptations the adaptations that should be applied to the annotation values
121
125
* @return a {@link Collector} which collects and synthesizes the
122
126
* annotations into a {@link LinkedMultiValueMap}
123
127
* @see #toMultiValueMap(MergedAnnotation.Adapt...)
@@ -130,21 +134,30 @@ private MergedAnnotationCollectors() {
130
134
IDENTITY_FINISH_CHARACTERISTICS : NO_CHARACTERISTICS );
131
135
return Collector .of (LinkedMultiValueMap ::new ,
132
136
(map , annotation ) -> annotation .asMap (adaptations ).forEach (map ::add ),
133
- MergedAnnotationCollectors ::merge , finisher , characteristics );
137
+ MergedAnnotationCollectors ::combiner , finisher , characteristics );
134
138
}
135
139
136
140
137
141
private static boolean isSameInstance (Object instance , Object candidate ) {
138
142
return instance == candidate ;
139
143
}
140
144
141
- private static <E , L extends List <E >> L addAll (L list , L additions ) {
142
- list .addAll (additions );
143
- return list ;
145
+ /**
146
+ * {@link Collector#combiner() Combiner} for collections.
147
+ * <p>This method is only invoked if the {@link java.util.stream.Stream} is
148
+ * processed in {@linkplain java.util.stream.Stream#parallel() parallel}.
149
+ */
150
+ private static <E , C extends Collection <E >> C combiner (C collection , C additions ) {
151
+ collection .addAll (additions );
152
+ return collection ;
144
153
}
145
154
146
- private static <K , V > MultiValueMap <K , V > merge (MultiValueMap <K , V > map ,
147
- MultiValueMap <K , V > additions ) {
155
+ /**
156
+ * {@link Collector#combiner() Combiner} for multi-value maps.
157
+ * <p>This method is only invoked if the {@link java.util.stream.Stream} is
158
+ * processed in {@linkplain java.util.stream.Stream#parallel() parallel}.
159
+ */
160
+ private static <K , V > MultiValueMap <K , V > combiner (MultiValueMap <K , V > map , MultiValueMap <K , V > additions ) {
148
161
map .addAll (additions );
149
162
return map ;
150
163
}
0 commit comments