|
@@ -1805,6 +1805,7 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
|
|
* Return time duration in the given time unit. Valid units are encoded in
|
|
* Return time duration in the given time unit. Valid units are encoded in
|
|
* properties as suffixes: nanoseconds (ns), microseconds (us), milliseconds
|
|
* properties as suffixes: nanoseconds (ns), microseconds (us), milliseconds
|
|
* (ms), seconds (s), minutes (m), hours (h), and days (d).
|
|
* (ms), seconds (s), minutes (m), hours (h), and days (d).
|
|
|
|
+ *
|
|
* @param name Property name
|
|
* @param name Property name
|
|
* @param defaultValue Value returned if no mapping exists.
|
|
* @param defaultValue Value returned if no mapping exists.
|
|
* @param unit Unit to convert the stored property, if it exists.
|
|
* @param unit Unit to convert the stored property, if it exists.
|
|
@@ -1813,20 +1814,44 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
|
|
* @return time duration in given time unit
|
|
* @return time duration in given time unit
|
|
*/
|
|
*/
|
|
public long getTimeDuration(String name, long defaultValue, TimeUnit unit) {
|
|
public long getTimeDuration(String name, long defaultValue, TimeUnit unit) {
|
|
|
|
+ return getTimeDuration(name, defaultValue, unit, unit);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public long getTimeDuration(String name, String defaultValue, TimeUnit unit) {
|
|
|
|
+ return getTimeDuration(name, defaultValue, unit, unit);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Return time duration in the given time unit. Valid units are encoded in
|
|
|
|
+ * properties as suffixes: nanoseconds (ns), microseconds (us), milliseconds
|
|
|
|
+ * (ms), seconds (s), minutes (m), hours (h), and days (d). If no unit is
|
|
|
|
+ * provided, the default unit is applied.
|
|
|
|
+ *
|
|
|
|
+ * @param name Property name
|
|
|
|
+ * @param defaultValue Value returned if no mapping exists.
|
|
|
|
+ * @param defaultUnit Default time unit if no valid suffix is provided.
|
|
|
|
+ * @param returnUnit The unit used for the returned value.
|
|
|
|
+ * @throws NumberFormatException If the property stripped of its unit is not
|
|
|
|
+ * a number
|
|
|
|
+ * @return time duration in given time unit
|
|
|
|
+ */
|
|
|
|
+ public long getTimeDuration(String name, long defaultValue,
|
|
|
|
+ TimeUnit defaultUnit, TimeUnit returnUnit) {
|
|
String vStr = get(name);
|
|
String vStr = get(name);
|
|
if (null == vStr) {
|
|
if (null == vStr) {
|
|
return defaultValue;
|
|
return defaultValue;
|
|
} else {
|
|
} else {
|
|
- return getTimeDurationHelper(name, vStr, unit);
|
|
|
|
|
|
+ return getTimeDurationHelper(name, vStr, defaultUnit, returnUnit);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- public long getTimeDuration(String name, String defaultValue, TimeUnit unit) {
|
|
|
|
|
|
+ public long getTimeDuration(String name, String defaultValue,
|
|
|
|
+ TimeUnit defaultUnit, TimeUnit returnUnit) {
|
|
String vStr = get(name);
|
|
String vStr = get(name);
|
|
if (null == vStr) {
|
|
if (null == vStr) {
|
|
- return getTimeDurationHelper(name, defaultValue, unit);
|
|
|
|
|
|
+ return getTimeDurationHelper(name, defaultValue, defaultUnit, returnUnit);
|
|
} else {
|
|
} else {
|
|
- return getTimeDurationHelper(name, vStr, unit);
|
|
|
|
|
|
+ return getTimeDurationHelper(name, vStr, defaultUnit, returnUnit);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -1834,26 +1859,43 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
|
|
* Return time duration in the given time unit. Valid units are encoded in
|
|
* Return time duration in the given time unit. Valid units are encoded in
|
|
* properties as suffixes: nanoseconds (ns), microseconds (us), milliseconds
|
|
* properties as suffixes: nanoseconds (ns), microseconds (us), milliseconds
|
|
* (ms), seconds (s), minutes (m), hours (h), and days (d).
|
|
* (ms), seconds (s), minutes (m), hours (h), and days (d).
|
|
|
|
+ *
|
|
* @param name Property name
|
|
* @param name Property name
|
|
* @param vStr The string value with time unit suffix to be converted.
|
|
* @param vStr The string value with time unit suffix to be converted.
|
|
* @param unit Unit to convert the stored property, if it exists.
|
|
* @param unit Unit to convert the stored property, if it exists.
|
|
*/
|
|
*/
|
|
public long getTimeDurationHelper(String name, String vStr, TimeUnit unit) {
|
|
public long getTimeDurationHelper(String name, String vStr, TimeUnit unit) {
|
|
|
|
+ return getTimeDurationHelper(name, vStr, unit, unit);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Return time duration in the given time unit. Valid units are encoded in
|
|
|
|
+ * properties as suffixes: nanoseconds (ns), microseconds (us), milliseconds
|
|
|
|
+ * (ms), seconds (s), minutes (m), hours (h), and days (d).
|
|
|
|
+ *
|
|
|
|
+ * @param name Property name
|
|
|
|
+ * @param vStr The string value with time unit suffix to be converted.
|
|
|
|
+ * @param defaultUnit Unit to convert the stored property, if it exists.
|
|
|
|
+ * @param returnUnit Unit for the returned value.
|
|
|
|
+ */
|
|
|
|
+ private long getTimeDurationHelper(String name, String vStr,
|
|
|
|
+ TimeUnit defaultUnit, TimeUnit returnUnit) {
|
|
vStr = vStr.trim();
|
|
vStr = vStr.trim();
|
|
vStr = StringUtils.toLowerCase(vStr);
|
|
vStr = StringUtils.toLowerCase(vStr);
|
|
ParsedTimeDuration vUnit = ParsedTimeDuration.unitFor(vStr);
|
|
ParsedTimeDuration vUnit = ParsedTimeDuration.unitFor(vStr);
|
|
if (null == vUnit) {
|
|
if (null == vUnit) {
|
|
- logDeprecation("No unit for " + name + "(" + vStr + ") assuming " + unit);
|
|
|
|
- vUnit = ParsedTimeDuration.unitFor(unit);
|
|
|
|
|
|
+ logDeprecation("No unit for " + name + "(" + vStr + ") assuming " +
|
|
|
|
+ defaultUnit);
|
|
|
|
+ vUnit = ParsedTimeDuration.unitFor(defaultUnit);
|
|
} else {
|
|
} else {
|
|
vStr = vStr.substring(0, vStr.lastIndexOf(vUnit.suffix()));
|
|
vStr = vStr.substring(0, vStr.lastIndexOf(vUnit.suffix()));
|
|
}
|
|
}
|
|
|
|
|
|
long raw = Long.parseLong(vStr);
|
|
long raw = Long.parseLong(vStr);
|
|
- long converted = unit.convert(raw, vUnit.unit());
|
|
|
|
- if (vUnit.unit().convert(converted, unit) < raw) {
|
|
|
|
|
|
+ long converted = returnUnit.convert(raw, vUnit.unit());
|
|
|
|
+ if (vUnit.unit().convert(converted, returnUnit) < raw) {
|
|
logDeprecation("Possible loss of precision converting " + vStr
|
|
logDeprecation("Possible loss of precision converting " + vStr
|
|
- + vUnit.suffix() + " to " + unit + " for " + name);
|
|
|
|
|
|
+ + vUnit.suffix() + " to " + returnUnit + " for " + name);
|
|
}
|
|
}
|
|
return converted;
|
|
return converted;
|
|
}
|
|
}
|