Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.vladsch.flexmark.tree.iteration.TreeIterator Maven / Gradle / Ivy
Go to download
flexmark-java library for recursive tree iteration with the filtering and recursion conditions provided by predicates
package com.vladsch.flexmark.tree.iteration;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Objects;
import java.util.function.Predicate;
public class TreeIterator {
final public static Logger LOG = LoggerFactory.getILoggerFactory().getLogger("com.vladsch.treeIteration.util.looping");
final public static Logger LOG_INFO = LoggerFactory.getILoggerFactory().getLogger("com.vladsch.treeIteration.util.looping-summary");
final public static Logger LOG_TRACE = LoggerFactory.getILoggerFactory().getLogger("com.vladsch.treeIteration.util.looping-detailed");
final public static Predicate TRUE = o -> true;
final public static Predicate FALSE = o -> false;
final public static Predicate NOT_NULL = Objects::nonNull;
final private IterationConditions myConstraints;
final private Predicate super N> myRecursion;
protected final Predicate super N> myFilter;
public TreeIterator(IterationConditions constraints, Predicate super N> filter) {
this(constraints, filter, FALSE);
}
public TreeIterator(IterationConditions constraints) {
this(constraints, TRUE, FALSE);
}
public TreeIterator(
IterationConditions constraints,
Predicate super N> filter,
Predicate super N> recursion
) {
myConstraints = constraints;
myRecursion = recursion;
myFilter = filter;
}
@NotNull
public Predicate getPredicate(@NotNull Class super N> clazz) {
return clazz::isInstance;
}
@NotNull
public Predicate getPredicate(@NotNull Class clazz, @NotNull Predicate super F> predicate) {
return (it) -> clazz.isInstance(it) && predicate.test(clazz.cast(it));
}
@NotNull
public IterationConditions getConstraints() {
return myConstraints;
}
public Predicate super N> getRecursion() {
return myRecursion;
}
public Predicate super N> getFilter() {
return myFilter;
}
@NotNull
public TreeIterator modifiedCopy(@NotNull IterationConditions constraints, @NotNull Predicate super N> filter, @NotNull Predicate super N> recursion) {
return new TreeIterator<>(constraints, filter, recursion);
}
@NotNull
public TreeIterator reversed() {
return modifiedCopy(myConstraints.getReversed(), myFilter, myRecursion);
}
@NotNull
public TreeIterator recursive() {
//noinspection unchecked
return modifiedCopy(myConstraints, myFilter, (Predicate) TRUE);
}
@NotNull
public TreeIterator nonRecursive() {
//noinspection unchecked
return modifiedCopy(myConstraints, myFilter, (Predicate) FALSE);
}
@NotNull
public TreeIterator recurse(@NotNull Predicate super N> predicate) {
return modifiedCopy(myConstraints, myFilter, it -> myRecursion.test(it) || predicate.test(it));
}
@NotNull
public TreeIterator recurse(@NotNull Class super N> clazz) {
return recurse(getPredicate(clazz));
}
@NotNull
public TreeIterator recurse(@NotNull Class clazz, @NotNull Predicate super F> predicate) {
return recurse(getPredicate(clazz, predicate));
}
@NotNull
public TreeIterator noRecurse(@NotNull Predicate super N> predicate) {
return modifiedCopy(myConstraints, myFilter, it -> myRecursion.test(it) && !predicate.test(it));
}
@NotNull
public TreeIterator noRecurse(@NotNull Class super N> clazz) {
return noRecurse(getPredicate(clazz));
}
@NotNull
public TreeIterator noRecurse(@NotNull Class clazz, @NotNull Predicate super F> predicate) {
return noRecurse(getPredicate(clazz, predicate));
}
@NotNull
public TreeIterator aborted() {
return modifiedCopy(myConstraints.getAborted(), myFilter, myRecursion);
}
@NotNull
public TreeIterator filterOut(@NotNull Predicate super N> predicate) {
return modifiedCopy(myConstraints, it -> myFilter.test(it) && !predicate.test(it), myRecursion);
}
@NotNull
public TreeIterator filterOut(@NotNull Class super N> clazz) {
return filterOut(getPredicate(clazz));
}
@NotNull
public TreeIterator filterOut(@NotNull Class clazz, @NotNull Predicate super F> predicate) {
return filterOut(getPredicate(clazz, predicate));
}
@NotNull
public TreeIterator filter(@NotNull Predicate super N> predicate) {
return modifiedCopy(myConstraints, it -> myFilter.test(it) && predicate.test(it), myRecursion);
}
@NotNull
public TreeIterator filter(@NotNull Class super N> clazz) {
return filter(getPredicate(clazz));
}
@NotNull
public TreeIterator filter(@NotNull Class clazz, @NotNull Predicate super F> predicate) {
return filter(getPredicate(clazz, predicate));
}
@NotNull
public static TreeIterator of(@NotNull IterationConditions constraints) {
return new TreeIterator<>(constraints);
}
@NotNull
public static TreeIterator of(@NotNull IterationConditions constraints, @NotNull Predicate super N> filter) {
return new TreeIterator<>(constraints, filter);
}
@NotNull
public static TreeIterator of(@NotNull IterationConditions constraints, @NotNull Predicate super N> filter, @NotNull Predicate super N> recursion) {
return new TreeIterator<>(constraints, filter, recursion);
}
@NotNull
public static Predicate TRUE() {
return n -> true;
}
@NotNull
public static Predicate FALSE() {
return n -> true;
}
public ValueIteration iterate(@NotNull N element, @NotNull R defaultValue, @NotNull ValueIterationConsumer super N, R> consumer) {
IteratorInstance instance = new IteratorInstance<>(getConstraints(), getFilter(), getRecursion(), element, defaultValue);
instance.iterate(consumer);
return instance;
}
public ValueIteration iterate(@NotNull N element, @NotNull R defaultValue, @NotNull ValueIterationAdapter super N, T> adapter, @NotNull ValueIterationConsumer super T, R> consumer) {
IteratorInstance instance = new IteratorInstance<>(getConstraints(), getFilter(), getRecursion(), element, defaultValue);
instance.iterate(adapter.getConsumerAdapter().getConsumer(consumer));
return instance;
}
public VoidIteration iterate(@NotNull N element, @NotNull VoidIterationConsumer super N> consumer) {
IteratorInstance instance = new IteratorInstance<>(getConstraints(), getFilter(), getRecursion(), element);
instance.iterate(consumer);
return instance;
}
public VoidIteration iterate(@NotNull N element, @NotNull ValueIterationAdapter super N, T> adapter, @NotNull VoidIterationConsumer super T> consumer) {
IteratorInstance instance = new IteratorInstance<>(getConstraints(), getFilter(), getRecursion(), element);
instance.iterate(adapter.getConsumerAdapter().getConsumer(consumer));
return instance;
}
@NotNull
public R doLoop(@NotNull N element, @NotNull R defaultValue, @NotNull ValueIterationConsumer super N, R> consumer) {
return iterate(element, defaultValue, consumer).getResult();
}
public void doLoop(@NotNull N element, @NotNull VoidIterationConsumer super N> consumer) {
iterate(element, consumer);
}
@NotNull
public R doLoop(@NotNull N element, @NotNull R defaultValue, @NotNull ValueIterationAdapter super N, T> adapter, @NotNull ValueIterationConsumer super T, R> consumer) {
return iterate(element, defaultValue, adapter, consumer).getResult();
}
public void doLoop(@NotNull N element, @NotNull ValueIterationAdapter super N, T> adapter, @NotNull VoidIterationConsumer super T> consumer) {
iterate(element, adapter, consumer);
}
}