Skip to content

Commit 3d0cb90

Browse files
marschallfmbenhassine
authored andcommitted
Remove double brace initialization
Remove double brace collection initialization as it is considered bad practice.
1 parent f15edd4 commit 3d0cb90

File tree

8 files changed

+38
-101
lines changed

8 files changed

+38
-101
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/support/BatchPropertyContext.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 the original author or authors.
2+
* Copyright 2013-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -218,7 +218,6 @@ public Properties getStepArtifactProperties(String stepName, String artifactName
218218
*
219219
* @param properties the step artifact {@link Properties} to add
220220
*/
221-
@SuppressWarnings("serial")
222221
public void setStepArtifactProperties(Map<String, Map<String, Properties>> properties) {
223222
Assert.notNull(properties, "Step artifact properties cannot be null");
224223

@@ -232,12 +231,10 @@ public void setStepArtifactProperties(Map<String, Map<String, Properties>> prope
232231
Map<String, Properties> artifactProperties = stepArtifactProperties.get(stepName);
233232

234233
if (artifactProperties == null) {
235-
stepArtifactProperties.put(stepName, new HashMap<String, Properties>() {{
236-
put(artifactName, props);
237-
}});
238-
} else {
239-
artifactProperties.put(artifactName, props);
234+
artifactProperties = new HashMap<>();
235+
stepArtifactProperties.put(stepName, artifactProperties);
240236
}
237+
artifactProperties.put(artifactName, props);
241238
}
242239
}
243240
}

spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/support/BatchPropertyContextTests.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 the original author or authors.
2+
* Copyright 2013-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717

1818
import static org.junit.Assert.assertEquals;
1919

20+
import java.util.Collections;
2021
import java.util.HashMap;
2122
import java.util.Map;
2223
import java.util.Properties;
@@ -65,25 +66,19 @@ public void setUp() {
6566
stepArtifactProperties.setProperty("readerProperty1", "readerProperty1value");
6667
stepArtifactProperties.setProperty("readerProperty2", "readerProperty2value");
6768

68-
this.stepArtifactProperties.put("step1", new HashMap<String, Properties>() {{
69-
put("reader", stepArtifactProperties);
70-
}});
69+
this.stepArtifactProperties.put("step1", Collections.singletonMap("reader", stepArtifactProperties));
7170

7271
final Properties partitionProperties = new Properties();
7372
partitionProperties.setProperty("writerProperty1", "writerProperty1valuePartition0");
7473
partitionProperties.setProperty("writerProperty2", "writerProperty2valuePartition0");
7574

76-
this.partitionProperties.put("step2:partition0", new HashMap<String, Properties>() {{
77-
put("writer", partitionProperties);
78-
}});
75+
this.partitionProperties.put("step2:partition0", Collections.singletonMap("writer", partitionProperties));
7976

8077
final Properties partitionStepProperties = new Properties();
8178
partitionStepProperties.setProperty("writerProperty1Step", "writerProperty1");
8279
partitionStepProperties.setProperty("writerProperty2Step", "writerProperty2");
8380

84-
this.partitionProperties.put("step2", new HashMap<String, Properties>() {{
85-
put("writer", partitionStepProperties);
86-
}});
81+
this.partitionProperties.put("step2", Collections.singletonMap("writer", partitionStepProperties));
8782
}
8883

8984
@Test

spring-batch-core/src/test/java/org/springframework/batch/core/step/builder/StepBuilderTests.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.batch.core.step.builder;
1717

18-
import java.util.ArrayList;
18+
import java.util.Arrays;
1919
import java.util.List;
2020
import java.util.function.Function;
2121

