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

com.cloudbees.groovy.cps.green.GreenWorld Maven / Gradle / Ivy

There is a newer version: 1.31
Show newest version
package com.cloudbees.groovy.cps.green;

import com.cloudbees.groovy.cps.Block;
import com.cloudbees.groovy.cps.Continuable;
import com.cloudbees.groovy.cps.Continuation;
import com.cloudbees.groovy.cps.Env;
import com.cloudbees.groovy.cps.Next;
import com.cloudbees.groovy.cps.Outcome;
import com.cloudbees.groovy.cps.impl.ProxyEnv;

import java.io.Serializable;

/**
 * Immutable representation of the combined states of all {@link GreenThreadState}s.
 *
 * The whole thing has to be immutable because cloning {@link Continuable} is just shallow-copying its variables.
 *
 * @author Kohsuke Kawaguchi
 */
class GreenWorld implements Serializable {
    final GreenThreadState[] threads;
    private final int cur;
    private final Env e;

    public GreenWorld(int cur, GreenThreadState... threads) {
        this.threads = threads;
        this.cur = cur;
        this.e = new ProxyEnv(currentThread().n.e);
    }

    GreenThreadState currentThread() {
        return threads[cur];
    }

    GreenWorld withNewThread(GreenThreadState s) {
        GreenThreadState[] a = new GreenThreadState[threads.length+1];
        System.arraycopy(threads,0,a,0, threads.length);
        a[threads.length] = s;
        return new GreenWorld(cur,a);
    }

    /**
     * Updates the thread state. If the thread is dead, it'll be removed.
     */
    GreenWorld with(GreenThreadState s) {
        int idx = -1;
        for (int i = 0; i < threads.length; i++) {
            if (threads[i].g==s.g) {
                threads[i] = s;
                idx = i;
                break;
            }
        }
        if (idx==-1)
            throw new IllegalStateException("No such thread: "+s.g);

        GreenWorld d;
        if (s.isDead()) {
            GreenThreadState[] a = new GreenThreadState[threads.length-1];
            System.arraycopy(threads,0,a,0,idx);
            System.arraycopy(threads,idx+1,a,cur, threads.length-idx);

            d = new GreenWorld(idx




© 2015 - 2024 Weber Informatics LLC | Privacy Policy