org.eclipse.microprofile.reactive.streams.operators.spi.Graph Java Examples

The following examples show how to use org.eclipse.microprofile.reactive.streams.operators.spi.Graph. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Engine.java    From smallrye-reactive-streams-operators with Apache License 2.0 6 votes vote down vote up
@Override
public <T> CompletionStage<T> buildCompletion(Graph graph) {
    Flowable<?> flowable = null;
    for (Stage stage : graph.getStages()) {
        Operator operator = Stages.lookup(stage);
        if (operator instanceof PublisherOperator) {
            flowable = createPublisher(stage, (PublisherOperator) operator);
        } else if (operator instanceof ProcessorOperator) {
            flowable = applyProcessors(flowable, stage, (ProcessorOperator) operator);
        } else {
            return applySubscriber(flowable, stage, (TerminalOperator) operator);
        }
    }

    throw new IllegalArgumentException("Graph did not have terminal stage");
}
 
Example #2
Source File: Engine.java    From smallrye-reactive-streams-operators with Apache License 2.0 6 votes vote down vote up
@Override
public <T, R> SubscriberWithCompletionStage<T, R> buildSubscriber(Graph graph) {
    Processor<T, T> processor = new ConnectableProcessor<>();
    Flowable<T> flowable = Flowable.fromPublisher(processor);
    for (Stage stage : graph.getStages()) {
        Operator operator = Stages.lookup(stage);
        if (operator instanceof ProcessorOperator) {
            flowable = applyProcessors(flowable, stage, (ProcessorOperator) operator);
        } else if (operator instanceof TerminalOperator) {
            CompletionStage<R> result = applySubscriber(Transformer.apply(flowable), stage,
                    (TerminalOperator) operator);
            return new DefaultSubscriberWithCompletionStage<>(processor, result);
        } else {
            throw new UnsupportedStageException(stage);
        }
    }

    throw new IllegalArgumentException("The graph does not have a valid final stage");
}
 
Example #3
Source File: ConcatStageFactoryTest.java    From smallrye-reactive-streams-operators with Apache License 2.0 6 votes vote down vote up
@Test(expected = NullPointerException.class)
public void testWithoutEngine() {
    Graph g1 = () -> Collections.singletonList((Stage.Of) () -> Arrays.asList(1, 2, 3));
    Graph g2 = () -> Collections.singletonList((Stage.Of) () -> Arrays.asList(1, 2, 3));
    factory.create(null, new Stage.Concat() {
        @Override
        public Graph getFirst() {
            return g1;
        }

        @Override
        public Graph getSecond() {
            return g2;
        }
    });
}
 
Example #4
Source File: Engine.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@Override
public <T> CompletionStage<T> buildCompletion(Graph graph) {
    Multi<?> flowable = null;
    for (Stage stage : graph.getStages()) {
        Operator operator = Stages.lookup(stage);
        if (operator instanceof PublisherOperator) {
            flowable = createPublisher(stage, (PublisherOperator) operator);
        } else if (operator instanceof ProcessorOperator) {
            flowable = applyProcessors(flowable, stage, (ProcessorOperator) operator);
        } else {
            return applySubscriber(flowable, stage, (TerminalOperator) operator);
        }
    }

    throw new IllegalArgumentException("Graph did not have terminal stage");
}
 
Example #5
Source File: FlatMapStageFactory.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Override
public Multi<O> apply(Multi<I> source) {
    return source
            .onItem().<O> producePublisher(item -> {
                Graph graph = mapper.apply(item);
                return engine.buildPublisher(Objects.requireNonNull(graph));
            })
            .concatenate();
}
 
Example #6
Source File: EngineTest.java    From smallrye-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test(expected = UnsupportedStageException.class)
public void testCreatingPublisherWithUnknownStage() {
    engine = new Engine();
    List<Stage> stages = new ArrayList<>();
    stages.add(new Stage() {
        // Unknown stage.
    });
    stages.add((Stage.Map) () -> i -> (int) i + 1);
    Graph graph = () -> stages;
    engine.buildPublisher(graph);
}
 
