reactor.core.publisher.Flux.singleOrEmpty()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(7.9k)|赞(0)|评价(0)|浏览(383)

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

Flux.singleOrEmpty介绍

[英]Expect and emit a single item from this Flux source, and accept an empty source but signal an IndexOutOfBoundsException for a source with more than one element.
[中]期望并从此通量源发出单个项,并接受空源,但对包含多个元素的源发出IndexOutOfBoundsException信号。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
public Mono<Object> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType,
    @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
  return decode(inputStream, elementType, mimeType, hints).singleOrEmpty();
}

代码示例来源:origin: org.springframework/spring-web

@Override
public Mono<Object> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType,
    @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
  return decode(inputStream, elementType, mimeType, hints).singleOrEmpty();
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public Mono<Object> decodeToMono(Publisher<DataBuffer> input, ResolvableType elementType,
    @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
  Flux<TokenBuffer> tokens = Jackson2Tokenizer.tokenize(Flux.from(input), this.jsonFactory, false);
  return decodeInternal(tokens, elementType, mimeType, hints).singleOrEmpty();
}

代码示例来源:origin: spring-projects/spring-data-redis

/**
 * Get random element from set at {@literal key}.
 *
 * @param key must not be {@literal null}.
 * @return
 * @see <a href="http://redis.io/commands/srandmember">Redis Documentation: SRANDMEMBER</a>
 */
default Mono<ByteBuffer> sRandMember(ByteBuffer key) {
  return sRandMember(key, 1L).singleOrEmpty();
}

代码示例来源:origin: spring-projects/spring-data-redis

/**
 * Check if given {@code scriptSha} exist in script cache.
 *
 * @param scriptSha The sha1 of the script is present in script cache. Must not be {@literal null}.
 * @return a {@link Mono} indicating if script is present.
 */
default Mono<Boolean> scriptExists(String scriptSha) {
  Assert.notNull(scriptSha, "ScriptSha must not be null!");
  return scriptExists(Collections.singletonList(scriptSha)).singleOrEmpty();
}

代码示例来源:origin: org.springframework/spring-web

@Override
public Mono<Object> decodeToMono(Publisher<DataBuffer> input, ResolvableType elementType,
    @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
  Flux<TokenBuffer> tokens = Jackson2Tokenizer.tokenize(Flux.from(input), this.jsonFactory, false);
  return decodeInternal(tokens, elementType, mimeType, hints).singleOrEmpty();
}

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

@GetMapping("/routes/{id}")
public Mono<ResponseEntity<RouteDefinition>> route(@PathVariable String id) {
  //TODO: missing RouteLocator
  return this.routeDefinitionLocator.getRouteDefinitions()
      .filter(route -> route.getId().equals(id))
      .singleOrEmpty()
      .map(ResponseEntity::ok)
      .switchIfEmpty(Mono.just(ResponseEntity.notFound().build()));
}

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

@Override
public StepVerifier.Step<T> then() {
  return step.consumeSubscriptionWith(s -> {
    //the current subscription
    Scannable lowest = Scannable.from(s);
    //attempt to go back to the leftmost parent to check the Context from its perspective
    Context c = Flux.<Scannable>
        fromStream(lowest.parents())
        .ofType(CoreSubscriber.class)
        .takeLast(1)
        .singleOrEmpty()
        //no parent? must be a ScalaSubscription or similar source
        .switchIfEmpty(
            Mono.just(lowest)
              //unless it is directly a CoreSubscriber, let's try to scan the leftmost, see if it has an ACTUAL
              .map(sc -> (sc instanceof CoreSubscriber) ?
                  sc :
                  sc.scanOrDefault(Scannable.Attr.ACTUAL, Scannable.from(null)))
              //we are ultimately only interested in CoreSubscribers' Context
              .ofType(CoreSubscriber.class)
        )
        .map(CoreSubscriber::currentContext)
        //if it wasn't a CoreSubscriber (eg. custom or vanilla Subscriber) there won't be a Context
        .block();
    this.contextExpectations.accept(c);
  });
}

代码示例来源:origin: spring-projects/spring-security

private <T extends WebFilter> Optional<T> getWebFilter(SecurityWebFilterChain filterChain, Class<T> filterClass) {
  return (Optional<T>) filterChain.getWebFilters()
      .filter(Objects::nonNull)
      .filter(filter -> filter.getClass().isAssignableFrom(filterClass))
      .singleOrEmpty()
      .blockOptional();
}

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

