Short circuit vavr Stream

(, de)

If you are in a Stream, have to deal with Exceptions and still want to see which elements were processed successfully, short circuiting makes sense.

Stream<S3ObjectSummary> files = s3Service.listFiles();

files
  .map(S3ObjectSummary::getKey)
  .map(this::readFile)
  //short circuit stream if we have an error during reading
  .takeUntil(Try::isFailure).flatMap(t -> t)
  .map(this::handleFile)
  //short circuit stream if we have an error during handling
  .takeUntil(Try::isFailure).flatMap(t -> t)
  .map(this::cleanFile)
  //short circuit stream if we have an error during cleaning
  .takeUntil(Try::isFailure).flatMap(t -> t);
}