001: /*
002: * $Id: AndFilter.java,v 1.1 2005/05/27 19:32:25 cburkey Exp $
003: */
004: package com.openedit.util.strainer;
005:
006: /**
007: * This is a combinatorial filter which ANDs together all of its sub-filters.
008: *
009: * @author Eric Galluzzo
010: */
011: public class AndFilter implements CompositeFilter {
012: protected Filter[] fieldFilters;
013:
014: /**
015: * This constructor should only be used for JavaBean-style creation.
016: */
017: public AndFilter() {
018: }
019:
020: /**
021: * Create a filter that ANDs together all the given sub-filters.
022: *
023: * @param inFilters The sub-filters to AND together
024: */
025: public AndFilter(Filter[] inFilters) {
026: fieldFilters = inFilters;
027: }
028:
029: /**
030: * Create a filter that ANDs together both of the given sub-filters.
031: *
032: * @param inFilter1 The first sub-filter
033: * @param inFilter2 The second sub-filter
034: */
035: public AndFilter(Filter inFilter1, Filter inFilter2) {
036: fieldFilters = new Filter[] { inFilter1, inFilter2 };
037: }
038:
039: /**
040: * Retrieve this filter's sub-filters.
041: *
042: * @return This filter's sub-filters
043: */
044: public Filter[] getFilters() {
045: return fieldFilters;
046: }
047:
048: /**
049: * Set this filter's sub-filters.
050: *
051: * @param newFilters The new sub-filters
052: */
053: public void setFilters(Filter[] newFilters) {
054: fieldFilters = newFilters;
055: }
056:
057: /**
058: * Determine whether the given object passes this filter by ANDing together
059: * all the sub-filters.
060: *
061: * @param inObj The object to check
062: *
063: * @return <code>true</code> if the object passes all of the sub-filters,
064: * <code>false</code> otherwise.
065: */
066: public boolean passes(Object inObj) throws FilterException {
067: for (int i = 0; i < fieldFilters.length; i++) {
068: if (!fieldFilters[i].passes(inObj)) {
069: return false;
070: }
071: }
072:
073: return true;
074: }
075:
076: /* (non-Javadoc)
077: * @see com.openedit.util.strainer.Filter#accept(com.openedit.util.strainer.FilterVisitor)
078: */
079: public void accept(FilterVisitor inFilterVisitor)
080: throws FilterException {
081: if (inFilterVisitor instanceof AndFilterVisitor) {
082: ((AndFilterVisitor) inFilterVisitor).visitAndFilter(this );
083: }
084: }
085:
086: /* (non-Javadoc)
087: * @see java.lang.Object#toString()
088: */
089: public String toString() {
090: StringBuffer buffer = new StringBuffer();
091: Filter[] filters = getFilters();
092: for (int i = 0; i < filters.length; i++) {
093: if (i > 0) {
094: buffer.append(" and ");
095: }
096: buffer.append("(");
097: buffer.append(filters[i].toString());
098: buffer.append(")");
099: }
100: return buffer.toString();
101: }
102: }
|