-
Notifications
You must be signed in to change notification settings - Fork 260
/
CronDefinitionBuilder.java
executable file
·391 lines (366 loc) · 12 KB
/
CronDefinitionBuilder.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
* 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.model.definition;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.cronutils.model.CronType;
import com.cronutils.model.field.CronFieldName;
import com.cronutils.model.field.definition.FieldDayOfWeekDefinitionBuilder;
import com.cronutils.model.field.definition.FieldDefinition;
import com.cronutils.model.field.definition.FieldDefinitionBuilder;
import com.cronutils.model.field.definition.FieldQuestionMarkDefinitionBuilder;
import com.cronutils.model.field.definition.FieldSpecialCharsDefinitionBuilder;
/**
* Builder that allows to define and create CronDefinition instances.
*/
public class CronDefinitionBuilder {
private final Map<CronFieldName, FieldDefinition> fields = new EnumMap<>(CronFieldName.class);
private final Set<CronConstraint> cronConstraints = new HashSet<>();
private boolean enforceStrictRanges;
private boolean matchDayOfWeekAndDayOfMonth;
/**
* Constructor.
*/
private CronDefinitionBuilder() {/*NOP*/}
/**
* Creates a builder instance.
*
* @return new CronDefinitionBuilder instance
*/
public static CronDefinitionBuilder defineCron() {
return new CronDefinitionBuilder();
}
/**
* Adds definition for seconds field.
*
* @return new FieldDefinitionBuilder instance
*/
public FieldDefinitionBuilder withSeconds() {
return new FieldDefinitionBuilder(this, CronFieldName.SECOND);
}
/**
* Adds definition for minutes field.
*
* @return new FieldDefinitionBuilder instance
*/
public FieldDefinitionBuilder withMinutes() {
return new FieldDefinitionBuilder(this, CronFieldName.MINUTE);
}
/**
* Adds definition for hours field.
*
* @return new FieldDefinitionBuilder instance
*/
public FieldDefinitionBuilder withHours() {
return new FieldDefinitionBuilder(this, CronFieldName.HOUR);
}
/**
* Adds definition for day of month field.
*
* @return new FieldSpecialCharsDefinitionBuilder instance
*/
public FieldSpecialCharsDefinitionBuilder withDayOfMonth() {
return new FieldSpecialCharsDefinitionBuilder(this, CronFieldName.DAY_OF_MONTH);
}
/**
* Adds definition for month field.
*
* @return new FieldDefinitionBuilder instance
*/
public FieldDefinitionBuilder withMonth() {
return new FieldDefinitionBuilder(this, CronFieldName.MONTH);
}
/**
* Adds definition for day of week field.
*
* @return new FieldSpecialCharsDefinitionBuilder instance
*/
public FieldDayOfWeekDefinitionBuilder withDayOfWeek() {
return new FieldDayOfWeekDefinitionBuilder(this, CronFieldName.DAY_OF_WEEK);
}
/**
* Adds definition for year field.
*
* @return new FieldDefinitionBuilder instance
*/
public FieldDefinitionBuilder withYear() {
return new FieldDefinitionBuilder(this, CronFieldName.YEAR);
}
/**
* Adds definition for day of year field.
*
* @return new FieldDefinitionBuilder instance
*/
public FieldQuestionMarkDefinitionBuilder withDayOfYear() {
return new FieldQuestionMarkDefinitionBuilder(this, CronFieldName.DAY_OF_YEAR);
}
/**
* Sets enforceStrictRanges value to true.
*
* @return this CronDefinitionBuilder instance
*/
public CronDefinitionBuilder enforceStrictRanges() {
enforceStrictRanges = true;
return this;
}
/**
* Sets matchDayOfWeekAndDayOfMonth value to true.
*
* @return this CronDefinitionBuilder instance
*/
public CronDefinitionBuilder matchDayOfWeekAndDayOfMonth() {
matchDayOfWeekAndDayOfMonth = true;
return this;
}
/**
* Adds a cron validation.
*
* @return this CronDefinitionBuilder instance
*/
public CronDefinitionBuilder withCronValidation(final CronConstraint validation) {
cronConstraints.add(validation);
return this;
}
/**
* Registers a certain FieldDefinition.
*
* @param definition - FieldDefinition instance, never null
*/
public void register(final FieldDefinition definition) {
//ensure that we can't register a mandatory definition if there are already optional ones
boolean hasOptionalField = false;
for (final FieldDefinition fieldDefinition : fields.values()) {
if (fieldDefinition.isOptional()) {
hasOptionalField = true;
break;
}
}
if (!definition.isOptional() && hasOptionalField) {
throw new IllegalArgumentException("Can't register mandatory definition after a optional definition.");
}
fields.put(definition.getFieldName(), definition);
}
/**
* Creates a new CronDefinition instance with provided field definitions.
*
* @return returns CronDefinition instance, never null
*/
public CronDefinition instance() {
final Set<CronConstraint> validations = new HashSet<>();
validations.addAll(cronConstraints);
final List<FieldDefinition> values = new ArrayList<>(fields.values());
values.sort(FieldDefinition.createFieldDefinitionComparator());
return new CronDefinition(values, validations, enforceStrictRanges, matchDayOfWeekAndDayOfMonth);
}
/**
* Creates CronDefinition instance matching cron4j specification.
*
* @return CronDefinition instance, never null;
*/
private static CronDefinition cron4j() {
return CronDefinitionBuilder.defineCron()
.withMinutes().and()
.withHours().and()
.withDayOfMonth().supportsL().and()
.withMonth().and()
.withDayOfWeek().withValidRange(0, 6).withMondayDoWValue(1).and()
.enforceStrictRanges()
.matchDayOfWeekAndDayOfMonth()
.instance();
}
/**
* Creates CronDefinition instance matching Quartz specification.
*
* <p>The cron expression is expected to be a string comprised of 6 or 7
* fields separated by white space. Fields can contain any of the allowed
* values, along with various combinations of the allowed special characters
* for that field. The fields are as follows:
*
* <table style="width:100%">
* <tr>
* <th>Field Name</th>
* <th>Mandatory</th>
* <th>Allowed Values</th>
* <th>Allowed Special Characters</th>
* </tr>
* <tr>
* <td>Seconds</td>
* <td>YES</td>
* <td>0-59</td>
* <td>* , - /</td>
* </tr>
* <tr>
* <td>Minutes</td>
* <td>YES</td>
* <td>0-59</td>
* <td>* , - /</td>
* </tr>
* <tr>
* <td>Hours</td>
* <td>YES</td>
* <td>0-23</td>
* <td>* , - /</td>
* </tr>
* <tr>
* <td>Day of month</td>
* <td>YES</td>
* <td>1-31</td>
* <td>* ? , - / L W</td>
* </tr>
* <tr>
* <td>Month</td>
* <td>YES</td>
* <td>1-12 or JAN-DEC</td>
* <td>* , -</td>
* </tr>
* <tr>
* <td>Day of week</td>
* <td>YES</td>
* <td>1-7 or SUN-SAT</td>
* <td>* ? , - / L #</td>
* </tr>
* <tr>
* <td>Year</td>
* <td>NO</td>
* <td>empty, 1970-2099</td>
* <td>* , - /</td>
* </tr>
* </table>
*
* <p>Thus in general Quartz cron expressions are as follows:
*
* <p>S M H DoM M DoW [Y]
*
* @return {@link CronDefinition} instance, never {@code null}
*/
private static CronDefinition quartz() {
return CronDefinitionBuilder.defineCron()
.withSeconds().and()
.withMinutes().and()
.withHours().and()
.withDayOfMonth().withValidRange(1, 32).supportsL().supportsW().supportsLW().supportsQuestionMark().and()
.withMonth().withValidRange(1, 13).and()
.withDayOfWeek().withValidRange(1, 7).withMondayDoWValue(2).supportsHash().supportsL().supportsQuestionMark().and()
.withYear().withValidRange(1970, 2099).optional().and()
.withCronValidation(CronConstraintsFactory.ensureEitherDayOfWeekOrDayOfMonth())
.instance();
}
/**
* Creates CronDefinition instance matching Spring specification.
*
* <p>The cron expression is expected to be a string comprised of 6
* fields separated by white space. Fields can contain any of the allowed
* values, along with various combinations of the allowed special characters
* for that field. The fields are as follows:
*
* <table style="width:100%">
* <tr>
* <th>Field Name</th>
* <th>Mandatory</th>
* <th>Allowed Values</th>
* <th>Allowed Special Characters</th>
* </tr>
* <tr>
* <td>Seconds</td>
* <td>YES</td>
* <td>0-59</td>
* <td>* , - /</td>
* </tr>
* <tr>
* <td>Minutes</td>
* <td>YES</td>
* <td>0-59</td>
* <td>* , - /</td>
* </tr>
* <tr>
* <td>Hours</td>
* <td>YES</td>
* <td>0-23</td>
* <td>* , - /</td>
* </tr>
* <tr>
* <td>Day of month</td>
* <td>YES</td>
* <td>1-31</td>
* <td>* ? , - /</td>
* </tr>
* <tr>
* <td>Month</td>
* <td>YES</td>
* <td>1-12 or JAN-DEC</td>
* <td>* , -</td>
* </tr>
* <tr>
* <td>Day of week</td>
* <td>YES</td>
* <td>1-7 or SUN-SAT</td>
* <td>* ? , - /</td>
* </tr>
* </table>
*
* <p>Thus in general Spring cron expressions are as follows:
*
* <p>S M H DoM M DoW
*
* @return {@link CronDefinition} instance, never {@code null}
*/
private static CronDefinition spring() {
return CronDefinitionBuilder.defineCron()
.withSeconds().and()
.withMinutes().and()
.withHours().and()
.withDayOfMonth().supportsQuestionMark().and()
.withMonth().and()
.withDayOfWeek().withValidRange(1, 7).withMondayDoWValue(2).supportsQuestionMark().and()
.instance();
}
/**
* Creates CronDefinition instance matching unix crontab specification.
*
* @return CronDefinition instance, never null;
*/
private static CronDefinition unixCrontab() {
return CronDefinitionBuilder.defineCron()
.withMinutes().and()
.withHours().and()
.withDayOfMonth().and()
.withMonth().and()
.withDayOfWeek().withValidRange(0, 7).withMondayDoWValue(1).withIntMapping(7, 0).and()
.enforceStrictRanges()
.instance();
}
/**
* Creates CronDefinition instance matching cronType specification.
*
* @param cronType - some cron type. If null, a RuntimeException will be raised.
* @return CronDefinition instance if definition is found; a RuntimeException otherwise.
*/
public static CronDefinition instanceDefinitionFor(final CronType cronType) {
switch (cronType) {
case CRON4J:
return cron4j();
case QUARTZ:
return quartz();
case UNIX:
return unixCrontab();
case SPRING:
return spring();
default:
throw new IllegalArgumentException(String.format("No cron definition found for %s", cronType));
}
}
}