Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (C) 2011-2012 Typesafe Inc.
*/
package com.crashnote.external.config.impl;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import com.crashnote.external.config.Config;
import com.crashnote.external.config.ConfigException;
import com.crashnote.external.config.ConfigList;
import com.crashnote.external.config.ConfigMergeable;
import com.crashnote.external.config.ConfigObject;
import com.crashnote.external.config.ConfigOrigin;
import com.crashnote.external.config.ConfigResolveOptions;
import com.crashnote.external.config.ConfigValue;
import com.crashnote.external.config.ConfigValueType;
/**
* One thing to keep in mind in the future: as Collection-like APIs are added
* here, including iterators or size() or anything, they should be consistent
* with a one-level java.util.Map from paths to non-null values. Null values are
* not "in" the map.
*/
final class SimpleConfig implements Config, MergeableValue, Serializable {
private static final long serialVersionUID = 1L;
final private AbstractConfigObject object;
SimpleConfig(final AbstractConfigObject object) {
this.object = object;
}
@Override
public AbstractConfigObject root() {
return object;
}
@Override
public ConfigOrigin origin() {
return object.origin();
}
@Override
public SimpleConfig resolve() {
return resolve(ConfigResolveOptions.defaults());
}
@Override
public SimpleConfig resolve(final ConfigResolveOptions options) {
final AbstractConfigValue resolved = ResolveContext.resolve(object, object, options);
if (resolved == object)
return this;
else
return new SimpleConfig((AbstractConfigObject) resolved);
}
@Override
public boolean hasPath(final String pathExpression) {
final Path path = Path.newPath(pathExpression);
final ConfigValue peeked;
try {
peeked = object.peekPath(path);
} catch (ConfigException.NotResolved e) {
throw ConfigImpl.improveNotResolved(path, e);
}
return peeked != null && peeked.valueType() != ConfigValueType.NULL;
}
@Override
public boolean isEmpty() {
return object.isEmpty();
}
private static void findPaths(final Set> entries, final Path parent,
final AbstractConfigObject obj) {
for (final Map.Entry entry : obj.entrySet()) {
final String elem = entry.getKey();
final ConfigValue v = entry.getValue();
Path path = Path.newKey(elem);
if (parent != null)
path = path.prepend(parent);
if (v instanceof AbstractConfigObject) {
findPaths(entries, path, (AbstractConfigObject) v);
} else if (v instanceof ConfigNull) {
// nothing; nulls are conceptually not in a Config
} else {
entries.add(new AbstractMap.SimpleImmutableEntry(path.render(), v));
}
}
}
@Override
public Set> entrySet() {
final Set> entries = new HashSet>();
findPaths(entries, null, object);
return entries;
}
static private AbstractConfigValue findKey(final AbstractConfigObject self, final String key,
final ConfigValueType expected, final Path originalPath) {
AbstractConfigValue v = self.peekAssumingResolved(key, originalPath);
if (v == null)
throw new ConfigException.Missing(originalPath.render());
if (expected != null)
v = DefaultTransformer.transform(v, expected);
if (v.valueType() == ConfigValueType.NULL)
throw new ConfigException.Null(v.origin(), originalPath.render(),
expected != null ? expected.name() : null);
else if (expected != null && v.valueType() != expected)
throw new ConfigException.WrongType(v.origin(), originalPath.render(), expected.name(),
v.valueType().name());
else
return v;
}
static private AbstractConfigValue find(final AbstractConfigObject self, final Path path,
final ConfigValueType expected, final Path originalPath) {
try {
final String key = path.first();
final Path next = path.remainder();
if (next == null) {
return findKey(self, key, expected, originalPath);
} else {
final AbstractConfigObject o = (AbstractConfigObject) findKey(self, key,
ConfigValueType.OBJECT,
originalPath.subPath(0, originalPath.length() - next.length()));
assert (o != null); // missing was supposed to throw
return find(o, next, expected, originalPath);
}
} catch (ConfigException.NotResolved e) {
throw ConfigImpl.improveNotResolved(path, e);
}
}
AbstractConfigValue find(final Path pathExpression, final ConfigValueType expected, final Path originalPath) {
return find(object, pathExpression, expected, originalPath);
}
AbstractConfigValue find(final String pathExpression, final ConfigValueType expected) {
final Path path = Path.newPath(pathExpression);
return find(path, expected, path);
}
@Override
public AbstractConfigValue getValue(final String path) {
return find(path, null);
}
@Override
public boolean getBoolean(final String path) {
final ConfigValue v = find(path, ConfigValueType.BOOLEAN);
return (Boolean) v.unwrapped();
}
private ConfigNumber getConfigNumber(final String path) {
final ConfigValue v = find(path, ConfigValueType.NUMBER);
return (ConfigNumber) v;
}
@Override
public Number getNumber(final String path) {
return getConfigNumber(path).unwrapped();
}
@Override
public int getInt(final String path) {
final ConfigNumber n = getConfigNumber(path);
return n.intValueRangeChecked(path);
}
@Override
public long getLong(final String path) {
return getNumber(path).longValue();
}
@Override
public double getDouble(final String path) {
return getNumber(path).doubleValue();
}
@Override
public String getString(final String path) {
final ConfigValue v = find(path, ConfigValueType.STRING);
return (String) v.unwrapped();
}
@Override
public ConfigList getList(final String path) {
final AbstractConfigValue v = find(path, ConfigValueType.LIST);
return (ConfigList) v;
}
@Override
public AbstractConfigObject getObject(final String path) {
final AbstractConfigObject obj = (AbstractConfigObject) find(path, ConfigValueType.OBJECT);
return obj;
}
@Override
public SimpleConfig getConfig(final String path) {
return getObject(path).toConfig();
}
@Override
public Object getAnyRef(final String path) {
final ConfigValue v = find(path, null);
return v.unwrapped();
}
@Override
public Long getBytes(final String path) {
Long size = null;
try {
size = getLong(path);
} catch (ConfigException.WrongType e) {
final ConfigValue v = find(path, ConfigValueType.STRING);
size = parseBytes((String) v.unwrapped(),
v.origin(), path);
}
return size;
}
@Override
public Long getMilliseconds(final String path) {
final long ns = getNanoseconds(path);
final long ms = TimeUnit.NANOSECONDS.toMillis(ns);
return ms;
}
@Override
public Long getNanoseconds(final String path) {
Long ns = null;
try {
ns = TimeUnit.MILLISECONDS.toNanos(getLong(path));
} catch (ConfigException.WrongType e) {
final ConfigValue v = find(path, ConfigValueType.STRING);
ns = parseDuration((String) v.unwrapped(), v.origin(), path);
}
return ns;
}
@SuppressWarnings("unchecked")
private List getHomogeneousUnwrappedList(final String path,
final ConfigValueType expected) {
final List l = new ArrayList();
final List extends ConfigValue> list = getList(path);
for (final 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;
}
@Override
public List getBooleanList(final String path) {
return getHomogeneousUnwrappedList(path, ConfigValueType.BOOLEAN);
}
@Override
public List getNumberList(final String path) {
return getHomogeneousUnwrappedList(path, ConfigValueType.NUMBER);
}
@Override
public List getIntList(final String path) {
final List l = new ArrayList();
final List numbers = getHomogeneousWrappedList(path, ConfigValueType.NUMBER);
for (final AbstractConfigValue v : numbers) {
l.add(((ConfigNumber) v).intValueRangeChecked(path));
}
return l;
}
@Override
public List getLongList(final String path) {
final List l = new ArrayList();
final List numbers = getNumberList(path);
for (final Number n : numbers) {
l.add(n.longValue());
}
return l;
}
@Override
public List getDoubleList(final String path) {
final List l = new ArrayList();
final List numbers = getNumberList(path);
for (final Number n : numbers) {
l.add(n.doubleValue());
}
return l;
}
@Override
public List getStringList(final String path) {
return getHomogeneousUnwrappedList(path, ConfigValueType.STRING);
}
@SuppressWarnings("unchecked")
private List getHomogeneousWrappedList(
final String path, final ConfigValueType expected) {
final List l = new ArrayList();
final List extends ConfigValue> list = getList(path);
for (final 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;
}
@Override
public List getObjectList(final String path) {
return getHomogeneousWrappedList(path, ConfigValueType.OBJECT);
}
@Override
public List extends Config> getConfigList(final String path) {
final List objects = getObjectList(path);
final List l = new ArrayList();
for (final ConfigObject o : objects) {
l.add(o.toConfig());
}
return l;
}
@Override
public List extends Object> getAnyRefList(final String path) {
final List