Example #7
Source File: EngineTest.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testInvalidSubscriber() {
    engine = new Engine();
    List<Stage> stages = new ArrayList<>();
    stages.add((Stage.Map) () -> i -> (int) i + 1);
    // This graph is not closed - so it's invalid
    Graph graph = () -> stages;
    engine.buildSubscriber(graph);
}
 
Example #8
Source File: EngineTest.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidCompletion() {
    engine = new Engine();
    List<Stage> stages = new ArrayList<>();
    stages.add((Stage.PublisherStage) () -> Multi.createFrom().empty());
    stages.add((Stage.Map) () -> i -> (int) i + 1);
    stages.add(new Stage.FindFirst() {
    });
    Graph graph = () -> stages;
    assertThat(engine.buildCompletion(graph)).isNotNull();
}
 
Example #9
Source File: EngineTest.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testInvalidCompletion() {
    engine = new Engine();
    List<Stage> stages = new ArrayList<>();
    stages.add((Stage.PublisherStage) () -> Multi.createFrom().empty());
    stages.add((Stage.Map) () -> i -> (int) i + 1);
    // This graph is not closed - so it's invalid
    Graph graph = () -> stages;
    engine.buildCompletion(graph);
}
 
Example #10
Source File: EngineTest.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Test(expected = UnsupportedStageException.class)
public void testCompletionWithUnknownStage() {
    engine = new Engine();
    List<Stage> stages = new ArrayList<>();
    stages.add((Stage.PublisherStage) () -> Multi.createFrom().empty());
    stages.add((Stage.Map) () -> i -> (int) i + 1);
    stages.add(new Stage() {
        // Unknown stage.
    });
    stages.add(new Stage.FindFirst() {
    });
    Graph graph = () -> stages;
    assertThat(engine.buildCompletion(graph)).isNotNull();
}
 
Example #11
Source File: EngineTest.java    From smallrye-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test(expected = UnsupportedStageException.class)
public void testCompletionWithUnknownStage() {
    engine = new Engine();
    List<Stage> stages = new ArrayList<>();
    stages.add((Stage.PublisherStage) Flowable::empty);
    stages.add((Stage.Map) () -> i -> (int) i + 1);
    stages.add(new Stage() {
        // Unknown stage.
    });
    stages.add(new Stage.FindFirst() {
    });
    Graph graph = () -> stages;
    assertThat(engine.buildCompletion(graph)).isNotNull();
}
 
Example #12
Source File: EngineTest.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidPublisher() {
    engine = new Engine();
    List<Stage> stages = new ArrayList<>();
    stages.add((Stage.Of) () -> Arrays.asList(1, 2, 3));
    stages.add((Stage.Map) () -> i -> (int) i + 1);
    Graph graph = () -> stages;
    assertThat(engine.buildPublisher(graph)).isNotNull();
}
 
Example #13
Source File: Engine.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Override
public <T, R> Processor<T, R> buildProcessor(Graph graph) {
    Processor<T, T> processor = new ConnectableProcessor<>();

    Multi<T> multi = Multi.createFrom().publisher(processor);
    for (Stage stage : graph.getStages()) {
        Operator operator = Stages.lookup(stage);
        multi = applyProcessors(multi, stage, (ProcessorOperator) operator);
    }

    //noinspection unchecked
    return (Processor<T, R>) new WrappedProcessor<>(processor, multi);
}
 
Example #14
Source File: ReactiveStreamsVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void iterate() {
    Graph graph = graphFor(rs.iterate(1, i -> i + 1));
    Iterator iter = getStage(Stage.Of.class, graph).getElements().iterator();
    assertTrue(iter.hasNext());
    assertEquals(iter.next(), 1);
    assertTrue(iter.hasNext());
    assertEquals(iter.next(), 2);
    assertTrue(iter.hasNext());
    assertEquals(iter.next(), 3);
}
 
Example #15
Source File: ProcessorBuilderVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void peek() {
    AtomicInteger peeked = new AtomicInteger();
    Graph graph = graphFor(builder().peek(peeked::set));
    ((Consumer) getAddedStage(Stage.Peek.class, graph).getConsumer()).accept(1);
    assertEquals(peeked.get(), 1);
}
 
