The aggregate change combines multiple changes into one while implementing the Change interface.
This commit is contained in:
parent
b30f806894
commit
d117052ca4
@ -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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user