/
PeriodStyle.java
276 lines (232 loc) · 7.21 KB
/
PeriodStyle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.convert;
import java.time.Period;
import java.time.temporal.ChronoUnit;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.util.Assert;
/**
* A standard set of {@link Period} units.
*
* @author Eddú Meléndez
* @author Edson Chávez
* @since 2.3.0
* @see Period
*/
public enum PeriodStyle {
/**
* Simple formatting, for example '1d'.
*/
SIMPLE("^" + "(?:([-+]?[0-9]+)Y)?" + "(?:([-+]?[0-9]+)M)?" + "(?:([-+]?[0-9]+)W)?" + "(?:([-+]?[0-9]+)D)?" + "$",
Pattern.CASE_INSENSITIVE) {
@Override
public Period parse(String value, ChronoUnit unit) {
try {
if (NUMERIC.matcher(value).matches()) {
return Unit.fromChronoUnit(unit).parse(value);
}
Matcher matcher = matcher(value);
Assert.state(matcher.matches(), "Does not match simple period pattern");
Assert.isTrue(hasAtLeastOneGroupValue(matcher), "'" + value + "' is not a valid simple period");
int years = parseInt(matcher, 1);
int months = parseInt(matcher, 2);
int weeks = parseInt(matcher, 3);
int days = parseInt(matcher, 4);
return Period.of(years, months, Math.addExact(Math.multiplyExact(weeks, 7), days));
}
catch (Exception ex) {
throw new IllegalArgumentException("'" + value + "' is not a valid simple period", ex);
}
}
boolean hasAtLeastOneGroupValue(Matcher matcher) {
for (int i = 0; i < matcher.groupCount(); i++) {
if (matcher.group(i + 1) != null) {
return true;
}
}
return false;
}
private int parseInt(Matcher matcher, int group) {
String value = matcher.group(group);
return (value != null) ? Integer.parseInt(value) : 0;
}
@Override
protected boolean matches(String value) {
return NUMERIC.matcher(value).matches() || matcher(value).matches();
}
@Override
public String print(Period value, ChronoUnit unit) {
if (value.isZero()) {
return Unit.fromChronoUnit(unit).print(value);
}
StringBuilder result = new StringBuilder();
append(result, value, Unit.YEARS);
append(result, value, Unit.MONTHS);
append(result, value, Unit.DAYS);
return result.toString();
}
private void append(StringBuilder result, Period value, Unit unit) {
if (!unit.isZero(value)) {
result.append(unit.print(value));
}
}
},
/**
* ISO-8601 formatting.
*/
ISO8601("^[+-]?P.*$", 0) {
@Override
public Period parse(String value, ChronoUnit unit) {
try {
return Period.parse(value);
}
catch (Exception ex) {
throw new IllegalArgumentException("'" + value + "' is not a valid ISO-8601 period", ex);
}
}
@Override
public String print(Period value, ChronoUnit unit) {
return value.toString();
}
};
private static final Pattern NUMERIC = Pattern.compile("^[-+]?[0-9]+$");
private final Pattern pattern;
PeriodStyle(String pattern, int flags) {
this.pattern = Pattern.compile(pattern, flags);
}
protected boolean matches(String value) {
return this.pattern.matcher(value).matches();
}
protected final Matcher matcher(String value) {
return this.pattern.matcher(value);
}
/**
* Parse the given value to a Period.
* @param value the value to parse
* @return a period
*/
public Period parse(String value) {
return parse(value, null);
}
/**
* Parse the given value to a period.
* @param value the value to parse
* @param unit the period unit to use if the value doesn't specify one ({@code null}
* will default to d)
* @return a period
*/
public abstract Period parse(String value, ChronoUnit unit);
/**
* Print the specified period.
* @param value the value to print
* @return the printed result
*/
public String print(Period value) {
return print(value, null);
}
/**
* Print the specified period using the given unit.
* @param value the value to print
* @param unit the value to use for printing
* @return the printed result
*/
public abstract String print(Period value, ChronoUnit unit);
/**
* Detect the style then parse the value to return a period.
* @param value the value to parse
* @return the parsed period
* @throws IllegalStateException if the value is not a known style or cannot be parsed
*/
public static Period detectAndParse(String value) {
return detectAndParse(value, null);
}
/**
* Detect the style then parse the value to return a period.
* @param value the value to parse
* @param unit the period unit to use if the value doesn't specify one ({@code null}
* will default to ms)
* @return the parsed period
* @throws IllegalStateException if the value is not a known style or cannot be parsed
*/
public static Period detectAndParse(String value, ChronoUnit unit) {
return detect(value).parse(value, unit);
}
/**
* Detect the style from the given source value.
* @param value the source value
* @return the period style
* @throws IllegalStateException if the value is not a known style
*/
public static PeriodStyle detect(String value) {
Assert.notNull(value, "Value must not be null");
for (PeriodStyle candidate : values()) {
if (candidate.matches(value)) {
return candidate;
}
}
throw new IllegalArgumentException("'" + value + "' is not a valid period");
}
enum Unit {
/**
* Days, represented by suffix {@code d}.
*/
DAYS(ChronoUnit.DAYS, "d", Period::getDays, Period::ofDays),
/**
* Months, represented by suffix {@code m}.
*/
MONTHS(ChronoUnit.MONTHS, "m", Period::getMonths, Period::ofMonths),
/**
* Years, represented by suffix {@code y}.
*/
YEARS(ChronoUnit.YEARS, "y", Period::getYears, Period::ofYears);
private final ChronoUnit chronoUnit;
private final String suffix;
private final Function<Period, Integer> intValue;
private final Function<Integer, Period> factory;
Unit(ChronoUnit chronoUnit, String suffix, Function<Period, Integer> intValue,
Function<Integer, Period> factory) {
this.chronoUnit = chronoUnit;
this.suffix = suffix;
this.intValue = intValue;
this.factory = factory;
}
private Period parse(String value) {
return this.factory.apply(Integer.parseInt(value));
}
private String print(Period value) {
return intValue(value) + this.suffix;
}
public boolean isZero(Period value) {
return intValue(value) == 0;
}
public int intValue(Period value) {
return this.intValue.apply(value);
}
public static Unit fromChronoUnit(ChronoUnit chronoUnit) {
if (chronoUnit == null) {
return Unit.DAYS;
}
for (Unit candidate : values()) {
if (candidate.chronoUnit == chronoUnit) {
return candidate;
}
}
throw new IllegalArgumentException("Unsupported unit " + chronoUnit);
}
}
}