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

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

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

Context.getOrDefault介绍

[英]Resolve a value given a key within the Context. If unresolved return the passed default value.
[中]在上下文中解析给定键的值。如果未解析,则返回传递的默认值。

代码示例

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

static final OnNextFailureStrategy onNextErrorStrategy(Context context) {
  OnNextFailureStrategy strategy = null;
  BiFunction<? super Throwable, Object, ? extends Throwable> fn = context.getOrDefault(
      OnNextFailureStrategy.KEY_ON_NEXT_ERROR_STRATEGY, null);
  if (fn instanceof OnNextFailureStrategy) {
    strategy = (OnNextFailureStrategy) fn;
  } else if (fn != null) {
    strategy = new OnNextFailureStrategy.LambdaOnNextErrorStrategy(fn);
  }
  if (strategy == null) strategy = Hooks.onNextErrorHook;
  if (strategy == null) strategy = OnNextFailureStrategy.STOP;
  return strategy;
}

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

@Override
public StepVerifier.ContextExpectations<T> containsAllOf(Map<?, ?> other) {
  this.contextExpectations = this.contextExpectations.andThen(c -> {
    boolean all = other.entrySet()
              .stream()
              .allMatch(e -> e.getValue().equals(c.getOrDefault(e.getKey(), null)));
    if (!all) {
      throw errorFormatter.assertionError(String.format("Expected Context %s to contain all of %s", c, other));
    }
  });
  return this;
}

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

@Override
public StepVerifier.ContextExpectations<T> containsAllOf(Context other) {
  this.contextExpectations = this.contextExpectations.andThen(c -> {
    boolean all = other.stream().allMatch(e -> e.getValue().equals(c.getOrDefault(e.getKey(), null)));
    if (!all) {
      throw errorFormatter.assertionError(String.format("Expected Context %s to contain all of %s", c, other));
    }
  });
  return this;
}

代码示例来源:origin: spring-cloud/spring-cloud-sleuth

protected void handle(HttpClientResponse httpClientResponse,
    Throwable throwable) {
  AtomicReference reference = httpClientResponse.currentContext()
      .getOrDefault(AtomicReference.class, null);
  if (reference == null || reference.get() == null) {
    return;
  }
  this.handler.handleReceive(httpClientResponse, throwable,
      (Span) reference.get());
}

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

@Override
public StepVerifier.ContextExpectations<T> contains(Object key, Object value) {
  this.contextExpectations = this.contextExpectations.andThen(c -> {
    Object realValue = c.getOrDefault(key, null);
    if (realValue == null)
      throw errorFormatter.assertionError(
          String.format("Expected value %s for key %s, key not present in Context %s", value, key, c));
    if (!value.equals(realValue))
      throw errorFormatter.assertionError(
          String.format("Expected value %s for key %s, got %s in Context %s", value, key, realValue, c));
  });
  return this;
}

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

@Override
public StepVerifier.ContextExpectations<T> containsOnly(Map<?, ?> other) {
  this.contextExpectations = this.contextExpectations.andThen(c -> {
    if (c.stream().count() != other.size()) {
      throw errorFormatter.assertionError(
          String.format("Expected Context %s to contain same values as %s, but they differ in size", c, other));
    }
    boolean all = other.entrySet()
              .stream()
              .allMatch(e -> e.getValue().equals(c.getOrDefault(e.getKey(), null)));
    if (!all) {
      throw errorFormatter.assertionError(
          String.format("Expected Context %s to contain same values as %s, but they differ in content", c, other));
    }
  });
  return this;
}

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

@Override
@Nullable
public Object getOrDefault(Object key, @Nullable Object defaultValue) {
  return Context.super.getOrDefault(key, defaultValue);
}

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

/**
 * An unexpected exception is about to be dropped.
 * <p>
 * If no hook is registered for {@link Hooks#onErrorDropped(Consumer)}, the dropped
 * error is logged at ERROR level and thrown (via {@link Exceptions#bubble(Throwable)}.
 *
 * @param e the dropped exception
 * @param context a context that might hold a local error consumer
 */
public static void onErrorDropped(Throwable e, Context context) {
  Consumer<? super Throwable> hook = context.getOrDefault(Hooks.KEY_ON_ERROR_DROPPED,null);
  if (hook == null) {
    hook = Hooks.onErrorDroppedHook;
  }
  if (hook == null) {
    log.error("Operator called default onErrorDropped", e);
    throw Exceptions.bubble(e);
  }
  hook.accept(e);
}

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

