/**
 * Copyright 2005-2007 Yu Zhang
 * Distributed under the GNU General Public License 2.0
 */

package com.xruby.runtime.builtin;

import java.util.Iterator;
import java.util.List;

import com.xruby.runtime.lang.RubyBlock;
import com.xruby.runtime.lang.RubyClass;
import com.xruby.runtime.lang.RubyNoArgMethod;
import com.xruby.runtime.lang.RubyOneArgMethod;
import com.xruby.runtime.lang.RubyRuntime;
import com.xruby.runtime.lang.RubyValue;
import com.xruby.runtime.value.RubyArray;
import com.xruby.runtime.value.RubyThread;
import com.xruby.runtime.value.RubyThreadGroup;

class ThreadGroup_list extends RubyNoArgMethod {
    protected RubyValue run(RubyValue receiver, RubyBlock block) {
        RubyThreadGroup group = (RubyThreadGroup)receiver;
        List list = group.list();
        Iterator iter = list.iterator();
        RubyArray result = new RubyArray(0);
        while(iter.hasNext()){
            result.add((RubyThread)iter.next());
        }
        return result;
    }
}

class ThreadGroup_new extends RubyNoArgMethod {
    protected RubyValue run(RubyValue receiver, RubyBlock block) {
        return new RubyThreadGroup();
    }
}

class ThreadGroup_add extends RubyOneArgMethod {
    protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
        RubyThread thread = (RubyThread)arg;
        ((RubyThreadGroup)receiver).add(thread);
        return receiver;
    }
}

public class ThreadGroupClassBuilder {
    public static void initialize() {
        RubyClass c = RubyRuntime.ThreadGroupClass;             
        c.defineMethod("add", new ThreadGroup_add());
        c.defineMethod("list", new ThreadGroup_list());      
        
        c.setConstant("Default",RubyThreadGroup.defaultThreadGroup);
        c.getSingletonClass().defineMethod("new", new ThreadGroup_new());
    }
}

@@ -450,24 +450,71 @@
 RubyValue;%0D%0A
+import com.xruby.runtime.value.ObjectFactory;%0D%0A
 import com.x
@@ -1504,16 +1504,520 @@
 %7D%0D%0A%7D%0D%0A%0D%0A
+class ThreadGroup_enclose extends RubyNoArgMethod %7B%0D%0A    protected RubyValue run(RubyValue receiver, RubyBlock block) %7B%0D%0A        RubyThreadGroup group = (RubyThreadGroup)receiver;%0D%0A        group.enclose();%0D%0A        return group;%0D%0A    %7D%0D%0A%7D%0D%0A%0D%0Aclass ThreadGroup_is_enclosed extends RubyNoArgMethod %7B%0D%0A    protected RubyValue run(RubyValue receiver, RubyBlock block) %7B%0D%0A        RubyThreadGroup group = (RubyThreadGroup)receiver;%0D%0A        return ObjectFactory.createBoolean(group.isEnclosed());%0D%0A    %7D%0D%0A%7D%0D%0A%0D%0A
 public c
@@ -2265,20 +2265,148 @@
 st());  
+%0D%0A
     
+    c.defineMethod(%22enclose%22, new ThreadGroup_enclose());%0D%0A        c.defineMethod(%22enclosed?%22, new ThreadGroup_is_enclosed());
 %0D%0A      

add ThreadGroup#enclose and ThreadGroup#enclosed?