io.helidon.config.Config.as()方法的使用及代码示例

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

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

Config.as介绍

[英]Typed value as a ConfigValue for a generic type. If appropriate mapper exists, returns a properly typed generic instance.

Example:

ConfigValue> myMapValue = config.as(new GenericType>(){}); 
myMapValue.ifPresent(map ->  
Integer port = map.get("service.port"); 
} 
}

[中]类型化值作为泛型类型的ConfigValue。如果存在适当的映射器,则返回正确类型的泛型实例。
例子:

ConfigValue> myMapValue = config.as(new GenericType>(){}); 
myMapValue.ifPresent(map ->  
Integer port = map.get("service.port"); 
} 
}

代码示例

代码示例来源:origin: oracle/helidon

/**
 * String typed value.
 *
 * @return typed value
 */
default ConfigValue<String> asString() {
  return as(String.class);
}

代码示例来源:origin: oracle/helidon

@Override
public <T> ConfigValue<List<T>> asList(Class<T> type) throws ConfigMappingException {
  return ConfigValues.createList(this,
                config -> config.as(type),
                config -> config.asList(type));
}

代码示例来源:origin: oracle/helidon

/**
 * Read a new instance from configuration.
 * The current node should contain a {@code "name"} and configuration for {@link TokenHandler}
 *
 * @param config configuration instance
 * @return new query parameter handler instance
 */
public static QueryParamMapping create(Config config) {
  String name = config.get("name").asString().get();
  TokenHandler handler = config.as(TokenHandler::create).get();
  return create(name, handler);
}

代码示例来源:origin: oracle/helidon

/**
 * Double typed value.
 *
 * @return typed value
 */
default ConfigValue<Double> asDouble() {
  return as(Double.class);
}

代码示例来源:origin: oracle/helidon

/**
 * Update builder from config.
 *
 * @param config Configuration to update from
 * @return update builder instance
 */
public Builder config(Config config) {
  config.get("header").asString().ifPresent(this::tokenHeader);
  config.get("prefix").asString().ifPresent(this::tokenPrefix);
  config.get("regexp").as(Pattern.class).ifPresent(this::tokenPattern);
  config.get("format").asString().ifPresent(this::tokenFormat);
  return this;
}

代码示例来源:origin: oracle/helidon

@Override
public <T> ConfigValue<List<T>> asList(Function<Config, T> mapper) throws ConfigMappingException {
  return ConfigValues.createList(this,
                config -> config.as(mapper),
                config -> config.asList(mapper));
}

代码示例来源:origin: oracle/helidon

/**
 * Long typed value.
 *
 * @return typed value
 */
default ConfigValue<Long> asLong() {
  return as(Long.class);
}

代码示例来源:origin: oracle/helidon

/**
 * Integer typed value.
 *
 * @return typed value
 */
default ConfigValue<Integer> asInt() {
  return as(Integer.class);
}

代码示例来源:origin: oracle/helidon

/**
 * Boolean typed value.
 *
 * @return typed value
 */
default ConfigValue<Boolean> asBoolean() {
  return as(Boolean.class);
}

代码示例来源:origin: oracle/helidon

static <T> ConfigValue<T> create(Config config,
                 Function<Config, T> mapper) {
  return new GenericConfigValueImpl<>(config,
                    () -> Optional.ofNullable(mapper.apply(config)),
                    aConfig -> aConfig.as(mapper));
}

代码示例来源:origin: oracle/helidon

@Override
public <T> ConfigValue<T> as(Class<T> type) {
  return ConfigValues.create(this, Optional::empty, aConfig -> aConfig.as(type));
}

代码示例来源:origin: oracle/helidon

@Override
public <T> ConfigValue<T> as(GenericType<T> genericType) {
  return ConfigValues.create(this, Optional::empty, aConfig -> aConfig.as(genericType));
}

代码示例来源:origin: oracle/helidon

@Override
public <T> ConfigValue<T> as(Function<Config, T> mapper) {
  return ConfigValues.create(this, Optional::empty, aConfig -> aConfig.as(mapper));
}

代码示例来源:origin: oracle/helidon

private Optional<RetryPolicy> custom(Class<?> clazz, Config properties) {
  final RetryPolicy retryPolicy;
  if (RetryPolicy.class.isAssignableFrom(clazz)) {
    retryPolicy = properties.as((Class<RetryPolicy>) clazz).get();
  } else {
    throw new ConfigException("Configured retry policy class " + clazz.getName() + " does not implement RetryPolicy");
  }
  return Optional.of(retryPolicy);
}

代码示例来源:origin: oracle/helidon

private Optional<ConfigSource> custom(Class<?> clazz, Config properties) {
  Object source = properties.as(clazz).get();
  if (source instanceof ConfigSource) {
    return Optional.of((ConfigSource) source);
  }
  throw new ConfigException("Failed to process configuration metadata, configured class " + clazz.getName() + " does "
                   + "not implement ConfigSource");
}

代码示例来源:origin: oracle/helidon

static <T> ConfigValue<T> create(Config config,
                 GenericType<T> genericType,
                 ConfigMapperManager mapperManager) {
  return new GenericConfigValueImpl<>(config,
                    () -> Optional.ofNullable(mapperManager.map(config, genericType)),
                    aConfig -> aConfig.as(genericType));
}

代码示例来源:origin: oracle/helidon

private Optional<RetryPolicy> builtin(String type, Config properties) {
  final RetryPolicy retryPolicy;
  switch (type) {
  case REPEAT_TYPE:
    retryPolicy = properties.as(RetryPolicies.Builder.class).get().get();
    break;
  default:
    retryPolicy = null;
  }
  return Optional.ofNullable(retryPolicy);
}

代码示例来源:origin: oracle/helidon

private static <T> void configure(Config config,
                 String key,
                 Optional<T> defaultValue,
                 Consumer<T> builderMethod,
                 Class<T> clazz) {
  config.get(key).as(clazz).or(() -> defaultValue).ifPresent(builderMethod);
}

代码示例来源:origin: oracle/helidon

private <T> T findValue(String propertyName, Class<T> propertyType) {
  if (propertyType == Config.class) {
    return config.get().get(propertyName).as(propertyType).get();
  }
  //first iterate over mp sources, than use config
  return findInMpSources(propertyName)
      .map(value -> convert(propertyType, value))
      .orElseGet(() -> config.get().get(propertyName).as(propertyType).get());
}

代码示例来源:origin: oracle/helidon

static <T> ConfigValue<T> create(Config config,
                 Class<T> type,
                 ConfigMapperManager mapperManager) {
  return new GenericConfigValueImpl<>(config,
                    () -> Optional.ofNullable(mapperManager.map(config, type)),
                    aConfig -> aConfig.as(type));
}

相关文章