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

eu.lucaventuri.fibry.SynchronousActor Maven / Gradle / Ivy

There is a newer version: 3.0.1
Show newest version
package eu.lucaventuri.fibry;

import eu.lucaventuri.functional.Either3;

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

/**
 * Actor that runs in the same thread as the caller; this is achieved with the Actor being the queue as well.
 * THis can be useful in particular cases.
 */
public class SynchronousActor extends Actor implements MiniFibryQueue> {
    public SynchronousActor(Consumer actorLogic, S initialState, Consumer initializer, Consumer finalizer, CloseStrategy closeStrategy, int pollTimeoutMs) {
        super(actorLogic, new MiniFibryQueue.MiniFibryQueueDelegate<>(), initialState, initializer, finalizer, closeStrategy, pollTimeoutMs);

        sendPoisonPillWhenExiting = false;
        ((MiniFibryQueue.MiniFibryQueueDelegate) queue).setQueue(this);
    }

    SynchronousActor(Function actorLogicReturn, S initialState, Consumer initializer, Consumer finalizer, CloseStrategy closeStrategy, int pollTimeoutMs) {
        super(actorLogicReturn, new MiniFibryQueue.MiniFibryQueueDelegate<>(), initialState, initializer, finalizer, closeStrategy, pollTimeoutMs);

        sendPoisonPillWhenExiting = false;
        ((MiniFibryQueue.MiniFibryQueueDelegate) queue).setQueue(this);
    }

    @Override
    protected void takeAndProcessSingleMessage() throws InterruptedException {
        throw new UnsupportedOperationException();
    }

    @Override
    protected void takeAndProcessSingleMessageTimeout() throws InterruptedException {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean add(Either3>, T, MessageWithAnswer> message) {
        message.ifEither(cns -> cns.accept(this), actorLogic::accept, actorLogicReturn::accept);

        return true;
    }

    @Override
    public Either3>, T, MessageWithAnswer> take() throws InterruptedException {
        throw new UnsupportedOperationException();
    }

    @Override
    public Either3>, T, MessageWithAnswer> poll(long timeout, TimeUnit unit) throws InterruptedException {
        throw new UnsupportedOperationException();
    }

    @Override
    public void clear() {
    }

    @Override
    public int size() {
        return 0;
    }

    @Override
    public Either3>, T, MessageWithAnswer> peek() {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean sendPoisonPill() {
        sendPoisonPillWhenExiting = false;
        askExit();
        return true;
    }
}