class RxFiles {

  @Nonnull
  public Observable<File> getFilesInDirectory(@Nonnull final File directory) {
    checkNotNull(directory, "\'directory\' must not be null.");
    
    final Observable<File> listFiles = Observable
        .fromCallable(() -> Optional.ofNullable(directory.listFiles()))
        .flatMap(optionalFiles -> optionalFiles.isPresent() ?
            Observable.fromIterable(Arrays.asList(optionalFiles.get())) :
            Observable.error(makeError("Failed to get files in", directory, "Reason unknown")));
    
    return exists(directory).flatMapObservable(exists -> exists ?
        isFile(directory)
            .flatMapObservable(isFile -> isFile ?
                Observable.error(makeError(
                    "Failed to get files in",
                    directory,
                    "Expected a directory but found a file")) :
                listFiles) :
        Observable.error(makeError("Failed to get files in", directory, "The directory does not exist")));
  }

}