-
Notifications
You must be signed in to change notification settings - Fork 270
Expand file tree
/
Copy pathCronMapper.java
More file actions
executable file
·331 lines (300 loc) · 14.2 KB
/
Copy pathCronMapper.java
File metadata and controls
executable file
·331 lines (300 loc) · 14.2 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
328
329
330
331
/*
* 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.mapper;
import com.cronutils.Function;
import com.cronutils.model.Cron;
import com.cronutils.model.CronType;
import com.cronutils.model.RebootCron;
import com.cronutils.model.SingleCron;
import com.cronutils.model.definition.CronDefinition;
import com.cronutils.model.definition.CronDefinitionBuilder;
import com.cronutils.model.definition.CronNicknames;
import com.cronutils.model.field.CronField;
import com.cronutils.model.field.CronFieldName;
import com.cronutils.model.field.constraint.FieldConstraints;
import com.cronutils.model.field.constraint.FieldConstraintsBuilder;
import com.cronutils.model.field.definition.DayOfWeekFieldDefinition;
import com.cronutils.model.field.definition.FieldDefinition;
import com.cronutils.model.field.expression.*;
import com.cronutils.model.field.expression.visitor.FieldExpressionVisitorAdaptor;
import com.cronutils.model.field.expression.visitor.ValueMappingFieldExpressionVisitor;
import com.cronutils.model.field.value.FieldValue;
import com.cronutils.model.field.value.IntegerFieldValue;
import com.cronutils.model.field.value.SpecialChar;
import com.cronutils.utils.Preconditions;
import com.cronutils.utils.VisibleForTesting;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import static com.cronutils.model.field.expression.Always.always;
import static com.cronutils.model.field.expression.QuestionMark.questionMark;
public class CronMapper {
private final Map<CronFieldName, Function<CronField, CronField>> mappings;
private final Function<Cron, Cron> cronRules;
private final CronDefinition to;
/**
* Constructor.
*
* @param from - source CronDefinition;
* if null a NullPointerException will be raised
* @param to - target CronDefinition;
* if null a NullPointerException will be raised
* @param cronRules - cron rules
*/
public CronMapper(final CronDefinition from, final CronDefinition to, final Function<Cron, Cron> cronRules) {
Preconditions.checkNotNull(from, "Source CronDefinition must not be null");
this.to = Preconditions.checkNotNull(to, "Destination CronDefinition must not be null");
this.cronRules = Preconditions.checkNotNull(cronRules, "CronRules must not be null");
mappings = new EnumMap<>(CronFieldName.class);
buildMappings(from, to);
}
/**
* Maps given cron to target cron definition.
*
* @param cron - Instance to be mapped;
* if null a NullPointerException will be raised
* @return new Cron instance, never null;
*/
public Cron map(final Cron cron) {
Preconditions.checkNotNull(cron, "Cron must not be null");
if(cron instanceof RebootCron){
if(this.to.getCronNicknames().contains(CronNicknames.REBOOT)){
return new RebootCron(this.to);
} else {
throw new IllegalArgumentException("The target cron definition does not support @reboot nickname");
}
}
final List<CronField> fields = new ArrayList<>();
for (final CronFieldName name : CronFieldName.values()) {
if (mappings.containsKey(name)) {
final CronField field = mappings.get(name).apply(cron.retrieve(name));
if (field != null) {
fields.add(field);
}
}
}
return cronRules.apply(new SingleCron(to, fields)).validate();
}
/**
* Creates a CronMapper that maps a cron4j expression to a quartz expression.
* @return a CronMapper for mapping from cron4j to quartz
*/
public static CronMapper fromCron4jToQuartz() {
return new CronMapper(
CronDefinitionBuilder.instanceDefinitionFor(CronType.CRON4J),
CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ),
setQuestionMark()
);
}
public static CronMapper fromQuartzToCron4j() {
return new CronMapper(
CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ),
CronDefinitionBuilder.instanceDefinitionFor(CronType.CRON4J),
sameCron()
);
}
public static CronMapper fromQuartzToUnix() {
return new CronMapper(
CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ),
CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX),
sameCron()
);
}
public static CronMapper fromUnixToQuartz() {
return new CronMapper(
CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX),
CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ),
setQuestionMark()
);
}
public static CronMapper fromQuartzToSpring() {
return new CronMapper(
CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ),
CronDefinitionBuilder.instanceDefinitionFor(CronType.SPRING),
setQuestionMark()
);
}
public static CronMapper fromSpringToQuartz() {
return new CronMapper(
CronDefinitionBuilder.instanceDefinitionFor(CronType.SPRING),
CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ),
setQuestionMark()
);
}
public static CronMapper sameCron(final CronDefinition cronDefinition) {
return new CronMapper(cronDefinition, cronDefinition, sameCron());
}
private static Function<Cron, Cron> sameCron() {
return cron -> cron;
}
private static Function<Cron, Cron> setQuestionMark() {
return cron -> {
final CronField dow = cron.retrieve(CronFieldName.DAY_OF_WEEK);
final CronField dom = cron.retrieve(CronFieldName.DAY_OF_MONTH);
if (dow == null && dom == null) {
return cron;
}
if (dow.getExpression() instanceof QuestionMark || dom.getExpression() instanceof QuestionMark) {
return cron;
}
final Map<CronFieldName, CronField> fields = new EnumMap<>(CronFieldName.class);
fields.putAll(cron.retrieveFieldsAsMap());
if (dow.getExpression() instanceof Always) {
fields.put(CronFieldName.DAY_OF_WEEK,
new CronField(CronFieldName.DAY_OF_WEEK, questionMark(), fields.get(CronFieldName.DAY_OF_WEEK).getConstraints()));
} else {
if (dom.getExpression() instanceof Always) {
fields.put(CronFieldName.DAY_OF_MONTH,
new CronField(CronFieldName.DAY_OF_MONTH, questionMark(), fields.get(CronFieldName.DAY_OF_MONTH).getConstraints()));
} else {
cron.validate();
}
}
return new SingleCron(cron.getCronDefinition(), new ArrayList<>(fields.values()));
};
}
/**
* Builds functions that map the fields from source CronDefinition to target.
*
* @param from - source CronDefinition
* @param to - target CronDefinition
*/
private void buildMappings(final CronDefinition from, final CronDefinition to) {
final Map<CronFieldName, FieldDefinition> sourceFieldDefinitions = getFieldDefinitions(from);
final Map<CronFieldName, FieldDefinition> destFieldDefinitions = getFieldDefinitions(to);
boolean startedDestMapping = false;
boolean startedSourceMapping = false;
for (final CronFieldName name : CronFieldName.values()) {
final FieldDefinition destinationFieldDefinition = destFieldDefinitions.get(name);
final FieldDefinition sourceFieldDefinition = sourceFieldDefinitions.get(name);
if (destinationFieldDefinition != null) {
startedDestMapping = true;
}
if (sourceFieldDefinition != null) {
startedSourceMapping = true;
}
if (startedDestMapping && destinationFieldDefinition == null) {
break;
}
//destination has fields before source definition starts. We default them to zero.
if (!startedSourceMapping && destinationFieldDefinition != null) {
mappings.put(name, returnOnZeroExpression(name));
}
//destination has fields after source definition was processed. We default them to always.
if (startedSourceMapping && sourceFieldDefinition == null && destinationFieldDefinition != null) {
mappings.put(name, returnAlwaysExpression(name));
}
if (sourceFieldDefinition == null || destinationFieldDefinition == null) {
continue;
}
if (CronFieldName.DAY_OF_WEEK.equals(name)) {
mappings.put(name, dayOfWeekMapping((DayOfWeekFieldDefinition) sourceFieldDefinition, (DayOfWeekFieldDefinition) destinationFieldDefinition));
} else if (CronFieldName.DAY_OF_MONTH.equals(name)) {
mappings.put(name, dayOfMonthMapping(sourceFieldDefinition, destinationFieldDefinition));
} else {
mappings.put(name, returnSameExpression());
}
}
}
private Map<CronFieldName, FieldDefinition> getFieldDefinitions(final CronDefinition from) {
final Map<CronFieldName, FieldDefinition> result = new EnumMap<>(CronFieldName.class);
for (final FieldDefinition fieldDefinition : from.getFieldDefinitions()) {
result.put(fieldDefinition.getFieldName(), fieldDefinition);
}
return result;
}
/**
* Creates a Function that returns same field.
*
* @return CronField -> CronField instance, never null
*/
@VisibleForTesting
static Function<CronField, CronField> returnSameExpression() {
return field -> field;
}
/**
* Creates a Function that returns a On instance with zero value.
*
* @param name - Cron field name
* @return new CronField -> CronField instance, never null
*/
@VisibleForTesting
static Function<CronField, CronField> returnOnZeroExpression(final CronFieldName name) {
return field -> {
final FieldConstraints constraints = FieldConstraintsBuilder.instance().forField(name).createConstraintsInstance();
return new CronField(name, new On(new IntegerFieldValue(0)), constraints);
};
}
/**
* Creates a Function that returns an Always instance.
*
* @param name - Cron field name
* @return new CronField -> CronField instance, never null
*/
@VisibleForTesting
static Function<CronField, CronField> returnAlwaysExpression(final CronFieldName name) {
return field -> new CronField(name, always(), FieldConstraintsBuilder.instance().forField(name).createConstraintsInstance());
}
private static IntegerFieldValue mapDayOfWeek(DayOfWeekFieldDefinition sourceDef, DayOfWeekFieldDefinition targetDef, IntegerFieldValue fieldValue) {
return new IntegerFieldValue(ConstantsMapper.weekDayMapping(sourceDef.getMondayDoWValue(), targetDef.getMondayDoWValue(), fieldValue.getValue()));
}
private static FieldValue<?> mapDayOfWeek(DayOfWeekFieldDefinition sourceDef, DayOfWeekFieldDefinition targetDef, FieldValue<?> fieldValue) {
if (fieldValue instanceof IntegerFieldValue) {
return mapDayOfWeek(sourceDef, targetDef, (IntegerFieldValue) fieldValue);
}
return fieldValue;
}
@VisibleForTesting
static Function<CronField, CronField> dayOfWeekMapping(final DayOfWeekFieldDefinition sourceDef, final DayOfWeekFieldDefinition targetDef) {
return field -> {
final FieldExpression expression = field.getExpression();
FieldExpression dest = null;
dest = expression.accept(new FieldExpressionVisitorAdaptor() {
public FieldExpression visit(Every every) {
return new Every(every.getExpression().accept(this), every.getPeriod());
}
public FieldExpression visit(On on) {
return new On(mapDayOfWeek(sourceDef, targetDef, on.getTime()), on.getSpecialChar());
}
@Override
public FieldExpression visit(Between between) {
return new Between(mapDayOfWeek(sourceDef, targetDef, between.getFrom()), mapDayOfWeek(sourceDef, targetDef, between.getTo()));
}
@Override
public FieldExpression visit(And and) {
And newAnd = new And();
for (FieldExpression expr : and.getExpressions()) {
newAnd.and(expr.accept(this));
}
return newAnd;
}
});
if (expression instanceof QuestionMark && !targetDef.getConstraints().getSpecialChars().contains(SpecialChar.QUESTION_MARK)) {
dest = always();
}
return new CronField(CronFieldName.DAY_OF_WEEK, dest, targetDef.getConstraints());
};
}
@VisibleForTesting
static Function<CronField, CronField> dayOfMonthMapping(final FieldDefinition sourceDef, final FieldDefinition targetDef) {
return field -> {
final FieldExpression expression = field.getExpression();
FieldExpression dest = expression;
if (expression instanceof QuestionMark && !targetDef.getConstraints().getSpecialChars().contains(SpecialChar.QUESTION_MARK)) {
dest = always();
}
return new CronField(CronFieldName.DAY_OF_MONTH, dest, targetDef.getConstraints());
};
}
}