@Test
public void singleEmptyFallbackJustHide() {
  StepVerifier.create(Flux.empty()
              .hide()
              .singleOrEmpty())
        .verifyComplete();
}

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

@Test
public void singleEmptyFallbackJustHideError() {
  StepVerifier.create(Flux.just(1, 2, 3)
              .hide()
              .singleOrEmpty())
        .verifyError(IndexOutOfBoundsException.class);
}

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

@Test
public void singleEmptyFallbackCallable() {
  StepVerifier.create(Mono.fromCallable(() -> 1)
              .flux()
              .singleOrEmpty())
        .expectNext(1)
        .verifyComplete();
}

代码示例来源:origin: com.aol.cyclops/cyclops-reactor

/**
 * @return
 * @see reactor.core.publisher.Flux#singleOrEmpty()
 */
public final Mono<T> singleOrEmpty() {
  return boxed.singleOrEmpty();
}
/**

代码示例来源:origin: org.springframework.data/spring-data-redis

/**
 * Check if given {@code scriptSha} exist in script cache.
 *
 * @param scriptSha The sha1 of the script is present in script cache. Must not be {@literal null}.
 * @return a {@link Mono} indicating if script is present.
 */
default Mono<Boolean> scriptExists(String scriptSha) {
  Assert.notNull(scriptSha, "ScriptSha must not be null!");
  return scriptExists(Collections.singletonList(scriptSha)).singleOrEmpty();
}

代码示例来源:origin: apache/servicemix-bundles

/**
 * Get random element from set at {@literal key}.
 *
 * @param key must not be {@literal null}.
 * @return
 * @see <a href="http://redis.io/commands/srandmember">Redis Documentation: SRANDMEMBER</a>
 */
default Mono<ByteBuffer> sRandMember(ByteBuffer key) {
  return sRandMember(key, 1L).singleOrEmpty();
}

代码示例来源:origin: apache/servicemix-bundles

@Override
public Mono<Object> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType,
    @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
  return decode(inputStream, elementType, mimeType, hints).singleOrEmpty();
}

代码示例来源:origin: apache/servicemix-bundles

@Override
public Mono<Object> decodeToMono(Publisher<DataBuffer> input, ResolvableType elementType,
    @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
  Flux<TokenBuffer> tokens = Jackson2Tokenizer.tokenize(Flux.from(input), this.jsonFactory, false);
  return decodeInternal(tokens, elementType, mimeType, hints).singleOrEmpty();
}

代码示例来源:origin: org.cloudfoundry/cloudfoundry-operations

private static Mono<String> getOrganizationId(CloudFoundryClient cloudFoundryClient, String organizationName) {
  return requestListOrganizations(cloudFoundryClient, organizationName)
    .singleOrEmpty()
    .map(ResourceUtils::getId)
    .switchIfEmpty(ExceptionUtils.illegalArgument("Organization %s not found", organizationName));
}

代码示例来源:origin: org.cloudfoundry/cloudfoundry-operations

private static Mono<String> getOrganizationId(CloudFoundryClient cloudFoundryClient, String organizationName) {
  return requestListOrganizations(cloudFoundryClient, organizationName)
    .singleOrEmpty()
    .map(ResourceUtils::getId)
    .switchIfEmpty(ExceptionUtils.illegalArgument("Organization %s not found", organizationName));
}

代码示例来源:origin: org.cloudfoundry/cloudfoundry-operations

private static Mono<String> getApplicationId(CloudFoundryClient cloudFoundryClient, ApplicationManifest manifest, String spaceId, String stackId) {
  return requestApplications(cloudFoundryClient, manifest.getName(), spaceId)
    .singleOrEmpty()
    .flatMap(application -> {
      Map<String, Object> environmentJsons = new HashMap<>(ResourceUtils.getEntity(application).getEnvironmentJsons());
      Optional.ofNullable(manifest.getEnvironmentVariables()).ifPresent(environmentJsons::putAll);
      return requestUpdateApplication(cloudFoundryClient, ResourceUtils.getId(application), environmentJsons, manifest, stackId)
        .map(ResourceUtils::getId);
    })
    .switchIfEmpty(requestCreateApplication(cloudFoundryClient, manifest, spaceId, stackId)
      .map(ResourceUtils::getId));
}

相关文章

微信公众号

最新文章

更多

Flux类方法