LaunchExclusionConfig.java 2.34 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.cq.wcm.launches.impl.msm.utils;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LaunchExclusionConfig {
    public static final LaunchExclusionConfig EMPTY_EXCLUSION_CONFIG = new LaunchExclusionConfig(null, null, null);
    private static final Set<Pattern> EMPTY_PATTERNS = Collections.unmodifiableSet(Collections.emptySet());
    private Set<Pattern> pagePropertyPatterns;
    private Set<Pattern> paragraphItemPatterns;
    private Set<Pattern> nodeTypePatterns;

    public LaunchExclusionConfig(Set<String> excludedPageProperties, Set<String> excludedParagraphItems, Set<String> excludedNodeTypes) {
        this.pagePropertyPatterns = this.buildPatterns(excludedPageProperties);
        this.paragraphItemPatterns = this.buildPatterns(excludedParagraphItems);
        this.nodeTypePatterns = this.buildPatterns(excludedNodeTypes);
    }

    public boolean matchExcludedParagraphItem(String itemName) {
        return this.match(itemName, this.getParagraphItems());
    }

    public boolean matchExcludedPageProperty(String propertyName) {
        return this.match(propertyName, this.getPageProperties());
    }

    public boolean matchExcludedNodeType(String nodeType) {
        return this.match(nodeType, this.getNodeTypes());
    }

    private Set<Pattern> getPageProperties() {
        return this.pagePropertyPatterns;
    }

    private Set<Pattern> getParagraphItems() {
        return this.paragraphItemPatterns;
    }

    private Set<Pattern> getNodeTypes() {
        return this.nodeTypePatterns;
    }

    private boolean match(String data, Set<Pattern> patterns) {
        if (data == null) {
            return false;
        }
        for (Pattern pattern : patterns) {
            if (!pattern.matcher(data).matches()) continue;
            return true;
        }
        return false;
    }

    private Set<Pattern> buildPatterns(Set<String> patterns) {
        if (patterns != null && patterns.size() > 0) {
            HashSet<Pattern> pats = new HashSet<Pattern>(patterns.size());
            for (String pattern : patterns) {
                pats.add(Pattern.compile(pattern));
            }
            return Collections.unmodifiableSet(pats);
        }
        return EMPTY_PATTERNS;
    }
}