mirror of
https://github.com/lightbend/config.git
synced 2025-02-06 09:30:07 +08:00
Merge pull request #256 from typesafehub/config-memory-size
add ConfigMemorySize and getters for it
This commit is contained in:
commit
cd634decc5
@ -585,6 +585,28 @@ public interface Config extends ConfigMergeable {
|
|||||||
*/
|
*/
|
||||||
Long getBytes(String path);
|
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>.
|
||||||
|
*
|
||||||
|
* @since 1.3.0
|
||||||
|
*
|
||||||
|
* @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
|
* 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
|
* number, then it's left alone; if it's a string, it's parsed understanding
|
||||||
@ -686,6 +708,17 @@ public interface Config extends ConfigMergeable {
|
|||||||
|
|
||||||
List<Long> getBytesList(String path);
|
List<Long> getBytesList(String path);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a list, converting each value in the list to a memory size, using the
|
||||||
|
* same rules as {@link #getMemorySize(String)}.
|
||||||
|
*
|
||||||
|
* @since 1.3.0
|
||||||
|
* @param path
|
||||||
|
* a path expression
|
||||||
|
* @return list of memory sizes
|
||||||
|
*/
|
||||||
|
List<ConfigMemorySize> getMemorySizeList(String path);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated As of release 1.1, replaced by {@link #getDurationList(String, TimeUnit)}
|
* @deprecated As of release 1.1, replaced by {@link #getDurationList(String, TimeUnit)}
|
||||||
*/
|
*/
|
||||||
|
@ -0,0 +1,60 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (C) 2015 Typesafe Inc. <http://typesafe.com>
|
||||||
|
*/
|
||||||
|
package com.typesafe.config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An immutable class representing an amount of memory. Use
|
||||||
|
* static factory methods such as {@link
|
||||||
|
* ConfigMemorySize#ofBytes(long)} to create instances.
|
||||||
|
*
|
||||||
|
* @since 1.3.0
|
||||||
|
*/
|
||||||
|
public final class ConfigMemorySize {
|
||||||
|
private final long bytes;
|
||||||
|
|
||||||
|
private ConfigMemorySize(long bytes) {
|
||||||
|
if (bytes < 0)
|
||||||
|
throw new IllegalArgumentException("Attempt to construct ConfigMemorySize with negative number: " + bytes);
|
||||||
|
this.bytes = bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a ConfigMemorySize representing the given
|
||||||
|
* number of bytes.
|
||||||
|
* @since 1.3.0
|
||||||
|
*/
|
||||||
|
public static ConfigMemorySize ofBytes(long bytes) {
|
||||||
|
return new ConfigMemorySize(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the size in bytes.
|
||||||
|
* @since 1.3.0
|
||||||
|
*/
|
||||||
|
public long toBytes() {
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "ConfigMemorySize(" + bytes + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object other) {
|
||||||
|
if (other instanceof ConfigMemorySize) {
|
||||||
|
return ((ConfigMemorySize)other).bytes == this.bytes;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
// in Java 8 this can become Long.hashCode(bytes)
|
||||||
|
return Long.valueOf(bytes).hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -18,6 +18,7 @@ import java.util.concurrent.Callable;
|
|||||||
import com.typesafe.config.Config;
|
import com.typesafe.config.Config;
|
||||||
import com.typesafe.config.ConfigException;
|
import com.typesafe.config.ConfigException;
|
||||||
import com.typesafe.config.ConfigIncluder;
|
import com.typesafe.config.ConfigIncluder;
|
||||||
|
import com.typesafe.config.ConfigMemorySize;
|
||||||
import com.typesafe.config.ConfigObject;
|
import com.typesafe.config.ConfigObject;
|
||||||
import com.typesafe.config.ConfigOrigin;
|
import com.typesafe.config.ConfigOrigin;
|
||||||
import com.typesafe.config.ConfigParseOptions;
|
import com.typesafe.config.ConfigParseOptions;
|
||||||
@ -273,6 +274,8 @@ public class ConfigImpl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return new SimpleConfigList(origin, values);
|
return new SimpleConfigList(origin, values);
|
||||||
|
} else if (object instanceof ConfigMemorySize) {
|
||||||
|
return new ConfigLong(origin, ((ConfigMemorySize) object).toBytes(), null);
|
||||||
} else {
|
} else {
|
||||||
throw new ConfigException.BugOrBroken(
|
throw new ConfigException.BugOrBroken(
|
||||||
"bug in method caller: not valid to create ConfigValue from: "
|
"bug in method caller: not valid to create ConfigValue from: "
|
||||||
|
@ -19,6 +19,7 @@ import java.util.concurrent.TimeUnit;
|
|||||||
import com.typesafe.config.Config;
|
import com.typesafe.config.Config;
|
||||||
import com.typesafe.config.ConfigException;
|
import com.typesafe.config.ConfigException;
|
||||||
import com.typesafe.config.ConfigList;
|
import com.typesafe.config.ConfigList;
|
||||||
|
import com.typesafe.config.ConfigMemorySize;
|
||||||
import com.typesafe.config.ConfigMergeable;
|
import com.typesafe.config.ConfigMergeable;
|
||||||
import com.typesafe.config.ConfigObject;
|
import com.typesafe.config.ConfigObject;
|
||||||
import com.typesafe.config.ConfigOrigin;
|
import com.typesafe.config.ConfigOrigin;
|
||||||
@ -245,6 +246,11 @@ final class SimpleConfig implements Config, MergeableValue, Serializable {
|
|||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ConfigMemorySize getMemorySize(String path) {
|
||||||
|
return ConfigMemorySize.ofBytes(getBytes(path));
|
||||||
|
}
|
||||||
|
|
||||||
@Deprecated
|
@Deprecated
|
||||||
@Override
|
@Override
|
||||||
public Long getMilliseconds(String path) {
|
public Long getMilliseconds(String path) {
|
||||||
@ -396,6 +402,16 @@ final class SimpleConfig implements Config, MergeableValue, Serializable {
|
|||||||
return l;
|
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
|
@Override
|
||||||
public List<Long> getDurationList(String path, TimeUnit unit) {
|
public List<Long> getDurationList(String path, TimeUnit unit) {
|
||||||
List<Long> l = new ArrayList<Long>();
|
List<Long> l = new ArrayList<Long>();
|
||||||
|
@ -0,0 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (C) 2015 Typesafe Inc. <http://typesafe.com>
|
||||||
|
*/
|
||||||
|
package com.typesafe.config.impl
|
||||||
|
|
||||||
|
import org.junit.Assert._
|
||||||
|
import org.junit._
|
||||||
|
import com.typesafe.config.ConfigMemorySize
|
||||||
|
|
||||||
|
class ConfigMemorySizeTest extends TestUtils {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
def testEquals() {
|
||||||
|
assertTrue("Equal ConfigMemorySize are equal",
|
||||||
|
ConfigMemorySize.ofBytes(10).equals(ConfigMemorySize.ofBytes(10)))
|
||||||
|
assertTrue("Different ConfigMemorySize are not equal",
|
||||||
|
!ConfigMemorySize.ofBytes(10).equals(ConfigMemorySize.ofBytes(11)))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
def testToUnits() {
|
||||||
|
val kilobyte = ConfigMemorySize.ofBytes(1024)
|
||||||
|
assertEquals(1024, kilobyte.toBytes)
|
||||||
|
}
|
||||||
|
}
|
@ -645,6 +645,10 @@ class ConfigTest extends TestUtils {
|
|||||||
conf.getBytes("nulls.null")
|
conf.getBytes("nulls.null")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
intercept[ConfigException.Null] {
|
||||||
|
conf.getMemorySize("nulls.null")
|
||||||
|
}
|
||||||
|
|
||||||
// should throw WrongType if key is wrong type and not convertible
|
// should throw WrongType if key is wrong type and not convertible
|
||||||
intercept[ConfigException.WrongType] {
|
intercept[ConfigException.WrongType] {
|
||||||
conf.getInt("booleans.trueAgain")
|
conf.getInt("booleans.trueAgain")
|
||||||
@ -674,6 +678,10 @@ class ConfigTest extends TestUtils {
|
|||||||
conf.getBytes("ints")
|
conf.getBytes("ints")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
intercept[ConfigException.WrongType] {
|
||||||
|
conf.getMemorySize("ints")
|
||||||
|
}
|
||||||
|
|
||||||
// should throw BadPath on various bad paths
|
// should throw BadPath on various bad paths
|
||||||
intercept[ConfigException.BadPath] {
|
intercept[ConfigException.BadPath] {
|
||||||
conf.getInt(".bad")
|
conf.getInt(".bad")
|
||||||
@ -700,6 +708,10 @@ class ConfigTest extends TestUtils {
|
|||||||
intercept[ConfigException.BadValue] {
|
intercept[ConfigException.BadValue] {
|
||||||
conf.getBytes("strings.a")
|
conf.getBytes("strings.a")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
intercept[ConfigException.BadValue] {
|
||||||
|
conf.getMemorySize("strings.a")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -787,6 +799,13 @@ class ConfigTest extends TestUtils {
|
|||||||
assertEquals(Seq(1024 * 1024L, 1024 * 1024L, 1024L * 1024L),
|
assertEquals(Seq(1024 * 1024L, 1024 * 1024L, 1024L * 1024L),
|
||||||
conf.getBytesList("memsizes.megsList").asScala)
|
conf.getBytesList("memsizes.megsList").asScala)
|
||||||
assertEquals(512 * 1024L, conf.getBytes("memsizes.halfMeg"))
|
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
|
@Test
|
||||||
|
@ -169,6 +169,12 @@ class PublicApiTest extends TestUtils {
|
|||||||
assertEquals("foo", ConfigValueFactory.fromIterable(treeSet, "foo").origin().description())
|
assertEquals("foo", ConfigValueFactory.fromIterable(treeSet, "foo").origin().description())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
def fromConfigMemorySize() {
|
||||||
|
testFromValue(longValue(1024), ConfigMemorySize.ofBytes(1024));
|
||||||
|
testFromValue(longValue(512), ConfigMemorySize.ofBytes(512));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def roundTripUnwrap() {
|
def roundTripUnwrap() {
|
||||||
val conf = ConfigFactory.load("test01")
|
val conf = ConfigFactory.load("test01")
|
||||||
|
Loading…
Reference in New Issue
Block a user