All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.datastax.oss.driver.internal.core.config.map.MapBasedDriverExecutionProfile Maven / Gradle / Ivy

/*
 * Copyright DataStax, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.datastax.oss.driver.internal.core.config.map;

import com.datastax.oss.driver.api.core.config.DriverExecutionProfile;
import com.datastax.oss.driver.api.core.config.DriverOption;
import com.datastax.oss.driver.shaded.guava.common.base.Preconditions;
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList;
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSortedSet;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.time.Duration;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;

/** @see MapBasedDriverConfigLoader */
public class MapBasedDriverExecutionProfile implements DriverExecutionProfile {

  private final String profileName;
  // The backing map for the current profile
  private final Map profile;
  // The backing map for the default profile (if the current one is not the default)
  private final Map defaultProfile;

  public MapBasedDriverExecutionProfile(
      Map> optionsMap, String profileName) {
    this(
        profileName,
        optionsMap.get(profileName),
        profileName.equals(DriverExecutionProfile.DEFAULT_NAME)
            ? Collections.emptyMap()
            : optionsMap.get(DriverExecutionProfile.DEFAULT_NAME));
    Preconditions.checkArgument(
        optionsMap.containsKey(profileName),
        "Unknown profile '%s'. Check your configuration.",
        profileName);
  }

  public MapBasedDriverExecutionProfile(
      String profileName,
      Map profile,
      Map defaultProfile) {
    this.profileName = profileName;
    this.profile = profile;
    this.defaultProfile = defaultProfile;
  }

  @NonNull
  @Override
  public String getName() {
    return profileName;
  }

  @Override
  public boolean isDefined(@NonNull DriverOption option) {
    return profile.containsKey(option) || defaultProfile.containsKey(option);
  }

  // Driver options don't encode the type, everything relies on the user putting the right types in
  // the backing map, so no point in trying to type-check.
  @SuppressWarnings({"unchecked", "TypeParameterUnusedInFormals"})
  @NonNull
  private  T get(@NonNull DriverOption option) {
    Object value = profile.getOrDefault(option, defaultProfile.get(option));
    if (value == null) {
      throw new IllegalArgumentException("Missing configuration option " + option.getPath());
    }
    return (T) value;
  }

  @Override
  public boolean getBoolean(@NonNull DriverOption option) {
    return get(option);
  }

  @NonNull
  @Override
  public List getBooleanList(@NonNull DriverOption option) {
    return get(option);
  }

  @Override
  public int getInt(@NonNull DriverOption option) {
    return get(option);
  }

  @NonNull
  @Override
  public List getIntList(@NonNull DriverOption option) {
    return get(option);
  }

  @Override
  public long getLong(@NonNull DriverOption option) {
    return get(option);
  }

  @NonNull
  @Override
  public List getLongList(@NonNull DriverOption option) {
    return get(option);
  }

  @Override
  public double getDouble(@NonNull DriverOption option) {
    return get(option);
  }

  @NonNull
  @Override
  public List getDoubleList(@NonNull DriverOption option) {
    return get(option);
  }

  @NonNull
  @Override
  public String getString(@NonNull DriverOption option) {
    return get(option);
  }

  @NonNull
  @Override
  public List getStringList(@NonNull DriverOption option) {
    return get(option);
  }

  @NonNull
  @Override
  public Map getStringMap(@NonNull DriverOption option) {
    return get(option);
  }

  @Override
  public long getBytes(@NonNull DriverOption option) {
    return get(option);
  }

  @NonNull
  @Override
  public List getBytesList(DriverOption option) {
    return get(option);
  }

  @NonNull
  @Override
  public Duration getDuration(@NonNull DriverOption option) {
    return get(option);
  }

  @NonNull
  @Override
  public List getDurationList(@NonNull DriverOption option) {
    return get(option);
  }

  @NonNull
  @Override
  public SortedSet> entrySet() {
    ImmutableSortedSet.Builder> builder =
        ImmutableSortedSet.orderedBy(Map.Entry.comparingByKey());
    for (Map backingMap :
        // builder.add() ignores duplicates, so process higher precedence backing maps first
        ImmutableList.of(profile, defaultProfile)) {
      for (Map.Entry entry : backingMap.entrySet()) {
        builder.add(new AbstractMap.SimpleEntry<>(entry.getKey().getPath(), entry.getValue()));
      }
    }
    return builder.build();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy