org.jdesktop.swingx.sort.RowFilters Maven / Gradle / Ivy
Show all versions of swingx-all Show documentation
/*
* $Id$
*
* Copyright 2009 Sun Microsystems, Inc., 4150 Network Circle,
* Santa Clara, California 95054, U.S.A. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
package org.jdesktop.swingx.sort;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.swing.RowFilter;
import org.jdesktop.swingx.util.Contract;
/**
* Factory of additional RowFilter
s.
*
* Trigger is the missing of Pattern/Regex+matchflags factory method in core.
* Can't do much other than c&p core as both abstract base class GeneralFilter and
* concrete RowFilter are private. Expose the base as public for custom subclasses
*
* @author Jeanette Winzenburg
*/
@SuppressWarnings("unchecked")
public class RowFilters {
/**
* Returns a RowFilter
that uses a regular
* expression to determine which entries to include. Only entries
* with at least one matching value are included. For
* example, the following creates a RowFilter
that
* includes entries with at least one value starting with
* "a":
*
* RowFilter.regexFilter("^a");
*
*
* The returned filter uses {@link java.util.regex.Matcher#find}
* to test for inclusion. To test for exact matches use the
* characters '^' and '$' to match the beginning and end of the
* string respectively. For example, "^foo$" includes only rows whose
* string is exactly "foo" and not, for example, "food". See
* {@link java.util.regex.Pattern} for a complete description of
* the supported regular-expression constructs.
*
* @param regex the regular expression to filter on
* @param indices the indices of the values to check. If not supplied all
* values are evaluated
* @return a RowFilter
implementing the specified criteria
* @throws NullPointerException if regex
is
* null
* @throws IllegalArgumentException if any of the indices
* are < 0
* @throws PatternSyntaxException if regex
is
* not a valid regular expression.
* @see java.util.regex.Pattern
*/
public static RowFilter regexFilter(String regex,
int... indices) {
return regexFilter(0, regex, indices);
}
/**
* Returns a RowFilter
that uses a regular
* expression to determine which entries to include. Only entries
* with at least one matching value are included. For
* example, the following creates a RowFilter
that
* includes entries with at least one value starting with
* "a" ignoring case:
*
* RowFilter.regexFilter(Pattern.CASE_INSENSITIVE, "^a");
*
*
* The returned filter uses {@link java.util.regex.Matcher#find}
* to test for inclusion. To test for exact matches use the
* characters '^' and '$' to match the beginning and end of the
* string respectively. For example, "^foo$" includes only rows whose
* string is exactly "foo" and not, for example, "food". See
* {@link java.util.regex.Pattern} for a complete description of
* the supported regular-expression constructs.
*
* @param matchFlags
* Match flags, a bit mask that may include
* {@link Pattern#CASE_INSENSITIVE}, {@link Pattern#MULTILINE}, {@link Pattern#DOTALL},
* {@link Pattern#UNICODE_CASE}, {@link Pattern#CANON_EQ}, {@link Pattern#UNIX_LINES},
* {@link Pattern#LITERAL} and {@link Pattern#COMMENTS}
*
* @param regex the regular expression to filter on
* @param indices the indices of the values to check. If not supplied all
* values are evaluated
* @return a RowFilter
implementing the specified criteria
* @throws NullPointerException if regex
is
* null
* @throws IllegalArgumentException if any of the indices
* are < 0
* @throws IllegalArgumentException
* If bit values other than those corresponding to the defined
* match flags are set in flags
* @throws PatternSyntaxException if regex
is
* not a valid regular expression.
* @see java.util.regex.Pattern
*/
public static RowFilter regexFilter(int matchFlags, String regex,
int... indices) {
return regexFilter(Pattern.compile(regex, matchFlags), indices);
}
/**
* Returns a RowFilter
that uses a regular
* expression to determine which entries to include.
*
* @param pattern the Pattern to use for matching
* @param indices the indices of the values to check. If not supplied all
* values are evaluated
* @return a RowFilter
implementing the specified criteria
* @throws NullPointerException if pattern
is
* null
* @see java.util.regex.Pattern
*/
public static RowFilter regexFilter(Pattern pattern,
int... indices) {
return (RowFilter)new RegexFilter(pattern, indices);
}
/**
* C&P from core Swing to allow subclassing.
*/
public static abstract class GeneralFilter extends RowFilter