0

Can someone help me to understand this portion of code? I'm trying to get some config files from database using a dataRepository class that returns an Observable of the config files in a special form ( it was developed by another developer)

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));
}

Thank you in advance!

  • The code is trying to convert list of ```ConfigFile``` to list of ```LegalBookDescriptor``` and leave out invalid config file. – zhh Jul 23 '18 at 15:27
  • Thank you @zhh, But the thing i can't understand is, how this code turns like an iterative instruction like for or while? How the return list in findAllConfigFiles are passing element by element into parameter of call method then be aggregated into a list of LegalBookDescriptor in return? Thanks and best regards, – Mohammed ESSABRI Jul 23 '18 at 15:40
  • I guess you don't understand ```flatMap``` right? https://stackoverflow.com/questions/26684562/whats-the-difference-between-map-and-flatmap-methods-in-java-8 – zhh Jul 23 '18 at 15:43
  • The both flatMap and Observable, As i red in a documentation, flatMap is converting array of arrays into a single array, but where is array of arrays here? Note that dataRepository.findAllConfigFiles(legalBookDescriptorsDir) returns Observable and not a List or an Array. Thanks and best regards, – Mohammed ESSABRI Jul 23 '18 at 15:47
  • You may need to read rxjava or java8 stream documentation. But for simplicity you can think Observable as list or array. Actually it is a stream of data, in your case it is used just as a list of data. – zhh Jul 23 '18 at 15:52
  • Thank you @zhh, so we can consider Observable result as a list, but how arguments are passed to call as ConfigFile object? – Mohammed ESSABRI Jul 23 '18 at 16:06
  • The argument of ```flatMap``` is a function (interface), let's call it ```fun```. Then we take one element (one config file) ```file``` in the observable (or list), and invoke ```fun.call(file)```, put the result back to the observable, then take next element. http://www.oracle.com/technetwork/articles/java/ma14-java-se-8-streams-2177646.html – zhh Jul 23 '18 at 16:13
  • Thank you @zhh for this clarification, i understand more now, thank you so much! Best regards, – Mohammed ESSABRI Jul 23 '18 at 16:50

0 Answers0