package tutorial;



import io.reactors.japi.*;
import java.util.concurrent.*;
import org.junit.Assert;
import org.junit.Test;



public class reactors {

  /*!begin-include!*/
  /*!begin-code!*/
  ReactorSystem system = ReactorSystem.create("test-system");
  /*!end-code!*/
  /*!end-include(reactors-java-reactors-system.html)!*/

  static class FakeSystem {
    public class Out {
      public LinkedTransferQueue<Object> queue = new LinkedTransferQueue<>();
      public void println(Object x) {
        queue.add(x);
      }
    }
    public Out out = new Out();
  }

  static FakeSystem System = new FakeSystem();

  @Test
  public void runsAnonymousReactor() {
    /*!begin-include!*/
    /*!begin-code!*/
    Proto<String> proto = Reactor.apply(
      self -> self.main().events().onEvent(x -> System.out.println(x))
    );
    /*!end-code!*/
    /*!end-include(reactors-java-reactors-anonymous.html)!*/

    /*!begin-include!*/
    /*!begin-code!*/
    Channel<String> ch = system.spawn(proto);
    /*!end-code!*/
    /*!end-include(reactors-java-reactors-spawn.html)!*/

    /*!begin-include!*/
    /*!begin-code!*/
    ch.send("Hola!");
    /*!end-code!*/
    /*!end-include(reactors-java-reactors-send.html)!*/

    try {
      Assert.assertEquals("Hola!", System.out.queue.take());
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  /*!begin-include!*/
  /*!begin-code!*/
  public static class HelloReactor extends Reactor<String> {
    public HelloReactor() {
      main().events().onEvent(
        x -> System.out.println(x)
      );
    }
  }
  /*!end-code!*/
  /*!end-include(reactors-java-reactors-template.html)!*/

  @Test
  public void runsHelloReactor() {
    ReactorSystem system = ReactorSystem.create("test-system");

    /*!begin-include!*/
    /*!begin-code!*/
    Proto<String> proto = Proto.create(HelloReactor.class);
    Channel<String> ch = system.spawn(proto);
    ch.send("Howdee!");
    /*!end-code!*/
    /*!end-include(reactors-java-reactors-spawn-template.html)!*/

    try {
      Assert.assertEquals("Howdee!", System.out.queue.take());
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    /*!begin-include!*/
    /*!begin-code!*/
    system.spawn(Proto.create(HelloReactor.class)
      .withScheduler(Scheduler.NEW_THREAD));
    /*!end-code!*/
    /*!end-include(reactors-java-reactors-with-scheduler.html)!*/

    /*!begin-include!*/
    /*!begin-code!*/
    system.bundle().registerScheduler("customTimer", Scheduler.timer(1000));
    Proto<String> protoWithTimer =
      Proto.create(HelloReactor.class).withScheduler("customTimer");
    Channel<String> periodic = system.spawn(protoWithTimer);
    periodic.send("Ohayo");
    /*!end-code!*/
    /*!end-include(reactors-java-reactors-custom-scheduler.html)!*/

    try {
      Assert.assertEquals("Ohayo", System.out.queue.take());
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
}

@@ -68,24 +68,72 @@
 ncurrent.*;%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0A
 import org.j
@@ -2991,10 +2991,979 @@
   %7D%0A  %7D%0A
+%0A  /*!begin-include!*/%0A  /*!begin-code!*/%0A  public static class Pair%3CK, V%3E %7B%0A    public K k;%0A    public V v;%0A    public Pair(K k, V v) %7B%0A      this.k = k;%0A      this.v = v;%0A    %7D%0A  %7D%0A%0A  public static class PutOnlyReactor%3CK, V%3E extends Reactor%3CPair%3CK, V%3E%3E %7B%0A    Map%3CK, V%3E map = new HashMap%3CK, V%3E();%0A%0A    public PutOnlyReactor() %7B%0A      main().events().onEvent(p -%3E map.put(p.k, p.v));%0A    %7D%0A  %7D%0A  /*!end-code!*/%0A  /*!end-include(reactors-java-reactors-put-only.html)!*/%0A%0A  /*!begin-include!*/%0A  /*!begin-code!*/%0A  public static interface Op%3CK, V%3E %7B%0A  %7D%0A%0A  public static class Put%3CK, V%3E implements Op%3CK, V%3E %7B%0A    public K k;%0A    public V v;%0A    public Put(K k, V v) %7B%0A      this.k = k;%0A      this.v = v;%0A    %7D%0A  %7D%0A%0A  public static class Get%3CK, V%3E implements Op%3CK, V%3E %7B%0A    public K k;%0A    public Channel%3CV%3E ch;%0A    public Get(K k, Channel%3CV%3E ch) %7B%0A      this.k = k;%0A      this.ch = ch;%0A    %7D%0A  %7D%0A  /*!end-code!*/%0A  /*!end-include(reactors-java-reactors-map-ops.html)!*/%0A%0A
 %7D%0A

Extend reactors Java snippets.