@@ -47,6 +47,9 @@ public TimeValue(long millis) {
47
47
}
48
48
49
49
public TimeValue (long duration , TimeUnit timeUnit ) {
50
+ if (duration < -1 ) {
51
+ throw new IllegalArgumentException ("duration cannot be negative, was given [" + duration + "]" );
52
+ }
50
53
this .duration = duration ;
51
54
this .timeUnit = timeUnit ;
52
55
}
@@ -201,7 +204,7 @@ public double getDaysFrac() {
201
204
* Returns a {@link String} representation of the current {@link TimeValue}.
202
205
*
203
206
* Note that this method might produce fractional time values (ex 1.6m) which cannot be
204
- * parsed by method like {@link TimeValue#parse(String, String, String)}.
207
+ * parsed by method like {@link TimeValue#parse(String, String, String, String )}.
205
208
*
206
209
* Also note that the maximum string value that will be generated is
207
210
* {@code 106751.9d} due to the way that values are internally converted
@@ -216,7 +219,7 @@ public String toString() {
216
219
* Returns a {@link String} representation of the current {@link TimeValue}.
217
220
*
218
221
* Note that this method might produce fractional time values (ex 1.6m) which cannot be
219
- * parsed by method like {@link TimeValue#parse(String, String, String)}. The number of
222
+ * parsed by method like {@link TimeValue#parse(String, String, String, String )}. The number of
220
223
* fractional decimals (up to 10 maximum) are truncated to the number of fraction pieces
221
224
* specified.
222
225
*
@@ -358,20 +361,20 @@ public static TimeValue parseTimeValue(String sValue, TimeValue defaultValue, St
358
361
}
359
362
final String normalized = sValue .toLowerCase (Locale .ROOT ).trim ();
360
363
if (normalized .endsWith ("nanos" )) {
361
- return new TimeValue (parse (sValue , normalized , "nanos" ), TimeUnit .NANOSECONDS );
364
+ return new TimeValue (parse (sValue , normalized , "nanos" , settingName ), TimeUnit .NANOSECONDS );
362
365
} else if (normalized .endsWith ("micros" )) {
363
- return new TimeValue (parse (sValue , normalized , "micros" ), TimeUnit .MICROSECONDS );
366
+ return new TimeValue (parse (sValue , normalized , "micros" , settingName ), TimeUnit .MICROSECONDS );
364
367
} else if (normalized .endsWith ("ms" )) {
365
- return new TimeValue (parse (sValue , normalized , "ms" ), TimeUnit .MILLISECONDS );
368
+ return new TimeValue (parse (sValue , normalized , "ms" , settingName ), TimeUnit .MILLISECONDS );
366
369
} else if (normalized .endsWith ("s" )) {
367
- return new TimeValue (parse (sValue , normalized , "s" ), TimeUnit .SECONDS );
370
+ return new TimeValue (parse (sValue , normalized , "s" , settingName ), TimeUnit .SECONDS );
368
371
} else if (sValue .endsWith ("m" )) {
369
372
// parsing minutes should be case-sensitive as 'M' means "months", not "minutes"; this is the only special case.
370
- return new TimeValue (parse (sValue , normalized , "m" ), TimeUnit .MINUTES );
373
+ return new TimeValue (parse (sValue , normalized , "m" , settingName ), TimeUnit .MINUTES );
371
374
} else if (normalized .endsWith ("h" )) {
372
- return new TimeValue (parse (sValue , normalized , "h" ), TimeUnit .HOURS );
375
+ return new TimeValue (parse (sValue , normalized , "h" , settingName ), TimeUnit .HOURS );
373
376
} else if (normalized .endsWith ("d" )) {
374
- return new TimeValue (parse (sValue , normalized , "d" ), TimeUnit .DAYS );
377
+ return new TimeValue (parse (sValue , normalized , "d" , settingName ), TimeUnit .DAYS );
375
378
} else if (normalized .matches ("-0*1" )) {
376
379
return TimeValue .MINUS_ONE ;
377
380
} else if (normalized .matches ("0+" )) {
@@ -383,10 +386,16 @@ public static TimeValue parseTimeValue(String sValue, TimeValue defaultValue, St
383
386
}
384
387
}
385
388
386
- private static long parse (final String initialInput , final String normalized , final String suffix ) {
389
+ private static long parse (final String initialInput , final String normalized , final String suffix , String settingName ) {
387
390
final String s = normalized .substring (0 , normalized .length () - suffix .length ()).trim ();
388
391
try {
389
- return Long .parseLong (s );
392
+ final long value = Long .parseLong (s );
393
+ if (value < -1 ) {
394
+ // -1 is magic, but reject any other negative values
395
+ throw new IllegalArgumentException ("failed to parse setting [" + settingName + "] with value [" + initialInput +
396
+ "] as a time value: negative durations are not supported" );
397
+ }
398
+ return value ;
390
399
} catch (final NumberFormatException e ) {
391
400
try {
392
401
@ SuppressWarnings ("unused" ) final double ignored = Double .parseDouble (s );
0 commit comments