com.vladsch.flexmark.tree.iteration.ValueIterationAdapterImpl Maven / Gradle / Ivy
Show all versions of flexmark-tree-iteration Show documentation
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 super P, ? extends T> myFunction;
final private @Nullable ValueIterationFilter super T> myFilter;
public ConsumerAdapter(@NotNull Function super P, ? extends T> function, @Nullable ValueIterationFilter super T> filter) {
myFunction = function;
myFilter = filter;
}
@NotNull
@Override
public ValueIterationConsumer super P, R> getConsumer(ValueIterationConsumer super T, R> valueConsumer) {
return new MyValueIterationConsumer<>(myFunction, myFilter, valueConsumer);
}
@NotNull
@Override
public ValueIterationConsumer super P, R> getConsumer(VoidIterationConsumer super T> voidConsumer) {
return new MyValueIterationConsumer<>(myFunction, myFilter, new VoidToValueIConsumerAdapter<>(voidConsumer));
}
}
private static class MyValueIterationConsumer implements ValueIterationConsumer
{
final private ValueIterationConsumer super T, R> myConsumer;
final private Function super P, ? extends T> myFunction;
final private @Nullable ValueIterationFilter super T> myFilter;
public MyValueIterationConsumer(@NotNull Function super P, ? extends T> function, @Nullable ValueIterationFilter super T> filter, ValueIterationConsumer super T, R> 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 super P, T> myBeforeAdapter;
final private ValueIterationConsumerAdapter super T, V> myAfterAdapter;
public ChainedConsumerAdapter(ValueIterationConsumerAdapter super P, T> beforeAdapter, ValueIterationConsumerAdapter super T, V> afterAdapter) {
myBeforeAdapter = beforeAdapter;
myAfterAdapter = afterAdapter;
}
@NotNull
@Override
public ValueIterationConsumer super P, R> getConsumer(ValueIterationConsumer super V, R> valueConsumer) {
return myBeforeAdapter.getConsumer(myAfterAdapter.getConsumer(valueConsumer));
}
@NotNull
@Override
public ValueIterationConsumer super P, R> getConsumer(VoidIterationConsumer super V> voidConsumer) {
return myBeforeAdapter.getConsumer(myAfterAdapter.getConsumer(voidConsumer));
}
}
public ValueIterationAdapterImpl(@NotNull Function super N, T> function) {
this(function, null);
}
public ValueIterationAdapterImpl(@NotNull Function super N, T> function, @Nullable ValueIterationFilter super T> 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 super T, V> after) {
return new ValueIterationAdapterImpl<>(new ChainedConsumerAdapter<>(myConsumerAdapter, after.getConsumerAdapter()));
}
@NotNull
@Override
public ValueIterationAdapter compose(ValueIterationAdapter super N, N> before) {
return new ValueIterationAdapterImpl<>(new ChainedConsumerAdapter<>(before.getConsumerAdapter(), myConsumerAdapter));
}
public static ValueIterationAdapter of() {
return new ValueIterationAdapterImpl<>(Function.identity());
}
public static ValueIterationAdapter of(ValueIterationFilter super N> filter) {
return new ValueIterationAdapterImpl<>(Function.identity(), filter);
}
public static ValueIterationAdapter of(Function super N, T> function) {
return new ValueIterationAdapterImpl<>(function);
}
public static ValueIterationAdapter of(Class extends T> clazz) {
return new ValueIterationAdapterImpl<>(it -> clazz.isInstance(it) ? clazz.cast(it) : null);
}
public static ValueIterationAdapter of(Iterable> clazzes) {
return new ValueIterationAdapterImpl<>(it -> {
for (Class extends T> clazz : clazzes) {
if (clazz.isInstance(it)) return clazz.cast(it);
}
return null;
});
}
public static ValueIterationAdapter of(Class extends T>... clazzes) {
return of(new ArrayIterable<>(clazzes));
}
public static ValueIterationAdapter of(Class extends T> clazz, Predicate super T> filter) {
return new ValueIterationAdapterImpl<>(it -> clazz.isInstance(it) ? clazz.cast(it) : null, (it, loop) -> filter.test(it));
}
public static ValueIterationAdapter of(Predicate super T> filter, Iterable> clazzes) {
return new ValueIterationAdapterImpl<>(it -> {
for (Class extends T> clazz : clazzes) {
if (clazz.isInstance(it)) return clazz.cast(it);
}
return null;
}, (it, loop) -> filter.test(it));
}
public static ValueIterationAdapter of(Predicate super T> filter, Class extends T>... clazzes) {
return of(filter, new ArrayIterable<>(clazzes));
}
public static ValueIterationAdapter of(Class clazz, ValueIterationFilter super T> filter) {
return new ValueIterationAdapterImpl<>(((it) -> clazz.isInstance(it) ? clazz.cast(it) : null), filter);
}
public static ValueIterationAdapter of(ValueIterationFilter super T> filter, Class extends T>... clazzes) {
return of(filter, new ArrayIterable<>(clazzes));
}
public static ValueIterationAdapter of(ValueIterationFilter super T> filter, Iterable> clazzes) {
return new ValueIterationAdapterImpl<>(it -> {
for (Class extends T> clazz : clazzes) {
if (clazz.isInstance(it)) return clazz.cast(it);
}
return null;
}, filter);
}
}