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

co.paralleluniverse.actors.behaviors.GenBehavior 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 co.paralleluniverse.actors.ActorRefDelegate;
import co.paralleluniverse.actors.ActorUtil;
import co.paralleluniverse.actors.ShutdownMessage;
import co.paralleluniverse.fibers.Joinable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 *
 * @author pron
 */
public class GenBehavior extends ActorRefDelegate implements java.io.Serializable {
    protected GenBehavior(ActorRef actor) {
        super(actor);
    }

    public void shutdown() {
        final ShutdownMessage message = new ShutdownMessage(ActorRef.self());
        ActorUtil.sendOrInterrupt(ref, message);
    }

    @Override
    public void close() {
        throw new UnsupportedOperationException();
    }

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

        @Override
        public GenBehavior writeReplace() throws java.io.ObjectStreamException {
            return new GenBehavior(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();
        }
    }
}