48 lines
953 B
Java
48 lines
953 B
Java
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);
|
|
}
|
|
}
|