All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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

There is a newer version: 0.64.8
Show newest version
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 myRecursion;
    protected final Predicate myFilter;

    public TreeIterator(IterationConditions constraints, Predicate filter) {
        this(constraints, filter, FALSE);
    }

    public TreeIterator(IterationConditions constraints) {
        this(constraints, TRUE, FALSE);
    }

    public TreeIterator(
            IterationConditions constraints,
            Predicate filter,
            Predicate recursion
    ) {
        myConstraints = constraints;
        myRecursion = recursion;
        myFilter = filter;
    }

    @NotNull
    public Predicate getPredicate(@NotNull Class clazz) {
        return clazz::isInstance;
    }

    @NotNull
    public  Predicate getPredicate(@NotNull Class clazz, @NotNull Predicate predicate) {
        return (it) -> clazz.isInstance(it) && predicate.test(clazz.cast(it));
    }

    @NotNull
    public IterationConditions getConstraints() {
        return myConstraints;
    }

    public Predicate getRecursion() {
        return myRecursion;
    }

    public Predicate getFilter() {
        return myFilter;
    }

    @NotNull
    public TreeIterator modifiedCopy(@NotNull IterationConditions constraints, @NotNull Predicate filter, @NotNull Predicate 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 predicate) {
        return modifiedCopy(myConstraints, myFilter, it -> myRecursion.test(it) || predicate.test(it));
    }

    @NotNull
    public TreeIterator recurse(@NotNull Class clazz) {
        return recurse(getPredicate(clazz));
    }

    @NotNull
    public  TreeIterator recurse(@NotNull Class clazz, @NotNull Predicate predicate) {
        return recurse(getPredicate(clazz, predicate));
    }

    @NotNull
    public TreeIterator noRecurse(@NotNull Predicate predicate) {
        return modifiedCopy(myConstraints, myFilter, it -> myRecursion.test(it) && !predicate.test(it));
    }

    @NotNull
    public TreeIterator noRecurse(@NotNull Class clazz) {
        return noRecurse(getPredicate(clazz));
    }

    @NotNull
    public  TreeIterator noRecurse(@NotNull Class clazz, @NotNull Predicate predicate) {
        return noRecurse(getPredicate(clazz, predicate));
    }

    @NotNull
    public TreeIterator aborted() {
        return modifiedCopy(myConstraints.getAborted(), myFilter, myRecursion);
    }

    @NotNull
    public TreeIterator filterOut(@NotNull Predicate predicate) {
        return modifiedCopy(myConstraints, it -> myFilter.test(it) && !predicate.test(it), myRecursion);
    }

    @NotNull
    public TreeIterator filterOut(@NotNull Class clazz) {
        return filterOut(getPredicate(clazz));
    }

    @NotNull
    public  TreeIterator filterOut(@NotNull Class clazz, @NotNull Predicate predicate) {
        return filterOut(getPredicate(clazz, predicate));
    }

    @NotNull
    public TreeIterator filter(@NotNull Predicate predicate) {
        return modifiedCopy(myConstraints, it -> myFilter.test(it) && predicate.test(it), myRecursion);
    }

    @NotNull
    public TreeIterator filter(@NotNull Class clazz) {
        return filter(getPredicate(clazz));
    }

    @NotNull
    public  TreeIterator filter(@NotNull Class clazz, @NotNull Predicate 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 filter) {
        return new TreeIterator<>(constraints, filter);
    }

    @NotNull
    public static  TreeIterator of(@NotNull IterationConditions constraints, @NotNull Predicate filter, @NotNull Predicate 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 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 adapter, @NotNull ValueIterationConsumer 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 consumer) {
        IteratorInstance instance = new IteratorInstance<>(getConstraints(), getFilter(), getRecursion(), element);
        instance.iterate(consumer);
        return instance;
    }

    public  VoidIteration iterate(@NotNull N element, @NotNull ValueIterationAdapter adapter, @NotNull VoidIterationConsumer 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 consumer) {
        return iterate(element, defaultValue, consumer).getResult();
    }

    public void doLoop(@NotNull N element, @NotNull VoidIterationConsumer consumer) {
        iterate(element, consumer);
    }

    @NotNull
    public  R doLoop(@NotNull N element, @NotNull R defaultValue, @NotNull ValueIterationAdapter adapter, @NotNull ValueIterationConsumer consumer) {
        return iterate(element, defaultValue, adapter, consumer).getResult();
    }

    public  void doLoop(@NotNull N element, @NotNull ValueIterationAdapter adapter, @NotNull VoidIterationConsumer consumer) {
        iterate(element, adapter, consumer);
    }
}