Skip to content

Commit

Permalink
Remove code duplication.
Browse files Browse the repository at this point in the history
Mainly reimplements the changes undone in 49e343f.
The check for presence of the ID property is implemented for all variants for save, as it should.

See #1924
  • Loading branch information
schauder committed Oct 29, 2024
1 parent 8e710c9 commit a7cea62
Showing 1 changed file with 8 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,7 @@ public <T> T save(T instance) {

@Override
public <T> List<T> saveAll(Iterable<T> instances) {

Assert.notNull(instances, "Aggregate instances must not be null");

if (!instances.iterator().hasNext()) {
return Collections.emptyList();
}

List<EntityAndChangeCreator<T>> entityAndChangeCreators = new ArrayList<>();
for (T instance : instances) {
verifyIdProperty(instance);
entityAndChangeCreators.add(new EntityAndChangeCreator<>(instance, changeCreatorSelectorForSave(instance)));
}
return performSaveAll(entityAndChangeCreators);
return doInBatch(instances, (first) -> (second -> changeCreatorSelectorForSave(first).apply(second)));
}

/**
Expand All @@ -206,21 +194,7 @@ public <T> T insert(T instance) {

@Override
public <T> List<T> insertAll(Iterable<T> instances) {

Assert.notNull(instances, "Aggregate instances must not be null");

if (!instances.iterator().hasNext()) {
return Collections.emptyList();
}

List<EntityAndChangeCreator<T>> entityAndChangeCreators = new ArrayList<>();
for (T instance : instances) {

Function<T, RootAggregateChange<T>> changeCreator = entity -> createInsertChange(prepareVersionForInsert(entity));
EntityAndChangeCreator<T> entityChange = new EntityAndChangeCreator<>(instance, changeCreator);
entityAndChangeCreators.add(entityChange);
}
return performSaveAll(entityAndChangeCreators);
return doInBatch(instances, (__) -> (entity -> createInsertChange(prepareVersionForInsert(entity))));
}

/**
Expand All @@ -241,6 +215,10 @@ public <T> T update(T instance) {

@Override
public <T> List<T> updateAll(Iterable<T> instances) {
return doInBatch(instances, (__) -> (entity -> createUpdateChange(prepareVersionForUpdate(entity))));
}

private <T> List<T> doInBatch(Iterable<T> instances,Function<T, Function<T, RootAggregateChange<T>>> changeCreatorFunction) {

Assert.notNull(instances, "Aggregate instances must not be null");

Expand All @@ -250,10 +228,8 @@ public <T> List<T> updateAll(Iterable<T> instances) {

List<EntityAndChangeCreator<T>> entityAndChangeCreators = new ArrayList<>();
for (T instance : instances) {

Function<T, RootAggregateChange<T>> changeCreator = entity -> createUpdateChange(prepareVersionForUpdate(entity));
EntityAndChangeCreator<T> entityChange = new EntityAndChangeCreator<>(instance, changeCreator);
entityAndChangeCreators.add(entityChange);
verifyIdProperty(instance);
entityAndChangeCreators.add(new EntityAndChangeCreator<T>(instance, changeCreatorFunction.apply(instance)));
}
return performSaveAll(entityAndChangeCreators);
}
Expand Down

0 comments on commit a7cea62

Please sign in to comment.