@Override
public StepVerifier.ContextExpectations<T> containsOnly(Context other) {
  this.contextExpectations = this.contextExpectations.andThen(c -> {
    if (c.stream().count() != other.stream().count()) {
      throw errorFormatter.assertionError(
          String.format("Expected Context %s to contain same values as %s, but they differ in size", c, other));
    }
    boolean all = other.stream()
              .allMatch(e -> e.getValue().equals(c.getOrDefault(e.getKey(), null)));
    if (!all) {
      throw errorFormatter.assertionError(
          String.format("Expected Context %s to contain same values as %s, but they differ in content", c, other));
    }
  });
  return this;
}

代码示例来源:origin: spring-cloud/spring-cloud-sleuth

@Override
public void accept(HttpClientRequest req, Connection connection) {
  if (this.propagation.keys().stream()
      .anyMatch(key -> req.requestHeaders().contains(key))) {
    // request already instrumented
    return;
  }
  AtomicReference reference = req.currentContext()
      .getOrDefault(AtomicReference.class, new AtomicReference());
  Span span = this.handler.handleSend(this.injector, req.requestHeaders(), req,
      (Span) reference.get());
  reference.set(span);
}

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

@Test
public void getUnknownWithDefault() throws Exception {
  assertThat(c.getOrDefault("peeka", "boo")).isEqualTo("boo");
}

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

@Test
public void getUnknownWithDefault() throws Exception {
  assertThat(c.getOrDefault("peeka", "boo")).isEqualTo("boo");
}

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

@Test
public void discardAdapterIsAdditive() {
  List<String> discardOrder = Collections.synchronizedList(new ArrayList<>(2));
  Function<Context, Context> first = Operators.discardLocalAdapter(Number.class, i -> discardOrder.add("FIRST"));
  Function<Context, Context> second = Operators.discardLocalAdapter(Integer.class, i -> discardOrder.add("SECOND"));
  Context ctx = first.apply(second.apply(Context.empty()));
  Consumer<Object> test = ctx.getOrDefault(Hooks.KEY_ON_DISCARD, o -> {});
  assertThat(test).isNotNull();
  test.accept(1);
  assertThat(discardOrder).as("consumers were combined").containsExactly("FIRST", "SECOND");
}

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

@Test
public void getUnknownWithDefaultNull() throws Exception {
  Object def = null;
  assertThat(c.getOrDefault("peeka", def)).isNull();
}

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

@Test
public void getUnknownWithDefaultNull() throws Exception {
  Object def = null;
  assertThat(c.getOrDefault("peeka", def)).isNull();
}

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

@Test
  public void nextError() {
    List<Tuple2<Signal, Context>> signalsAndContext = new ArrayList<>();
    Mono.just(0)
      .map(i -> 10 / i)
      .doOnEach(s -> signalsAndContext.add(Tuples.of(s,s.getContext())))
      .subscriberContext(Context.of("foo", "bar"))
      .subscribe();

    assertThat(signalsAndContext)
        .hasSize(1)
        .allSatisfy(t2 -> {
          assertThat(t2.getT1())
              .isNotNull();
          assertThat(t2.getT2().getOrDefault("foo", "baz"))
              .isEqualTo("bar");
        });

    assertThat(signalsAndContext.stream().map(t2 -> t2.getT1().getType()))
        .containsExactly(SignalType.ON_ERROR);
  }
}

代码示例来源: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 contextSimple3() {
  String key = "message";
  Mono<String> r = Mono.subscriberContext() // <1>
             .map( ctx -> ctx.put(key, "Hello")) // <2>
             .flatMap( ctx -> Mono.subscriberContext()) // <3>
             .map( ctx -> ctx.getOrDefault(key,"Default")); // <4>
  StepVerifier.create(r)
        .expectNext("Default") // <5>
        .verifyComplete();
}

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

@Test
public void nextComplete() {
  List<Tuple2<Signal, Context>> signalsAndContext = new ArrayList<>();
  Mono.just(1)
    .hide()
    .doOnEach(s -> signalsAndContext.add(Tuples.of(s, s.getContext())))
    .subscriberContext(Context.of("foo", "bar"))
    .subscribe();
  assertThat(signalsAndContext)
      .hasSize(2)
      .allSatisfy(t2 -> {
        assertThat(t2.getT1())
            .isNotNull();
        assertThat(t2.getT2().getOrDefault("foo", "baz"))
            .isEqualTo("bar");
      });
  assertThat(signalsAndContext.stream().map(t2 -> t2.getT1().getType()))
      .containsExactly(SignalType.ON_NEXT, SignalType.ON_COMPLETE);
}

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

@Test
public void withInitialContext() {
  StepVerifier.create(Mono.subscriberContext(),
      StepVerifierOptions.create().withInitialContext(Context.of("foo", "bar")))
        .assertNext(c -> Assertions.assertThat(c.getOrDefault("foo", "baz"))
                      .isEqualTo("bar"))
        .verifyComplete();
}

相关文章