コード例 #1
0
  @Test
  public void sequentialStartCallsInputsAndOutputs() throws Exception {
    final List<Integer> inputs = new ArrayList<>();
    final List<Integer> outputs = new ArrayList<>();

    final UIComponent<Integer, Integer, Object> component =
        factory.createUIComponent(
            () -> new Object(),
            o -> {
              final FlowInput<Integer> flowInput = factory.createInput();
              final FlowOutput<Integer> flowOutput = factory.createOutput();
              setKey(o, flowInput);
              setKey(o, flowOutput);
              inputs.add(flowInput.get());
              final int output = flowInput.get() + 1;
              outputs.add(output);
              flowOutput.submit(output);
            },
            o -> {},
            "Adder");

    component.start(
        0,
        n1 -> {
          component.start(
              n1,
              n2 -> {
                component.start(n2, n3 -> {});
              });
        });

    assertEquals(asList(0, 1, 2), inputs);
    assertEquals(asList(1, 2, 3), outputs);
  }
コード例 #2
0
  @Test
  public void nestedStartCallsInputsAndOutputs() throws Exception {
    final List<Integer> inputs = new ArrayList<>();
    final List<Integer> outputs = new ArrayList<>();

    final Function<Function<Integer, Integer>, UIComponent<Integer, Integer, Object>>
        componentFunc =
            consumer ->
                factory.createUIComponent(
                    () -> new Object(),
                    o -> {
                      final FlowInput<Integer> flowInput = factory.createInput();
                      final FlowOutput<Integer> flowOutput = factory.createOutput();
                      setKey(o, flowInput);
                      setKey(o, flowOutput);
                      final Integer beforeInput = flowInput.get();
                      inputs.add(beforeInput);
                      final int intermediateOutput = beforeInput + 1;
                      final Integer finalOutput = consumer.apply(intermediateOutput);
                      assertEquals(beforeInput, flowInput.get());
                      outputs.add(finalOutput);
                      flowOutput.submit(finalOutput);
                    },
                    o -> {},
                    "Adder");

    componentFunc
        .apply(
            n1 -> {
              final Ref<Integer> ref1 = new Ref<>();
              componentFunc
                  .apply(
                      n2 -> {
                        final Ref<Integer> ref2 = new Ref<>();
                        componentFunc
                            .apply(n3 -> n3 + 1)
                            .start(
                                n2,
                                val -> {
                                  ref2.val = val;
                                });
                        return ref2.val;
                      })
                  .start(
                      n1,
                      val -> {
                        ref1.val = val;
                      });
              return ref1.val;
            })
        .start(0, val -> {});

    assertEquals(asList(0, 1, 2), inputs);
    assertEquals(asList(4, 4, 4), outputs);
  }