From 5b2519863e8ad957801fc69f1c63f646584fb96a Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Tue, 24 Feb 2015 20:21:03 -0500 Subject: [PATCH] Add getMemorySize and getMemorySizeList to Config --- .../main/java/com/typesafe/config/Config.java | 22 +++++++++++++++++++ .../typesafe/config/impl/SimpleConfig.java | 16 ++++++++++++++ .../com/typesafe/config/impl/ConfigTest.scala | 19 ++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/config/src/main/java/com/typesafe/config/Config.java b/config/src/main/java/com/typesafe/config/Config.java index 2d795243..001f2be7 100644 --- a/config/src/main/java/com/typesafe/config/Config.java +++ b/config/src/main/java/com/typesafe/config/Config.java @@ -585,6 +585,26 @@ public interface Config extends ConfigMergeable { */ Long getBytes(String path); + /** + * Gets a value as an amount of memory (parses special strings like "128M"). If + * the value is already a number, then it's left alone; if it's a string, + * it's parsed understanding unit suffixes such as "128K", as documented in + * the the + * spec. + * + * @param path + * path expression + * @return the value at the requested path, in bytes + * @throws ConfigException.Missing + * if value is absent or null + * @throws ConfigException.WrongType + * if value is not convertible to Long or String + * @throws ConfigException.BadValue + * if value cannot be parsed as a size in bytes + */ + ConfigMemorySize getMemorySize(String path); + /** * Get value as a duration in milliseconds. If the value is already a * number, then it's left alone; if it's a string, it's parsed understanding @@ -686,6 +706,8 @@ public interface Config extends ConfigMergeable { List getBytesList(String path); + List getMemorySizeList(String path); + /** * @deprecated As of release 1.1, replaced by {@link #getDurationList(String, TimeUnit)} */ diff --git a/config/src/main/java/com/typesafe/config/impl/SimpleConfig.java b/config/src/main/java/com/typesafe/config/impl/SimpleConfig.java index fb6c7053..6b8f8f9f 100644 --- a/config/src/main/java/com/typesafe/config/impl/SimpleConfig.java +++ b/config/src/main/java/com/typesafe/config/impl/SimpleConfig.java @@ -19,6 +19,7 @@ import java.util.concurrent.TimeUnit; import com.typesafe.config.Config; import com.typesafe.config.ConfigException; import com.typesafe.config.ConfigList; +import com.typesafe.config.ConfigMemorySize; import com.typesafe.config.ConfigMergeable; import com.typesafe.config.ConfigObject; import com.typesafe.config.ConfigOrigin; @@ -245,6 +246,11 @@ final class SimpleConfig implements Config, MergeableValue, Serializable { return size; } + @Override + public ConfigMemorySize getMemorySize(String path) { + return ConfigMemorySize.ofBytes(getBytes(path)); + } + @Deprecated @Override public Long getMilliseconds(String path) { @@ -396,6 +402,16 @@ final class SimpleConfig implements Config, MergeableValue, Serializable { return l; } + @Override + public List getMemorySizeList(String path) { + List list = getBytesList(path); + List builder = new ArrayList(); + for (Long v : list) { + builder.add(ConfigMemorySize.ofBytes(v)); + } + return builder; + } + @Override public List getDurationList(String path, TimeUnit unit) { List l = new ArrayList(); diff --git a/config/src/test/scala/com/typesafe/config/impl/ConfigTest.scala b/config/src/test/scala/com/typesafe/config/impl/ConfigTest.scala index 634c0d69..3d1f0017 100644 --- a/config/src/test/scala/com/typesafe/config/impl/ConfigTest.scala +++ b/config/src/test/scala/com/typesafe/config/impl/ConfigTest.scala @@ -645,6 +645,10 @@ class ConfigTest extends TestUtils { conf.getBytes("nulls.null") } + intercept[ConfigException.Null] { + conf.getMemorySize("nulls.null") + } + // should throw WrongType if key is wrong type and not convertible intercept[ConfigException.WrongType] { conf.getInt("booleans.trueAgain") @@ -674,6 +678,10 @@ class ConfigTest extends TestUtils { conf.getBytes("ints") } + intercept[ConfigException.WrongType] { + conf.getMemorySize("ints") + } + // should throw BadPath on various bad paths intercept[ConfigException.BadPath] { conf.getInt(".bad") @@ -700,6 +708,10 @@ class ConfigTest extends TestUtils { intercept[ConfigException.BadValue] { conf.getBytes("strings.a") } + + intercept[ConfigException.BadValue] { + conf.getMemorySize("strings.a") + } } @Test @@ -787,6 +799,13 @@ class ConfigTest extends TestUtils { assertEquals(Seq(1024 * 1024L, 1024 * 1024L, 1024L * 1024L), conf.getBytesList("memsizes.megsList").asScala) assertEquals(512 * 1024L, conf.getBytes("memsizes.halfMeg")) + + // should get size as a ConfigMemorySize + assertEquals(1024 * 1024L, conf.getMemorySize("memsizes.meg").toBytes) + assertEquals(1024 * 1024L, conf.getMemorySize("memsizes.megAsNumber").toBytes) + assertEquals(Seq(1024 * 1024L, 1024 * 1024L, 1024L * 1024L), + conf.getMemorySizeList("memsizes.megsList").asScala.map(_.toBytes)) + assertEquals(512 * 1024L, conf.getMemorySize("memsizes.halfMeg").toBytes) } @Test