<commit_before>package tutorial;



/*!begin-include!*/
/*!begin-code!*/
import io.reactors.japi.*;
/*!end-code!*/
/*!end-include(reactors-java-event-streams-import.html)!*/
import java.util.ArrayList;
import java.util.Arrays;
import org.junit.Assert;
import org.junit.Test;



public class event_streams {
  private Events<String> createEventStream() {
    return Events.never();
  }

  static FakeSystem System = new FakeSystem();

  @Test
  public void eventsOnEvent() {
    /*!begin-include!*/
    /*!begin-code!*/
    Events<String> myEvents = createEventStream();
    /*!end-code!*/
    /*!end-include(reactors-java-event-streams-create.html)!*/

    /*!begin-include!*/
    /*!begin-code!*/
    myEvents.onEvent(x -> System.out.println(x));
    /*!end-code!*/
    /*!end-include(reactors-java-event-streams-on-event.html)!*/
  }

  /*!begin-include!*/
  /*!begin-code!*/
  public <T> void trace(Events<T> events) {
    events.onEvent(x -> System.out.println(x));
  }
  /*!end-code!*/
  /*!end-include(reactors-java-event-streams-trace.html)!*/

  @Test
  public void emitterReact() {
    /*!begin-include!*/
    /*!begin-code!*/
    Events.Emitter<Integer> emitter = Events.emitter();
    /*!end-code!*/
    /*!end-include(reactors-java-event-streams-emitter.html)!*/

    /*!begin-include!*/
    /*!begin-code!*/
    final int[] luckyNumber = new int[] { 0 };
    emitter.onEvent(x -> luckyNumber[0] = x);
    emitter.react(7);
    Assert.assertEquals(7, luckyNumber[0]);
    /*!end-code!*/
    /*!end-include(reactors-java-event-streams-lucky-number.html)!*/
  }

  @Test
  public void emitterLifecycle() {
    /*!begin-include!*/
    /*!begin-code!*/
    ArrayList<Integer> seen = new ArrayList<Integer>();
    ArrayList<String> errors = new ArrayList<String>();
    int[] done = new int[] { 0 };
    Events.Emitter<Integer> e = Events.emitter();
    e.onReaction(Observer.create(
      x -> seen.add(x),
      t -> errors.add(t.getMessage()),
      () -> done[0]++));
    /*!end-code!*/
    /*!end-include(reactors-java-event-streams-observer.html)!*/

    /*!begin-include!*/
    /*!begin-code!*/
    e.react(1);
    Assert.assertEquals(seen, new ArrayList<Integer>(Arrays.asList(1)));
    /*!end-code!*/
    /*!end-include(reactors-java-event-streams-observer-test.html)!*/
  }
}<commit_msg>Complete emitter lifecycle `japi` example.<commit_after>package tutorial;



/*!begin-include!*/
/*!begin-code!*/
import io.reactors.japi.*;
/*!end-code!*/
/*!end-include(reactors-java-event-streams-import.html)!*/
import java.util.ArrayList;
import java.util.Arrays;
import org.junit.Assert;
import org.junit.Test;



public class event_streams {
  private Events<String> createEventStream() {
    return Events.never();
  }

  static FakeSystem System = new FakeSystem();

  @Test
  public void eventsOnEvent() {
    /*!begin-include!*/
    /*!begin-code!*/
    Events<String> myEvents = createEventStream();
    /*!end-code!*/
    /*!end-include(reactors-java-event-streams-create.html)!*/

    /*!begin-include!*/
    /*!begin-code!*/
    myEvents.onEvent(x -> System.out.println(x));
    /*!end-code!*/
    /*!end-include(reactors-java-event-streams-on-event.html)!*/
  }

  /*!begin-include!*/
  /*!begin-code!*/
  public <T> void trace(Events<T> events) {
    events.onEvent(x -> System.out.println(x));
  }
  /*!end-code!*/
  /*!end-include(reactors-java-event-streams-trace.html)!*/

  @Test
  public void emitterReact() {
    /*!begin-include!*/
    /*!begin-code!*/
    Events.Emitter<Integer> emitter = Events.emitter();
    /*!end-code!*/
    /*!end-include(reactors-java-event-streams-emitter.html)!*/

    /*!begin-include!*/
    /*!begin-code!*/
    final int[] luckyNumber = new int[] { 0 };
    emitter.onEvent(x -> luckyNumber[0] = x);
    emitter.react(7);
    Assert.assertEquals(7, luckyNumber[0]);
    /*!end-code!*/
    /*!end-include(reactors-java-event-streams-lucky-number.html)!*/
  }

  @Test
  public void emitterLifecycle() {
    /*!begin-include!*/
    /*!begin-code!*/
    ArrayList<Integer> seen = new ArrayList<Integer>();
    ArrayList<String> errors = new ArrayList<String>();
    int[] done = new int[] { 0 };
    Events.Emitter<Integer> e = Events.emitter();
    e.onReaction(Observer.create(
      x -> seen.add(x),
      t -> errors.add(t.getMessage()),
      () -> done[0]++));
    /*!end-code!*/
    /*!end-include(reactors-java-event-streams-observer.html)!*/

    /*!begin-include!*/
    /*!begin-code!*/
    e.react(1);
    Assert.assertEquals(seen, new ArrayList<Integer>(Arrays.asList(1)));
    e.react(2);
    Assert.assertEquals(seen, new ArrayList<Integer>(Arrays.asList(1, 2)));
    Assert.assertEquals(done[0], 0);
    e.except(new Exception("^_^"));
    Assert.assertEquals(errors, new ArrayList<String>(Arrays.asList("^_^")));
    e.react(3);
    Assert.assertEquals(seen, new ArrayList<Integer>(Arrays.asList(1, 2, 3)));
    Assert.assertEquals(done[0], 0);
    e.unreact();
    Assert.assertEquals(done[0], 1);
    e.react(4);
    e.except(new Exception("o_O"));
    Assert.assertEquals(seen, new ArrayList<Integer>(Arrays.asList(1, 2, 3)));
    Assert.assertEquals(errors, new ArrayList<String>(Arrays.asList("^_^")));
    Assert.assertEquals(done[0], 1);
    /*!end-code!*/
    /*!end-include(reactors-java-event-streams-observer-test.html)!*/
  }
}
{'fraction_non_alphanumeric': 0.20783132530120482, 'fraction_numerical': 0.006965361445783132, 'mean_word_length': 3.2846774193548387, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1356897', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1795, 'n_words': 284}