add Config.empty variants that take origin description; use singleton for default-named empty object

This commit is contained in:
Havoc Pennington 2011-11-15 21:08:29 -05:00
parent 1841dd9c67
commit ed75c315bc
4 changed files with 37 additions and 12 deletions

View File

@ -81,13 +81,22 @@ public final class Config {
}
public static ConfigRoot emptyRoot(String rootPath) {
return ConfigImpl.emptyRoot(rootPath);
return emptyRoot(rootPath, null);
}
public static ConfigObject empty() {
return ConfigImpl.empty();
return empty(null);
}
public static ConfigRoot emptyRoot(String rootPath, String originDescription) {
return ConfigImpl.emptyRoot(rootPath, originDescription);
}
public static ConfigObject empty(String originDescription) {
return ConfigImpl.empty(originDescription);
}
public static ConfigRoot systemPropertiesRoot(String rootPath) {
return ConfigImpl.systemPropertiesRoot(rootPath);
}
@ -250,7 +259,7 @@ public final class Config {
* ConfigObject interface. The units parsed are interpreted as powers of
* two, that is, the convention for memory rather than the convention for
* disk space.
*
*
* @param input
* the string to parse
* @param originForException

View File

@ -111,14 +111,17 @@ public class ConfigImpl {
}
/** For use ONLY by library internals, DO NOT TOUCH not guaranteed ABI */
public static ConfigRoot emptyRoot(String rootPath) {
return SimpleConfigObject.empty(new SimpleConfigOrigin(rootPath))
public static ConfigRoot emptyRoot(String rootPath, String originDescription) {
String desc = originDescription != null ? originDescription : rootPath;
return SimpleConfigObject.empty(new SimpleConfigOrigin(desc))
.asRoot(
Parser.parsePath(rootPath));
}
public static ConfigObject empty() {
return SimpleConfigObject.empty();
public static ConfigObject empty(String originDescription) {
return SimpleConfigObject
.empty(originDescription != null ? new SimpleConfigOrigin(
originDescription) : null);
}
/** For use ONLY by library internals, DO NOT TOUCH not guaranteed ABI */

View File

@ -113,16 +113,20 @@ final class SimpleConfigObject extends AbstractConfigObject {
return new HashSet<ConfigValue>(value.values());
}
final static String EMPTY_NAME = "empty config";
final private static String EMPTY_NAME = "empty config";
final private static SimpleConfigObject emptyInstance = empty(new SimpleConfigOrigin(
EMPTY_NAME));
final static SimpleConfigObject empty() {
return new SimpleConfigObject(new SimpleConfigOrigin(EMPTY_NAME),
Collections.<String, AbstractConfigValue> emptyMap());
return emptyInstance;
}
final static SimpleConfigObject empty(ConfigOrigin origin) {
return new SimpleConfigObject(origin,
Collections.<String, AbstractConfigValue> emptyMap());
if (origin == null)
return empty();
else
return new SimpleConfigObject(origin,
Collections.<String, AbstractConfigValue> emptyMap());
}
final static SimpleConfigObject emptyMissing(ConfigOrigin baseOrigin) {

View File

@ -71,4 +71,13 @@ class PublicApiTest extends TestUtils {
conf.getInt("fromProps.one")
}
}
@Test
def emptyObjects() {
assertEquals(0, Config.empty().size())
assertEquals(0, Config.empty("foo").size())
assertEquals("foo", Config.empty("foo").origin().description())
assertEquals(0, Config.emptyRoot("foo.bar").size())
assertEquals("foo.bar", Config.emptyRoot("foo.bar").origin().description())
}
}