reactor.util.context.Context.put()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(8.1k)|赞(0)|评价(0)|浏览(325)

本文整理了Java中reactor.util.context.Context.put()方法的一些代码示例,展示了Context.put()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Context.put()方法的具体详情如下:
包路径:reactor.util.context.Context
类名称:Context
方法名:put

Context.put介绍

[英]Create a new Context that contains all current key/value pairs plus the given key/value pair. If that key existed in the current Context, its associated value is replaced in the resulting Context.
[中]创建包含所有当前键/值对和给定键/值对的新上下文。如果该键存在于当前上下文中,则其关联值将在结果上下文中替换。

代码示例

代码示例来源:origin: reactor/reactor-core

@Test
public void putDifferentKeyContextN() throws Exception {
  Context put = c.put(7, "Abis");
  assertThat(put)
      .isInstanceOf(ContextN.class);
  assertThat(put.stream().map(Map.Entry::getKey))
      .containsExactly(1, 2, 3, 4, 5, 6, 7);
  assertThat(put.stream().map(Map.Entry::getValue))
      .containsExactly("A", "B", "C", "D", "E", "F", "Abis");
}

代码示例来源:origin: reactor/reactor-core

@Test
public void contextSize() { //renamed to allow import of size() Condition from ContextTest
  assertThat(c.size()).isEqualTo(6);
  assertThat(c.put("sizeGrows", "yes").size()).isEqualTo(7);
}

代码示例来源:origin: reactor/reactor-core

@Test
public void putAnyKeyContext1() throws Exception {
  Context put = c.put(1, "Abis");
  assertThat(put)
      .isInstanceOf(Context1.class);
  assertThat(put.stream().map(Map.Entry::getKey))
      .containsExactly(1);
  assertThat(put.stream().map(Map.Entry::getValue))
      .containsExactly("Abis");
}

代码示例来源:origin: reactor/reactor-core

@Test
public void replaceKey1NewContext() throws Exception {
  Context put = c.put(1, "foo");
  assertThat(put)
      .isInstanceOf(ContextN.class)
      .isNotSameAs(c);
  assertThat(put.stream().map(Map.Entry::getKey))
      .containsExactly(1, 2, 3, 4, 5, 6);
  assertThat(put.stream().map(Map.Entry::getValue))
      .containsExactly("foo", "B", "C", "D", "E", "F");
}

代码示例来源:origin: reactor/reactor-core

@Test
public void replaceKey5NewContext() {
  Context put = c.put(5, "foo");
  assertThat(put)
      .isInstanceOf(ContextN.class)
      .isNotSameAs(c);
  assertThat(put.stream().map(Map.Entry::getKey))
      .containsExactly(1, 2, 3, 4, 5, 6);
  assertThat(put.stream().map(Map.Entry::getValue))
      .containsExactly("A", "B", "C", "D", "foo", "F");
}

代码示例来源:origin: reactor/reactor-core

@Test
public void replaceKey4NewContext() {
  Context put = c.put(4, "foo");
  assertThat(put)
      .isInstanceOf(ContextN.class)
      .isNotSameAs(c);
  assertThat(put.stream().map(Map.Entry::getKey))
      .containsExactly(1, 2, 3, 4, 5, 6);
  assertThat(put.stream().map(Map.Entry::getValue))
      .containsExactly("A", "B", "C", "foo", "E", "F");
}

代码示例来源:origin: reactor/reactor-core

@Test
public void replaceKey3NewContext() {
  Context put = c.put(3, "foo");
  assertThat(put)
      .isInstanceOf(ContextN.class)
      .isNotSameAs(c);
  assertThat(put.stream().map(Map.Entry::getKey))
      .containsExactly(1, 2, 3, 4, 5, 6);
  assertThat(put.stream().map(Map.Entry::getValue))
      .containsExactly("A", "B", "foo", "D", "E", "F");
}

代码示例来源:origin: reactor/reactor-core

@Test
public void replaceKey6NewContext() {
  Context put = c.put(6, "foo");
  assertThat(put)
      .isInstanceOf(ContextN.class)
      .isNotSameAs(c);
  assertThat(put.stream().map(Map.Entry::getKey))
      .containsExactly(1, 2, 3, 4, 5, 6);
  assertThat(put.stream().map(Map.Entry::getValue))
      .containsExactly("A", "B", "C", "D", "E", "foo");
}

代码示例来源:origin: reactor/reactor-core

