1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.util;
15
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 public class Duration {
34
35 private final static String DOUBLE_PART = "([0-9]*(.[0-9]+)?)";
36 private final static int DOUBLE_GROUP = 1;
37
38 private final static String UNIT_PART = "(|milli(second)?|second(e)?|minute|hour|day)s?";
39 private final static int UNIT_GROUP = 3;
40
41 private static final Pattern DURATION_PATTERN = Pattern.compile(DOUBLE_PART
42 + "\\s*" + UNIT_PART, Pattern.CASE_INSENSITIVE);
43
44 static final long SECONDS_COEFFICIENT = 1000;
45 static final long MINUTES_COEFFICIENT = 60 * SECONDS_COEFFICIENT;
46 static final long HOURS_COEFFICIENT = 60 * MINUTES_COEFFICIENT;
47 static final long DAYS_COEFFICIENT = 24 * HOURS_COEFFICIENT;
48
49 final long millis;
50
51 public Duration(long millis) {
52 this.millis = millis;
53 }
54
55 public static Duration buildByMilliseconds(double value) {
56 return new Duration((long) (value));
57 }
58
59 public static Duration buildBySeconds(double value) {
60 return new Duration((long) (SECONDS_COEFFICIENT * value));
61 }
62
63 public static Duration buildByMinutes(double value) {
64 return new Duration((long) (MINUTES_COEFFICIENT * value));
65 }
66
67 public static Duration buildByHours(double value) {
68 return new Duration((long) (HOURS_COEFFICIENT * value));
69 }
70
71 public static Duration buildByDays(double value) {
72 return new Duration((long) (DAYS_COEFFICIENT * value));
73 }
74
75 public static Duration buildUnbounded() {
76 return new Duration(Long.MAX_VALUE);
77 }
78
79 public long getMilliseconds() {
80 return millis;
81 }
82
83 public static Duration valueOf(String durationStr) {
84 Matcher matcher = DURATION_PATTERN.matcher(durationStr);
85
86 if (matcher.matches()) {
87 String doubleStr = matcher.group(DOUBLE_GROUP);
88 String unitStr = matcher.group(UNIT_GROUP);
89
90 double doubleValue = Double.valueOf(doubleStr);
91 if (unitStr.equalsIgnoreCase("milli")
92 || unitStr.equalsIgnoreCase("millisecond") || unitStr.length() == 0) {
93 return buildByMilliseconds(doubleValue);
94 } else if (unitStr.equalsIgnoreCase("second")
95 || unitStr.equalsIgnoreCase("seconde")) {
96 return buildBySeconds(doubleValue);
97 } else if (unitStr.equalsIgnoreCase("minute")) {
98 return buildByMinutes(doubleValue);
99 } else if (unitStr.equalsIgnoreCase("hour")) {
100 return buildByHours(doubleValue);
101 } else if (unitStr.equalsIgnoreCase("day")) {
102 return buildByDays(doubleValue);
103 } else {
104 throw new IllegalStateException("Unexpected " + unitStr);
105 }
106 } else {
107 throw new IllegalArgumentException("String value [" + durationStr
108 + "] is not in the expected format.");
109 }
110 }
111
112 @Override
113 public String toString() {
114 if (millis < SECONDS_COEFFICIENT) {
115 return millis + " milliseconds";
116 } else if (millis < MINUTES_COEFFICIENT) {
117 return millis / SECONDS_COEFFICIENT + " seconds";
118 } else if (millis < HOURS_COEFFICIENT) {
119 return millis / MINUTES_COEFFICIENT + " minutes";
120 } else {
121 return millis / HOURS_COEFFICIENT + " hours";
122 }
123
124 }
125 }