4
4
import com .squareup .javapoet .FieldSpec ;
5
5
import com .squareup .javapoet .ParameterSpec ;
6
6
import com .squareup .javapoet .TypeName ;
7
+ import net .jbock .compiler .TypeTool ;
7
8
9
+ import javax .lang .model .type .TypeKind ;
8
10
import javax .lang .model .type .TypeMirror ;
9
11
import java .util .Optional ;
12
+ import java .util .stream .Collectors ;
10
13
11
14
public final class Coercion {
12
15
@@ -20,7 +23,7 @@ public final class Coercion {
20
23
private final CodeBlock initMapper ;
21
24
22
25
// helper.build
23
- private final CodeBlock initCollector ;
26
+ private final Optional < CodeBlock > initCollector ;
24
27
25
28
// impl constructor
26
29
private final CodeBlock extract ;
@@ -31,32 +34,38 @@ public final class Coercion {
31
34
// impl
32
35
private final FieldSpec field ;
33
36
37
+ private final boolean isDefaultCollector ;
38
+
34
39
private Coercion (
35
40
Optional <ParameterSpec > collectorParam ,
36
41
Optional <CodeBlock > mapExpr ,
37
42
CodeBlock initMapper ,
38
- CodeBlock initCollector ,
43
+ Optional < CodeBlock > initCollector ,
39
44
CodeBlock extract ,
40
45
TypeMirror paramType ,
41
- FieldSpec field ) {
46
+ FieldSpec field ,
47
+ boolean isDefaultCollector ) {
42
48
this .collectorParam = collectorParam ;
43
49
this .mapExpr = mapExpr ;
44
50
this .initMapper = initMapper ;
45
51
this .initCollector = initCollector ;
46
52
this .extract = extract ;
47
53
this .paramType = paramType ;
48
54
this .field = field ;
55
+ this .isDefaultCollector = isDefaultCollector ;
49
56
}
50
57
51
58
public static Coercion create (
52
59
Optional <ParameterSpec > collectorParam ,
53
60
Optional <CodeBlock > mapExpr ,
54
61
CodeBlock initMapper ,
55
- CodeBlock initCollector ,
62
+ TypeMirror mapperReturnType ,
63
+ Optional <CodeBlock > initCollector ,
56
64
CodeBlock extract ,
57
65
TypeMirror paramType ,
58
66
BasicInfo basicInfo ) {
59
- return new Coercion (collectorParam , mapExpr , initMapper , initCollector , extract , paramType , basicInfo .fieldSpec ());
67
+ boolean isDefaultCollector = isDefaultCollector (initCollector , paramType , mapperReturnType );
68
+ return new Coercion (collectorParam , mapExpr , initMapper , initCollector , extract , paramType , basicInfo .fieldSpec (), isDefaultCollector );
60
69
}
61
70
62
71
/**
@@ -70,7 +79,10 @@ public CodeBlock initMapper() {
70
79
return initMapper ;
71
80
}
72
81
73
- public CodeBlock initCollector () {
82
+ public Optional <CodeBlock > initCollector () {
83
+ if (skipMapCollect ()) {
84
+ return Optional .empty ();
85
+ }
74
86
return initCollector ;
75
87
}
76
88
@@ -86,26 +98,41 @@ public CodeBlock extract() {
86
98
return extract ;
87
99
}
88
100
89
-
90
101
public Optional <ParameterSpec > collectorParam () {
102
+ if (skipMapCollect ()) {
103
+ return Optional .empty ();
104
+ }
91
105
return collectorParam ;
92
106
}
93
107
94
108
public Optional <CodeBlock > collectExpr () {
95
- if (isDefaultCollector ()) {
96
- return Optional .of (CollectorInfo .standardCollectorInit ());
109
+ if (skipMapCollect ()) {
110
+ return Optional .empty ();
111
+ }
112
+ if (isDefaultCollector ) {
113
+ return Optional .of (CodeBlock .of ("$T.toList()" , Collectors .class ));
97
114
}
98
115
if (!collectorParam .isPresent ()) {
99
116
return Optional .empty ();
100
117
}
101
- return Optional .of (CodeBlock .builder (). add ( "$N" , collectorParam .get ()). build ( ));
118
+ return Optional .of (CodeBlock .of ( "$N" , collectorParam .get ()));
102
119
}
103
120
104
121
public boolean skipMapCollect () {
105
- return !mapExpr .isPresent () && isDefaultCollector () ;
122
+ return !mapExpr .isPresent () && isDefaultCollector ;
106
123
}
107
124
108
- public boolean isDefaultCollector () {
109
- return initCollector .equals (CollectorInfo .standardCollectorInit ());
125
+ private static boolean isDefaultCollector (
126
+ Optional <CodeBlock > initCollector ,
127
+ TypeMirror paramType ,
128
+ TypeMirror mapperReturnType ) {
129
+ if (initCollector .isPresent ()) {
130
+ return false ;
131
+ }
132
+ if (mapperReturnType .getKind () != TypeKind .DECLARED ) {
133
+ return false ;
134
+ }
135
+ TypeTool tool = TypeTool .get ();
136
+ return tool .isSameType (paramType , tool .listOf (mapperReturnType ));
110
137
}
111
138
}
0 commit comments