38
38
import org .slf4j .Logger ;
39
39
import org .slf4j .LoggerFactory ;
40
40
41
+ import java .util .ArrayList ;
41
42
import java .util .LinkedHashMap ;
42
43
import java .util .LinkedList ;
43
44
import java .util .List ;
44
45
import java .util .Map ;
45
46
import java .util .function .Consumer ;
46
47
import java .util .function .Function ;
47
48
import java .util .function .Supplier ;
48
- import java .util .stream .Collectors ;
49
49
50
50
/**
51
51
* Transform a record using a {@link Fix}.
@@ -72,17 +72,20 @@ private RecordTransformer(final Metafix metafix, final List<Expression> expressi
72
72
vars = metafix .getVars ();
73
73
74
74
expressions .forEach (e -> {
75
+ final List <String > params = e .getParams ();
76
+ final Map <String , String > options = options (e .getOptions ());
77
+
75
78
if (e instanceof Do ) {
76
- processDo ((Do ) e );
79
+ processDo ((Do ) e , params , options );
77
80
}
78
81
else if (e instanceof If ) {
79
- processIf ((If ) e );
82
+ processIf ((If ) e , params , options );
80
83
}
81
84
else if (e instanceof Unless ) {
82
- processUnless ((Unless ) e );
85
+ processUnless ((Unless ) e , params , options );
83
86
}
84
87
else if (e instanceof MethodCall ) {
85
- processFunction ((MethodCall ) e );
88
+ processFunction ((MethodCall ) e , params , options );
86
89
}
87
90
else {
88
91
throw new FixProcessException (executionExceptionMessage (e ));
@@ -100,16 +103,16 @@ public void transform(final Record record) {
100
103
});
101
104
}
102
105
103
- private void processDo (final Do expression ) {
106
+ private void processDo (final Do expression , final List < String > params , final Map < String , String > options ) {
104
107
processFix (() -> executionExceptionMessage (expression ), () -> {
105
108
final FixContext context = getInstance (expression .getName (), FixContext .class , FixBind ::valueOf );
106
109
final RecordTransformer recordTransformer = new RecordTransformer (metafix , expression .getElements ());
107
110
108
- return record -> context .execute (metafix , record , params ( expression . getParams ()), options ( expression . getOptions () ), recordTransformer );
111
+ return record -> context .execute (metafix , record , resolveParams ( params ), resolveOptions ( options ), recordTransformer );
109
112
});
110
113
}
111
114
112
- private void processIf (final If ifExpression ) {
115
+ private void processIf (final If ifExpression , final List < String > ifParams , final Map < String , String > ifOptions ) {
113
116
final ElsIf elseIfExpression = ifExpression .getElseIf ();
114
117
final Else elseExpression = ifExpression .getElse ();
115
118
@@ -118,21 +121,37 @@ private void processIf(final If ifExpression) {
118
121
119
122
processFix (() -> executionExceptionMessage (ifExpression , ifExpression .eResource ()), () -> {
120
123
final FixPredicate ifPredicate = getInstance (ifExpression .getName (), FixPredicate .class , FixConditional ::valueOf );
121
- final FixPredicate elseIfPredicate = elseIfExpression != null ? getInstance (elseIfExpression .getName (), FixPredicate .class , FixConditional ::valueOf ) : null ;
122
-
123
124
final RecordTransformer ifTransformer = new RecordTransformer (metafix , ifExpression .getElements ());
124
- final RecordTransformer elseIfTransformer = elseIfExpression != null ? new RecordTransformer (metafix , elseIfExpression .getElements ()) : null ;
125
+
126
+ final FixPredicate elseIfPredicate ;
127
+ final List <String > elseIfParams ;
128
+ final Map <String , String > elseIfOptions ;
129
+ final RecordTransformer elseIfTransformer ;
130
+
131
+ if (elseIfExpression != null ) {
132
+ elseIfPredicate = getInstance (elseIfExpression .getName (), FixPredicate .class , FixConditional ::valueOf );
133
+ elseIfParams = elseIfExpression .getParams ();
134
+ elseIfOptions = options (elseIfExpression .getOptions ());
135
+ elseIfTransformer = new RecordTransformer (metafix , elseIfExpression .getElements ());
136
+ }
137
+ else {
138
+ elseIfPredicate = null ;
139
+ elseIfParams = null ;
140
+ elseIfOptions = null ;
141
+ elseIfTransformer = null ;
142
+ }
143
+
125
144
final RecordTransformer elseTransformer = elseExpression != null ? new RecordTransformer (metafix , elseExpression .getElements ()) : null ;
126
145
127
146
return record -> {
128
- if (ifPredicate .test (metafix , record , params ( ifExpression . getParams ()), options ( null ))) { // TODO: options
147
+ if (ifPredicate .test (metafix , record , resolveParams ( ifParams ), resolveOptions ( ifOptions ))) {
129
148
ifTransformer .transform (record );
130
149
}
131
150
else {
132
151
if (elseIfExpression != null ) {
133
152
currentMessageSupplier = elseIfMessageSupplier ;
134
153
135
- if (elseIfPredicate .test (metafix , record , params ( elseIfExpression . getParams ()), options ( null ))) { // TODO: options
154
+ if (elseIfPredicate .test (metafix , record , resolveParams ( elseIfParams ), resolveOptions ( elseIfOptions ))) {
136
155
elseIfTransformer .transform (record );
137
156
return ;
138
157
}
@@ -147,23 +166,23 @@ private void processIf(final If ifExpression) {
147
166
});
148
167
}
149
168
150
- private void processUnless (final Unless expression ) {
169
+ private void processUnless (final Unless expression , final List < String > params , final Map < String , String > options ) {
151
170
processFix (() -> executionExceptionMessage (expression , expression .eResource ()), () -> {
152
171
final FixPredicate predicate = getInstance (expression .getName (), FixPredicate .class , FixConditional ::valueOf );
153
172
final RecordTransformer recordTransformer = new RecordTransformer (metafix , expression .getElements ());
154
173
155
174
return record -> {
156
- if (!predicate .test (metafix , record , params ( expression . getParams ()), options ( null ))) { // TODO: options
175
+ if (!predicate .test (metafix , record , resolveParams ( params ), resolveOptions ( options ))) {
157
176
recordTransformer .transform (record );
158
177
}
159
178
};
160
179
});
161
180
}
162
181
163
- private void processFunction (final MethodCall expression ) {
182
+ private void processFunction (final MethodCall expression , final List < String > params , final Map < String , String > options ) {
164
183
processFix (() -> executionExceptionMessage (expression ), () -> {
165
184
final FixFunction function = getInstance (expression .getName (), FixFunction .class , FixMethod ::valueOf );
166
- return record -> function .apply (metafix , record , params ( expression . getParams ()), options ( expression . getOptions () ));
185
+ return record -> function .apply (metafix , record , resolveParams ( params ), resolveOptions ( options ));
167
186
});
168
187
}
169
188
@@ -223,8 +242,14 @@ private String resolveVars(final String value) {
223
242
return value == null ? null : StringUtil .format (value , Metafix .VAR_START , Metafix .VAR_END , false , vars );
224
243
}
225
244
226
- private List <String > params (final List <String > params ) {
227
- return params .stream ().map (this ::resolveVars ).collect (Collectors .toList ());
245
+ private List <String > resolveParams (final List <String > params ) {
246
+ final List <String > list = new ArrayList <>(params .size ());
247
+
248
+ for (final String entry : params ) {
249
+ list .add (resolveVars (entry ));
250
+ }
251
+
252
+ return list ;
228
253
}
229
254
230
255
private Map <String , String > options (final Options options ) {
@@ -235,11 +260,21 @@ private Map<String, String> options(final Options options) {
235
260
final List <String > values = options .getValues ();
236
261
237
262
for (int i = 0 ; i < keys .size (); i += 1 ) {
238
- map .put (resolveVars ( keys .get (i )), resolveVars ( values .get (i ) ));
263
+ map .put (keys .get (i ), values .get (i ));
239
264
}
240
265
}
241
266
242
267
return map ;
243
268
}
244
269
270
+ private Map <String , String > resolveOptions (final Map <String , String > options ) {
271
+ final Map <String , String > map = new LinkedHashMap <>(options .size ());
272
+
273
+ for (final Map .Entry <String , String > entry : options .entrySet ()) {
274
+ map .put (resolveVars (entry .getKey ()), resolveVars (entry .getValue ()));
275
+ }
276
+
277
+ return map ;
278
+ }
279
+
245
280
}
0 commit comments