mirror of
https://github.com/lightbend/config.git
synced 2025-01-15 23:01:05 +08:00
Add getMemorySize and getMemorySizeList to Config
This commit is contained in:
parent
287e927129
commit
5b2519863e
@ -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 <a
|
||||
* href="https://github.com/typesafehub/config/blob/master/HOCON.md">the
|
||||
* spec</a>.
|
||||
*
|
||||
* @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<Long> getBytesList(String path);
|
||||
|
||||
List<ConfigMemorySize> getMemorySizeList(String path);
|
||||
|
||||
/**
|
||||
* @deprecated As of release 1.1, replaced by {@link #getDurationList(String, TimeUnit)}
|
||||
*/
|
||||
|
@ -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<ConfigMemorySize> getMemorySizeList(String path) {
|
||||
List<Long> list = getBytesList(path);
|
||||
List<ConfigMemorySize> builder = new ArrayList<ConfigMemorySize>();
|
||||
for (Long v : list) {
|
||||
builder.add(ConfigMemorySize.ofBytes(v));
|
||||
}
|
||||
return builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Long> getDurationList(String path, TimeUnit unit) {
|
||||
List<Long> l = new ArrayList<Long>();
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user