@@ -59,7 +59,6 @@
5959
* @author Parikshit Dutta
6060
*
6161
*/
62-
@SuppressWarnings("serial")
6362
public class StepBuilderTests {
6463

6564
@Test
@@ -181,15 +180,10 @@ public void testItemListeners() throws Exception {
181180
jobRepository.add(execution);
182181
PlatformTransactionManager transactionManager = new ResourcelessTransactionManager();
183182

184-
List<String> items = new ArrayList<String>() {{
185-
add("1");
186-
add("2");
187-
add("3");
188-
}};
183+
List<String> items = Arrays.asList("1", "2", "3");
189184

190185
ItemReader<String> reader = new ListItemReader<>(items);
191186

192-
@SuppressWarnings("unchecked")
193187
SimpleStepBuilder<String, String> builder = new StepBuilder("step")
194188
.repository(jobRepository)
195189
.transactionManager(transactionManager)
@@ -229,16 +223,11 @@ private void assertStepFunctions(boolean faultTolerantStep) throws Exception {
229223
jobRepository.add(execution);
230224
PlatformTransactionManager transactionManager = new ResourcelessTransactionManager();
231225

232-
List<Long> items = new ArrayList<Long>() {{
233-
add(1L);
234-
add(2L);
235-
add(3L);
236-
}};
226+
List<Long> items = Arrays.asList(1L, 2L, 3L);
237227

238228
ItemReader<Long> reader = new ListItemReader<>(items);
239229

240230
ListItemWriter<String> itemWriter = new ListItemWriter<>();
241-
242231
SimpleStepBuilder<Object, String> builder = new StepBuilder("step")
243232
.repository(jobRepository)
244233
.transactionManager(transactionManager)

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/MongoItemReaderTests.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.batch.item.data;
1717

1818
import java.util.ArrayList;
19+
import java.util.Collections;
1920
import java.util.HashMap;
2021
import java.util.Map;
2122

@@ -201,12 +202,9 @@ public void testQueryWithHint() {
201202
assertEquals("{ $natural : 1}", query.getHint());
202203
}
203204

204-
@SuppressWarnings("serial")
205205
@Test
206206
public void testQueryWithParameters() {
207-
reader.setParameterValues(new ArrayList<Object>(){{
208-
add("foo");
209-
}});
207+
reader.setParameterValues(Collections.singletonList("foo"));
210208

211209
reader.setQuery("{ name : ?0 }");
212210
ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class);
@@ -222,12 +220,9 @@ public void testQueryWithParameters() {
222220
assertEquals("{\"name\": -1}", query.getSortObject().toJson());
223221
}
224222

225-
@SuppressWarnings("serial")
226223
@Test
227224
public void testQueryWithCollection() {
228-
reader.setParameterValues(new ArrayList<Object>(){{
229-
add("foo");
230-
}});
225+
reader.setParameterValues(Collections.singletonList("foo"));
231226

232227
reader.setQuery("{ name : ?0 }");
233228
reader.setCollection("collection");

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/MongoItemWriterTests.java

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.batch.item.data;
1717

1818
import java.util.ArrayList;
19+
import java.util.Arrays;
1920
import java.util.Collections;
2021
import java.util.List;
2122

@@ -60,7 +61,6 @@
6061
* @author Parikshit Dutta
6162
* @author Mahmoud Ben Hassine
6263
*/
63-
@SuppressWarnings("serial")
6464
public class MongoItemWriterTests {
6565

6666
@Rule
@@ -106,10 +106,7 @@ public void testAfterPropertiesSet() throws Exception {
106106

107107
@Test
108108
public void testWriteNoTransactionNoCollection() throws Exception {
109-
List<Item> items = new ArrayList<Item>() {{
110-
add(new Item("Foo"));
111-
add(new Item("Bar"));
112-
}};
109+
List<Item> items = Arrays.asList(new Item("Foo"), new Item("Bar"));
113110

114111
writer.write(items);
115112

@@ -119,10 +116,7 @@ public void testWriteNoTransactionNoCollection() throws Exception {
119116

120117
@Test
121118
public void testWriteNoTransactionWithCollection() throws Exception {
122-
List<Object> items = new ArrayList<Object>() {{
123-
add(new Item("Foo"));
124-
add(new Item("Bar"));
125-
}};
119+
List<Object> items = Arrays.asList(new Item("Foo"), new Item("Bar"));
126120

127121
writer.setCollection("collection");
128122

@@ -142,10 +136,7 @@ public void testWriteNoTransactionNoItems() throws Exception {
142136

143137
@Test
144138
public void testWriteTransactionNoCollection() throws Exception {
145-
final List<Object> items = new ArrayList<Object>() {{
146-
add(new Item("Foo"));
147-
add(new Item("Bar"));
148-
}};
139+
final List<Object> items = Arrays.asList(new Item("Foo"), new Item("Bar"));
149140

150141
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
151142
try {
@@ -163,10 +154,7 @@ public void testWriteTransactionNoCollection() throws Exception {
163154

164155
@Test
165156
public void testWriteTransactionWithCollection() throws Exception {
166-
final List<Object> items = new ArrayList<Object>() {{
167-
add(new Item("Foo"));
168-
add(new Item("Bar"));
169-
}};
157+
final List<Object> items = Arrays.asList(new Item("Foo"), new Item("Bar"));
170158

171159
writer.setCollection("collection");
172160

@@ -186,10 +174,7 @@ public void testWriteTransactionWithCollection() throws Exception {
186174

187175
@Test
188176
public void testWriteTransactionFails() throws Exception {
189-
final List<Object> items = new ArrayList<Object>() {{
190-
add(new Item("Foo"));
191-
add(new Item("Bar"));
192-
}};
177+
final List<Object> items = Arrays.asList(new Item("Foo"), new Item("Bar"));
193178

194179
writer.setCollection("collection");
195180

@@ -218,10 +203,7 @@ public void testWriteTransactionFails() throws Exception {
218203
*/
219204
@Test
220205
public void testWriteTransactionReadOnly() throws Exception {
221-
final List<Object> items = new ArrayList<Object>() {{
222-
add(new Item("Foo"));
223-
add(new Item("Bar"));
224-
}};
206+
final List<Object> items = Arrays.asList(new Item("Foo"), new Item("Bar"));
225207

226208
writer.setCollection("collection");
227209

@@ -247,10 +229,7 @@ public void testWriteTransactionReadOnly() throws Exception {
247229
@Test
248230
public void testRemoveNoObjectIdNoCollection() throws Exception {
249231
writer.setDelete(true);
250-
List<Object> items = new ArrayList<Object>() {{
251-
add(new Item("Foo"));
252-
add(new Item("Bar"));
253-
}};
232+
List<Object> items = Arrays.asList(new Item("Foo"), new Item("Bar"));
254233

255234
writer.write(items);
256235

@@ -261,10 +240,7 @@ public void testRemoveNoObjectIdNoCollection() throws Exception {
261240
@Test
262241
public void testRemoveNoObjectIdWithCollection() throws Exception {
263242
writer.setDelete(true);
264-
List<Object> items = new ArrayList<Object>() {{
265-
add(new Item("Foo"));
266-
add(new Item("Bar"));
267-
}};
243+
List<Object> items = Arrays.asList(new Item("Foo"), new Item("Bar"));
268244

269245
writer.setCollection("collection");
270246
writer.write(items);
@@ -276,10 +252,7 @@ public void testRemoveNoObjectIdWithCollection() throws Exception {
276252
@Test
277253
public void testRemoveNoTransactionNoCollection() throws Exception {
278254
writer.setDelete(true);
279-
List<Object> items = new ArrayList<Object>() {{
280-
add(new Item(1));
281-
add(new Item(2));
282-
}};
255+
List<Object> items = Arrays.asList(new Item(1), new Item(2));
283256

284257
writer.write(items);
285258

@@ -290,10 +263,7 @@ public void testRemoveNoTransactionNoCollection() throws Exception {
290263
@Test
291264
public void testRemoveNoTransactionWithCollection() throws Exception {
292265
writer.setDelete(true);
293-
List<Object> items = new ArrayList<Object>() {{
294-
add(new Item(1));
295-
add(new Item(2));
296-
}};
266+
List<Object> items = Arrays.asList(new Item(1), new Item(2));
297267

298268
writer.setCollection("collection");
299269

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcBatchItemWriterNamedParameterTests.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.springframework.batch.item.database;
1717

1818
import java.util.Collections;
19-
import java.util.HashMap;
2019
import java.util.Map;
2120

2221
import org.hamcrest.BaseMatcher;
@@ -131,7 +130,7 @@ public void testWriteAndFlush() throws Exception {
131130
writer.write(Collections.singletonList(new Foo("bar")));
132131
}
133132

134-
@SuppressWarnings({ "rawtypes", "serial", "unchecked" })
133+
@SuppressWarnings({ "rawtypes", "unchecked" })
135134
@Test
136135
public void testWriteAndFlushMap() throws Exception {
137136
JdbcBatchItemWriter<Map<String, Object>> mapWriter = new JdbcBatchItemWriter<>();
@@ -145,14 +144,13 @@ public void testWriteAndFlushMap() throws Exception {
145144
when(namedParameterJdbcOperations.batchUpdate(eq(sql),
146145
captor.capture()))
147146
.thenReturn(new int[] {1});
148-
mapWriter.write(Collections.singletonList(new HashMap<String, Object>() {{put("foo", "bar");}}));
147+
mapWriter.write(Collections.singletonList(Collections.singletonMap("foo", "bar")));
149148

150149
assertEquals(1, captor.getValue().length);
151150
Map<String, Object> results = captor.getValue()[0];
152151
assertEquals("bar", results.get("foo"));
153152
}
154153

155-
@SuppressWarnings( "serial" )
156154
@Test
157155
public void testWriteAndFlushMapWithItemSqlParameterSourceProvider() throws Exception {
158156
JdbcBatchItemWriter<Map<String, Object>> mapWriter = new JdbcBatchItemWriter<>();
@@ -172,7 +170,7 @@ public SqlParameterSource createSqlParameterSource(Map<String, Object> item) {
172170
when(namedParameterJdbcOperations.batchUpdate(any(String.class),
173171
captor.capture()))
174172
.thenReturn(new int[] {1});
175-
mapWriter.write(Collections.singletonList(new HashMap<String, Object>() {{put("foo", "bar");}}));
173+
mapWriter.write(Collections.singletonList(Collections.singletonMap("foo", "bar")));
176174

177175
assertEquals(1, captor.getValue().length);
178176
SqlParameterSource results = captor.getValue()[0];

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/CompositeItemProcessorTests.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2013 the original author or authors.
2+
* Copyright 2008-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import static org.mockito.Mockito.when;
2323

2424
import java.util.ArrayList;
25+
import java.util.Arrays;
2526

2627
import org.junit.Assert;
2728
import org.junit.Before;
@@ -41,15 +42,13 @@ public class CompositeItemProcessorTests {
4142
private ItemProcessor<Object, Object> processor1;
4243
private ItemProcessor<Object, Object> processor2;
4344

44-
@SuppressWarnings({ "unchecked", "serial" })
45+
@SuppressWarnings("unchecked")
4546
@Before
4647
public void setUp() throws Exception {
4748
processor1 = mock(ItemProcessor.class);
4849
processor2 = mock(ItemProcessor.class);
4950

50-
composite.setDelegates(new ArrayList<ItemProcessor<Object,Object>>() {{
51-
add(processor1); add(processor2);
52-
}});
51+
composite.setDelegates(Arrays.asList(processor1, processor2));
5352

5453
composite.afterPropertiesSet();
5554
}
@@ -76,14 +75,13 @@ public void testTransform() throws Exception {
7675
* Test that the CompositeItemProcessor can work with generic types for the ItemProcessor delegates.
7776
*/
7877
@Test
79-
@SuppressWarnings({"unchecked", "serial"})
78+
@SuppressWarnings("unchecked")
8079
public void testItemProcessorGenerics() throws Exception {
8180
CompositeItemProcessor<String, String> composite = new CompositeItemProcessor<>();
8281
final ItemProcessor<String, Integer> processor1 = mock(ItemProcessor.class);
8382
final ItemProcessor<Integer, String> processor2 = mock(ItemProcessor.class);
84-
composite.setDelegates(new ArrayList<ItemProcessor<?,?>>() {{
85-
add(processor1); add(processor2);
86-
}});
83+
composite.setDelegates(Arrays.asList(processor1, processor2));
84+
8785
composite.afterPropertiesSet();
8886

8987
when(processor1.process("input")).thenReturn(5);

0 commit comments

Comments
 (0)