package com.datastax.oss.driver.internal.core.config.map;
import com.datastax.oss.driver.api.core.config.DriverConfig;
import com.datastax.oss.driver.api.core.config.DriverExecutionProfile;
import com.datastax.oss.driver.api.core.config.DriverOption;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class MapBasedDriverConfig implements DriverConfig {
private final Map<String, Map<DriverOption, Object>> optionsMap;
private final Map<String, MapBasedDriverExecutionProfile> profiles = new ConcurrentHashMap<>();
public MapBasedDriverConfig(Map<String, Map<DriverOption, Object>> optionsMap) {
this.optionsMap = optionsMap;
if (!optionsMap.containsKey(DriverExecutionProfile.DEFAULT_NAME)) {
throw new IllegalArgumentException(
"The options map must contain a profile named " + DriverExecutionProfile.DEFAULT_NAME);
}
createMissingProfiles();
}
@NonNull
@Override
public DriverExecutionProfile getProfile(@NonNull String profileName) {
return profiles.computeIfAbsent(profileName, this::newProfile);
}
@NonNull
@Override
public Map<String, ? extends DriverExecutionProfile> getProfiles() {
createMissingProfiles();
return Collections.unmodifiableMap(profiles);
}
private void createMissingProfiles() {
for (Map.Entry<String, Map<DriverOption, Object>> entry : optionsMap.entrySet()) {
String profileName = entry.getKey();
if (!profiles.containsKey(profileName)) {
profiles.put(profileName, newProfile(profileName));
}
}
}
private MapBasedDriverExecutionProfile newProfile(String profileName) {
return new MapBasedDriverExecutionProfile(optionsMap, profileName);
}
}