KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: OrFilter.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 ORs together all of its sub-filters.
8  *
9  * @author Eric Galluzzo
10  */

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

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

27     public OrFilter(Filter[] inFilters)
28     {
29         fieldFilters = inFilters;
30     }
31
32     /**
33      * Create a filter that ORs 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 OrFilter(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 ORing together
65      * all the sub-filters.
66      *
67      * @param inObj The object to check
68      *
69      * @return <code>true</code> if the object passes at least one of the
70      * sub-filters, <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 true;
79             }
80         }
81         return false;
82     }
83
84     /* (non-Javadoc)
85      * @see com.openedit.util.strainer.Filter#accept(com.openedit.util.strainer.FilterVisitor)
86      */

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

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