18
18
19
19
import java .util .Collections ;
20
20
import java .util .Iterator ;
21
- import java .util .LinkedHashMap ;
22
21
import java .util .Map ;
23
22
import java .util .Map .Entry ;
24
23
import java .util .function .Function ;
24
+ import java .util .stream .Collectors ;
25
25
26
26
import org .springframework .util .Assert ;
27
27
37
37
*/
38
38
abstract class NamedContributorsMapAdapter <V , C > implements NamedContributors <C > {
39
39
40
- private final Map <String , C > namedContributorsMap ;
40
+ private final Map <String , C > map ;
41
41
42
42
NamedContributorsMapAdapter (Map <String , V > map , Function <V , ? extends C > valueAdapter ) {
43
43
Assert .notNull (map , "Map must not be null" );
44
44
Assert .notNull (valueAdapter , "ValueAdapter must not be null" );
45
- this .namedContributorsMap = getContributorsMap (map , valueAdapter );
46
- }
47
-
48
- private Map <String , C > getContributorsMap (Map <String , V > map , Function <V , ? extends C > valueAdapter ) {
49
- Map <String , C > contributorsMap = new LinkedHashMap <>(map .size ());
50
- map .forEach ((name , value ) -> {
51
- this .validateKey (name );
52
- C contributor = adapt (value , valueAdapter );
53
- Assert .notNull (contributor , "Map must not contain null values" );
54
- contributorsMap .put (name , contributor );
55
- });
56
- return Collections .unmodifiableMap (contributorsMap );
45
+ map .keySet ().forEach (this ::validateKey );
46
+ this .map = Collections .unmodifiableMap (map .entrySet ().stream ()
47
+ .collect (Collectors .toMap (Entry ::getKey , (entry ) -> adapt (entry .getValue (), valueAdapter ))));
57
48
}
58
49
59
50
private void validateKey (String value ) {
60
51
Assert .notNull (value , "Map must not contain null keys" );
61
52
Assert .isTrue (!value .contains ("/" ), "Map keys must not contain a '/'" );
53
+ }
62
54
55
+ private C adapt (V value , Function <V , ? extends C > valueAdapter ) {
56
+ C contributor = (value != null ) ? valueAdapter .apply (value ) : null ;
57
+ Assert .notNull (contributor , "Map must not contain null values" );
58
+ return contributor ;
63
59
}
64
60
65
61
@ Override
66
62
public Iterator <NamedContributor <C >> iterator () {
67
- Iterator <Entry <String , C >> iterator = this .namedContributorsMap .entrySet ().iterator ();
63
+ Iterator <Entry <String , C >> iterator = this .map .entrySet ().iterator ();
68
64
return new Iterator <NamedContributor <C >>() {
69
65
70
66
@ Override
@@ -83,11 +79,7 @@ public NamedContributor<C> next() {
83
79
84
80
@ Override
85
81
public C getContributor (String name ) {
86
- return this .namedContributorsMap .get (name );
87
- }
88
-
89
- private C adapt (V value , Function <V , ? extends C > valueAdapter ) {
90
- return (value != null ) ? valueAdapter .apply (value ) : null ;
82
+ return this .map .get (name );
91
83
}
92
84
93
85
}
0 commit comments