在JAVA中将FlatMap与Observable一起使用

时间:2018-07-23 15:19:56

标签: java spring observable flatmap functional-interface

有人可以帮助我理解这部分代码吗? 我正在尝试使用dataRepository类从数据库中获取一些配置文件,该类以特殊形式(由另一位开发人员开发)返回配置文件的Observable

final List<LegalBookDescriptor> legalBookDescriptors = dataRepository.findAllConfigFiles(legalBookDescriptorsDir)
        .flatMap(new Func1<ConfigFile, Observable<LegalBookDescriptor>>() {
            @Override
            public Observable<LegalBookDescriptor> call(ConfigFile configFile) {
                try {
                    final LegalBookDescriptor legalBookDescriptor = conversionService.convert(configFile.getContent(), LegalBookDescriptor.class);

                    LOG.info(String.format("Successfully loaded [Legal Book Descriptor] from file [%s]", configFile.getPath()));

                    return Observable.just(legalBookDescriptor);
                } catch (Exception e) {
                    LOG.error(String.format("Failed to load [Legal Book Descriptor] from file [%s]", configFile.getPath()), e);

                    return Observable.empty();
                }
            }
        })
        .toList()
        .toBlocking()
        .single();

if (legalBookDescriptors.isEmpty()) {
    LOG.warn(String.format("Hasn't found any valid Legal Book Descriptor file in the root directory [%s].", legalBookDescriptorsDir));
}

提前谢谢!

0 个答案:

没有答案