Example #16
Source File: PublisherBuilderVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void toMultipleStages() {
    Graph graph = graphFor(builder().to(
        rs.<Integer>builder().map(Function.identity()).cancel()));
    assertEquals(graph.getStages().size(), 3);
    Iterator<Stage> stages = graph.getStages().iterator();
    assertTrue(stages.next() instanceof Stage.Of);
    assertTrue(stages.next() instanceof Stage.Map);
    assertTrue(stages.next() instanceof Stage.Cancel);
}
 
Example #17
Source File: EngineTest.java    From smallrye-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test(expected = UnsupportedStageException.class)
public void testUnknownTerminalStage() {
    engine = new Engine();
    List<Stage> stages = new ArrayList<>();
    stages.add((Stage.Map) () -> i -> (int) i + 1);
    stages.add(new Stage() {
        // Unknown stage
    });
    Graph graph = () -> stages;
    engine.buildSubscriber(graph);
}
 
Example #18
Source File: PublisherBuilderVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void viaBuilderFromDifferentReactiveStreamsImplementation() {
    Graph graph = graphFor(builder().via(Mocks.PROCESSOR_BUILDER));
    assertEquals(graph.getStages().size(), 3);
    Iterator<Stage> stages = graph.getStages().iterator();
    assertTrue(stages.next() instanceof Stage.Of);
    assertTrue(stages.next() instanceof Stage.Distinct);
    assertTrue(stages.next() instanceof Stage.Limit);
}
 
Example #19
Source File: ProcessorBuilderVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void forEach() {
    AtomicInteger received = new AtomicInteger();
    Graph graph = graphFor(builder().forEach(received::set));
    Collector collector = getAddedStage(Stage.Collect.class, graph).getCollector();
    Object container = collector.supplier().get();
    collector.accumulator().accept(container, 1);
    assertEquals(received.get(), 1);
    assertNull(collector.finisher().apply(container));
}
 
Example #20
Source File: ProcessorBuilderVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
private <S extends Stage> S getAddedStage(Class<S> clazz, Graph graph) {
    assertEquals(graph.getStages().size(), 2, "Graph does not have two stages");
    Iterator<Stage> stages = graph.getStages().iterator();
    Stage first = stages.next();
    assertTrue(first instanceof Stage.Map, "First stage " + first + " is not a " + Stage.Map.class);
    Stage second = stages.next();
    assertTrue(clazz.isInstance(second), "Second stage " + second + " is not a " + clazz);
    return clazz.cast(second);
}
 
Example #21
Source File: OnErrorResumeWithStageFactory.java    From smallrye-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <I, O> ProcessingStage<I, O> create(Engine engine, Stage.OnErrorResumeWith stage) {
    Function<Throwable, Graph> function = Objects.requireNonNull(stage).getFunction();
    Objects.requireNonNull(function);

    return source -> (Flowable<O>) RxJavaPlugins.onAssembly(
            new OnErrorResumeWith<>(source, (Throwable err) -> {
                Graph graph = function.apply(err);
                return Flowable.fromPublisher(
                        Objects.requireNonNull(engine.buildPublisher(Objects.requireNonNull(graph))));
            }));
}
 
Example #22
Source File: ProcessorBuilderVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void reduce() {
    Graph graph = graphFor(builder().reduce((a, b) -> a - b));
    Collector collector = getAddedStage(Stage.Collect.class, graph).getCollector();
    Object container1 = collector.supplier().get();
    assertEquals(collector.finisher().apply(container1), Optional.empty());
    Object container2 = collector.supplier().get();
    collector.accumulator().accept(container2, 2);
    assertEquals(collector.finisher().apply(container2), Optional.of(2));
    Object container3 = collector.supplier().get();
    collector.accumulator().accept(container3, 5);
    collector.accumulator().accept(container3, 2);
    assertEquals(collector.finisher().apply(container3), Optional.of(3));
}
 
