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

io.atleon.core.DiscardHook Maven / Gradle / Ivy

package io.atleon.core;

import reactor.util.context.Context;
import reactor.util.context.ContextView;

import java.util.function.Consumer;
import java.util.function.Function;

/**
 * A hook that may be applied when elements from a pipeline are discarded, e.g. via filtering
 */
interface DiscardHook extends Consumer {

    /**
     * Creates a function that will prepend the provided hook to hooks applied later in a pipeline
     */
    static  Function newContextModifier(Class type, Consumer hook) {
        DiscardHook discardHook = object -> {
            if (type.isInstance(object)) {
                hook.accept(type.cast(object));
            }
        };

        return context -> {
            DiscardHook contextualHook = context.getOrDefault(DiscardHook.class, null);
            return context.put(DiscardHook.class, contextualHook == null ? discardHook : discardHook.andThen(contextualHook));
        };
    }

    static DiscardHook choose(ContextView contextView) {
        return contextView.getOrDefault(DiscardHook.class, DiscardHook.noOp());
    }

    static DiscardHook noOp() {
        return __ -> {

        };
    }

    @Override
    default DiscardHook andThen(Consumer after) {
        return object -> {
            accept(object);
            after.accept(object);
        };
    }
}