|
25 | 25 | import com.cronutils.model.field.constraint.FieldConstraintsBuilder; |
26 | 26 | import com.cronutils.model.field.definition.DayOfWeekFieldDefinition; |
27 | 27 | import com.cronutils.model.field.definition.FieldDefinition; |
28 | | -import com.cronutils.model.field.expression.Always; |
29 | | -import com.cronutils.model.field.expression.FieldExpression; |
30 | | -import com.cronutils.model.field.expression.On; |
31 | | -import com.cronutils.model.field.expression.QuestionMark; |
| 28 | +import com.cronutils.model.field.expression.*; |
| 29 | +import com.cronutils.model.field.expression.visitor.FieldExpressionVisitorAdaptor; |
32 | 30 | import com.cronutils.model.field.expression.visitor.ValueMappingFieldExpressionVisitor; |
| 31 | +import com.cronutils.model.field.value.FieldValue; |
33 | 32 | import com.cronutils.model.field.value.IntegerFieldValue; |
34 | 33 | import com.cronutils.model.field.value.SpecialChar; |
35 | 34 | import com.cronutils.utils.Preconditions; |
@@ -261,27 +260,45 @@ static Function<CronField, CronField> returnAlwaysExpression(final CronFieldName |
261 | 260 | return field -> new CronField(name, always(), FieldConstraintsBuilder.instance().forField(name).createConstraintsInstance()); |
262 | 261 | } |
263 | 262 |
|
| 263 | + private static IntegerFieldValue mapDayOfWeek(DayOfWeekFieldDefinition sourceDef, DayOfWeekFieldDefinition targetDef, IntegerFieldValue fieldValue) { |
| 264 | + return new IntegerFieldValue(ConstantsMapper.weekDayMapping(sourceDef.getMondayDoWValue(), targetDef.getMondayDoWValue(), fieldValue.getValue())); |
| 265 | + } |
| 266 | + |
| 267 | + private static FieldValue<?> mapDayOfWeek(DayOfWeekFieldDefinition sourceDef, DayOfWeekFieldDefinition targetDef, FieldValue<?> fieldValue) { |
| 268 | + if (fieldValue instanceof IntegerFieldValue) { |
| 269 | + return mapDayOfWeek(sourceDef, targetDef, (IntegerFieldValue) fieldValue); |
| 270 | + } |
| 271 | + return fieldValue; |
| 272 | + } |
| 273 | + |
264 | 274 | @VisibleForTesting |
265 | 275 | static Function<CronField, CronField> dayOfWeekMapping(final DayOfWeekFieldDefinition sourceDef, final DayOfWeekFieldDefinition targetDef) { |
266 | 276 | return field -> { |
267 | 277 | final FieldExpression expression = field.getExpression(); |
268 | 278 | FieldExpression dest = null; |
269 | | - dest = expression.accept( |
270 | | - new ValueMappingFieldExpressionVisitor( |
271 | | - fieldValue -> { |
272 | | - if (fieldValue instanceof IntegerFieldValue) { |
273 | | - return new IntegerFieldValue( |
274 | | - ConstantsMapper.weekDayMapping( |
275 | | - sourceDef.getMondayDoWValue(), |
276 | | - targetDef.getMondayDoWValue(), |
277 | | - ((IntegerFieldValue) fieldValue).getValue() |
278 | | - ) |
279 | | - ); |
280 | | - } |
281 | | - return fieldValue; |
282 | | - } |
283 | | - ) |
284 | | - ); |
| 279 | + dest = expression.accept(new FieldExpressionVisitorAdaptor() { |
| 280 | + public FieldExpression visit(Every every) { |
| 281 | + return new Every(every.getExpression().accept(this), every.getPeriod()); |
| 282 | + } |
| 283 | + |
| 284 | + public FieldExpression visit(On on) { |
| 285 | + return new On(mapDayOfWeek(sourceDef, targetDef, on.getTime()), on.getSpecialChar()); |
| 286 | + } |
| 287 | + |
| 288 | + @Override |
| 289 | + public FieldExpression visit(Between between) { |
| 290 | + return new Between(mapDayOfWeek(sourceDef, targetDef, between.getFrom()), mapDayOfWeek(sourceDef, targetDef, between.getTo())); |
| 291 | + } |
| 292 | + |
| 293 | + @Override |
| 294 | + public FieldExpression visit(And and) { |
| 295 | + And newAnd = new And(); |
| 296 | + for (FieldExpression expr : and.getExpressions()) { |
| 297 | + newAnd.and(expr.accept(this)); |
| 298 | + } |
| 299 | + return newAnd; |
| 300 | + } |
| 301 | + }); |
285 | 302 |
|
286 | 303 | if (expression instanceof QuestionMark && !targetDef.getConstraints().getSpecialChars().contains(SpecialChar.QUESTION_MARK)) { |
287 | 304 | dest = always(); |
|
0 commit comments