MessageRule.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.messages;

import java.util.Collections;
import java.util.List;

import net.sourceforge.plantuml.core.UmlSource;
import net.sourceforge.plantuml.error.cron.CronExpression;

/**
 * One entry of the {@code rules} section of messages.yaml, already validated.
 *
 * Every condition present must be true; a missing condition is always true.
 */
public final class MessageRule {

	private final String id;
	private final List<CronExpression> crons;
	private final int from;
	private final int until;
	private final String sourceContains;
	private final double probability;
	private final MessageDefinition message;
	private final MessagePosition position;

	/**
	 * @param crons          OR between them, empty for every minute
	 * @param from           first day as 20270115, 0 for none
	 * @param until          last day as 20270215, 0 for none
	 * @param sourceContains lower case text to find in the source, or null
	 */
	MessageRule(String id, List<CronExpression> crons, int from, int until, String sourceContains, double probability,
			MessageDefinition message, MessagePosition position) {
		this.id = id;
		this.crons = Collections.unmodifiableList(crons);
		this.from = from;
		this.until = until;
		this.sourceContains = sourceContains;
		this.probability = probability;
		this.message = message;
		this.position = position;
	}

	/**
	 * Whether every condition of this rule is true, except the probability, which
	 * the caller draws.
	 *
	 * @param source the source of the diagram, or null when there is none (then a
	 *               rule with {@code source-contains} never matches)
	 */
	public boolean matches(MessageMoment now, UmlSource source) {
		if (from != 0 && now.getDate() < from)
			return false;

		if (until != 0 && now.getDate() > until)
			return false;

		if (crons.isEmpty() == false && matchesOneCron(now) == false)
			return false;

		if (sourceContains != null && (source == null || source.containsIgnoreCase(sourceContains) == false))
			return false;

		return true;
	}

	private boolean matchesOneCron(MessageMoment now) {
		for (CronExpression cron : crons)
			if (cron.matches(now.getMinute(), now.getHour(), now.getDayOfMonth(), now.getMonth(), now.getDayOfWeek()))
				return true;

		return false;
	}

	public String getId() {
		return id;
	}

	public List<CronExpression> getCrons() {
		return crons;
	}

	public double getProbability() {
		return probability;
	}

	public MessageDefinition getMessage() {
		return message;
	}

	public MessagePosition getPosition() {
		return position;
	}

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

}