mirror of
https://github.com/lightbend/config.git
synced 2025-03-13 18:50:45 +08:00
Code review improvements
This commit is contained in:
parent
db6b6dcaf6
commit
179e6d647a
@ -46,19 +46,26 @@ public final class ConfigMemorySize {
|
|||||||
/**
|
/**
|
||||||
* Gets the size in bytes.
|
* Gets the size in bytes.
|
||||||
*
|
*
|
||||||
* @deprecated use {@link #getBytes()} to handle to bytes conversion of huge memory units.
|
|
||||||
* @since 1.3.0
|
* @since 1.3.0
|
||||||
* @return how many bytes
|
* @return how many bytes
|
||||||
*/
|
*/
|
||||||
public long toBytes() {
|
public long toBytes() {
|
||||||
return bytes.longValueExact();
|
if (bytes.bitLength() < 64)
|
||||||
|
return bytes.longValue();
|
||||||
|
else
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"size-in-bytes value is out of range for a 64-bit long: '" + bytes + "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the size in bytes.
|
* Gets the size in bytes. The behavior of this method
|
||||||
|
* is the same as that of the {@link #toBytes()} method,
|
||||||
|
* except that the number of bytes returned as a
|
||||||
|
* BigInteger value. Use it when memory value in bytes
|
||||||
|
* doesn't fit in a long value.
|
||||||
* @return how many bytes
|
* @return how many bytes
|
||||||
*/
|
*/
|
||||||
public BigInteger getBytes() {
|
public BigInteger toBytesBigInteger() {
|
||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import com.typesafe.config.Config;
|
import com.typesafe.config.Config;
|
||||||
import com.typesafe.config.ConfigException;
|
import com.typesafe.config.ConfigException;
|
||||||
@ -282,15 +283,7 @@ final class SimpleConfig implements Config, MergeableValue, Serializable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Long getBytes(String path) {
|
public Long getBytes(String path) {
|
||||||
Long size = null;
|
return getMemorySize(path).toBytes();
|
||||||
try {
|
|
||||||
size = getLong(path);
|
|
||||||
} catch (ConfigException.WrongType e) {
|
|
||||||
ConfigValue v = find(path, ConfigValueType.STRING);
|
|
||||||
size = parseBytes((String) v.unwrapped(),
|
|
||||||
v.origin(), path);
|
|
||||||
}
|
|
||||||
return size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -490,22 +483,9 @@ final class SimpleConfig implements Config, MergeableValue, Serializable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Long> getBytesList(String path) {
|
public List<Long> getBytesList(String path) {
|
||||||
List<Long> l = new ArrayList<Long>();
|
return getMemorySizeList(path).stream()
|
||||||
List<? extends ConfigValue> list = getList(path);
|
.map(ConfigMemorySize::toBytes)
|
||||||
for (ConfigValue v : list) {
|
.collect(Collectors.toList());
|
||||||
if (v.valueType() == ConfigValueType.NUMBER) {
|
|
||||||
l.add(((Number) v.unwrapped()).longValue());
|
|
||||||
} else if (v.valueType() == ConfigValueType.STRING) {
|
|
||||||
String s = (String) v.unwrapped();
|
|
||||||
Long n = parseBytes(s, v.origin(), path);
|
|
||||||
l.add(n);
|
|
||||||
} else {
|
|
||||||
throw new ConfigException.WrongType(v.origin(), path,
|
|
||||||
"memory size string or number of bytes", v.valueType()
|
|
||||||
.name());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return l;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -868,45 +848,12 @@ final class SimpleConfig implements Config, MergeableValue, Serializable {
|
|||||||
*/
|
*/
|
||||||
public static long parseBytes(String input, ConfigOrigin originForException,
|
public static long parseBytes(String input, ConfigOrigin originForException,
|
||||||
String pathForException) {
|
String pathForException) {
|
||||||
String s = ConfigImplUtil.unicodeTrim(input);
|
BigInteger result = parseBytesAsBigInteger(input, originForException, pathForException);
|
||||||
String unitString = getUnits(s);
|
if (result.bitLength() < 64)
|
||||||
String numberString = ConfigImplUtil.unicodeTrim(s.substring(0,
|
return result.longValue();
|
||||||
s.length() - unitString.length()));
|
else
|
||||||
|
|
||||||
// this would be caught later anyway, but the error message
|
|
||||||
// is more helpful if we check it here.
|
|
||||||
if (numberString.length() == 0)
|
|
||||||
throw new ConfigException.BadValue(originForException,
|
|
||||||
pathForException, "No number in size-in-bytes value '"
|
|
||||||
+ input + "'");
|
|
||||||
|
|
||||||
MemoryUnit units = MemoryUnit.parseUnit(unitString);
|
|
||||||
|
|
||||||
if (units == null) {
|
|
||||||
throw new ConfigException.BadValue(originForException, pathForException,
|
throw new ConfigException.BadValue(originForException, pathForException,
|
||||||
"Could not parse size-in-bytes unit '" + unitString
|
"size-in-bytes value is out of range for a 64-bit long: '" + input + "'");
|
||||||
+ "' (try k, K, kB, KiB, kilobytes, kibibytes)");
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
BigInteger result;
|
|
||||||
// if the string is purely digits, parse as an integer to avoid
|
|
||||||
// possible precision loss; otherwise as a double.
|
|
||||||
if (numberString.matches("[0-9]+")) {
|
|
||||||
result = units.bytes.multiply(new BigInteger(numberString));
|
|
||||||
} else {
|
|
||||||
BigDecimal resultDecimal = (new BigDecimal(units.bytes)).multiply(new BigDecimal(numberString));
|
|
||||||
result = resultDecimal.toBigInteger();
|
|
||||||
}
|
|
||||||
if (result.bitLength() < 64)
|
|
||||||
return result.longValue();
|
|
||||||
else
|
|
||||||
throw new ConfigException.BadValue(originForException, pathForException,
|
|
||||||
"size-in-bytes value is out of range for a 64-bit long: '" + input + "'");
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
throw new ConfigException.BadValue(originForException, pathForException,
|
|
||||||
"Could not parse size-in-bytes number '" + numberString + "'");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -28,6 +28,6 @@ class ConfigMemorySizeTest extends TestUtils {
|
|||||||
@Test
|
@Test
|
||||||
def testGetBytes() {
|
def testGetBytes() {
|
||||||
val yottabyte = ConfigMemorySize.ofBytes(new BigInteger("1000000000000000000000000"))
|
val yottabyte = ConfigMemorySize.ofBytes(new BigInteger("1000000000000000000000000"))
|
||||||
assertEquals(new BigInteger("1000000000000000000000000"), yottabyte.getBytes)
|
assertEquals(new BigInteger("1000000000000000000000000"), yottabyte.toBytesBigInteger)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -833,9 +833,9 @@ class ConfigTest extends TestUtils {
|
|||||||
conf.getMemorySizeList("memsizes.megsList").asScala.map(_.toBytes))
|
conf.getMemorySizeList("memsizes.megsList").asScala.map(_.toBytes))
|
||||||
assertEquals(512 * 1024L, conf.getMemorySize("memsizes.halfMeg").toBytes)
|
assertEquals(512 * 1024L, conf.getMemorySize("memsizes.halfMeg").toBytes)
|
||||||
|
|
||||||
assertEquals(new BigInteger("1000000000000000000000000"), conf.getMemorySize("memsizes.yottabyte").getBytes)
|
assertEquals(new BigInteger("1000000000000000000000000"), conf.getMemorySize("memsizes.yottabyte").toBytesBigInteger)
|
||||||
assertEquals(Seq(new BigInteger("1000000000000000000000000"), new BigInteger("500000000000000000000000")),
|
assertEquals(Seq(new BigInteger("1000000000000000000000000"), new BigInteger("500000000000000000000000")),
|
||||||
conf.getMemorySizeList("memsizes.yottabyteList").asScala.map(_.getBytes))
|
conf.getMemorySizeList("memsizes.yottabyteList").asScala.map(_.toBytesBigInteger))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
Reference in New Issue
Block a user