Search in sources :

Example 1 with ConfigMemorySize

use of com.typesafe.config.ConfigMemorySize in project config by typesafehub.

the class ConfigImpl method fromAnyRef.

static AbstractConfigValue fromAnyRef(Object object, ConfigOrigin origin, FromMapMode mapMode) {
    if (origin == null)
        throw new ConfigException.BugOrBroken("origin not supposed to be null");
    if (object == null) {
        if (origin != defaultValueOrigin)
            return new ConfigNull(origin);
        else
            return defaultNullValue;
    } else if (object instanceof AbstractConfigValue) {
        return (AbstractConfigValue) object;
    } else if (object instanceof Boolean) {
        if (origin != defaultValueOrigin) {
            return new ConfigBoolean(origin, (Boolean) object);
        } else if ((Boolean) object) {
            return defaultTrueValue;
        } else {
            return defaultFalseValue;
        }
    } else if (object instanceof String) {
        return new ConfigString.Quoted(origin, (String) object);
    } else if (object instanceof Number) {
        // Double, Integer, or Long.
        if (object instanceof Double) {
            return new ConfigDouble(origin, (Double) object, null);
        } else if (object instanceof Integer) {
            return new ConfigInt(origin, (Integer) object, null);
        } else if (object instanceof Long) {
            return new ConfigLong(origin, (Long) object, null);
        } else {
            return ConfigNumber.newNumber(origin, ((Number) object).doubleValue(), null);
        }
    } else if (object instanceof Duration) {
        return new ConfigLong(origin, ((Duration) object).toMillis(), null);
    } else if (object instanceof Map) {
        if (((Map<?, ?>) object).isEmpty())
            return emptyObject(origin);
        if (mapMode == FromMapMode.KEYS_ARE_KEYS) {
            Map<String, AbstractConfigValue> values = new HashMap<String, AbstractConfigValue>();
            for (Map.Entry<?, ?> entry : ((Map<?, ?>) object).entrySet()) {
                Object key = entry.getKey();
                if (!(key instanceof String))
                    throw new ConfigException.BugOrBroken("bug in method caller: not valid to create ConfigObject from map with non-String key: " + key);
                AbstractConfigValue value = fromAnyRef(entry.getValue(), origin, mapMode);
                values.put((String) key, value);
            }
            return new SimpleConfigObject(origin, values);
        } else {
            return PropertiesParser.fromPathMap(origin, (Map<?, ?>) object);
        }
    } else if (object instanceof Iterable) {
        Iterator<?> i = ((Iterable<?>) object).iterator();
        if (!i.hasNext())
            return emptyList(origin);
        List<AbstractConfigValue> values = new ArrayList<AbstractConfigValue>();
        while (i.hasNext()) {
            AbstractConfigValue v = fromAnyRef(i.next(), origin, mapMode);
            values.add(v);
        }
        return new SimpleConfigList(origin, values);
    } else if (object instanceof ConfigMemorySize) {
        return new ConfigLong(origin, ((ConfigMemorySize) object).toBytes(), null);
    } else {
        throw new ConfigException.BugOrBroken("bug in method caller: not valid to create ConfigValue from: " + object);
    }
}
Also used : HashMap(java.util.HashMap) ConfigException(com.typesafe.config.ConfigException) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Duration(java.time.Duration) ConfigMemorySize(com.typesafe.config.ConfigMemorySize) ConfigObject(com.typesafe.config.ConfigObject) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ConfigException (com.typesafe.config.ConfigException)1 ConfigMemorySize (com.typesafe.config.ConfigMemorySize)1 ConfigObject (com.typesafe.config.ConfigObject)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1