/**
 * Copyright 2005-2007 Xue Yong Zhi
 * Distributed under the GNU General Public License 2.0
 */

package com.xruby.runtime.builtin;

import com.xruby.runtime.lang.*;
import com.xruby.runtime.value.ObjectFactory;
import com.xruby.runtime.value.RubyArray;
import com.xruby.runtime.value.RubyThread;

class Thread_join extends RubyNoArgMethod {
    protected RubyValue run(RubyValue receiver, RubyBlock block) {
        RubyThread t = (RubyThread) receiver;
        t.join();
        return t;
    }
}

class Thread_new extends RubyVarArgMethod {
    protected RubyValue run(RubyValue receiver, RubyArray args, RubyBlock block) {
        return ObjectFactory.createThread(block);
    }
}

class Thread_current extends RubyNoArgMethod {
    protected RubyValue run(RubyValue receiver, RubyBlock block) {
        return RubyThread.current();
    }
}

//c ruby does not have this method, but since we wrapped java's Thread, can no longer use java's == operator
class Thread_equal extends RubyOneArgMethod {
    protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
        if (receiver.equals(arg)) {
            return ObjectFactory.TRUE_VALUE;
        } else {
            return ObjectFactory.FALSE_VALUE;
        }
    }
}

class Thread_array_access extends RubyOneArgMethod {
    protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
        String s = RubyTypesUtil.convertToJavaString(arg);
        return RubyThread.getVariable(s);
    }
}

class Thread_array_set extends RubyTwoArgMethod {
    protected RubyValue run(RubyValue receiver, RubyValue arg1, RubyValue arg2, RubyBlock block) {
        String s = RubyTypesUtil.convertToJavaString(arg1);
        return RubyThread.setVariable(s, arg2);
    }
}

public class ThreadClassBuilder {
    public static void initialize() {
        RubyClass c = RubyRuntime.ThreadClass;
        c.defineMethod("join", new Thread_join());
        c.defineMethod("[]", new Thread_array_access());
        c.defineMethod("[]=", new Thread_array_set());
        c.defineMethod("==", new Thread_equal());
        c.getSingletonClass().defineMethod("current", new Thread_current());
        c.getSingletonClass().defineMethod("new", new Thread_new());
    }
}

@@ -33,18 +33,28 @@
 Yong Zhi
+, Yu Zhang
 %0D%0A
-
  * Distr
@@ -158,32 +158,417 @@
 ort 
-com.xruby.runtime.lang.*
+java.util.Iterator;%0D%0Aimport java.util.List;%0D%0A%0D%0Aimport com.xruby.runtime.lang.RubyBlock;%0D%0Aimport com.xruby.runtime.lang.RubyClass;%0D%0Aimport com.xruby.runtime.lang.RubyNoArgMethod;%0D%0Aimport com.xruby.runtime.lang.RubyOneArgMethod;%0D%0Aimport com.xruby.runtime.lang.RubyRuntime;%0D%0Aimport com.xruby.runtime.lang.RubyTwoArgMethod;%0D%0Aimport com.xruby.runtime.lang.RubyValue;%0D%0Aimport com.xruby.runtime.lang.RubyVarArgMethod
 ;%0D%0Ai
@@ -2206,16 +2206,860 @@
 %7D%0D%0A%7D%0D%0A%0D%0A
+class Thread_list extends RubyNoArgMethod %7B%0D%0A    protected RubyValue run(RubyValue receiver, RubyBlock block) %7B%0D%0A        List list = RubyThread.getThreadList();%0D%0A        Iterator iter = list.iterator();%0D%0A        RubyArray result = new RubyArray(0);%0D%0A        while(iter.hasNext())%7B%0D%0A            result.add((RubyThread)iter.next());%0D%0A        %7D%0D%0A        return result;%0D%0A    %7D%0D%0A%7D%0D%0A%0D%0Aclass Thread_main extends RubyNoArgMethod %7B%0D%0A    protected RubyValue run(RubyValue receiver, RubyBlock block) %7B%0D%0A        return RubyThread.getMainThread();%0D%0A    %7D%0D%0A%7D%0D%0A%0D%0Aclass Thread_is_alive extends RubyNoArgMethod %7B%0D%0A    protected RubyValue run(RubyValue receiver, RubyBlock block) %7B%0D%0A        if(((RubyThread)receiver).isAlive())%7B%0D%0A            return ObjectFactory.TRUE_VALUE;%0D%0A        %7Delse%7B%0D%0A            return ObjectFactory.FALSE_VALUE;%0D%0A        %7D%0D%0A    %7D%0D%0A%7D%0D%0A%0D%0A
 public c
@@ -3120,24 +3120,62 @@
 ialize() %7B%0D%0A
+        RubyThread.init();%0D%0A        %0D%0A
         Ruby
@@ -3372,24 +3372,24 @@
 ay_set());%0D%0A
-
         c.de
@@ -3423,24 +3423,226 @@
 _equal());%0D%0A
+        c.defineMethod(%22alive?%22, new Thread_is_alive());%0D%0A        c.getSingletonClass().defineMethod(%22main%22, new Thread_main());%0D%0A        c.getSingletonClass().defineMethod(%22list%22, new Thread_list());%0D%0A
         c.ge

add Thread.alive?,Thread#main and Thread#list