Example #23
Source File: CoupledStageFactory.java    From smallrye-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Override
public <I, O> ProcessingStage<I, O> create(Engine engine, Stage.Coupled stage) {
    Graph source = Objects.requireNonNull(stage.getPublisher());
    Graph sink = Objects.requireNonNull(stage.getSubscriber());

    Publisher<O> publisher = engine.buildPublisher(source);
    SubscriberWithCompletionStage<I, ?> subscriber = engine.buildSubscriber(sink);

    return upstream -> Flowable.fromPublisher(
            new CouplingProcessor<>(upstream, subscriber.getSubscriber(), publisher));
}
 
Example #24
Source File: ProcessorBuilderVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void onErrorResumeWithRsPublisher() {
    Graph graph = graphFor(builder().onErrorResumeWithRsPublisher(t -> Mocks.PUBLISHER));
    Graph resumeWith = getAddedStage(Stage.OnErrorResumeWith.class, graph).getFunction().apply(new RuntimeException());
    assertEquals(resumeWith.getStages().size(), 1);
    assertSame(((Stage.PublisherStage) resumeWith.getStages().iterator().next()).getRsPublisher(), Mocks.PUBLISHER);
}
 
Example #25
Source File: ProcessorBuilderVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void collectComponents() {
    Supplier supplier = () -> null;
    BiConsumer accumulator = (a, b) -> {};
    Graph graph = graphFor(builder().collect(supplier, accumulator));
    Collector collector = getAddedStage(Stage.Collect.class, graph).getCollector();
    assertSame(collector.supplier(), supplier);
    assertSame(collector.accumulator(), accumulator);
    // Finisher must be identity function
    Object myObject = new Object();
    assertSame(collector.finisher().apply(myObject), myObject);
}
 
Example #26
Source File: ProcessorBuilderVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void toList() {
    Graph graph = graphFor(builder().toList());
    Collector collector = getAddedStage(Stage.Collect.class, graph).getCollector();
    Object container = collector.supplier().get();
    collector.accumulator().accept(container, 1);
    collector.accumulator().accept(container, 2);
    collector.accumulator().accept(container, 3);
    assertEquals(collector.finisher().apply(container), Arrays.asList(1, 2, 3));
}
 
Example #27
Source File: PublisherBuilderVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void toList() {
    Graph graph = graphFor(builder().toList());
    Collector collector = getAddedStage(Stage.Collect.class, graph).getCollector();
    Object container = collector.supplier().get();
    collector.accumulator().accept(container, 1);
    collector.accumulator().accept(container, 2);
    collector.accumulator().accept(container, 3);
    assertEquals(collector.finisher().apply(container), Arrays.asList(1, 2, 3));
}
 
Example #28
Source File: PublisherBuilderVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void onErrorResumeWith() {
    Graph graph = graphFor(builder().onErrorResumeWith(t -> rs.empty()));
    Graph resumeWith = getAddedStage(Stage.OnErrorResumeWith.class, graph).getFunction().apply(new RuntimeException());
    assertEquals(resumeWith.getStages().size(), 1);
    assertEmptyStage(resumeWith.getStages().iterator().next());
}
 
Example #29
Source File: PublisherBuilderVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void toBuilderFromDifferentReactiveStreamsImplementation() {
    Graph graph = graphFor(builder().to(Mocks.SUBSCRIBER_BUILDER));
    assertEquals(graph.getStages().size(), 3);
    Iterator<Stage> stages = graph.getStages().iterator();
    assertTrue(stages.next() instanceof Stage.Of);
    assertTrue(stages.next() instanceof Stage.Distinct);
    assertTrue(stages.next() instanceof Stage.Cancel);
}
 
Example #30
Source File: ProcessorBuilderVerification.java    From microprofile-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Test
public void viaBuilderFromDifferentReactiveStreamsImplementation() {
    Graph graph = graphFor(builder().via(Mocks.PROCESSOR_BUILDER));
    assertEquals(graph.getStages().size(), 3);
    Iterator<Stage> stages = graph.getStages().iterator();
    assertTrue(stages.next() instanceof Stage.Map);
    assertTrue(stages.next() instanceof Stage.Distinct);
    assertTrue(stages.next() instanceof Stage.Limit);
}