com.typesafe.config.ConfigValue

Here are the examples of the java api class com.typesafe.config.ConfigValue taken from open source projects.

1. SimpleConfig#getDurationList()

Project: config
File: SimpleConfig.java
@Override
public List<Long> getDurationList(String path, TimeUnit unit) {
    List<Long> l = new ArrayList<Long>();
    List<? extends ConfigValue> list = getList(path);
    for (ConfigValue v : list) {
        if (v.valueType() == ConfigValueType.NUMBER) {
            Long n = unit.convert(((Number) v.unwrapped()).longValue(), TimeUnit.MILLISECONDS);
            l.add(n);
        } else if (v.valueType() == ConfigValueType.STRING) {
            String s = (String) v.unwrapped();
            Long n = unit.convert(parseDuration(s, v.origin(), path), TimeUnit.NANOSECONDS);
            l.add(n);
        } else {
            throw new ConfigException.WrongType(v.origin(), path, "duration string or number of milliseconds", v.valueType().name());
        }
    }
    return l;
}

2. SimpleConfig#getBytesList()

Project: config
File: SimpleConfig.java
@Override
public List<Long> getBytesList(String path) {
    List<Long> l = new ArrayList<Long>();
    List<? extends ConfigValue> list = getList(path);
    for (ConfigValue v : list) {
        if (v.valueType() == ConfigValueType.NUMBER) {
            l.add(((Number) v.unwrapped()).longValue());
        } else if (v.valueType() == ConfigValueType.STRING) {
            String s = (String) v.unwrapped();
            Long n = parseBytes(s, v.origin(), path);
            l.add(n);
        } else {
            throw new ConfigException.WrongType(v.origin(), path, "memory size string or number of bytes", v.valueType().name());
        }
    }
    return l;
}

3. SimpleConfig#getHomogeneousWrappedList()

Project: config
File: SimpleConfig.java
@SuppressWarnings("unchecked")
private <T extends ConfigValue> List<T> getHomogeneousWrappedList(String path, ConfigValueType expected) {
    List<T> l = new ArrayList<T>();
    List<? extends ConfigValue> list = getList(path);
    for (ConfigValue cv : list) {
        // variance would be nice, but stupid cast will do
        AbstractConfigValue v = (AbstractConfigValue) cv;
        if (expected != null) {
            v = DefaultTransformer.transform(v, expected);
        }
        if (v.valueType() != expected)
            throw new ConfigException.WrongType(v.origin(), path, "list of " + expected.name(), "list of " + v.valueType().name());
        l.add((T) v);
    }
    return l;
}

4. SimpleConfig#getHomogeneousUnwrappedList()

Project: config
File: SimpleConfig.java
@SuppressWarnings("unchecked")
private <T> List<T> getHomogeneousUnwrappedList(String path, ConfigValueType expected) {
    List<T> l = new ArrayList<T>();
    List<? extends ConfigValue> list = getList(path);
    for (ConfigValue cv : list) {
        // variance would be nice, but stupid cast will do
        AbstractConfigValue v = (AbstractConfigValue) cv;
        if (expected != null) {
            v = DefaultTransformer.transform(v, expected);
        }
        if (v.valueType() != expected)
            throw new ConfigException.WrongType(v.origin(), path, "list of " + expected.name(), "list of " + v.valueType().name());
        l.add((T) v.unwrapped());
    }
    return l;
}

5. SimpleConfig#getAnyRef()

Project: config
File: SimpleConfig.java
@Override
public Object getAnyRef(String path) {
    ConfigValue v = find(path, null);
    return v.unwrapped();
}

6. JobsResource#formatConfig()

Project: hydra
File: JobsResource.java
private Response formatConfig(String format, String configBody) {
    if (format == null) {
        return Response.ok("attachment; filename=expanded_job.json", MediaType.APPLICATION_OCTET_STREAM).entity(configBody).header("topic", "expanded_job").build();
    }
    String normalizedFormat = format.toLowerCase();
    String formattedConfig;
    Config config = ConfigFactory.parseString(configBody, ConfigParseOptions.defaults().setOriginDescription("job.conf"));
    Config jobConfig = config;
    PluginRegistry pluginRegistry;
    if (config.hasPath("global")) {
        Config globalDefaults = config.getConfig("global").withFallback(ConfigFactory.load()).resolve();
        pluginRegistry = new PluginRegistry(globalDefaults);
        jobConfig = config.withoutPath("global");
    } else {
        pluginRegistry = PluginRegistry.defaultRegistry();
    }
    jobConfig = jobConfig.resolve(ConfigResolveOptions.defaults().setAllowUnresolved(true)).resolveWith(pluginRegistry.config());
    ConfigValue expandedConfig = Configs.expandSugar(TaskRunnable.class, jobConfig.root(), pluginRegistry);
    switch(normalizedFormat) {
        // auto json/hocon + json output
        case "json":
            formattedConfig = expandedConfig.render(ConfigRenderOptions.concise().setFormatted(true));
            return Response.ok("attachment; filename=expanded_job.json", MediaType.APPLICATION_JSON).entity(formattedConfig).header("topic", "expanded_job").build();
        case "hocon":
            // hocon parse + non-json output
            formattedConfig = expandedConfig.render(ConfigRenderOptions.defaults());
            return Response.ok("attachment; filename=expanded_job.json", MediaType.APPLICATION_OCTET_STREAM).entity(formattedConfig).header("topic", "expanded_job").build();
        default:
            throw new IllegalArgumentException("invalid config format specified: " + normalizedFormat);
    }
}

7. TemporaryJobs#getListByKey()

Project: helios
File: TemporaryJobs.java
private static List<String> getListByKey(final String key, final Config config) {
    final ConfigList endpointList = config.getList(key);
    final List<String> stringList = Lists.newArrayList();
    for (final ConfigValue v : endpointList) {
        if (v.valueType() != ConfigValueType.STRING) {
            throw new RuntimeException("Item in " + key + " list [" + v + "] is not a string");
        }
        stringList.add((String) v.unwrapped());
    }
    return stringList;
}

8. SimpleConfig#getAnyRefList()

Project: config
File: SimpleConfig.java
@Override
public List<? extends Object> getAnyRefList(String path) {
    List<Object> l = new ArrayList<Object>();
    List<? extends ConfigValue> list = getList(path);
    for (ConfigValue v : list) {
        l.add(v.unwrapped());
    }
    return l;
}

9. SimpleConfig#getDuration()

Project: config
File: SimpleConfig.java
@Override
public Duration getDuration(String path) {
    ConfigValue v = find(path, ConfigValueType.STRING);
    long nanos = parseDuration((String) v.unwrapped(), v.origin(), path);
    return Duration.ofNanos(nanos);
}

10. SimpleConfig#getDuration()

Project: config
File: SimpleConfig.java
@Override
public long getDuration(String path, TimeUnit unit) {
    ConfigValue v = find(path, ConfigValueType.STRING);
    long result = unit.convert(parseDuration((String) v.unwrapped(), v.origin(), path), TimeUnit.NANOSECONDS);
    return result;
}

11. SimpleConfig#getEnum()

Project: config
File: SimpleConfig.java
@Override
public <T extends Enum<T>> T getEnum(Class<T> enumClass, String path) {
    ConfigValue v = find(path, ConfigValueType.STRING);
    return getEnumValue(path, enumClass, v);
}

12. SimpleConfig#getString()

Project: config
File: SimpleConfig.java
@Override
public String getString(String path) {
    ConfigValue v = find(path, ConfigValueType.STRING);
    return (String) v.unwrapped();
}

13. SimpleConfig#getConfigNumber()

Project: config
File: SimpleConfig.java
private ConfigNumber getConfigNumber(String path) {
    ConfigValue v = find(path, ConfigValueType.NUMBER);
    return (ConfigNumber) v;
}

14. SimpleConfig#getBoolean()

Project: config
File: SimpleConfig.java
@Override
public boolean getBoolean(String path) {
    ConfigValue v = find(path, ConfigValueType.BOOLEAN);
    return (Boolean) v.unwrapped();
}

15. SimpleConfig#hasPathOrNull()

Project: config
File: SimpleConfig.java
@Override
public boolean hasPathOrNull(String path) {
    ConfigValue peeked = hasPathPeek(path);
    return peeked != null;
}

16. SimpleConfig#hasPath()

Project: config
File: SimpleConfig.java
@Override
public boolean hasPath(String pathExpression) {
    ConfigValue peeked = hasPathPeek(pathExpression);
    return peeked != null && peeked.valueType() != ConfigValueType.NULL;
}

17. SimpleConfig#hasPathPeek()

Project: config
File: SimpleConfig.java
private ConfigValue hasPathPeek(String pathExpression) {
    Path path = Path.newPath(pathExpression);
    ConfigValue peeked;
    try {
        peeked = object.peekPath(path);
    } catch (ConfigException.NotResolved e) {
        throw ConfigImpl.improveNotResolved(path, e);
    }
    return peeked;
}