Debian Codegraph @

owner-java8-extras/src/main/java/org/aeonbits/owner/converters/DurationConverter.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2012-2016, Luigi R. Viggiano
3 * All rights reserved.
4 *
5 * This software is distributable under the BSD license.
6 * See the terms of the BSD license in the documentation provided with this software.
7 */
8
9 package org.aeonbits.owner.converters;
10
11 import org.aeonbits.owner.Converter;
12
13 import java.lang.reflect.Method;
14 import java.time.Duration;
15 import java.time.temporal.ChronoUnit;
16
17 /**
18 * A duration converter for the OWNER configuration system.
19 *
20 * This converter will convert various duration formatted strings over to {@link java.time.Duration} objects.
21 *
22 * The class supports two formats for the duration string:
23 * <ul>
24 * <li>
25 * The ISO-8601 based format that the {@link java.time.Duration#parse(CharSequence)} method supports
26 * (<a href="https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html#parse-java.lang.CharSequence-"
27 * target="_blank">see the official Java 8 documentation</a>, although note that currently there is an
28 * <a href="https://bugs.openjdk.java.net/browse/JDK-8146173" target="_blank">error in the documentation</a>).
29 * The implementation will check whether the input string starts with <code>P</code> with an optional plus/minus
30 * prefix and if so, will use this method for parsing.
31 * </li>
32 * <li>
33 * A "<code>value time_unit</code>" string where the <code>value</code> is an integer and <code>time_unit</code>
34 * is one of:
35 * <ul>
36 * <li>ns / nanos / nanoseconds</li>
37 * <li>us / µs / micros / microseconds</li>
38 * <li>ms / millis / milliseconds</li>
39 * <li>s / seconds</li>
40 * <li>m / minutes</li>
41 * <li>h / hours</li>
42 * <li>d / days</li>
43 * </ul>
44 * <p>
45 * Note that the <code>time_unit</code> string is case sensitive.
46 * <p>
47 * If no <code>time_unit</code> is specified, <code>milliseconds</code> is assumed.
48 * </li>
49 * </ul>
50 */
51 public class DurationConverter implements Converter<Duration> {
52
53 @Override
54 public Duration convert(Method method, String input) {
55 // If it looks like a string that Duration.parse can handle, let's try that.
56 if (input.startsWith("P") || input.startsWith("-P") || input.startsWith("+P")) {
57 return Duration.parse(input);
58 }
59 // ...otherwise we'll perform our own parsing
60 return parseDuration(input);
61 }
62
63 /**
64 * Parses a duration string. If no units are specified in the string, it is
65 * assumed to be in milliseconds.
66 *
67 * This implementation was blatantly stolen/adapted from the typesafe-config project:
68 * https://github.com/typesafehub/config/blob/v1.3.0/config/src/main/java/com/typesafe/config/impl/SimpleConfig.java#L551-L624
69 *
70 * @param input the string to parse
71 * @return duration
72 * @throws IllegalArgumentException if input is invalid
73 */
74 private static Duration parseDuration(String input) {
75 String[] parts = ConverterUtil.splitNumericAndChar(input);
76 String numberString = parts[0];
77 String originalUnitString = parts[1];
78 String unitString = originalUnitString;
79
80 if (numberString.length() == 0) {
81 throw new IllegalArgumentException(String.format("No number in duration value '%s'", input));
82 }
83
84 if (unitString.length() > 2 && !unitString.endsWith("s")) {
85 unitString = unitString + "s";
86 }
87
88 ChronoUnit units;
89 // note that this is deliberately case-sensitive
90 switch (unitString) {
91 case "ns":
92 case "nanos":
93 case "nanoseconds":
94 units = ChronoUnit.NANOS;
95 break;
96 case "us":
97 case "µs":
98 case "micros":
99 case "microseconds":
100 units = ChronoUnit.MICROS;
101 break;
102 case "":
103 case "ms":
104 case "millis":
105 case "milliseconds":
106 units = ChronoUnit.MILLIS;
107 break;
108 case "s":
109 case "seconds":
110 units = ChronoUnit.SECONDS;
111 break;
112 case "m":
113 case "minutes":
114 units = ChronoUnit.MINUTES;
115 break;
116 case "h":
117 case "hours":
118 units = ChronoUnit.HOURS;
119 break;
120 case "d":
121 case "days":
122 units = ChronoUnit.DAYS;
123 break;
124 default:
125 throw new IllegalArgumentException(
126 String.format("Could not parse time unit '%s' (try ns, us, ms, s, m, h, d)", originalUnitString));
127 }
128
129 return Duration.of(Long.parseLong(numberString), units);
130 }
131 }