mirror of
https://github.com/lightbend/config.git
synced 2025-02-23 09:41:01 +08:00
Deprecating getMilliseconds* and getNanoseconds* and add getDuration*
This commit is contained in:
parent
0b1eb35900
commit
796f25acac
@ -6,6 +6,7 @@ package com.typesafe.config;
|
|||||||
import java.util.List;
|
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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An immutable map from config paths to config values.
|
* An immutable map from config paths to config values.
|
||||||
@ -440,6 +441,8 @@ public interface Config extends ConfigMergeable {
|
|||||||
* href="https://github.com/typesafehub/config/blob/master/HOCON.md">the
|
* href="https://github.com/typesafehub/config/blob/master/HOCON.md">the
|
||||||
* spec</a>.
|
* spec</a>.
|
||||||
*
|
*
|
||||||
|
* @deprecated As of release 1.1, replaced by {@link #getDuration(String, TimeUnit)}
|
||||||
|
*
|
||||||
* @param path
|
* @param path
|
||||||
* path expression
|
* path expression
|
||||||
* @return the duration value at the requested path, in milliseconds
|
* @return the duration value at the requested path, in milliseconds
|
||||||
@ -450,13 +453,15 @@ public interface Config extends ConfigMergeable {
|
|||||||
* @throws ConfigException.BadValue
|
* @throws ConfigException.BadValue
|
||||||
* if value cannot be parsed as a number of milliseconds
|
* if value cannot be parsed as a number of milliseconds
|
||||||
*/
|
*/
|
||||||
Long getMilliseconds(String path);
|
@Deprecated Long getMilliseconds(String path);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get value as a duration in nanoseconds. If the value is already a number
|
* Get value as a duration in nanoseconds. If the value is already a number
|
||||||
* it's taken as milliseconds and converted to nanoseconds. If it's a
|
* it's taken as milliseconds and converted to nanoseconds. If it's a
|
||||||
* string, it's parsed understanding unit suffixes, as for
|
* string, it's parsed understanding unit suffixes, as for
|
||||||
* {@link #getMilliseconds(String)}.
|
* {@link #getDuration(String, TimeUnit)}.
|
||||||
|
*
|
||||||
|
* @deprecated As of release 1.1, replaced by {@link #getDuration(String, TimeUnit)}
|
||||||
*
|
*
|
||||||
* @param path
|
* @param path
|
||||||
* path expression
|
* path expression
|
||||||
@ -468,7 +473,29 @@ public interface Config extends ConfigMergeable {
|
|||||||
* @throws ConfigException.BadValue
|
* @throws ConfigException.BadValue
|
||||||
* if value cannot be parsed as a number of nanoseconds
|
* if value cannot be parsed as a number of nanoseconds
|
||||||
*/
|
*/
|
||||||
Long getNanoseconds(String path);
|
@Deprecated Long getNanoseconds(String path);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get value as a duration in a specified TimeUnit. Naturally the precision will depend on the configured value.
|
||||||
|
* If the value is already a
|
||||||
|
* number, then it's interpreted to be in Milliseconds and then be converted to the requested TimeUnit;
|
||||||
|
* if it's a string, it's parsed understanding units suffixes like "10m" or "5ns" as documented in the <a
|
||||||
|
* href="https://github.com/typesafehub/config/blob/master/HOCON.md">the
|
||||||
|
* spec</a>.
|
||||||
|
*
|
||||||
|
* @param path
|
||||||
|
* path expression
|
||||||
|
* @param unit
|
||||||
|
* The TimeUnit in which the returned long should be
|
||||||
|
* @return the duration value at the requested path, in the given TimeUnit
|
||||||
|
* @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 number of the given TimeUnit
|
||||||
|
*/
|
||||||
|
Long getDuration(String path, TimeUnit unit);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a list value (with any element type) as a {@link ConfigList}, which
|
* Gets a list value (with any element type) as a {@link ConfigList}, which
|
||||||
@ -505,9 +532,17 @@ public interface Config extends ConfigMergeable {
|
|||||||
|
|
||||||
List<Long> getBytesList(String path);
|
List<Long> getBytesList(String path);
|
||||||
|
|
||||||
List<Long> getMillisecondsList(String path);
|
/**
|
||||||
|
* @deprecated As of release 1.1, replaced by {@link #getDurationList(String, TimeUnit)}
|
||||||
|
*/
|
||||||
|
@Deprecated List<Long> getMillisecondsList(String path);
|
||||||
|
|
||||||
List<Long> getNanosecondsList(String path);
|
/**
|
||||||
|
* @deprecated As of release 1.1, replaced by {@link #getDurationList(String, TimeUnit)}
|
||||||
|
*/
|
||||||
|
@Deprecated List<Long> getNanosecondsList(String path);
|
||||||
|
|
||||||
|
List<Long> getDurationList(String path, TimeUnit unit);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clone the config with only the given path (and its children) retained;
|
* Clone the config with only the given path (and its children) retained;
|
||||||
|
@ -234,23 +234,30 @@ final class SimpleConfig implements Config, MergeableValue, Serializable {
|
|||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
@Override
|
@Override
|
||||||
public Long getMilliseconds(String path) {
|
public Long getMilliseconds(String path) {
|
||||||
long ns = getNanoseconds(path);
|
return getDuration(path, TimeUnit.MILLISECONDS);
|
||||||
long ms = TimeUnit.NANOSECONDS.toMillis(ns);
|
}
|
||||||
return ms;
|
|
||||||
|
@Deprecated
|
||||||
|
@Override
|
||||||
|
public Long getNanoseconds(String path) {
|
||||||
|
return getDuration(path, TimeUnit.NANOSECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Long getNanoseconds(String path) {
|
public Long getDuration(String path, TimeUnit unit) {
|
||||||
Long ns = null;
|
Long result = null;
|
||||||
try {
|
try {
|
||||||
ns = TimeUnit.MILLISECONDS.toNanos(getLong(path));
|
result = unit.convert(getLong(path), TimeUnit.MILLISECONDS);
|
||||||
} catch (ConfigException.WrongType e) {
|
} catch (ConfigException.WrongType e) {
|
||||||
ConfigValue v = find(path, ConfigValueType.STRING);
|
ConfigValue v = find(path, ConfigValueType.STRING);
|
||||||
ns = parseDuration((String) v.unwrapped(), v.origin(), path);
|
result = unit.convert(
|
||||||
|
parseDuration((String) v.unwrapped(), v.origin(), path),
|
||||||
|
TimeUnit.NANOSECONDS);
|
||||||
}
|
}
|
||||||
return ns;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@ -384,36 +391,42 @@ final class SimpleConfig implements Config, MergeableValue, Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Long> getMillisecondsList(String path) {
|
public List<Long> getDurationList(String path, TimeUnit unit) {
|
||||||
List<Long> nanos = getNanosecondsList(path);
|
|
||||||
List<Long> l = new ArrayList<Long>();
|
|
||||||
for (Long n : nanos) {
|
|
||||||
l.add(TimeUnit.NANOSECONDS.toMillis(n));
|
|
||||||
}
|
|
||||||
return l;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<Long> getNanosecondsList(String path) {
|
|
||||||
List<Long> l = new ArrayList<Long>();
|
List<Long> l = new ArrayList<Long>();
|
||||||
List<? extends ConfigValue> list = getList(path);
|
List<? extends ConfigValue> list = getList(path);
|
||||||
for (ConfigValue v : list) {
|
for (ConfigValue v : list) {
|
||||||
if (v.valueType() == ConfigValueType.NUMBER) {
|
if (v.valueType() == ConfigValueType.NUMBER) {
|
||||||
l.add(TimeUnit.MILLISECONDS.toNanos(((Number) v.unwrapped())
|
Long n = unit.convert(
|
||||||
.longValue()));
|
((Number) v.unwrapped()).longValue(),
|
||||||
|
TimeUnit.MILLISECONDS);
|
||||||
|
l.add(n);
|
||||||
} else if (v.valueType() == ConfigValueType.STRING) {
|
} else if (v.valueType() == ConfigValueType.STRING) {
|
||||||
String s = (String) v.unwrapped();
|
String s = (String) v.unwrapped();
|
||||||
Long n = parseDuration(s, v.origin(), path);
|
Long n = unit.convert(
|
||||||
|
parseDuration(s, v.origin(), path),
|
||||||
|
TimeUnit.NANOSECONDS);
|
||||||
l.add(n);
|
l.add(n);
|
||||||
} else {
|
} else {
|
||||||
throw new ConfigException.WrongType(v.origin(), path,
|
throw new ConfigException.WrongType(v.origin(), path,
|
||||||
"duration string or number of nanoseconds", v
|
"duration string or number of " + unit.name(),
|
||||||
.valueType().name());
|
v.valueType().name());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
|
@Override
|
||||||
|
public List<Long> getMillisecondsList(String path) {
|
||||||
|
return getDurationList(path, TimeUnit.MILLISECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
|
@Override
|
||||||
|
public List<Long> getNanosecondsList(String path) {
|
||||||
|
return getDurationList(path, TimeUnit.NANOSECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AbstractConfigObject toFallbackValue() {
|
public AbstractConfigObject toFallbackValue() {
|
||||||
return object;
|
return object;
|
||||||
|
@ -13,6 +13,7 @@ import java.util.concurrent.TimeUnit
|
|||||||
import scala.collection.JavaConverters._
|
import scala.collection.JavaConverters._
|
||||||
import com.typesafe.config.ConfigResolveOptions
|
import com.typesafe.config.ConfigResolveOptions
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.util.concurrent.TimeUnit.{ SECONDS, NANOSECONDS, MICROSECONDS, MILLISECONDS, MINUTES, DAYS, HOURS }
|
||||||
import com.typesafe.config.ConfigParseOptions
|
import com.typesafe.config.ConfigParseOptions
|
||||||
import com.typesafe.config.ConfigFactory
|
import com.typesafe.config.ConfigFactory
|
||||||
import com.typesafe.config.ConfigMergeable
|
import com.typesafe.config.ConfigMergeable
|
||||||
@ -736,6 +737,28 @@ class ConfigTest extends TestUtils {
|
|||||||
conf.getNanosecondsList("durations.secondsList").asScala)
|
conf.getNanosecondsList("durations.secondsList").asScala)
|
||||||
assertEquals(500L, conf.getMilliseconds("durations.halfSecond"))
|
assertEquals(500L, conf.getMilliseconds("durations.halfSecond"))
|
||||||
|
|
||||||
|
def assertDurationAsTimeUnit(unit: TimeUnit): Unit = {
|
||||||
|
def ms2unit(l: Long) = unit.convert(l, MILLISECONDS)
|
||||||
|
def s2unit(i: Int) = unit.convert(i, SECONDS)
|
||||||
|
assertEquals(ms2unit(1000L), conf.getDuration("durations.second", unit))
|
||||||
|
assertEquals(s2unit(1), conf.getDuration("durations.second", unit))
|
||||||
|
assertEquals(ms2unit(1000L), conf.getDuration("durations.secondAsNumber", unit))
|
||||||
|
assertEquals(s2unit(1), conf.getDuration("durations.secondAsNumber", unit))
|
||||||
|
assertEquals(Seq(1000L, 2000L, 3000L, 4000L) map ms2unit,
|
||||||
|
conf.getDurationList("durations.secondsList", unit).asScala)
|
||||||
|
assertEquals(Seq(1, 2, 3, 4) map s2unit,
|
||||||
|
conf.getDurationList("durations.secondsList", unit).asScala)
|
||||||
|
assertEquals(ms2unit(500L), conf.getDuration("durations.halfSecond", unit))
|
||||||
|
}
|
||||||
|
|
||||||
|
assertDurationAsTimeUnit(NANOSECONDS)
|
||||||
|
assertDurationAsTimeUnit(MICROSECONDS)
|
||||||
|
assertDurationAsTimeUnit(MILLISECONDS)
|
||||||
|
assertDurationAsTimeUnit(SECONDS)
|
||||||
|
assertDurationAsTimeUnit(MINUTES)
|
||||||
|
assertDurationAsTimeUnit(HOURS)
|
||||||
|
assertDurationAsTimeUnit(DAYS)
|
||||||
|
|
||||||
// should get size in bytes
|
// should get size in bytes
|
||||||
assertEquals(1024 * 1024L, conf.getBytes("memsizes.meg"))
|
assertEquals(1024 * 1024L, conf.getBytes("memsizes.meg"))
|
||||||
assertEquals(1024 * 1024L, conf.getBytes("memsizes.megAsNumber"))
|
assertEquals(1024 * 1024L, conf.getBytes("memsizes.megAsNumber"))
|
||||||
|
Loading…
Reference in New Issue
Block a user