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

com.vladsch.flexmark.tree.iteration.MappedIterator 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 java.util.function.Function;
import java.util.function.Predicate;

public class MappedIterator {
    protected final @NotNull B myElement;
    protected final @NotNull ValueIterationAdapter myAdapter;
    protected final @NotNull TreeIterator myTreeIterator;

    public MappedIterator(@NotNull B element, @NotNull ValueIterationAdapter adapter, @NotNull TreeIterator treeIterator) {
        myElement = element;
        myAdapter = adapter;
        myTreeIterator = treeIterator;
    }

    // *******************************************************
    //
    // Looping delegated
    //
    // *******************************************************

    @NotNull
    final public TreeIterator getTreeIterator() {
        return myTreeIterator;
    }

    @NotNull
    final public  R doLoop(@NotNull R defaultValue, @NotNull ValueIterationConsumer consumer) {
        return myTreeIterator.doLoop(myElement, defaultValue, myAdapter, consumer);
    }

    final public void doLoop(@NotNull VoidIterationConsumer consumer) {
        myTreeIterator.doLoop(myElement, myAdapter, consumer);
    }

    // *******************************************************
    //
    // Need Subclass Constructors
    //
    // *******************************************************

    @NotNull
    public MappedIterator getModifiedCopy(B element, ValueIterationAdapter adapter, TreeIterator treeIterator) {
        return new MappedIterator<>(element, adapter, treeIterator);
    }

    @NotNull
    public  MappedIterator getModifiedCopyF(B element, ValueIterationAdapter adapter, TreeIterator treeIterator) {
        return new MappedIterator<>(element, adapter, treeIterator);
    }

    // *******************************************************
    //
    // Need Overrides with cast to sub-class
    //
    // *******************************************************

    @NotNull
    public MappedIterator reversed() {
        return getModifiedCopy(myElement, myAdapter, myTreeIterator.reversed());
    }

    @NotNull

    public MappedIterator recursive() {
        return getModifiedCopy(myElement, myAdapter, myTreeIterator.recursive());
    }

    @NotNull
    public MappedIterator nonRecursive() {
        return getModifiedCopy(myElement, myAdapter, myTreeIterator.nonRecursive());
    }

    @NotNull
    public MappedIterator recursive(boolean recursive) {
        return recursive ? recursive() : nonRecursive();
    }

    @NotNull
    public MappedIterator nonRecursive(boolean nonRecursive) {
        return nonRecursive ? nonRecursive() : recursive();
    }

    @NotNull
    public MappedIterator recurse(@NotNull Predicate predicate) {
        return getModifiedCopy(myElement, myAdapter, myTreeIterator.recurse(predicate));
    }

    @NotNull
    public MappedIterator recurse(@NotNull Class clazz) {
        return getModifiedCopy(myElement, myAdapter, myTreeIterator.recurse(clazz));
    }

    @NotNull
    public  MappedIterator recurse(@NotNull Class clazz, @NotNull Predicate predicate) {
        return getModifiedCopy(myElement, myAdapter, myTreeIterator.recurse(clazz, predicate));
    }

    @NotNull
    public MappedIterator noRecurse(@NotNull Predicate predicate) {
        return getModifiedCopy(myElement, myAdapter, myTreeIterator.noRecurse(predicate));
    }

    @NotNull
    public MappedIterator noRecurse(@NotNull Class clazz) {
        return getModifiedCopy(myElement, myAdapter, myTreeIterator.noRecurse(clazz));
    }

    @NotNull
    public  MappedIterator noRecurse(@NotNull Class clazz, @NotNull Predicate predicate) {
        return getModifiedCopy(myElement, myAdapter, myTreeIterator.recurse(clazz, predicate));
    }

    @NotNull
    public MappedIterator filterFalse() {
        return aborted();
    }

    @NotNull
    public MappedIterator aborted() {
        return getModifiedCopy(myElement, myAdapter, myTreeIterator.aborted());
    }

    @NotNull
    public MappedIterator filterOut(@NotNull Predicate predicate) {
        return getModifiedCopy(myElement, myAdapter, myTreeIterator.filterOut(predicate));
    }

    @NotNull
    public MappedIterator filterOut(@NotNull Class clazz) {
        return getModifiedCopy(myElement, myAdapter, myTreeIterator.filterOut(clazz));
    }

    @NotNull
    public  MappedIterator filterOut(@NotNull Class clazz, @NotNull Predicate predicate) {
        return getModifiedCopy(myElement, myAdapter, myTreeIterator.filterOut(clazz, predicate));
    }

    @NotNull
    public MappedIterator filter(@NotNull Predicate predicate) {
        return getModifiedCopy(myElement, myAdapter, myTreeIterator.filter(predicate));
    }

    @NotNull
    public MappedIterator acceptFilter(@NotNull ValueIterationFilter filter) {
        return getModifiedCopy(myElement, myAdapter.andThen(ValueIterationAdapterImpl.of(filter)), myTreeIterator);
    }

    // *******************************************************
    //
    // Mapping Functions
    //
    // *******************************************************

    @NotNull
    public  MappedIterator filter(@NotNull Class clazz) {
        return getModifiedCopyF(myElement, myAdapter.andThen(ValueIterationAdapterImpl.of(clazz)), myTreeIterator);
    }

    @NotNull
    public  MappedIterator filter(@NotNull Class clazz, @NotNull Predicate predicate) {
        return getModifiedCopyF(myElement, myAdapter.andThen(ValueIterationAdapterImpl.of(clazz, predicate)), myTreeIterator);
    }

    @NotNull
    public  MappedIterator adapt(@NotNull Function adapter) {
        return getModifiedCopyF(myElement, myAdapter.andThen(ValueIterationAdapterImpl.of(adapter)), myTreeIterator);
    }

    @NotNull
    public  MappedIterator adapt(@NotNull ValueIterationAdapter adapter) {
        return getModifiedCopyF(myElement, myAdapter.andThen(adapter), myTreeIterator);
    }

    @NotNull
    public MappedIterator toObjectMapped(Class clazz) {
        Function objectToB = it -> clazz.isInstance(it) ? clazz.cast(it) : null;
        Function tToObject = it -> it;
        FixedIterationConditions constraints = FixedIterationConditions.mapTtoB(myTreeIterator.getConstraints(), objectToB, tToObject);
        return new MappedIterator<>(myElement, new ValueIterationAdapterImpl<>(objectToB), new TreeIterator<>(constraints));
    }

    // *******************************************************
    //
    // Subclass specific
    //
    // *******************************************************

    // *******************************************************
    //
    // Static Factories
    //
    // *******************************************************

    public static  MappedIterator create(N element, @NotNull TreeIterator treeIterator) {
        return new MappedIterator<>(element, ValueIterationAdapterImpl.of(), treeIterator);
    }
}