Skip to content

Commit

Permalink
feature: ParentFunction can early terminate
Browse files Browse the repository at this point in the history
  • Loading branch information
pvojtechovsky committed Mar 9, 2017
1 parent 4caa359 commit 635ac73
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/main/java/spoon/reflect/visitor/filter/ParentFunction.java
Expand Up @@ -20,6 +20,8 @@
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.visitor.chain.CtConsumableFunction;
import spoon.reflect.visitor.chain.CtConsumer;
import spoon.reflect.visitor.chain.CtQuery;
import spoon.reflect.visitor.chain.CtQueryAware;

/**
* This Function expects a {@link CtElement} as input
Expand All @@ -28,9 +30,10 @@
* By default input is not returned,
* but this behavior can be changed by call of {@link #includingSelf(boolean)} with value true
*/
public class ParentFunction implements CtConsumableFunction<CtElement> {
public class ParentFunction implements CtConsumableFunction<CtElement>, CtQueryAware {

private boolean includingSelf = false;
private CtQuery query;

public ParentFunction() {
}
Expand All @@ -53,9 +56,14 @@ public void apply(CtElement input, CtConsumer<Object> outputConsumer) {
}
CtPackage rootPackage = input.getFactory().getModel().getRootPackage();
CtElement parent = input;
while (parent != null && parent != rootPackage) {
while (parent != null && parent != rootPackage && query.isTerminated() == false) {
parent = parent.getParent();
outputConsumer.accept(parent);
}
}

@Override
public void setQuery(CtQuery query) {
this.query = query;
}
}

0 comments on commit 635ac73

Please sign in to comment.