-
Notifications
You must be signed in to change notification settings - Fork 267
Expand file tree
/
Copy pathTimeDescriptionStrategy.java
More file actions
327 lines (301 loc) · 12.8 KB
/
TimeDescriptionStrategy.java
File metadata and controls
327 lines (301 loc) · 12.8 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* Copyright 2014 jmrozanec
* 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
* http://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 com.cronutils.descriptor;
import com.cronutils.Function;
import com.cronutils.model.field.expression.*;
import com.cronutils.model.field.value.IntegerFieldValue;
import com.cronutils.utils.Preconditions;
import com.cronutils.utils.StringUtils;
import java.util.HashSet;
import java.util.ResourceBundle;
import java.util.Set;
import static com.cronutils.model.field.expression.FieldExpression.always;
/**
* Strategy to provide a human readable description to hh:mm:ss variations.
*/
class TimeDescriptionStrategy extends DescriptionStrategy {
private final FieldExpression hours;
private final FieldExpression minutes;
private final FieldExpression seconds;
private final Set<Function<TimeFields, String>> descriptions;
private static final int DEFAULTSECONDS = 0;
private static final String EVERY = "every";
private static final String SECOND = "second";
private static final String MINUTE = "minute";
private static final String HOUR = "hour";
private static final String EVERY_MINUTE_FORMAT = "%s %s ";
/**
* Constructor.
*
* @param bundle - locale considered when creating the description
* @param hours - CronFieldExpression for hours. If no instance is provided,
* an Always instance is created.
* @param minutes - CronFieldExpression for minutes. If no instance is provided,
* an Always instance is created.
* @param seconds - CronFieldExpression for seconds. If no instance is provided,
* an On instance is created.
*/
TimeDescriptionStrategy(final ResourceBundle bundle, final FieldExpression hours, final FieldExpression minutes,
final FieldExpression seconds) {
super(bundle);
this.hours = ensureInstance(hours, always());
this.minutes = ensureInstance(minutes, always());
this.seconds = ensureInstance(seconds, new On(new IntegerFieldValue(DEFAULTSECONDS)));
descriptions = new HashSet<>();
registerFunctions();
}
/**
* Give an expression instance, will return it if is not null. Otherwise will
* return the defaultExpression;
*
* @param expression - CronFieldExpression instance; may be null
* @param defaultExpression - CronFieldExpression, never null;
* @return the given expression or the given defaultExpression in case the given
* expression is {@code null}
*/
private FieldExpression ensureInstance(final FieldExpression expression, final FieldExpression defaultExpression) {
Preconditions.checkNotNull(defaultExpression, "Default expression must not be null");
if (expression != null) {
return expression;
} else {
return defaultExpression;
}
}
@Override
public String describe() {
final TimeFields fields = new TimeFields(hours, minutes, seconds);
for (final Function<TimeFields, String> function : descriptions) {
if (!"".equals(function.apply(fields))) {
return function.apply(fields);
}
}
String secondsDesc = "";
String minutesDesc = "";
String hoursDesc = "";
if (!(hours instanceof Always)) {
hoursDesc = addTimeExpressions(describe(hours), bundle.getString(HOUR), bundle.getString("hours"));
}
if (!(minutes instanceof On && isDefault((On) minutes)) && !((minutes instanceof Always) && (hours instanceof Always))) {
minutesDesc = addTimeExpressions(describe(minutes), bundle.getString(MINUTE), bundle.getString("minutes"));
}
if (!(seconds instanceof On && isDefault((On) seconds))) {
secondsDesc = addTimeExpressions(describe(seconds), bundle.getString(SECOND), bundle.getString("seconds"));
}
return String.format("%s %s %s", secondsDesc, minutesDesc, hoursDesc);
}
protected String describe(final Always always, final boolean and) {
return describe(new Every(new IntegerFieldValue(1)), and);
}
private String addTimeExpressions(final String description, final String singular, final String plural) {
return description.replaceAll("%s", singular).replaceAll("replace_plural", plural);
}
/**
* Registers functions that map TimeFields to a human readable description.
*/
private void registerFunctions() {
// case: every second
// case: every minute at x second
descriptions.add(timeFields -> {
if (timeFields.hours instanceof Always && timeFields.minutes instanceof Always) {
if (timeFields.seconds instanceof Always) {
return String.format(EVERY_MINUTE_FORMAT, bundle.getString(EVERY), bundle.getString(SECOND));
}
if (timeFields.seconds instanceof On) {
if (TimeDescriptionStrategy.this.isDefault((On) timeFields.seconds)) {
return String.format(EVERY_MINUTE_FORMAT, bundle.getString(EVERY), bundle.getString(MINUTE));
} else {
return String.format("%s %s %s %s %02d", bundle.getString(EVERY), bundle.getString(MINUTE),
bundle.getString("at"), bundle.getString(SECOND),
((On) timeFields.seconds).getTime().getValue());
}
}
}
return StringUtils.EMPTY;
});
// case: At minute x
descriptions.add(timeFields -> {
if (timeFields.hours instanceof Always && timeFields.minutes instanceof On
&& timeFields.seconds instanceof On) {
if (TimeDescriptionStrategy.this.isDefault((On) timeFields.seconds)) {
if (TimeDescriptionStrategy.this.isDefault((On) timeFields.minutes)) {
return String.format(EVERY_MINUTE_FORMAT, bundle.getString(EVERY), bundle.getString(HOUR));
}
return String.format("%s %s %s %s %s", bundle.getString(EVERY), bundle.getString(HOUR),
bundle.getString("at"), bundle.getString(MINUTE),
((On) timeFields.minutes).getTime().getValue());
} else {
return String.format("%s %s %s %s %s %s %s %s", bundle.getString(EVERY), bundle.getString(HOUR),
bundle.getString("at"), bundle.getString(MINUTE),
((On) timeFields.minutes).getTime().getValue(), bundle.getString("and"),
bundle.getString(SECOND), ((On) timeFields.seconds).getTime().getValue());
}
}
return StringUtils.EMPTY;
});
// case: 11:45
descriptions.add(timeFields -> {
if (timeFields.hours instanceof On && timeFields.minutes instanceof On
&& timeFields.seconds instanceof Always) {
return String.format("%s %s %s %02d:%02d", bundle.getString(EVERY), bundle.getString(SECOND),
bundle.getString("at"), ((On) hours).getTime().getValue(), ((On) minutes).getTime().getValue());
}
return StringUtils.EMPTY;
});
// case: 11:30:45
// case: 11:30:00 -> 11:30
descriptions.add(timeFields -> {
if (timeFields.hours instanceof On && timeFields.minutes instanceof On
&& timeFields.seconds instanceof On) {
if (TimeDescriptionStrategy.this.isDefault((On) timeFields.seconds)) {
return String.format("%s %02d:%02d", bundle.getString("at"), ((On) hours).getTime().getValue(),
((On) minutes).getTime().getValue());
} else {
return String.format("%s %02d:%02d:%02d", bundle.getString("at"), ((On) hours).getTime().getValue(),
((On) minutes).getTime().getValue(), ((On) seconds).getTime().getValue());
}
}
return StringUtils.EMPTY;
});
// 11 -> 11:00
descriptions.add(timeFields -> {
if (timeFields.hours instanceof On && timeFields.minutes instanceof Always
&& timeFields.seconds instanceof Always) {
return String.format("%s %02d:00", bundle.getString("at"), ((On) hours).getTime().getValue());
}
return StringUtils.EMPTY;
});
// case: every minute between 11:00 and 11:10
// case: every second between 11:00 and 11:10
descriptions.add(timeFields -> {
if (timeFields.hours instanceof On && timeFields.minutes instanceof Between) {
if (timeFields.seconds instanceof On) {
return String.format("%s %s %s %02d:%02d %s %02d:%02d", bundle.getString(EVERY),
bundle.getString(MINUTE), bundle.getString("between"),
((On) timeFields.hours).getTime().getValue(),
((Between) timeFields.minutes).getFrom().getValue(), bundle.getString("and"),
((On) timeFields.hours).getTime().getValue(),
((Between) timeFields.minutes).getTo().getValue());
}
if (timeFields.seconds instanceof Always) {
return String.format("%s %s %s %02d:%02d %s %02d:%02d", bundle.getString(EVERY),
bundle.getString(SECOND), bundle.getString("between"),
((On) timeFields.hours).getTime().getValue(),
((Between) timeFields.minutes).getFrom().getValue(), bundle.getString("and"),
((On) timeFields.hours).getTime().getValue(),
((Between) timeFields.minutes).getTo().getValue());
}
}
return StringUtils.EMPTY;
});
// case: every x minutes
descriptions.add(timeFields -> {
if (timeFields.hours instanceof Always && timeFields.minutes instanceof Every
&& timeFields.seconds instanceof On) {
final Every minute = (Every) timeFields.minutes;
String desc;
if (minute.getPeriod().getValue() == 1
&& TimeDescriptionStrategy.this.isDefault((On) timeFields.seconds)) {
desc = String.format(EVERY_MINUTE_FORMAT, bundle.getString(EVERY), bundle.getString(MINUTE));
} else {
desc = String.format("%s %s %s ", bundle.getString(EVERY), minute.getPeriod().getValue(),
bundle.getString("minutes"));
}
if (minute.getExpression() instanceof Between) {
return StringUtils.EMPTY;
}
return desc;
}
return StringUtils.EMPTY;
});
// case: every x hours
descriptions.add(timeFields -> {
if (timeFields.hours instanceof Every && timeFields.minutes instanceof On
&& timeFields.seconds instanceof On) {
// every hour
if (((On) timeFields.minutes).getTime().getValue() == 0
&& ((On) timeFields.seconds).getTime().getValue() == 0) {
final Integer period = ((Every) timeFields.hours).getPeriod().getValue();
if (period == null || period == 1) {
return String.format(EVERY_MINUTE_FORMAT, bundle.getString(EVERY), bundle.getString(HOUR));
}
}
final String result = String.format("%s %s %s %s %s %s", bundle.getString(EVERY),
((Every) hours).getPeriod().getValue(), bundle.getString("hours"), bundle.getString("at"),
bundle.getString(MINUTE), ((On) minutes).getTime().getValue());
if (TimeDescriptionStrategy.this.isDefault((On) timeFields.seconds)) {
return result;
} else {
return result + String.format(" %s %s %s", bundle.getString("and"), bundle.getString(SECOND),
((On) seconds).getTime().getValue());
}
}
return StringUtils.EMPTY;
});
// case: every second at minute 00
descriptions.add(timeFields -> {
if (timeFields.hours instanceof Always && timeFields.seconds instanceof Always) {
if (timeFields.minutes instanceof On) {
// Every
return String.format("%s %s %s %s %02d %s", bundle.getString(EVERY), bundle.getString(SECOND),
bundle.getString("at"), bundle.getString(MINUTE),
((On) timeFields.minutes).getTime().getValue(), bundle.getString("of_every_hour"));
}
}
return StringUtils.EMPTY;
});
// case: every second at minute 00 and every x minutes
descriptions.add(timeFields -> {
if (timeFields.hours instanceof Always && timeFields.seconds instanceof Always) {
if (timeFields.minutes instanceof And) {
// Every
String minutesDesc = addTimeExpressions(describe(timeFields.minutes),
bundle.getString("minute"), bundle.getString("minutes"));
return String.format("%s %s %s", bundle.getString(EVERY), bundle.getString(SECOND), minutesDesc);
}
}
return StringUtils.EMPTY;
});
// case: every second at x, y and z hours
descriptions.add(timeFields -> {
if (timeFields.hours instanceof And && timeFields.minutes instanceof Always && timeFields.seconds instanceof Always) {
// Every
String hoursDesc = addTimeExpressions(describe(timeFields.hours),
bundle.getString(HOUR), bundle.getString("hours"));
return String.format("%s %s %s", bundle.getString(EVERY), bundle.getString(SECOND), hoursDesc);
}
return StringUtils.EMPTY;
});
}
/**
* Contains CronFieldExpression instances for hours, minutes and seconds.
*/
class TimeFields {
private final FieldExpression seconds;
private final FieldExpression minutes;
private final FieldExpression hours;
public TimeFields(final FieldExpression hours, final FieldExpression minutes, final FieldExpression seconds) {
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
}
}
/**
* Checks if On instance has a default value.
*
* @param on - On instance
* @return boolean - true if time value matches a default; false otherwise.
*/
private boolean isDefault(final On on) {
return on.getTime().getValue() == DEFAULTSECONDS;
}
}