@@ -173,19 +173,7 @@ public <T> T save(T instance) {
173
173
174
174
@ Override
175
175
public <T > List <T > saveAll (Iterable <T > instances ) {
176
-
177
- Assert .notNull (instances , "Aggregate instances must not be null" );
178
-
179
- if (!instances .iterator ().hasNext ()) {
180
- return Collections .emptyList ();
181
- }
182
-
183
- List <EntityAndChangeCreator <T >> entityAndChangeCreators = new ArrayList <>();
184
- for (T instance : instances ) {
185
- verifyIdProperty (instance );
186
- entityAndChangeCreators .add (new EntityAndChangeCreator <>(instance , changeCreatorSelectorForSave (instance )));
187
- }
188
- return performSaveAll (entityAndChangeCreators );
176
+ return doInBatch (instances , (first ) -> (second -> changeCreatorSelectorForSave (first ).apply (second )));
189
177
}
190
178
191
179
/**
@@ -206,21 +194,7 @@ public <T> T insert(T instance) {
206
194
207
195
@ Override
208
196
public <T > List <T > insertAll (Iterable <T > instances ) {
209
-
210
- Assert .notNull (instances , "Aggregate instances must not be null" );
211
-
212
- if (!instances .iterator ().hasNext ()) {
213
- return Collections .emptyList ();
214
- }
215
-
216
- List <EntityAndChangeCreator <T >> entityAndChangeCreators = new ArrayList <>();
217
- for (T instance : instances ) {
218
-
219
- Function <T , RootAggregateChange <T >> changeCreator = entity -> createInsertChange (prepareVersionForInsert (entity ));
220
- EntityAndChangeCreator <T > entityChange = new EntityAndChangeCreator <>(instance , changeCreator );
221
- entityAndChangeCreators .add (entityChange );
222
- }
223
- return performSaveAll (entityAndChangeCreators );
197
+ return doInBatch (instances , (__ ) -> (entity -> createInsertChange (prepareVersionForInsert (entity ))));
224
198
}
225
199
226
200
/**
@@ -241,6 +215,10 @@ public <T> T update(T instance) {
241
215
242
216
@ Override
243
217
public <T > List <T > updateAll (Iterable <T > instances ) {
218
+ return doInBatch (instances , (__ ) -> (entity -> createUpdateChange (prepareVersionForUpdate (entity ))));
219
+ }
220
+
221
+ private <T > List <T > doInBatch (Iterable <T > instances ,Function <T , Function <T , RootAggregateChange <T >>> changeCreatorFunction ) {
244
222
245
223
Assert .notNull (instances , "Aggregate instances must not be null" );
246
224
@@ -250,10 +228,8 @@ public <T> List<T> updateAll(Iterable<T> instances) {
250
228
251
229
List <EntityAndChangeCreator <T >> entityAndChangeCreators = new ArrayList <>();
252
230
for (T instance : instances ) {
253
-
254
- Function <T , RootAggregateChange <T >> changeCreator = entity -> createUpdateChange (prepareVersionForUpdate (entity ));
255
- EntityAndChangeCreator <T > entityChange = new EntityAndChangeCreator <>(instance , changeCreator );
256
- entityAndChangeCreators .add (entityChange );
231
+ verifyIdProperty (instance );
232
+ entityAndChangeCreators .add (new EntityAndChangeCreator <T >(instance , changeCreatorFunction .apply (instance )));
257
233
}
258
234
return performSaveAll (entityAndChangeCreators );
259
235
}
0 commit comments