public static void main(String[] args) throws IOException {

    Environment.initialize();

    Stream<Long> stream = Streams.period(1);

    stream.consume(n -> logger.debug("\t A[{}]", n));

    // 2nd subscriber starts 5 seconds later to emphasize independent streams
    sleep(5);

    stream.consume(n -> logger.debug("\t\t\t B[{}]", n));

    System.in.read();
  }
  private static Mono<Void> deleteRoutes(
      final CloudFoundryClient cloudFoundryClient, Optional<List<Route>> routes) {
    return routes
        .map(
            new Function<List<Route>, Stream<Route>>() {

              @Override
              public Stream<Route> apply(List<Route> routes) {
                return Stream.fromIterable(routes);
              }
            })
        .orElse(Stream.<Route>empty())
        .map(
            new Function<Route, String>() {

              @Override
              public String apply(Route route) {
                return route.getId();
              }
            })
        .flatMap(
            new Function<String, Mono<Void>>() {

              @Override
              public Mono<Void> apply(String routeId) {
                return requestDeleteRoute(cloudFoundryClient, routeId);
              }
            })
        .after();
  }
 private Mono<String> waitForStarting(String applicationId) {
   return this.cloudFoundryClient
       .applicationsV2()
       .instances(ApplicationInstancesRequest.builder().applicationId(applicationId).build())
       .flatMap(response -> Stream.fromIterable(response.values()))
       .as(Stream::from)
       .filter(applicationInstanceInfo -> "RUNNING".equals(applicationInstanceInfo.getState()))
       .repeatWhen(DelayUtils.exponentialBackOff(1, 10, SECONDS, 10))
       .single()
       .map(info -> applicationId);
 }
  private static List<String> toUrls(List<Route> routes) {
    return Stream.fromIterable(routes)
        .map(
            new Function<Route, String>() {

              @Override
              public String apply(Route route) {
                return toUrl(route);
              }
            })
        .toList()
        .get();
  }
  private static List<ApplicationDetail.InstanceDetail> toInstanceDetailList(
      ApplicationInstancesResponse instancesResponse,
      final ApplicationStatisticsResponse statisticsResponse) {
    return Stream.fromIterable(instancesResponse.entrySet())
        .map(
            new Function<
                Map.Entry<String, ApplicationInstanceInfo>, ApplicationDetail.InstanceDetail>() {

              @Override
              public ApplicationDetail.InstanceDetail apply(
                  Map.Entry<String, ApplicationInstanceInfo> entry) {
                return toInstanceDetail(entry, statisticsResponse);
              }
            })
        .toList()
        .get();
  }
 @Test
 public void copy() {
   Mono.when(this.applicationId, this.spaceId)
       .then(
           function(
               (sourceId, spaceId) ->
                   this.cloudFoundryClient
                       .applicationsV2()
                       .create(
                           CreateApplicationRequest.builder()
                               .name("copy-application")
                               .spaceId(spaceId)
                               .build())
                       .map(ResourceUtils::getId)
                       .and(Mono.just(sourceId))))
       .then(
           function(
               (targetId, sourceId) ->
                   this.cloudFoundryClient
                       .applicationsV2()
                       .copy(
                           CopyApplicationRequest.builder()
                               .applicationId(targetId)
                               .sourceApplicationId(sourceId)
                               .build())
                       .map(ResourceUtils::getId)))
       .flatMap(
           applicationCopyId ->
               Stream.from(
                       this.cloudFoundryClient
                           .applicationsV2()
                           .list(ListApplicationsRequest.builder().build()))
                   .flatMap(ResourceUtils::getResources)
                   .filter(
                       r -> {
                         String name = ResourceUtils.getEntity(r).getName();
                         return TEST_APPLICATION_NAME.equals(name)
                             || "copy-application".equals(name);
                       }))
       .subscribe(testSubscriber().assertCount(2));
 }
 /**
  * Return a stream of resources from a response
  *
  * @param response the response
  * @param <R> the resource type
  * @param <U> the response type
  * @return a stream of resources from the response
  */
 public static <R extends Resource<?>, U extends PaginatedResponse<R>> Stream<R> getResources(
     U response) {
   return Stream.fromIterable(response.getResources());
 }
 private static Stream<SpaceApplicationSummary> extractApplications(
     GetSpaceSummaryResponse getSpaceSummaryResponse) {
   return Stream.fromIterable(getSpaceSummaryResponse.getApplications());
 }