package com.onaro.util.jfc.tables.filter; import java.util.Collections; import java.util.List; import org.apache.commons.lang3.StringUtils; import com.onaro.util.jfc.tables.filter.matcher.MatchRange; public abstract class AbstractFilter implements Filter { /** * A pattern isn't valid if it doesn't match the syntax described in {@link com.onaro.util.jfc.tables.filter.NumberFilter}. */ protected boolean valid = true; /** * The "filter by" expression is a plain string (no special syntax). A value will match this filter if it contains * this string (regardless of character case). */ protected Object pattern; /** * Gets the "filter by" expression. * @return the "filter by" expression, null represents "no filter/pattern" */ public Object getPattern() { return pattern; } /** * Return a string representation of the given pattern, for display in a tooltip * @param pattern the pattern to format * @return the formatted representation */ public String formatPatternForTooltip(Object pattern) { // By default just call toString() on the pattern. if (pattern == null) return StringUtils.EMPTY; return pattern.toString(); } /** * Returns usage text to be used for tooltips when the filter's editor it active. * * @return usage text to be used for tooltips when the filter's editor it active. */ public String getUsageTooltip() { return(null); } /** * Tells if the "filter by" expression is using the correct syntax. * @return true if the pattern follows the syntax described in {@link com.onaro.util.jfc.tables.filter.NumberFilter} */ public boolean isValid() { return valid; } /** * This filter is active if a non-empty pattern was set. * @return true if the pattern is not empty */ public boolean isActive() { return pattern != null; } /** * Sets the "filter by" expression and parse it according to the syntax described in {@link com.onaro.util.jfc.tables.filter.NumberFilter}. The filter * will become "invalid" if the expression doesn't follow that syntax. * @param pattern the "filter by" expression */ public void setPattern(Object pattern) { this.pattern = pattern; valid = true; try { parsePattern(); } catch (FilterException e) { valid = false; } } /** * Default implementation. Override to use * @param value the filtered value * @return empty list */ public List getAcceptedRange(Object value) { return Collections.emptyList(); } @Override public AbstractFilter clone() { AbstractFilter clone = null; try { clone = (AbstractFilter)super.clone(); // Ensure the logic in setPattern and parsePattern occurs clone.setPattern(getPattern()); clone.parsePattern(); } catch (CloneNotSupportedException e) { // Should not happen } catch (FilterException e) { // May happen, but ok } return clone; } protected abstract void parsePattern() throws FilterException; }