Skip to content

Commit

Permalink
[java] Refactoring to add more Java 8 flavor
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Nov 10, 2020
1 parent 8bde622 commit 2e83325
Showing 1 changed file with 12 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.util.function.Function;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;

public class NodeOptions {
Expand Down Expand Up @@ -93,28 +94,20 @@ public Map<Capabilities, Collection<SessionFactory>> getSessionFactories(
}

private void addDriverFactoriesFromConfig(ImmutableMultimap.Builder<Capabilities, SessionFactory> sessionFactories) {
Optional<List<String>> additionalDriverFactories = config.getAll("node", "driver-factories");
if (!additionalDriverFactories.isPresent()) {
return;
}

List<String> allConfigs = additionalDriverFactories.get();
if (allConfigs.size() % 2 != 0) {
throw new ConfigException("Expected each driver class to be mapped to a config");
}

for (int i = 0; i < allConfigs.size(); i++) {
String clazz = allConfigs.get(i);
i++;
if (i == allConfigs.size()) {
throw new ConfigException("Unable to find JSON config");
config.getAll("node", "driver-factories").ifPresent(allConfigs -> {
if (allConfigs.size() % 2 != 0) {
throw new ConfigException("Expected each driver class to be mapped to a config");
}
Capabilities stereotype = JSON.toType(allConfigs.get(i), Capabilities.class);

SessionFactory sessionFactory = createSessionFactory(clazz, stereotype);
Map<String, String> configMap = IntStream.range(0, allConfigs.size()/2).boxed()
.collect(Collectors.toMap(i -> allConfigs.get(2*i), i -> allConfigs.get(2*i + 1)));

sessionFactories.put(stereotype, sessionFactory);
}
configMap.forEach((clazz, config) -> {
Capabilities stereotype = JSON.toType(config, Capabilities.class);
SessionFactory sessionFactory = createSessionFactory(clazz, stereotype);
sessionFactories.put(stereotype, sessionFactory);
});
});
}

private SessionFactory createSessionFactory(String clazz, Capabilities stereotype) {
Expand Down

0 comments on commit 2e83325

Please sign in to comment.