<commit_before>/*
 * $Id$
 *
 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 */

package org.jdesktop.swingx.decorator;

import java.util.ArrayList;
import java.util.regex.Pattern;

/**
 * Pluggable pattern filter.
 *
 * @author <NAME>
 */
public class PatternFilter extends Filter implements PatternMatcher {
    private ArrayList<Integer>	toPrevious;
    protected Pattern	pattern = null;

    public PatternFilter() {
        this(null, 0, 0);
    }

    public PatternFilter(String regularExpr, int matchFlags, int col) {
        super(col);
        setPattern(regularExpr, matchFlags);
    }

    protected void init() {
		toPrevious = new ArrayList<Integer>();
    }

    public void setPattern(String regularExpr, int matchFlags) {
        if ((regularExpr == null) || (regularExpr.length() == 0)) {
            regularExpr = ".*";
        }
        setPattern(Pattern.compile(regularExpr, matchFlags));
    }

    /**
     * Sets the pattern used by this filter for matching.
     *
     * @param pattern the pattern used by this filter for matching
     * @see java.util.regex.Pattern
     */
    public void setPattern(Pattern pattern) {
        this.pattern = pattern;
        refresh();
    }

    /**
     * Returns the pattern used by this filter for matching.
     *
     * @return the pattern used by this filter for matching
     * @see java.util.regex.Pattern
     */
    public Pattern getPattern() {
        return pattern;
    }

    /**
     * Resets the internal row mappings from this filter to the previous filter.
     */
    protected void reset() {
        toPrevious.clear();
        int inputSize = getInputSize();
        fromPrevious = new int[inputSize];  // fromPrevious is inherited protected
        for (int i = 0; i < inputSize; i++) {
            fromPrevious[i] = -1;
        }
    }

    protected void filter() {
        if (pattern != null) {
            int inputSize = getInputSize();
            int current = 0;
            for (int i = 0; i < inputSize; i++) {
                if (test(i)) {
                    toPrevious.add(new Integer(i));
                    // generate inverse map entry while we are here
                    fromPrevious[i] = current++;
                }
            }
        }
    }

    public boolean test(int row) {
        if (pattern == null) {
            return false;
        }

        // ask the adapter is the column should be includes
        if (!adapter.isTestable(getColumnIndex())) {
            return false; 
        }

        Object	value = getInputValue(row, getColumnIndex());

        if (value == null) {
            return false;
        }
        else {
            boolean	matches = pattern.matcher(value.toString()).find();
            return matches;
        }
    }

    public int getSize() {
        return toPrevious.size();
    }

    protected int mapTowardModel(int row) {
        return toPrevious.get(row);
    }
}
<commit_msg>consistent behaviour of setPattern(Pattern) and setPattern(String) on null parameter<commit_after>/*
 * $Id$
 *
 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 */

package org.jdesktop.swingx.decorator;

import java.util.ArrayList;
import java.util.regex.Pattern;

/**
 * Pluggable pattern filter.
 *
 * @author <NAME>
 */
public class PatternFilter extends Filter implements PatternMatcher {
    private ArrayList<Integer>	toPrevious;
    protected Pattern	pattern = null;

    public PatternFilter() {
        this(null, 0, 0);
    }

    public PatternFilter(String regularExpr, int matchFlags, int col) {
        super(col);
        setPattern(regularExpr, matchFlags);
    }

    protected void init() {
		toPrevious = new ArrayList<Integer>();
    }

    public void setPattern(String regularExpr, int matchFlags) {
        if ((regularExpr == null) || (regularExpr.length() == 0)) {
            regularExpr = ".*";
        }
        setPattern(Pattern.compile(regularExpr, matchFlags));
    }

    /**
     * Sets the pattern used by this filter for matching.
     *
     * @param pattern the pattern used by this filter for matching
     * @see java.util.regex.Pattern
     */
    public void setPattern(Pattern pattern) {
        if (pattern == null) {
            setPattern(null, 0);
        } else {
            this.pattern = pattern;
            refresh();
        }
    }

    /**
     * Returns the pattern used by this filter for matching.
     *
     * @return the pattern used by this filter for matching
     * @see java.util.regex.Pattern
     */
    public Pattern getPattern() {
        return pattern;
    }

    /**
     * Resets the internal row mappings from this filter to the previous filter.
     */
    protected void reset() {
        toPrevious.clear();
        int inputSize = getInputSize();
        fromPrevious = new int[inputSize];  // fromPrevious is inherited protected
        for (int i = 0; i < inputSize; i++) {
            fromPrevious[i] = -1;
        }
    }

    protected void filter() {
        if (pattern != null) {
            int inputSize = getInputSize();
            int current = 0;
            for (int i = 0; i < inputSize; i++) {
                if (test(i)) {
                    toPrevious.add(new Integer(i));
                    // generate inverse map entry while we are here
                    fromPrevious[i] = current++;
                }
            }
        }
    }

    /**
     * @param row
     * @return
     */
    public boolean test(int row) {
        // PENDING: wrong false?
        // null pattern should be treated the same as null searchString
        // which is open
        // !testable should be clarified to mean "ignore" when filtering
        if (pattern == null) {
            return false;
        }

        // ask the adapter is the column should be includes
        if (!adapter.isTestable(getColumnIndex())) {
            return false; 
        }

        Object	value = getInputValue(row, getColumnIndex());

        if (value == null) {
            return false;
        }
        else {
            boolean	matches = pattern.matcher(value.toString()).find();
            return matches;
        }
    }

    public int getSize() {
        return toPrevious.size();
    }

    protected int mapTowardModel(int row) {
        return toPrevious.get(row);
    }
}

{'fraction_non_alphanumeric': 0.1047087980173482, 'fraction_numerical': 0.006350681536555143, 'mean_word_length': 1.8864550737594994, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3519404', 'n_tokens_mistral': 1788, 'n_tokens_neox': 1699, 'n_words': 536}