class RxFiles {

  @Nonnull
  public Completable createNewFile(@Nonnull final File file) {
    checkNotNull(file, "\'file\' must not be null.");
    
    final Completable createFile = Single
        .fromCallable(file::createNewFile)
        .flatMapCompletable(createdSuccessfully -> createdSuccessfully ?
            Completable.complete() :
            Completable.error(makeError("Failed to create file", file, "Reason unknown")));
    
    return exists(file)
        .flatMapCompletable(exists -> exists ?
            Completable.error(makeError(
                "Failed to create file",
                file,
                "A file or directory already exists at the location")) :
            exists(file.getParentFile())
                .flatMapCompletable(parentExists -> parentExists ?
                    createFile :
                    Completable.error(makeError("Failed to create file", file, "The parent directory does not exist"))));
  }

}