CronField.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;

/**
 * One field of a cron expression, for instance {@code 1,8,13,55} or
 * {@code 1-5}, stored as the set of the values it matches.
 *
 * Accepted syntax, items being separated by commas:
 * <ul>
 * <li>{@code *}: every value</li>
 * <li>{@code 15}: one value</li>
 * <li>{@code 9-18}: an inclusive range</li>
 * <li><code>*&#47;10</code> or {@code 0-30/5}: a step over every value or over
 * a range</li>
 * </ul>
 *
 * This class does not use regular expressions nor {@code java.time}, to stay
 * easy to run under TeaVM.
 */
public final class CronField {

	// Every bound is lower than 64: one bit per value is enough
	private final long values;
	private final CronFieldType type;

	private CronField(CronFieldType type, long values) {
		this.type = type;
		this.values = values;
	}

	public static CronField parse(CronFieldType type, String field) throws CronSyntaxException {
		if (field == null || field.length() == 0)
			throw new CronSyntaxException("Empty " + type.getLabel() + " field");

		long values = 0;
		int start = 0;
		while (true) {
			final int comma = field.indexOf(',', start);
			final String item = comma == -1 ? field.substring(start) : field.substring(start, comma);
			values |= parseItem(type, item, field);
			if (comma == -1)
				break;
			start = comma + 1;
		}
		return new CronField(type, values);
	}

	private static long parseItem(CronFieldType type, String item, String field) throws CronSyntaxException {
		if (item.length() == 0)
			throw new CronSyntaxException("Empty item in " + type.getLabel() + " field \"" + field + "\"");

		final String range;
		final int step;
		final int slash = item.indexOf('/');
		if (slash == -1) {
			range = item;
			step = 1;
		} else {
			range = item.substring(0, slash);
			step = parseStep(type, item.substring(slash + 1), field);
			if (range.equals("*") == false && range.indexOf('-') == -1)
				throw new CronSyntaxException(
						"A step needs * or a range before it in " + type.getLabel() + " field \"" + field + "\"");
		}

		final int low;
		final int high;
		if (range.equals("*")) {
			low = type.getMin();
			high = type.getStarMax();
		} else {
			final int dash = range.indexOf('-');
			if (dash == -1) {
				low = parseValue(type, range, field);
				high = low;
			} else {
				low = parseValue(type, range.substring(0, dash), field);
				high = parseValue(type, range.substring(dash + 1), field);
				if (low > high)
					throw new CronSyntaxException(
							"Decreasing range \"" + range + "\" in " + type.getLabel() + " field \"" + field + "\"");
			}
		}

		long result = 0;
		for (int v = low; v <= high; v += step)
			result |= 1L << type.normalize(v);

		return result;
	}

	private static int parseStep(CronFieldType type, String s, String field) throws CronSyntaxException {
		final int step = parseNumber(s);
		if (step < 1 || step > type.getMax())
			throw new CronSyntaxException(
					"Invalid step \"" + s + "\" in " + type.getLabel() + " field \"" + field + "\"");

		return step;
	}

	private static int parseValue(CronFieldType type, String s, String field) throws CronSyntaxException {
		final int value = parseNumber(s);
		if (value == -1)
			throw new CronSyntaxException(
					"Invalid value \"" + s + "\" in " + type.getLabel() + " field \"" + field + "\"");

		if (value < type.getMin() || value > type.getMax())
			throw new CronSyntaxException("Value " + value + " out of range " + type.getMin() + "-" + type.getMax()
					+ " in " + type.getLabel() + " field \"" + field + "\"");

		return value;
	}

	/**
	 * A small unsigned decimal number, or -1. Longer strings are refused so that
	 * the result cannot overflow.
	 */
	private static int parseNumber(String s) {
		if (s.length() == 0 || s.length() > 4)
			return -1;

		int result = 0;
		for (int i = 0; i < s.length(); i++) {
			final char c = s.charAt(i);
			if (c < '0' || c > '9')
				return -1;

			result = result * 10 + (c - '0');
		}
		return result;
	}

	public CronFieldType getType() {
		return type;
	}

	/**
	 * Whether this field matches the given value. For the day of week, both 0 and 7
	 * mean Sunday.
	 */
	public boolean matches(int value) {
		if (value < type.getMin() || value > type.getMax())
			return false;

		return (values & (1L << type.normalize(value))) != 0;
	}

}