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