KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > util > strainer > AndFilter


1 /*
2  * $Id: AndFilter.java,v 1.1 2005/05/27 19:32:25 cburkey Exp $
3  */

4 package com.openedit.util.strainer;
5
6 /**
7  * This is a combinatorial filter which ANDs together all of its sub-filters.
8  *
9  * @author Eric Galluzzo
10  */

11 public class AndFilter implements CompositeFilter
12 {
13     protected Filter[] fieldFilters;
14
15     /**
16      * This constructor should only be used for JavaBean-style creation.
17      */

18     public AndFilter()
19     {
20     }
21
22     /**
23      * Create a filter that ANDs together all the given sub-filters.
24      *
25      * @param inFilters The sub-filters to AND together
26      */

27     public AndFilter(Filter[] inFilters)
28     {
29         fieldFilters = inFilters;
30     }
31
32     /**
33      * Create a filter that ANDs together both of the given sub-filters.
34      *
35      * @param inFilter1 The first sub-filter
36      * @param inFilter2 The second sub-filter
37      */

38     public AndFilter(Filter inFilter1, Filter inFilter2)
39     {
40         fieldFilters = new Filter[] { inFilter1, inFilter2 };
41     }
42
43     /**
44      * Retrieve this filter's sub-filters.
45      *
46      * @return This filter's sub-filters
47      */

48     public Filter[] getFilters()
49     {
50         return fieldFilters;
51     }
52
53     /**
54      * Set this filter's sub-filters.
55      *
56      * @param newFilters The new sub-filters
57      */

58     public void setFilters(Filter[] newFilters)
59     {
60         fieldFilters = newFilters;
61     }
62
63     /**
64      * Determine whether the given object passes this filter by ANDing together
65      * all the sub-filters.
66      *
67      * @param inObj The object to check
68      *
69      * @return <code>true</code> if the object passes all of the sub-filters,
70      * <code>false</code> otherwise.
71      */

72     public boolean passes(Object JavaDoc inObj) throws FilterException
73     {
74         for (int i = 0; i < fieldFilters.length; i++)
75         {
76             if (!fieldFilters[i].passes(inObj))
77             {
78                 return false;
79             }
80         }
81
82         return true;
83     }
84
85     /* (non-Javadoc)
86      * @see com.openedit.util.strainer.Filter#accept(com.openedit.util.strainer.FilterVisitor)
87      */

88     public void accept(FilterVisitor inFilterVisitor) throws FilterException
89     {
90         if (inFilterVisitor instanceof AndFilterVisitor)
91         {
92             ((AndFilterVisitor) inFilterVisitor).visitAndFilter(this);
93         }
94     }
95
96     /* (non-Javadoc)
97      * @see java.lang.Object#toString()
98      */

99     public String JavaDoc toString()
100     {
101         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
102         Filter[] filters = getFilters();
103         for (int i = 0; i < filters.length; i++)
104         {
105             if (i > 0)
106             {
107                 buffer.append(" and ");
108             }
109             buffer.append("(");
110             buffer.append(filters[i].toString());
111             buffer.append(")");
112         }
113         return buffer.toString();
114     }
115 }
116
Popular Tags