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

net.fortuna.ical4j.transform.RuleManager Maven / Gradle / Ivy

There is a newer version: 4.0.8
Show newest version
/*
 * Rfc5545RuleManager.java Feb 21, 2014
 * 
 * Copyright (c) 2014 1&1 Internet AG. All rights reserved.
 * 
 * $Id$
 */
package net.fortuna.ical4j.transform;

import net.fortuna.ical4j.model.Component;
import net.fortuna.ical4j.model.Property;
import net.fortuna.ical4j.transform.component.Rfc5545ComponentRule;
import net.fortuna.ical4j.transform.property.Rfc5545PropertyRule;

import java.util.LinkedHashSet;
import java.util.ServiceLoader;
import java.util.Set;

/**
 * Manages the rules that can be applied to ICS elements. New rules can be simply added by creating new implementations
 * of {@link Rfc5545PropertyRule} or {@link Rfc5545ComponentRule}.
 * 
 * @author corneliu dobrota
 * @author daniel grigore
 * @see Property
 * @see Component
 *
 */
public class RuleManager {

    private static final Set> PROPERTY_RULES = new LinkedHashSet>();
    private static final Set> COMPONENT_RULES = new LinkedHashSet>();

    static {
        for (Rfc5545PropertyRule rule : ServiceLoader.load(Rfc5545PropertyRule.class)) {
            if (rule.getSupportedType() == null) {
                throw new NullPointerException();
            }
            PROPERTY_RULES.add(rule);
        }
        for (Rfc5545ComponentRule rule : ServiceLoader.load(Rfc5545ComponentRule.class)) {
            if (rule.getSupportedType() == null) {
                throw new NullPointerException();
            }
            COMPONENT_RULES.add(rule);
        }
    }

    public static void applyTo(Property element) {
        for (Rfc5545PropertyRule rule : getSupportedRulesFor(element)) {
            rule.applyTo(element);
        }
    }

    public static void applyTo(Component element) {
        for (Rfc5545ComponentRule rule : getSupportedRulesFor(element)) {
            rule.applyTo(element);
        }
    }

    @SuppressWarnings("unchecked")
    private static Set> getSupportedRulesFor(Property element) {
        if (element == null) {
            throw new NullPointerException();
        }
        Set> rules = new LinkedHashSet>(1);
        for (Rfc5545Rule rule : PROPERTY_RULES) {
            if (rule.getSupportedType().isInstance(element)) {
                rules.add((Rfc5545PropertyRule) rule);
            }
        }
        return rules;
    }

    @SuppressWarnings("unchecked")
    private static Set> getSupportedRulesFor(Component element) {
        if (element == null) {
            throw new NullPointerException();
        }
        Set> rules = new LinkedHashSet>(1);
        for (Rfc5545Rule rule : COMPONENT_RULES) {
            if (rule.getSupportedType().isInstance(element)) {
                rules.add((Rfc5545ComponentRule) rule);
            }
        }
        return rules;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy