mirror of
https://github.com/lightbend/config.git
synced 2025-01-15 23:01:05 +08:00
change getMemorySize to getMemorySizeInBytes
Preserves the memory size vs. disk size distinction (powers of two vs. ten) But specifies that it will be in bytes, for consistency with getMilliseconds and clarity in general.
This commit is contained in:
parent
598c5d045f
commit
68a22668bd
@ -247,8 +247,10 @@ public final class Config {
|
||||
* Parses a memory-size string. If no units are specified in the string, it
|
||||
* is assumed to be in bytes. The returned value is in bytes. The purpose of
|
||||
* this function is to implement the memory-size-related methods in the
|
||||
* ConfigObject interface.
|
||||
*
|
||||
* 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
|
||||
@ -259,7 +261,7 @@ public final class Config {
|
||||
* @throws ConfigException
|
||||
* if string is invalid
|
||||
*/
|
||||
public static long parseMemorySize(String input,
|
||||
public static long parseMemorySizeInBytes(String input,
|
||||
ConfigOrigin originForException, String pathForException) {
|
||||
String s = ConfigUtil.unicodeTrim(input);
|
||||
String unitStringMaybePlural = getUnits(s);
|
||||
|
@ -68,10 +68,12 @@ public interface ConfigObject extends ConfigValue, Map<String, ConfigValue> {
|
||||
*/
|
||||
ConfigValue getValue(String path);
|
||||
|
||||
/** Get value as a size in bytes (parses special strings like "128M") */
|
||||
// rename getSizeInBytes ? clearer. allows a megabyte version
|
||||
// or just getBytes is consistent with getMilliseconds
|
||||
Long getMemorySize(String path);
|
||||
/**
|
||||
* Get value as a size in bytes (parses special strings like "128M"). The
|
||||
* size units are interpreted as for memory, not as for disk space, so they
|
||||
* are in powers of two.
|
||||
*/
|
||||
Long getMemorySizeInBytes(String path);
|
||||
|
||||
/**
|
||||
* Get value as a duration in milliseconds. If the value is already a
|
||||
@ -114,7 +116,7 @@ public interface ConfigObject extends ConfigValue, Map<String, ConfigValue> {
|
||||
|
||||
List<? extends Object> getAnyRefList(String path);
|
||||
|
||||
List<Long> getMemorySizeList(String path);
|
||||
List<Long> getMemorySizeInBytesList(String path);
|
||||
|
||||
List<Long> getMillisecondsList(String path);
|
||||
|
||||
|
@ -414,13 +414,13 @@ abstract class AbstractConfigObject extends AbstractConfigValue implements
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getMemorySize(String path) {
|
||||
public Long getMemorySizeInBytes(String path) {
|
||||
Long size = null;
|
||||
try {
|
||||
size = getLong(path);
|
||||
} catch (ConfigException.WrongType e) {
|
||||
ConfigValue v = find(path, ConfigValueType.STRING, path);
|
||||
size = Config.parseMemorySize((String) v.unwrapped(), v.origin(),
|
||||
size = Config.parseMemorySizeInBytes((String) v.unwrapped(), v.origin(),
|
||||
path);
|
||||
}
|
||||
return size;
|
||||
@ -534,7 +534,7 @@ abstract class AbstractConfigObject extends AbstractConfigValue implements
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Long> getMemorySizeList(String path) {
|
||||
public List<Long> getMemorySizeInBytesList(String path) {
|
||||
List<Long> l = new ArrayList<Long>();
|
||||
List<? extends ConfigValue> list = getList(path);
|
||||
for (ConfigValue v : list) {
|
||||
@ -542,7 +542,7 @@ abstract class AbstractConfigObject extends AbstractConfigValue implements
|
||||
l.add(((Number) v.unwrapped()).longValue());
|
||||
} else if (v.valueType() == ConfigValueType.STRING) {
|
||||
String s = (String) v.unwrapped();
|
||||
Long n = Config.parseMemorySize(s, v.origin(), path);
|
||||
Long n = Config.parseMemorySizeInBytes(s, v.origin(), path);
|
||||
l.add(n);
|
||||
} else {
|
||||
throw new ConfigException.WrongType(v.origin(), path,
|
||||
|
@ -465,7 +465,7 @@ class ConfigTest extends TestUtils {
|
||||
}
|
||||
|
||||
intercept[ConfigException.Null] {
|
||||
conf.getMemorySize("nulls.null")
|
||||
conf.getMemorySizeInBytes("nulls.null")
|
||||
}
|
||||
|
||||
// should throw WrongType if key is wrong type and not convertible
|
||||
@ -494,7 +494,7 @@ class ConfigTest extends TestUtils {
|
||||
}
|
||||
|
||||
intercept[ConfigException.WrongType] {
|
||||
conf.getMemorySize("ints")
|
||||
conf.getMemorySizeInBytes("ints")
|
||||
}
|
||||
|
||||
// should throw BadPath on various bad paths
|
||||
@ -521,7 +521,7 @@ class ConfigTest extends TestUtils {
|
||||
}
|
||||
|
||||
intercept[ConfigException.BadValue] {
|
||||
conf.getMemorySize("strings.a")
|
||||
conf.getMemorySizeInBytes("strings.a")
|
||||
}
|
||||
}
|
||||
|
||||
@ -581,11 +581,11 @@ class ConfigTest extends TestUtils {
|
||||
assertEquals(500L, conf.getMilliseconds("durations.halfSecond"))
|
||||
|
||||
// should get size in bytes
|
||||
assertEquals(1024 * 1024L, conf.getMemorySize("memsizes.meg"))
|
||||
assertEquals(1024 * 1024L, conf.getMemorySize("memsizes.megAsNumber"))
|
||||
assertEquals(1024 * 1024L, conf.getMemorySizeInBytes("memsizes.meg"))
|
||||
assertEquals(1024 * 1024L, conf.getMemorySizeInBytes("memsizes.megAsNumber"))
|
||||
assertEquals(Seq(1024 * 1024L, 1024 * 1024L, 1024L * 1024L),
|
||||
conf.getMemorySizeList("memsizes.megsList").asScala)
|
||||
assertEquals(512 * 1024L, conf.getMemorySize("memsizes.halfMeg"))
|
||||
conf.getMemorySizeInBytesList("memsizes.megsList").asScala)
|
||||
assertEquals(512 * 1024L, conf.getMemorySizeInBytes("memsizes.halfMeg"))
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -38,7 +38,7 @@ class UnitParserTest extends TestUtils {
|
||||
}
|
||||
|
||||
@Test
|
||||
def parseMemorySize() {
|
||||
def parseMemorySizeInBytes() {
|
||||
val oneMegs = List("1048576", "1048576b", "1048576bytes", "1048576byte",
|
||||
"1048576 b", "1048576 bytes",
|
||||
" 1048576 b ", " 1048576 bytes ",
|
||||
@ -47,7 +47,7 @@ class UnitParserTest extends TestUtils {
|
||||
"1m", "1M", "1 M", "1 megabytes", "1 megabyte",
|
||||
"0.0009765625g", "0.0009765625G", "0.0009765625 gigabytes", "0.0009765625 gigabyte")
|
||||
|
||||
def parseMem(s: String) = Config.parseMemorySize(s, fakeOrigin(), "test")
|
||||
def parseMem(s: String) = Config.parseMemorySizeInBytes(s, fakeOrigin(), "test")
|
||||
|
||||
for (s <- oneMegs) {
|
||||
val result = parseMem(s)
|
||||
@ -61,13 +61,13 @@ class UnitParserTest extends TestUtils {
|
||||
|
||||
// bad units
|
||||
val e = intercept[ConfigException.BadValue] {
|
||||
Config.parseMemorySize("100 dollars", fakeOrigin(), "test")
|
||||
Config.parseMemorySizeInBytes("100 dollars", fakeOrigin(), "test")
|
||||
}
|
||||
assertTrue(e.getMessage().contains("size unit"))
|
||||
|
||||
// bad number
|
||||
val e2 = intercept[ConfigException.BadValue] {
|
||||
Config.parseMemorySize("1 00 bytes", fakeOrigin(), "test")
|
||||
Config.parseMemorySizeInBytes("1 00 bytes", fakeOrigin(), "test")
|
||||
}
|
||||
assertTrue(e2.getMessage().contains("size number"))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user