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

com.vladsch.flexmark.tree.iteration.ValueIterationAdapterImpl 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 com.vladsch.flexmark.util.collection.iteration.ArrayIterable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.function.Function;
import java.util.function.Predicate;

public class ValueIterationAdapterImpl implements ValueIterationAdapter {
    final private @NotNull ValueIterationConsumerAdapter myConsumerAdapter;

    static class ConsumerAdapter implements ValueIterationConsumerAdapter {
        final private @NotNull Function myFunction;
        final private @Nullable ValueIterationFilter myFilter;

        public ConsumerAdapter(@NotNull Function function, @Nullable ValueIterationFilter filter) {
            myFunction = function;
            myFilter = filter;
        }

        @NotNull
        @Override
        public  ValueIterationConsumer getConsumer(ValueIterationConsumer valueConsumer) {
            return new MyValueIterationConsumer<>(myFunction, myFilter, valueConsumer);
        }

        @NotNull
        @Override
        public  ValueIterationConsumer getConsumer(VoidIterationConsumer voidConsumer) {
            return new MyValueIterationConsumer<>(myFunction, myFilter, new VoidToValueIConsumerAdapter<>(voidConsumer));
        }
    }

    private static class MyValueIterationConsumer implements ValueIterationConsumer {
        final private ValueIterationConsumer myConsumer;
        final private Function myFunction;
        final private @Nullable ValueIterationFilter myFilter;

        public MyValueIterationConsumer(@NotNull Function function, @Nullable ValueIterationFilter filter, ValueIterationConsumer consumer) {
            myFunction = function;
            myFilter = filter;
            myConsumer = consumer;
        }

        @Override
        public void accept(@NotNull P it, @NotNull ValueIteration iteration) {
            T applied = myFunction.apply(it);
            if (applied == null || myFilter != null && !myFilter.filter(applied, iteration)) {
                //loop.Continue();
            } else {
                myConsumer.accept(applied, iteration);
            }
        }

        @Override
        public void beforeStart(@NotNull ValueIteration iteration) {
            myConsumer.beforeStart(iteration);
        }

        @Override
        public void startRecursion(@NotNull VoidIteration iteration) {
            myConsumer.startRecursion(iteration);
        }

        @Override
        public void endRecursion(@NotNull VoidIteration iteration) {
            myConsumer.endRecursion(iteration);
        }

        @Override
        public void afterEnd(@NotNull ValueIteration iteration) {
            myConsumer.afterEnd(iteration);
        }
    }

    static class ChainedConsumerAdapter implements ValueIterationConsumerAdapter {
        final private ValueIterationConsumerAdapter myBeforeAdapter;
        final private ValueIterationConsumerAdapter myAfterAdapter;

        public ChainedConsumerAdapter(ValueIterationConsumerAdapter beforeAdapter, ValueIterationConsumerAdapter afterAdapter) {
            myBeforeAdapter = beforeAdapter;
            myAfterAdapter = afterAdapter;
        }

        @NotNull
        @Override
        public  ValueIterationConsumer getConsumer(ValueIterationConsumer valueConsumer) {
            return myBeforeAdapter.getConsumer(myAfterAdapter.getConsumer(valueConsumer));
        }

        @NotNull
        @Override
        public  ValueIterationConsumer getConsumer(VoidIterationConsumer voidConsumer) {
            return myBeforeAdapter.getConsumer(myAfterAdapter.getConsumer(voidConsumer));
        }
    }

    public ValueIterationAdapterImpl(@NotNull Function function) {
        this(function, null);
    }

    public ValueIterationAdapterImpl(@NotNull Function function, @Nullable ValueIterationFilter filter) {
        this(new ConsumerAdapter<>(function, filter));
    }

    public ValueIterationAdapterImpl(@NotNull ValueIterationConsumerAdapter consumerAdapter) {
        myConsumerAdapter = consumerAdapter;
    }

    @NotNull
    @Override
    public ValueIterationConsumerAdapter getConsumerAdapter() {
        return myConsumerAdapter;
    }

    @NotNull
    @Override
    public  ValueIterationAdapter andThen(ValueIterationAdapter after) {
        return new ValueIterationAdapterImpl<>(new ChainedConsumerAdapter<>(myConsumerAdapter, after.getConsumerAdapter()));
    }

    @NotNull
    @Override
    public ValueIterationAdapter compose(ValueIterationAdapter before) {
        return new ValueIterationAdapterImpl<>(new ChainedConsumerAdapter<>(before.getConsumerAdapter(), myConsumerAdapter));
    }

    public static  ValueIterationAdapter of() {
        return new ValueIterationAdapterImpl<>(Function.identity());
    }

    public static  ValueIterationAdapter of(ValueIterationFilter filter) {
        return new ValueIterationAdapterImpl<>(Function.identity(), filter);
    }

    public static  ValueIterationAdapter of(Function function) {
        return new ValueIterationAdapterImpl<>(function);
    }

    public static  ValueIterationAdapter of(Class clazz) {
        return new ValueIterationAdapterImpl<>(it -> clazz.isInstance(it) ? clazz.cast(it) : null);
    }

    public static  ValueIterationAdapter of(Iterable> clazzes) {
        return new ValueIterationAdapterImpl<>(it -> {
            for (Class clazz : clazzes) {
                if (clazz.isInstance(it)) return clazz.cast(it);
            }
            return null;
        });
    }

    public static  ValueIterationAdapter of(Class... clazzes) {
        return of(new ArrayIterable<>(clazzes));
    }

    public static  ValueIterationAdapter of(Class clazz, Predicate filter) {
        return new ValueIterationAdapterImpl<>(it -> clazz.isInstance(it) ? clazz.cast(it) : null, (it, loop) -> filter.test(it));
    }

    public static  ValueIterationAdapter of(Predicate filter, Iterable> clazzes) {
        return new ValueIterationAdapterImpl<>(it -> {
            for (Class clazz : clazzes) {
                if (clazz.isInstance(it)) return clazz.cast(it);
            }
            return null;
        }, (it, loop) -> filter.test(it));
    }

    public static  ValueIterationAdapter of(Predicate filter, Class... clazzes) {
        return of(filter, new ArrayIterable<>(clazzes));
    }

    public static  ValueIterationAdapter of(Class clazz, ValueIterationFilter filter) {
        return new ValueIterationAdapterImpl<>(((it) -> clazz.isInstance(it) ? clazz.cast(it) : null), filter);
    }

    public static  ValueIterationAdapter of(ValueIterationFilter filter, Class... clazzes) {
        return of(filter, new ArrayIterable<>(clazzes));
    }

    public static  ValueIterationAdapter of(ValueIterationFilter filter, Iterable> clazzes) {
        return new ValueIterationAdapterImpl<>(it -> {
            for (Class clazz : clazzes) {
                if (clazz.isInstance(it)) return clazz.cast(it);
            }
            return null;
        }, filter);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy