CronExpression.java

/* ========================================================================
 * PlantUML : a free UML diagram generator
 * ========================================================================
 *
 * (C) Copyright 2009-2024, Arnaud Roques
 *
 * Project Info:  https://plantuml.com
 * 
 * If you like this project or if you find it useful, you can support us at:
 * 
 * https://plantuml.com/patreon (only 1$ per month!)
 * https://plantuml.com/paypal
 * 
 * This file is part of PlantUML.
 *
 * PlantUML is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PlantUML distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 *
 *
 * Original Author:  Arnaud Roques
 *
 */
package net.sourceforge.plantuml.error.cron;

import java.util.ArrayList;
import java.util.List;

/**
 * A cron expression of five fields:
 * {@code minute hour day-of-month month day-of-week}.
 *
 * Unlike Unix cron, the five fields are <b>always</b> combined with AND,
 * including day of month and day of week: {@code * * 13 * 5} only matches on
 * Friday the 13th. An OR is written with several expressions.
 *
 * The matching only works on integers given by the caller: this class never
 * reads the clock, and uses neither regular expressions nor {@code java.time},
 * to stay easy to run under TeaVM.
 *
 * See {@code docs/MESSAGES_ERREUR.md}.
 */
public final class CronExpression {

	private final String source;
	private final CronField minute;
	private final CronField hour;
	private final CronField dayOfMonth;
	private final CronField month;
	private final CronField dayOfWeek;

	private CronExpression(String source, CronField minute, CronField hour, CronField dayOfMonth, CronField month,
			CronField dayOfWeek) {
		this.source = source;
		this.minute = minute;
		this.hour = hour;
		this.dayOfMonth = dayOfMonth;
		this.month = month;
		this.dayOfWeek = dayOfWeek;
	}

	public static CronExpression parse(String expression) throws CronSyntaxException {
		if (expression == null)
			throw new CronSyntaxException("Missing cron expression");

		final String[] fields = splitFields(expression);
		if (fields.length != 5)
			throw new CronSyntaxException(
					"Expected 5 fields but found " + fields.length + " in cron expression \"" + expression + "\"");

		return new CronExpression(expression.trim(), //
				CronField.parse(CronFieldType.MINUTE, fields[0]), //
				CronField.parse(CronFieldType.HOUR, fields[1]), //
				CronField.parse(CronFieldType.DAY_OF_MONTH, fields[2]), //
				CronField.parse(CronFieldType.MONTH, fields[3]), //
				CronField.parse(CronFieldType.DAY_OF_WEEK, fields[4]));
	}

	// Fields are separated by any run of spaces or tabs
	private static String[] splitFields(String expression) {
		final List<String> result = new ArrayList<>();
		int i = 0;
		final int len = expression.length();
		while (i < len) {
			while (i < len && isBlank(expression.charAt(i)))
				i++;

			if (i == len)
				break;

			final int start = i;
			while (i < len && isBlank(expression.charAt(i)) == false)
				i++;

			result.add(expression.substring(start, i));
		}
		return result.toArray(new String[result.size()]);
	}

	private static boolean isBlank(char c) {
		return c == ' ' || c == '\t';
	}

	/**
	 * Whether this expression matches the given moment.
	 *
	 * @param minute     0-59
	 * @param hour       0-23
	 * @param dayOfMonth 1-31
	 * @param month      1-12 (January is 1)
	 * @param dayOfWeek  0-6 (Sunday is 0; 7 is also accepted for Sunday)
	 */
	public boolean matches(int minute, int hour, int dayOfMonth, int month, int dayOfWeek) {
		return this.minute.matches(minute) && this.hour.matches(hour) && this.dayOfMonth.matches(dayOfMonth)
				&& this.month.matches(month) && this.dayOfWeek.matches(dayOfWeek);
	}

	@Override
	public String toString() {
		return source;
	}

}