package com.onaro.util.jfc.tables.filter; import com.onaro.sanscreen.client.view.tabular.value.*; import com.onaro.util.enumeration.*; import java.util.*; /** * This filter supports filtering based on either enumeration or numeric values. * If the pattern matches one of the enumeration names, any rows with that name will pass the filter. * Otheriwse, the pattersn of {@link NumberFilter} is supported. */ public class NumberEnumFilter extends NumberColumnFilter { /** * Maps between the name of the enumeration (lowercase) to the enumeration itself * When an enum pattern is written in the filter editor, the enumeration is looked for in this map */ private Map namesToValues = new HashMap(); /** * Holds patterns that are enumerations (not numbers) */ private EnumPresentation.Attributes enumPattern = null; public NumberEnumFilter(EnumPresentation.Attributes[] enumValues) { if (enumValues == null) return; //keeps a mapping between the supported enum names and values for (int i = 0; i < enumValues.length; i++) { String name = enumValues[i].name.toLowerCase(); namesToValues.put(name, enumValues[i]); } } protected void parsePattern() throws FilterException{ super.parsePattern(); enumPattern = isActive() ? namesToValues.get(pattern.toString().toLowerCase()) : null; } public boolean isAccepted(Object value) { //if the pattern is enum, accept only values with that name if (enumPattern != null) { return enumPattern.equals(value); } else { //if the value is a number, pass it to the NumberFilter, otherwise (e.g. enum value when the pattern is not enum) reject. return value instanceof NumberValue && super.isAccepted(value); } } //the filter is valid if either enumeration is defined or a valid number filter is defined public boolean isValid() { return enumPattern != null || super.isValid(); } }