|
| 1 | +/* |
| 2 | + * Copyright 2019 fahmpeermoh |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | + * limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package com.cronutils.converter; |
| 15 | + |
| 16 | +import java.time.ZoneId; |
| 17 | +import java.util.Calendar; |
| 18 | +import java.util.TimeZone; |
| 19 | + |
| 20 | +import org.apache.commons.lang3.StringUtils; |
| 21 | +import org.slf4j.Logger; |
| 22 | +import org.slf4j.LoggerFactory; |
| 23 | + |
| 24 | +import lombok.Setter; |
| 25 | + |
| 26 | +public class CronConverter { |
| 27 | + |
| 28 | + private static final Logger LOGGER = LoggerFactory |
| 29 | + .getLogger(CronConverter.class); |
| 30 | + |
| 31 | + private static final String CRON_FIELDS_SEPARATOR = " "; |
| 32 | + |
| 33 | + private String[] cronParts; |
| 34 | + |
| 35 | + private Calendar fromCalendar; |
| 36 | + |
| 37 | + private String sourceCron; |
| 38 | + |
| 39 | + private ZoneId sourceZoneId; |
| 40 | + |
| 41 | + private ZoneId targetZoneId; |
| 42 | + |
| 43 | + @Setter |
| 44 | + CronToCalendarTransformer toCalendarConverter = new CronToCalendarTransformer(); |
| 45 | + |
| 46 | + @Setter |
| 47 | + CalendarToCronTransformer toCronConverter = new CalendarToCronTransformer(); |
| 48 | + |
| 49 | + public CronConverter using(String cronExpression) { |
| 50 | + this.sourceCron = cronExpression; |
| 51 | + cronParts = cronExpression.split(CRON_FIELDS_SEPARATOR); |
| 52 | + LOGGER.debug("Cron '{}' split into {}", cronExpression, cronParts); |
| 53 | + return this; |
| 54 | + } |
| 55 | + |
| 56 | + public CronConverter from(ZoneId zoneId) { |
| 57 | + sourceZoneId = zoneId; |
| 58 | + fromCalendar = getCalendar(zoneId); |
| 59 | + toCalendarConverter.apply(cronParts, fromCalendar); |
| 60 | + LOGGER.debug("Calendar object built using cron :{} and zoneID {} => {}", |
| 61 | + cronParts, zoneId, fromCalendar); |
| 62 | + return this; |
| 63 | + } |
| 64 | + |
| 65 | + public CronConverter to(ZoneId zoneId) { |
| 66 | + targetZoneId = zoneId; |
| 67 | + Calendar toCalendar = getCalendar(zoneId); |
| 68 | + toCalendar.setTimeInMillis(fromCalendar.getTimeInMillis()); |
| 69 | + LOGGER.debug( |
| 70 | + "Calendar object built from calendar {} and zoneID {} => {}", |
| 71 | + fromCalendar, zoneId, toCalendar); |
| 72 | + toCronConverter.apply(cronParts, toCalendar); |
| 73 | + LOGGER.debug("cron after applying calendar {} => {}", toCalendar, |
| 74 | + cronParts); |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + public String convert() { |
| 79 | + String targetCron = StringUtils.join(cronParts, CRON_FIELDS_SEPARATOR); |
| 80 | + LOGGER.info("Converted CRON -- {} :[{}] => {} :[{}]", sourceZoneId, |
| 81 | + sourceCron, targetZoneId, targetCron); |
| 82 | + return targetCron; |
| 83 | + } |
| 84 | + |
| 85 | + private Calendar getCalendar(ZoneId id) { |
| 86 | + return Calendar.getInstance(TimeZone.getTimeZone(id)); |
| 87 | + } |
| 88 | +} |
0 commit comments