From bd5c7f088974bbde574ee00fbe9d2b4b2e687c8c Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Sun, 14 Feb 2016 09:37:10 -0500 Subject: [PATCH] Inline TimeValue#parseTimeValue Relates #16725 --- .../org/elasticsearch/common/unit/TimeValue.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/unit/TimeValue.java b/core/src/main/java/org/elasticsearch/common/unit/TimeValue.java index bbe1fbbf055..c467a0c18a8 100644 --- a/core/src/main/java/org/elasticsearch/common/unit/TimeValue.java +++ b/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;