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

io.bitsensor.lib.privacy.Re2jFinder Maven / Gradle / Ivy

package io.bitsensor.lib.privacy;

import com.google.re2j.Matcher;
import com.google.re2j.Pattern;
import io.dataapps.chlorine.finder.Finder;

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

import static com.google.re2j.Pattern.CASE_INSENSITIVE;
import static com.google.re2j.Pattern.MULTILINE;

public class Re2jFinder implements Finder {
    public static final int DEFAULT_FLAGS =
            CASE_INSENSITIVE | MULTILINE;

    private String name;
    Pattern pattern;

    /**
     * Construct a Regular expression Based Finder using a pattern and default flags.
     *
     * @param name    - name of the Finder
     * @param pattern - pattern of the regular expression.
     */
    public Re2jFinder(String name, String pattern) {
        this(name, pattern, DEFAULT_FLAGS);
    }

    /**
     * Construct a Regular expression Based Finder using pattern and flags.
     *
     * @param name    - name of the Finder
     * @param pattern - pattern of the regular expression.
     * @param flags   - Flags to be used while compiling the pattern.
     */
    public Re2jFinder(String name, String pattern, int flags) {
        this.name = name;
        this.pattern = Pattern.compile(pattern, flags);
    }

    /**
     * set the name of the Finder
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * get Finder's name.
     */
    public String getName() {
        return name;
    }

    /**
     * set the Pattern
     *
     * @param pattern
     */
    public void setPattern(Pattern pattern) {
        this.pattern = pattern;
    }

    /**
     * Get Pattern.
     */
    public Pattern getPattern() {
        return pattern;
    }

    /**
     * Scan the list of inputs using the finders. Return a list of actual matched values.
     *
     * @return a list of matches
     */
    public List find(String input) {
        List matches = new ArrayList<>();
        Matcher matcher = pattern.matcher(input);
        while (matcher.find()) {
            matches.add(removeCommas(
                    input.substring(matcher.start(), matcher.end())).trim());
        }
        return matches;
    }

    static String removeCommas(String match) {
        if (match.endsWith(",")) {
            match = match.substring(0, match.length() - 1);
        }
        if (match.startsWith(",")) {
            match = match.substring(1);
        }
        return match;
    }

    @Override
    public List find(Collection inputs) {
        List matches = new ArrayList<>();
        for (String input : inputs) {
            matches.addAll(find(input));
        }
        return matches;
    }

    private String replace(String input, String replacement) {
        Matcher matcher = pattern.matcher(input);
        return matcher.replaceAll(replacement);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy