Part (a)
public class OrFilter implements Filter
{
private List<Filter> filters;
public OrFilter(Filter f1, Filter f2)
{
filters = new LinkedList<Filter>(); 1
filters.add(f1);
filters.add(f2);
}
public void add(Filter f)
{
filters.add(f);
}
public boolean accept(String text)
{
for (Filter f : filters)
if (f.accept(text))
return true;
return false;
}
}
Notes:
- Or
ArrayList<Filter>
Part (b)
/** @param desirable contains strings that are allowed
* Precondition: desirable.length > 1
* @param notAllowed the string that is not allowed
* @return a Filter that accepts strings that contain at least one string
* in desirable and do not contain notAllowed.
*/
public static Filter buildFilter(String[] desirable,
String notAllowed)
{
OrFilter orF = new OrFilter(new SimpleFilter(desirable[0]),
new SimpleFilter(desirable[1]));
for (int i = 2; i < desirable.length; i++)
orF.add(new SimpleFilter(desirable[i]));
NotFilter notF = new NotFilter(new SimpleFilter(notAllowed));
return new AndFilter(orF, notF);
}
|