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

io.jexxa.core.FluentInterceptor Maven / Gradle / Ivy

There is a newer version: 8.1.5
Show newest version
package io.jexxa.core;

import io.jexxa.adapterapi.invocation.InvocationContext;
import io.jexxa.adapterapi.invocation.InvocationManager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.function.Consumer;

@SuppressWarnings("unused")
public final class FluentInterceptor
{
    private final Collection targetObjects = new ArrayList<>();
    private final JexxaMain jexxaMain;

    FluentInterceptor(JexxaMain jexxaMain, Object... targetObjects )
    {
        this.targetObjects.addAll(Arrays.asList(targetObjects));

        this.jexxaMain = jexxaMain;
    }

    @SuppressWarnings("UnusedReturnValue")
    public JexxaMain before(Consumer consumer)
    {
        targetObjects.stream()
                .map(InvocationManager::getRootInterceptor)
                .forEach( interceptor -> interceptor.registerBefore(consumer::accept));

        return jexxaMain;
    }

    public FluentInterceptor beforeAnd(Consumer consumer)
    {
        before(consumer);
        return this;
    }

    @SuppressWarnings("UnusedReturnValue")
    public JexxaMain after(Consumer consumer)
    {
        targetObjects.stream()
                .map(InvocationManager::getRootInterceptor)
                .forEach( interceptor -> interceptor.registerAfter(consumer::accept));

        return jexxaMain;
    }

    @SuppressWarnings("UnusedReturnValue")
    public FluentInterceptor afterAnd(Consumer consumer)
    {
        after(consumer);
        return this;
    }

    @SuppressWarnings("UnusedReturnValue")
    public JexxaMain around(Consumer consumer)
    {
        targetObjects.stream()
                .map(InvocationManager::getRootInterceptor)
                .forEach( interceptor -> interceptor.registerAround(consumer::accept));

        return jexxaMain;
    }

    @SuppressWarnings("UnusedReturnValue")
    public FluentInterceptor aroundAnd(Consumer consumer)
    {
        around(consumer);
        return this;
    }
}