Deprecating getMilliseconds* and getNanoseconds* and add getDuration*

This commit is contained in:
Viktor Klang 2013-02-19 20:36:46 +01:00 committed by Havoc Pennington
parent 0b1eb35900
commit 796f25acac
3 changed files with 100 additions and 29 deletions

View File

@ -6,6 +6,7 @@ package com.typesafe.config;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* 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
* spec</a>.
*
* @deprecated As of release 1.1, replaced by {@link #getDuration(String, TimeUnit)}
*
* @param path
* path expression
* @return the duration value at the requested path, in milliseconds
@ -450,13 +453,15 @@ public interface Config extends ConfigMergeable {
* @throws ConfigException.BadValue
* 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
* it's taken as milliseconds and converted to nanoseconds. If it's a
* 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
* path expression
@ -468,7 +473,29 @@ public interface Config extends ConfigMergeable {
* @throws ConfigException.BadValue
* 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
@ -505,9 +532,17 @@ public interface Config extends ConfigMergeable {
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;

View File

@ -234,23 +234,30 @@ final class SimpleConfig implements Config, MergeableValue, Serializable {
return size;
}
@Deprecated
@Override
public Long getMilliseconds(String path) {
long ns = getNanoseconds(path);
long ms = TimeUnit.NANOSECONDS.toMillis(ns);
return ms;
return getDuration(path, TimeUnit.MILLISECONDS);
}
@Deprecated
@Override
public Long getNanoseconds(String path) {
return getDuration(path, TimeUnit.NANOSECONDS);
}
@Override
public Long getNanoseconds(String path) {
Long ns = null;
public Long getDuration(String path, TimeUnit unit) {
Long result = null;
try {
ns = TimeUnit.MILLISECONDS.toNanos(getLong(path));
result = unit.convert(getLong(path), TimeUnit.MILLISECONDS);
} catch (ConfigException.WrongType e) {
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")
@ -384,36 +391,42 @@ final class SimpleConfig implements Config, MergeableValue, Serializable {
}
@Override
public List<Long> getMillisecondsList(String path) {
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) {
public List<Long> getDurationList(String path, TimeUnit unit) {
List<Long> l = new ArrayList<Long>();
List<? extends ConfigValue> list = getList(path);
for (ConfigValue v : list) {
if (v.valueType() == ConfigValueType.NUMBER) {
l.add(TimeUnit.MILLISECONDS.toNanos(((Number) v.unwrapped())
.longValue()));
Long n = unit.convert(
((Number) v.unwrapped()).longValue(),
TimeUnit.MILLISECONDS);
l.add(n);
} else if (v.valueType() == ConfigValueType.STRING) {
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);
} else {
throw new ConfigException.WrongType(v.origin(), path,
"duration string or number of nanoseconds", v
.valueType().name());
"duration string or number of " + unit.name(),
v.valueType().name());
}
}
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
public AbstractConfigObject toFallbackValue() {
return object;

View File

@ -13,6 +13,7 @@ import java.util.concurrent.TimeUnit
import scala.collection.JavaConverters._
import com.typesafe.config.ConfigResolveOptions
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.ConfigFactory
import com.typesafe.config.ConfigMergeable
@ -736,6 +737,28 @@ class ConfigTest extends TestUtils {
conf.getNanosecondsList("durations.secondsList").asScala)
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
assertEquals(1024 * 1024L, conf.getBytes("memsizes.meg"))
assertEquals(1024 * 1024L, conf.getBytes("memsizes.megAsNumber"))