Skip to content

Commit 1a2a57c

Browse files
Use stream to make code more concise
1 parent c127f0f commit 1a2a57c

File tree

2 files changed

+6
-19
lines changed

2 files changed

+6
-19
lines changed

packages/plugins/java/java/src/visitor.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ export class JavaResolversVisitor extends BaseVisitor<
7373
}
7474

7575
if (this._addListImport) {
76-
allImports.push(`java.util.ArrayList`);
7776
allImports.push(`java.util.List`);
7877
allImports.push(`java.util.stream.Collectors`);
7978
}
@@ -238,15 +237,9 @@ export class JavaResolversVisitor extends BaseVisitor<
238237
}
239238
return indentMultiline(
240239
`if (args.get("${arg.name.value}") != null) {
241-
this.${arg.name.value} = new ArrayList<${typeToUse.baseType}>();
242-
for (var o : (${this.config.listType}<Map<String, Object>>) args.get("${arg.name.value}")) {
243-
if (o != null) {
244-
this.${arg.name.value}.add(new ${typeToUse.baseType}(o));
245-
}
246-
else {
247-
this.${arg.name.value}.add(null);
248-
}
249-
}
240+
this.${arg.name.value} = ((${this.config.listType}<Map<String, Object>>) args.get("${arg.name.value}")).stream()
241+
.map(o -> o == null ? null : new ${typeToUse.baseType}(o))
242+
.collect(Collectors.toList());
250243
}`,
251244
3,
252245
);

packages/plugins/java/java/tests/java.spec.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,9 @@ describe('Java', () => {
158158
if (args != null) {
159159
this.f = (Iterable<String>) args.get("f");
160160
if (args.get("g") != null) {
161-
this.g = new ArrayList<SearchUserInput>();
162-
for (var o : (Iterable<Map<String, Object>>) args.get("g")) {
163-
if (o != null) {
164-
this.g.add(new SearchUserInput(o));
165-
}
166-
else {
167-
this.g.add(null);
168-
}
169-
}
161+
this.g = ((Iterable<Map<String, Object>>) args.get("g")).stream()
162+
.map(o -> o == null ? null : new SearchUserInput(o))
163+
.collect(Collectors.toList());
170164
}
171165
}
172166
}

0 commit comments

Comments
 (0)