By Jeff Chilton
"Nothing is particularly hard if you divide it into small jobs."
-- Henry Ford (American Industrialist, 1863-1947)
We started out in Part I by creating a simple framework for the purpose of obtaining an ArrayList of Struts LabelValueBeans to support an html:options tag. In Part II, we added an additional implementing class to our portfolio, then went back and enhanced our base framework. This time, we'll reverse the order of things and first enhance our base framework, then add yet another implementing class.
Incremental Improvement #1: adding support for filtered lists
Currently, our little framework supports two kinds of lists: those with just the valid options and those with the valid options plus an additional option for leaving the field empty. In either case, the list of valid options is always the same. There are situations, however, where the available options will change based on some other event or data element.
For example, based on your security level, you may only be able to choose from four items off of a list while someone with a different security level may be authorized to select from six. In this case, the different lists of possible choices are filtered based on who you are.
To support filtered option lists, we can add two new methods to our interface that take as an argument an Object for the filter. We will need two of them to parallel the two existing methods that do not take a filter as an argument, one that includes the "required" flag and one that does not. We could have used a String for the filter, but by using an Object, we maximize the flexibility of the interface. That way certain implementations that require more than one filter String or use as a filter something other than a String can still implement the same methods. Listing 1 contains our revised interface.
package step.three;
import java.util.ArrayList;
/**
* This interface specifies the required methods for an "Option
* List" source, which returns the name/value pairs for the options
* related to an HTML "select" tag.
*/
public interface OptionListSource {
/**
* Returns a filtered array of LabelValueBean
* objects defining the available options.
*
* This method should return the same results as using the method
* getOptions(filter, true).
*
* @param filter an implementation-defined filter parameter
* further identifying or limiting the available options
* @return an ArrayList of LabelValueBean
* objects defining the available options
*/
public ArrayList getOptions(Object filter);
/**
* Returns a filtered array of LabelValueBean
* objects defining the available options.
*
* @param filter an implementation-defined filter parameter
* further identifying or limiting the available options
* @param required a boolean indicating whether or not input is
* required for this field. Setting this indicator to false will
* cause the generation of an additional option for no selection.
* @return an ArrayList of LabelValueBean
* objects defining the available options
*/
public ArrayList getOptions(Object filter, boolean required);
/**
* Returns an ArrayList of LabelValueBean
* objects defining the available options.
*
* This method should return the same results as using the method
* getOptions(true).
*
* @return an ArrayList of LabelValueBean
* objects defining the available options
*/
public ArrayList getOptions();
/**
* Returns an ArrayList of LabelValueBean
* objects defining the available options.
*
* @param required a boolean indicating whether or not input is
* required for this field. Setting this indicator to false will
* cause the generation of an additional option for no selection.
* @return an ArrayList of LabelValueBean
* objects defining the available options
*/
public ArrayList getOptions(boolean required);
}