Aggregate Change
zdm/undo-redo/pipeline/head This commit looks good Details

The aggregate change combines multiple changes into one while
implementing the Change interface.
This commit is contained in:
Kai S. K. Engelbart 2022-02-11 09:48:41 +01:00
parent b30f806894
commit d117052ca4
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
1 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,47 @@
package dev.kske.undoredo.core;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author Kai S. K. Engelbart
* @since 0.2.0
*/
public final class AggregateChange<C extends Change> implements Change {
private final List<C> changes = new ArrayList<>();
@SafeVarargs
public AggregateChange(C... changes) {
this(Arrays.asList(changes));
}
public AggregateChange(Collection<C> changes) {
changes.addAll(changes);
}
@Override
public void apply() {
changes.forEach(Change::apply);
}
@Override
public Change invert() {
List<Change> invertedChanges =
changes
.parallelStream()
.map(Change::invert)
.collect(Collectors.toList());
Collections.reverse(invertedChanges);
return new AggregateChange<>(invertedChanges);
}
@Override
public boolean isIdentity() {
return changes.stream().allMatch(Change::isIdentity);
}
public List<C> changes() {
return Collections.unmodifiableList(changes);
}
}