TimeValue#parseTimeValue author is bad, feels bad

I stumbled on this code today and I hated it; I wrote it. I did not like
that you could not tell at a glance whether or not the method parameters
were correct. This commit fixes it.

Relates #24522
This commit is contained in:
Jason Tedor 2017-05-05 18:37:09 -04:00 committed by GitHub
parent 4e5537d9ed
commit 4027c9da7b
1 changed files with 11 additions and 13 deletions

View File

@ -254,7 +254,7 @@ public class TimeValue implements Writeable, Comparable<TimeValue> {
* Returns a {@link String} representation of the current {@link TimeValue}. * Returns a {@link String} representation of the current {@link TimeValue}.
* *
* Note that this method might produce fractional time values (ex 1.6m) which cannot be * Note that this method might produce fractional time values (ex 1.6m) which cannot be
* parsed by method like {@link TimeValue#parse(String, String, int)}. * parsed by method like {@link TimeValue#parse(String, String, String)}.
*/ */
@Override @Override
public String toString() { public String toString() {
@ -326,22 +326,20 @@ public class TimeValue implements Writeable, Comparable<TimeValue> {
} }
final String normalized = sValue.toLowerCase(Locale.ROOT).trim(); final String normalized = sValue.toLowerCase(Locale.ROOT).trim();
if (normalized.endsWith("nanos")) { if (normalized.endsWith("nanos")) {
return new TimeValue(parse(sValue, normalized, 5), TimeUnit.NANOSECONDS); return new TimeValue(parse(sValue, normalized, "nanos"), TimeUnit.NANOSECONDS);
} else if (normalized.endsWith("micros")) { } else if (normalized.endsWith("micros")) {
return new TimeValue(parse(sValue, normalized, 6), TimeUnit.MICROSECONDS); return new TimeValue(parse(sValue, normalized, "micros"), TimeUnit.MICROSECONDS);
} else if (normalized.endsWith("ms")) { } else if (normalized.endsWith("ms")) {
return new TimeValue(parse(sValue, normalized, 2), TimeUnit.MILLISECONDS); return new TimeValue(parse(sValue, normalized, "ms"), TimeUnit.MILLISECONDS);
} else if (normalized.endsWith("s")) { } else if (normalized.endsWith("s")) {
return new TimeValue(parse(sValue, normalized, 1), TimeUnit.SECONDS); return new TimeValue(parse(sValue, normalized, "s"), TimeUnit.SECONDS);
} else if (sValue.endsWith("m")) { } else if (sValue.endsWith("m")) {
// parsing minutes should be case sensitive as `M` is generally // parsing minutes should be case-sensitive as 'M' means "months", not "minutes"; this is the only special case.
// accepted to mean months not minutes. This is the only case where return new TimeValue(parse(sValue, normalized, "m"), TimeUnit.MINUTES);
// the upper and lower case forms indicate different time units
return new TimeValue(parse(sValue, normalized, 1), TimeUnit.MINUTES);
} else if (normalized.endsWith("h")) { } else if (normalized.endsWith("h")) {
return new TimeValue(parse(sValue, normalized, 1), TimeUnit.HOURS); return new TimeValue(parse(sValue, normalized, "h"), TimeUnit.HOURS);
} else if (normalized.endsWith("d")) { } else if (normalized.endsWith("d")) {
return new TimeValue(parse(sValue, normalized, 1), TimeUnit.DAYS); return new TimeValue(parse(sValue, normalized, "d"), TimeUnit.DAYS);
} else if (normalized.matches("-0*1")) { } else if (normalized.matches("-0*1")) {
return TimeValue.MINUS_ONE; return TimeValue.MINUS_ONE;
} else if (normalized.matches("0+")) { } else if (normalized.matches("0+")) {
@ -355,8 +353,8 @@ public class TimeValue implements Writeable, Comparable<TimeValue> {
} }
} }
private static long parse(final String initialInput, final String normalized, final int suffixLength) { private static long parse(final String initialInput, final String normalized, final String suffix) {
final String s = normalized.substring(0, normalized.length() - suffixLength).trim(); final String s = normalized.substring(0, normalized.length() - suffix.length()).trim();
try { try {
return Long.parseLong(s); return Long.parseLong(s);
} catch (final NumberFormatException e) { } catch (final NumberFormatException e) {