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

de.malkusch.whoisServerList.publicSuffixList.index.tree.TreeIndex Maven / Gradle / Ivy

package de.malkusch.whoisServerList.publicSuffixList.index.tree;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import net.jcip.annotations.Immutable;

import org.apache.commons.lang3.StringUtils;

import de.malkusch.whoisServerList.publicSuffixList.index.Index;
import de.malkusch.whoisServerList.publicSuffixList.rule.Rule;

/**
 * Tree based implementation with O(log(n)) complexity.
 *
 * @author [email protected]
 * @see Donations
 */
@Immutable
final class TreeIndex extends Index {

    /**
     * The root.
     */
    private final ImmutableNode root;

    /**
     * Sets the tree root.
     *
     * @param root  the root, not null
     */
    TreeIndex(final ImmutableNode root) {
        this.root = root;
    }

    /**
     * Returns the canonical label.
     *
     * I.e. "DE" equals "de".
     *
     * @param label  the label in any case, null returns null
     * @return the canonical label, or null
     */
    static String getCanonicalLabel(final String label) {
        return StringUtils.lowerCase(label);
    }

    @Override
    protected Collection findRules(final String domain) {
        Collection rules = new ArrayList<>();
        for (ImmutableNode node : root.findNodes(getCanonicalLabel(domain))) {
            Rule rule = node.getRule();
            if (rule != null) {
                rules.add(rule);

            }
        }
        return rules;
    }

    @Override
    public List getRules() {
        List rules = new ArrayList<>();
        for (ImmutableNode node : root.getDescendants()) {
            Rule rule = node.getRule();
            if (rule != null) {
                rules.add(rule);

            }
        }
        return rules;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy