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

co.paralleluniverse.actors.behaviors.GenEvent Maven / Gradle / Ivy

There is a newer version: 0.8.0
Show newest version
/*
 * Quasar: lightweight threads and actors for the JVM.
 * Copyright (C) 2013, Parallel Universe Software Co. All rights reserved.
 * 
 * This program and the accompanying materials are dual-licensed under
 * either the terms of the Eclipse Public License v1.0 as published by
 * the Eclipse Foundation
 *  
 *   or (per the licensee's choosing)
 *  
 * under the terms of the GNU Lesser General Public License version 3.0
 * as published by the Free Software Foundation.
 */
package co.paralleluniverse.actors.behaviors;

import co.paralleluniverse.actors.Actor;
import co.paralleluniverse.actors.ActorBuilder;
import co.paralleluniverse.actors.ActorRef;
import static co.paralleluniverse.actors.behaviors.RequestReplyHelper.call;
import co.paralleluniverse.fibers.Joinable;
import co.paralleluniverse.fibers.SuspendExecution;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 *
 * @author pron
 */
public class GenEvent extends GenBehavior {
    GenEvent(ActorRef actor) {
        super(actor);
    }

    public boolean addHandler(EventHandler handler) throws SuspendExecution, InterruptedException {
        if(isInActor())
            return GenEventActor.currentGenEvent().addHandler(handler);
        
        final GenResponseMessage res = call(this, new GenEventActor.HandlerMessage(RequestReplyHelper.from(), null, handler, true));
        return ((GenValueResponseMessage) res).getValue();
    }

    public boolean removeHandler(EventHandler handler) throws SuspendExecution, InterruptedException {
        if(isInActor())
            return GenEventActor.currentGenEvent().removeHandler(handler);
        
        final GenResponseMessage res = call(this, new GenEventActor.HandlerMessage(RequestReplyHelper.from(), null, handler, false));
        return ((GenValueResponseMessage) res).getValue();
    }

    public void notify(Event event) throws SuspendExecution {
        send(event);
    }

    static final class Local extends GenEvent implements LocalBehavior> {
        Local(ActorRef actor) {
            super(actor);
        }

        @Override
        public GenEvent writeReplace() throws java.io.ObjectStreamException {
            return new GenEvent<>(ref);
        }

        @Override
        public Actor build() {
            return ((ActorBuilder) ref).build();
        }

        @Override
        public void join() throws ExecutionException, InterruptedException {
            ((Joinable) ref).join();
        }

        @Override
        public void join(long timeout, TimeUnit unit) throws ExecutionException, InterruptedException, TimeoutException {
            ((Joinable) ref).join(timeout, unit);
        }

        @Override
        public Void get() throws ExecutionException, InterruptedException {
            return ((Joinable) ref).get();
        }

        @Override
        public Void get(long timeout, TimeUnit unit) throws ExecutionException, InterruptedException, TimeoutException {
            return ((Joinable) ref).get(timeout, unit);
        }

        @Override
        public boolean isDone() {
            return ((Joinable) ref).isDone();
        }
    }
}