Created
July 18, 2014 21:33
-
-
Save okram/476dfe924992f08d5b5e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class IfThenElseStep<S, E> extends FlatMapStep<S, E> { | |
public final SPredicate<Traverser<S>> ifPredicate; | |
public final Traversal<S, E> thenTraversal; | |
public final Traversal<S, E> elseTraversal; | |
public IfThenElseStep(final Traversal traversal, final SPredicate<Traverser<S>> ifPredicate, final Traversal<S, E> thenTraversal, final Traversal<S, E> elseTraversal) { | |
super(traversal); | |
this.ifPredicate = ifPredicate; | |
this.thenTraversal = thenTraversal; | |
this.elseTraversal = elseTraversal; | |
this.setFunction(traverser -> { | |
if (this.ifPredicate.test(traverser)) { | |
this.thenTraversal.addStarts(new SingleIterator<>(traverser)); | |
return this.thenTraversal; | |
} else { | |
if (null == this.elseTraversal) { | |
return Collections.emptyIterator(); | |
} else { | |
this.elseTraversal.addStarts(new SingleIterator<>(traverser)); | |
return this.elseTraversal; | |
} | |
} | |
}); | |
} | |
public IfThenElseStep(final Traversal traversal, final SPredicate<Traverser<S>> ifPredicate, final Traversal<S, E> thenTraversal) { | |
this(traversal, ifPredicate, thenTraversal, null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment