瀏覽代碼

Inline TimeValue#parseTimeValue

Relates #16725
Jason Tedor 9 年之前
父節點
當前提交
bd5c7f0889
共有 1 個文件被更改,包括 10 次插入6 次删除
  1. 10 6
      core/src/main/java/org/elasticsearch/common/unit/TimeValue.java

+ 10 - 6
core/src/main/java/org/elasticsearch/common/unit/TimeValue.java

@@ -265,17 +265,17 @@ public class TimeValue implements Streamable {
             long millis;
             String lowerSValue = sValue.toLowerCase(Locale.ROOT).trim();
             if (lowerSValue.endsWith("ms")) {
-                millis = (long) (Double.parseDouble(lowerSValue.substring(0, lowerSValue.length() - 2)));
+                millis = parse(lowerSValue, 2, 1);
             } else if (lowerSValue.endsWith("s")) {
-                millis = (long) Double.parseDouble(lowerSValue.substring(0, lowerSValue.length() - 1)) * 1000;
+                millis = parse(lowerSValue, 1, 1000);
             } else if (lowerSValue.endsWith("m")) {
-                millis = (long) (Double.parseDouble(lowerSValue.substring(0, lowerSValue.length() - 1)) * 60 * 1000);
+                millis = parse(lowerSValue, 1, 60 * 1000);
             } else if (lowerSValue.endsWith("h")) {
-                millis = (long) (Double.parseDouble(lowerSValue.substring(0, lowerSValue.length() - 1)) * 60 * 60 * 1000);
+                millis = parse(lowerSValue, 1, 60 * 60 * 1000);
             } else if (lowerSValue.endsWith("d")) {
-                millis = (long) (Double.parseDouble(lowerSValue.substring(0, lowerSValue.length() - 1)) * 24 * 60 * 60 * 1000);
+                millis = parse(lowerSValue, 1, 24 * 60 * 60 * 1000);
             } else if (lowerSValue.endsWith("w")) {
-                millis = (long) (Double.parseDouble(lowerSValue.substring(0, lowerSValue.length() - 1)) * 7 * 24 * 60 * 60 * 1000);
+                millis = parse(lowerSValue, 1, 7 * 24 * 60 * 60 * 1000);
             } else if (lowerSValue.equals("-1")) {
                 // Allow this special value to be unit-less:
                 millis = -1;
@@ -292,6 +292,10 @@ public class TimeValue implements Streamable {
         }
     }
 
+    private static long parse(String s, int suffixLength, long scale) {
+        return (long) (Double.parseDouble(s.substring(0, s.length() - suffixLength)) * scale);
+    }
+
     static final long C0 = 1L;
     static final long C1 = C0 * 1000L;
     static final long C2 = C1 * 1000L;