All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.fortuna.ical4j.validate.component.VEventValidator Maven / Gradle / Ivy

There is a newer version: 4.0.8
Show newest version
package net.fortuna.ical4j.validate.component;

import net.fortuna.ical4j.model.component.VAlarm;
import net.fortuna.ical4j.model.component.VEvent;
import net.fortuna.ical4j.validate.ComponentValidator;
import net.fortuna.ical4j.validate.ValidationException;
import net.fortuna.ical4j.validate.ValidationRule;
import net.fortuna.ical4j.validate.Validator;

import static net.fortuna.ical4j.model.Component.VALARM;
import static net.fortuna.ical4j.model.Property.*;
import static net.fortuna.ical4j.validate.ValidationRule.ValidationType.One;
import static net.fortuna.ical4j.validate.ValidationRule.ValidationType.OneOrLess;

public class VEventValidator extends ComponentValidator {

    private static final Validator itipValidator = new ComponentValidator<>(
            new ValidationRule<>(One, ACTION, TRIGGER),
            new ValidationRule<>(OneOrLess, DESCRIPTION, DURATION, REPEAT, SUMMARY));

    private final boolean alarmsAllowed;

    @SafeVarargs
    public VEventValidator(ValidationRule... rules) {
        this(true, rules);
    }

    @SafeVarargs
    public VEventValidator(boolean alarmsAllowed, ValidationRule... rules) {
        super(rules);
        this.alarmsAllowed = alarmsAllowed;
    }

    @Override
    public void validate(VEvent target) throws ValidationException {
        super.validate(target);

        if (alarmsAllowed) {
            target.getAlarms().getAll().forEach(itipValidator::validate);
        } else {
            Validator.assertFalse(input -> input.parallelStream().anyMatch(c -> c.getName().equals(VALARM)),
                    ASSERT_NONE_MESSAGE, false, target.getAlarms().getAll(), VALARM);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy