|
@@ -54,6 +54,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
import java.util.regex.PatternSyntaxException;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
@@ -1145,6 +1146,93 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
|
|
|
: Enum.valueOf(defaultValue.getDeclaringClass(), val);
|
|
|
}
|
|
|
|
|
|
+ enum ParsedTimeDuration {
|
|
|
+ NS {
|
|
|
+ TimeUnit unit() { return TimeUnit.NANOSECONDS; }
|
|
|
+ String suffix() { return "ns"; }
|
|
|
+ },
|
|
|
+ US {
|
|
|
+ TimeUnit unit() { return TimeUnit.MICROSECONDS; }
|
|
|
+ String suffix() { return "us"; }
|
|
|
+ },
|
|
|
+ MS {
|
|
|
+ TimeUnit unit() { return TimeUnit.MILLISECONDS; }
|
|
|
+ String suffix() { return "ms"; }
|
|
|
+ },
|
|
|
+ S {
|
|
|
+ TimeUnit unit() { return TimeUnit.SECONDS; }
|
|
|
+ String suffix() { return "s"; }
|
|
|
+ },
|
|
|
+ M {
|
|
|
+ TimeUnit unit() { return TimeUnit.MINUTES; }
|
|
|
+ String suffix() { return "m"; }
|
|
|
+ },
|
|
|
+ H {
|
|
|
+ TimeUnit unit() { return TimeUnit.HOURS; }
|
|
|
+ String suffix() { return "h"; }
|
|
|
+ },
|
|
|
+ D {
|
|
|
+ TimeUnit unit() { return TimeUnit.DAYS; }
|
|
|
+ String suffix() { return "d"; }
|
|
|
+ };
|
|
|
+ abstract TimeUnit unit();
|
|
|
+ abstract String suffix();
|
|
|
+ static ParsedTimeDuration unitFor(String s) {
|
|
|
+ for (ParsedTimeDuration ptd : values()) {
|
|
|
+ // iteration order is in decl order, so SECONDS matched last
|
|
|
+ if (s.endsWith(ptd.suffix())) {
|
|
|
+ return ptd;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ static ParsedTimeDuration unitFor(TimeUnit unit) {
|
|
|
+ for (ParsedTimeDuration ptd : values()) {
|
|
|
+ if (ptd.unit() == unit) {
|
|
|
+ return ptd;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Set the value of <code>name</code> to the given time duration. This
|
|
|
+ * is equivalent to <code>set(<name>, value + <time suffix>)</code>.
|
|
|
+ * @param name Property name
|
|
|
+ * @param value Time duration
|
|
|
+ * @param unit Unit of time
|
|
|
+ */
|
|
|
+ public void setTimeDuration(String name, long value, TimeUnit unit) {
|
|
|
+ set(name, value + ParsedTimeDuration.unitFor(unit).suffix());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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 defaultValue Value returned if no mapping exists.
|
|
|
+ * @param unit Unit to convert the stored property, if it exists.
|
|
|
+ * @throws NumberFormatException If the property stripped of its unit is not
|
|
|
+ * a number
|
|
|
+ */
|
|
|
+ public long getTimeDuration(String name, long defaultValue, TimeUnit unit) {
|
|
|
+ String vStr = get(name);
|
|
|
+ if (null == vStr) {
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+ vStr = vStr.trim();
|
|
|
+ ParsedTimeDuration vUnit = ParsedTimeDuration.unitFor(vStr);
|
|
|
+ if (null == vUnit) {
|
|
|
+ LOG.warn("No unit for " + name + "(" + vStr + ") assuming " + unit);
|
|
|
+ vUnit = ParsedTimeDuration.unitFor(unit);
|
|
|
+ } else {
|
|
|
+ vStr = vStr.substring(0, vStr.lastIndexOf(vUnit.suffix()));
|
|
|
+ }
|
|
|
+ return unit.convert(Long.parseLong(vStr), vUnit.unit());
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Get the value of the <code>name</code> property as a <code>Pattern</code>.
|
|
|
* If no such property is specified, or if the specified value is not a valid
|