|
| 1 | +package org.variantsync.diffdetective.variation.diff.transform; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.function.Predicate; |
| 6 | + |
| 7 | +import org.variantsync.diffdetective.util.Assert; |
| 8 | +import org.variantsync.diffdetective.variation.Label; |
| 9 | +import org.variantsync.diffdetective.variation.diff.DiffNode; |
| 10 | +import org.variantsync.diffdetective.variation.diff.VariationDiff; |
| 11 | + |
| 12 | +/** |
| 13 | + * This transformer prunes some changes in a variation diffs. |
| 14 | + * The transformation requires a predicate on {@link DiffNode nodes} to identify the changes which should be undone. |
| 15 | + * An inserted node will be removed, so that the insertion does not happen. |
| 16 | + * A removed node will be made unchanged so that it will not be deleted. |
| 17 | + * When a node is made unchanged, also its parent node will be made unchanged if necessary to retain diff consistency. |
| 18 | + * @see DiffNode#makeUnchanged() |
| 19 | + * @author Paul Bittner |
| 20 | + */ |
| 21 | +public class HideSomeChanges<L extends Label> implements Transformer<VariationDiff<L>> { |
| 22 | + private Predicate<DiffNode<L>> isInteresting; |
| 23 | + |
| 24 | + public HideSomeChanges(final Predicate<DiffNode<L>> isInteresting) { |
| 25 | + Assert.assertNotNull(isInteresting); |
| 26 | + this.isInteresting = isInteresting; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void transform(VariationDiff<L> d) { |
| 31 | + final List<DiffNode<L>> nodesToEliminate = new ArrayList<>(); |
| 32 | + final List<DiffNode<L>> toUnchanged = new ArrayList<>(); |
| 33 | + d.forAll((DiffNode<L> node) -> { |
| 34 | + if (isInteresting.test(node)) { |
| 35 | + if (node.isAdd()) { |
| 36 | + nodesToEliminate.add(node); |
| 37 | + } else if (node.isRem()) { |
| 38 | + toUnchanged.add(node); |
| 39 | + } |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + for (DiffNode<L> zombie : nodesToEliminate) { |
| 44 | + // We also drop all children. |
| 45 | + zombie.drop(); |
| 46 | + } |
| 47 | + |
| 48 | + for (DiffNode<L> n : toUnchanged) { |
| 49 | + n.makeUnchanged(); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments