com.typesafe.config.Config.hasPath()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(8.7k)|赞(0)|评价(0)|浏览(331)

本文整理了Java中com.typesafe.config.Config.hasPath()方法的一些代码示例,展示了Config.hasPath()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Config.hasPath()方法的具体详情如下:
包路径:com.typesafe.config.Config
类名称:Config
方法名:hasPath

Config.hasPath介绍

[英]Checks whether a value is present and non-null at the given path. This differs in two ways from Map.containsKey() as implemented by ConfigObject: it looks for a path expression, not a key; and it returns false for null values, while containsKey() returns true indicating that the object contains a null value for the key.

If a path exists according to #hasPath(String), then #getValue(String) will never throw an exception. However, the typed getters, such as #getInt(String), will still throw if the value is not convertible to the requested type.

Note that path expressions have a syntax and sometimes require quoting (see ConfigUtil#joinPath and ConfigUtil#splitPath).
[中]检查给定路径上的值是否存在且不为null。这与Map在两个方面有所不同。由ConfigObject实现的containsKey():它查找路径表达式,而不是键;对于null值,它返回false,而containsKey()返回true,指示对象包含键的null值。
如果根据#hasPath(String)存在路径,那么#getValue(String)永远不会抛出异常。但是,如果值不能转换为请求的类型,则类型化getter(如#getInt(String))仍将抛出。
请注意,路径表达式具有语法,有时需要引用(请参见ConfigUtil“joinPath”和ConfigUtil“splitPath”)。

代码示例

代码示例来源:origin: OryxProject/oryx

/**
 * @param config configuration to query for value
 * @param key configuration path key
 * @return value for given key, or {@code null} if none exists
 */
public static String getOptionalString(Config config, String key) {
 return config.hasPath(key) ? config.getString(key) : null;
}

代码示例来源:origin: ben-manes/caffeine

/** Adds the entry listeners settings. */
private void addListeners() {
 for (String path : merged.getStringList("listeners")) {
  Config listener = root.getConfig(path);
  Factory<? extends CacheEntryListener<? super K, ? super V>> listenerFactory =
    factoryCreator.factoryOf(listener.getString("class"));
  Factory<? extends CacheEntryEventFilter<? super K, ? super V>> filterFactory = null;
  if (listener.hasPath("filter")) {
   filterFactory = factoryCreator.factoryOf(listener.getString("filter"));
  }
  boolean oldValueRequired = listener.getBoolean("old-value-required");
  boolean synchronous = listener.getBoolean("synchronous");
  configuration.addCacheEntryListenerConfiguration(
    new MutableCacheEntryListenerConfiguration<>(
      listenerFactory, filterFactory, oldValueRequired, synchronous));
 }
}

代码示例来源:origin: apache/incubator-gobblin

public HOCONInputStreamJobTemplate(Config config, URI uri, JobCatalogWithTemplates catalog)
   throws SpecNotFoundException, TemplateException {
  super(uri, config.hasPath(VERSION_KEY) ? config.getString(VERSION_KEY) : DEFAULT_VERSION,
    config.hasPath(ConfigurationKeys.JOB_DESCRIPTION_KEY) ? config.getString(ConfigurationKeys.JOB_DESCRIPTION_KEY) : "",
    config, catalog);
 }
}

代码示例来源:origin: ethereum/ethereumj

public String githubTestsPath() {
  return config.hasPath("GitHubTests.testPath") ?
      config.getString("GitHubTests.testPath") : "";
}

代码示例来源:origin: ethereum/ethereumj

public String blocksLoader() {
  return config.hasPath("blocks.loader") ?
      config.getString("blocks.loader") : DEFAULT_BLOCKS_LOADER;
}

代码示例来源:origin: kairosdb/kairosdb

public String getString(String path, String def)
{
  if (m_config.hasPath(path))
    return m_config.getString(path);
  else
    return def;
}

代码示例来源:origin: apache/incubator-gobblin

/**
 * Check if the given <code>key</code> exists in <code>config</code> and it is not null or empty
 * Uses {@link StringUtils#isNotBlank(CharSequence)}
 * @param config which may have the key
 * @param key to look for in the config
 *
 * @return True if key exits and not null or empty. False otherwise
 */
public static boolean hasNonEmptyPath(Config config, String key) {
 return config.hasPath(key) && StringUtils.isNotBlank(config.getString(key));
}

代码示例来源:origin: apache/incubator-gobblin

private String getJobConfigurationFileExtensionsString() {
  String propValue = this.cfg.hasPath(ConfigurationKeys.JOB_CONFIG_FILE_EXTENSIONS_KEY) ?
    this.cfg.getString(ConfigurationKeys.JOB_CONFIG_FILE_EXTENSIONS_KEY).toLowerCase() :
     ConfigurationKeys.DEFAULT_JOB_CONFIG_FILE_EXTENSIONS;
  return propValue;
 }
}

代码示例来源:origin: ethereum/ethereumj

public boolean githubTestsLoadLocal() {
  return config.hasPath("GitHubTests.testPath") &&
      !config.getString("GitHubTests.testPath").isEmpty();
}

代码示例来源:origin: apache/incubator-gobblin

public JobConfigurationManager(EventBus eventBus, Config config) {
 this.eventBus = eventBus;
 this.config = config;
 this.jobConfDirPath =
   config.hasPath(GobblinClusterConfigurationKeys.JOB_CONF_PATH_KEY) ? Optional
     .of(config.getString(GobblinClusterConfigurationKeys.JOB_CONF_PATH_KEY)) : Optional.<String>absent();
}

代码示例来源:origin: apache/incubator-gobblin

private FileSystem buildFileSystem(Config config, Configuration conf)
  throws IOException {
 return config.hasPath(ConfigurationKeys.FS_URI_KEY) ? FileSystem
   .get(URI.create(config.getString(ConfigurationKeys.FS_URI_KEY)), conf)
   : FileSystem.get(conf);
}

代码示例来源:origin: apache/incubator-gobblin

/**
 * Build the {@link FileSystem} for the Application Master.
 */
private FileSystem buildFileSystem(Config config) throws IOException {
 return config.hasPath(ConfigurationKeys.FS_URI_KEY) ? FileSystem
   .get(URI.create(config.getString(ConfigurationKeys.FS_URI_KEY)), new Configuration())
   : FileSystem.get(new Configuration());
}

代码示例来源:origin: apache/incubator-gobblin

private FileSystem buildFileSystem(Config config)
  throws IOException {
 return config.hasPath(ConfigurationKeys.FS_URI_KEY) ? FileSystem
   .get(URI.create(config.getString(ConfigurationKeys.FS_URI_KEY)), new Configuration())
   : FileSystem.get(new Configuration());
}

代码示例来源:origin: apache/incubator-gobblin

public GobblinInstancePlugin createPlugin(Config sysConfig) {
  if (!sysConfig.hasPath(PluginStaticKeys.LOGIN_USER_FULL_KEY)) {
   throw new RuntimeException("Missing required sys config: " + PluginStaticKeys.LOGIN_USER_FULL_KEY);
  }
  if (!sysConfig.hasPath(PluginStaticKeys.LOGIN_USER_KEYTAB_FILE_FULL_KEY)) {
   throw new RuntimeException("Missing required sys config: " + PluginStaticKeys.LOGIN_USER_KEYTAB_FILE_FULL_KEY);
  }
  String loginUser = sysConfig.getString(PluginStaticKeys.LOGIN_USER_FULL_KEY);
  String loginUserKeytabFile = sysConfig.getString(PluginStaticKeys.LOGIN_USER_KEYTAB_FILE_FULL_KEY);
  return new HadoopKerberosKeytabAuthenticationPlugin(sysConfig, loginUser, loginUserKeytabFile);
 }
}

代码示例来源:origin: apache/incubator-gobblin

private static Duration getDuration(Config config) {
  Preconditions
    .checkArgument(config.hasPath(RETENTION_TIMEBASED_DURATION_KEY) || config.hasPath(RETENTION_MINUTES_KEY),
      String.format("Either %s or %s needs to be set", RETENTION_TIMEBASED_DURATION_KEY, RETENTION_MINUTES_KEY));

  if (config.hasPath(RETENTION_TIMEBASED_DURATION_KEY)) {
   return parseDuration(config.getString(RETENTION_TIMEBASED_DURATION_KEY));
  } else {
   return Duration.standardMinutes(Long.parseLong(config.getString(RETENTION_MINUTES_KEY)));
  }
 }
}

代码示例来源:origin: mpusher/mpush

static Config load() {
  Config config = ConfigFactory.load();//扫描加载所有可用的配置文件
  String custom_conf = "mp.conf";//加载自定义配置, 值来自jvm启动参数指定-Dmp.conf
  if (config.hasPath(custom_conf)) {
    File file = new File(config.getString(custom_conf));
    if (file.exists()) {
      Config custom = ConfigFactory.parseFile(file);
      config = custom.withFallback(config);
    }
  }
  return config;
}

代码示例来源:origin: ethereum/ethereumj

public <T> T getProperty(String propName, T defaultValue) {
  if (!config.hasPath(propName)) return defaultValue;
  String string = config.getString(propName);
  if (string.trim().isEmpty()) return defaultValue;
  return (T) config.getAnyRef(propName);
}

代码示例来源:origin: apache/incubator-gobblin

public SelectBetweenTimeBasedPolicy(Config conf) {
 this(conf.hasPath(TIME_BASED_SELECTION_MIN_LOOK_BACK_TIME_KEY) ? Optional.of(getLookBackPeriod(conf
   .getString(TIME_BASED_SELECTION_MIN_LOOK_BACK_TIME_KEY))) : Optional.<Period> absent(), conf
   .hasPath(TIME_BASED_SELECTION_MAX_LOOK_BACK_TIME_KEY) ? Optional.of(getLookBackPeriod(conf
   .getString(TIME_BASED_SELECTION_MAX_LOOK_BACK_TIME_KEY))) : Optional.<Period> absent());
}

代码示例来源:origin: apache/incubator-gobblin

private static Period getMaxLookbackTime(Config conf) {
  Preconditions.checkArgument(conf.hasPath(TIME_BASED_SELECTION_LOOK_BACK_TIME_KEY),
    String.format("Required property %s is not specified", TIME_BASED_SELECTION_LOOK_BACK_TIME_KEY));
  return SelectBetweenTimeBasedPolicy.getLookBackPeriod(conf.getString(TIME_BASED_SELECTION_LOOK_BACK_TIME_KEY));
 }
}

代码示例来源:origin: apache/incubator-gobblin

public Builder withReplicationSource(Config config)
  throws InstantiationException, IllegalAccessException, ClassNotFoundException {
 Preconditions.checkArgument(config.hasPath(REPLICATION_SOURCE),
   "missing required config entry " + REPLICATION_SOURCE);
 Config sourceConfig = config.getConfig(REPLICATION_SOURCE);
 String endPointFactory = sourceConfig.hasPath(END_POINT_FACTORY_CLASS)
   ? sourceConfig.getString(END_POINT_FACTORY_CLASS) : DEFAULT_END_POINT_FACTORY_CLASS;
 EndPointFactory factory = endPointFactoryResolver.resolveClass(endPointFactory).newInstance();
 this.source = factory.buildSource(sourceConfig, this.selectionConfig);
 return this;
}

相关文章