@Test
public void contextSimple4() {
  String key = "message";
  Mono<String> r = Mono.just("Hello")
          .flatMap( s -> Mono.subscriberContext()
                    .map( ctx -> s + " " + ctx.get(key)))
          .subscriberContext(ctx -> ctx.put(key, "Reactor")) //<1>
          .subscriberContext(ctx -> ctx.put(key, "World")); //<2>
  StepVerifier.create(r)
        .expectNext("Hello Reactor") //<3>
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void currentContext() throws InterruptedException {
  StepVerifier.create(Mono.just("foo")
              .flatMap(d -> Mono.subscriberContext()
                       .map(c -> d + c.get(Integer.class)))
              .subscriberContext(ctx ->
                  ctx.put(Integer.class, ctx.get(Integer.class) + 1))
              .flatMapMany(Mono::just)
              .subscriberContext(ctx -> ctx.put(Integer.class, 0)))
        .expectNext("foo1")
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void contextTest() {
  UnicastProcessor<Integer> p = UnicastProcessor.create();
  p.subscriberContext(ctx -> ctx.put("foo", "bar")).subscribe();
  assertThat(p.sink().currentContext().get("foo").toString()).isEqualTo("bar");
}

代码示例来源:origin: reactor/reactor-core

@Test
public void contextSimple1() {
  String key = "message";
  Mono<String> r = Mono.just("Hello")
             .flatMap( s -> Mono.subscriberContext() //<2>
                      .map( ctx -> s + " " + ctx.get(key))) //<3>
             .subscriberContext(ctx -> ctx.put(key, "World")); //<1>
  StepVerifier.create(r)
        .expectNext("Hello World") //<4>
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void contextSimple2() {
  String key = "message";
  Mono<String> r = Mono.just("Hello")
             .subscriberContext(ctx -> ctx.put(key, "World")) //<1>
             .flatMap( s -> Mono.subscriberContext()
                      .map( ctx -> s + " " + ctx.getOrDefault(key, "Stranger")));  //<2>
  StepVerifier.create(r)
        .expectNext("Hello Stranger") //<3>
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void contextTest() {
  StepVerifier.create(Flux.create(s -> IntStream.range(0, 10).forEach(i -> s.next(s
      .currentContext()
                              .get(AtomicInteger.class)
                              .incrementAndGet())))
              .take(10)
              .subscriberContext(ctx -> ctx.put(AtomicInteger.class,
                  new AtomicInteger())))
        .expectNext(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void contextTestPush() {
  StepVerifier.create(Flux.push(s -> IntStream.range(0, 10).forEach(i -> s.next(s
      .currentContext()
                              .get(AtomicInteger.class)
                              .incrementAndGet())))
              .take(10)
              .subscriberContext(ctx -> ctx.put(AtomicInteger.class,
                  new AtomicInteger())))
        .expectNext(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void contextTest() {
  StepVerifier.create(Mono.create(s -> s.success(s.currentContext()
                          .get(AtomicInteger.class)
                          .incrementAndGet()))
              .subscriberContext(ctx -> ctx.put(AtomicInteger.class,
                  new AtomicInteger())))
        .expectNext(1)
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void contextTest() {
  StepVerifier.create(Flux.generate(s -> s.next(s.currentContext()
                          .get(AtomicInteger.class)
                          .incrementAndGet()))
              .take(10)
              .subscriberContext(ctx -> ctx.put(AtomicInteger.class,
                  new AtomicInteger())))
        .expectNext(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void contextTest() {
  StepVerifier.create(Flux.just("foo")
              .handle((d, s) -> s.next(s.currentContext()
                          .get(AtomicInteger.class)
                          .incrementAndGet()))
              .repeat(9)
              .subscriberContext(ctx -> ctx.put(AtomicInteger.class,
                  new AtomicInteger())))
        .expectNext(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void contextGetMono() throws InterruptedException {
  StepVerifier.create(Mono.just(1)
              .log()
              .handle((d, c) -> c.next(c.currentContext().get("test") + "" + d))
              .handle((d, c) -> c.next(c.currentContext().get("test2") + "" + d))
              .subscriberContext(ctx -> ctx.put("test2", "bar"))
              .subscriberContext(ctx -> ctx.put("test", "foo"))
              .log())
        .expectNext("barfoo1")
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void contextGetHideMono() throws InterruptedException {
  StepVerifier.create(Mono.just(1)
              .hide()
              .log()
              .handle((d, c) -> c.next(c.currentContext().get("test") + "" + d))
              .handle((d, c) -> c.next(c.currentContext().get("test2") + "" + d))
              .subscriberContext(ctx -> ctx.put("test", "foo"))
              .subscriberContext(ctx -> ctx.put("test2", "bar"))
              .log())
        .expectNext("barfoo1")
        .verifyComplete();
}

相关文章