1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.io.filefilter;
18
19 import java.io.File;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.List;
24
25 /**
26 * A {@link java.io.FileFilter} providing conditional OR logic across a list of
27 * file filters. This filter returns <code>true</code> if any filters in the
28 * list return <code>true</code>. Otherwise, it returns <code>false</code>.
29 * Checking of the file filter list stops when the first filter returns
30 * <code>true</code>.
31 *
32 * @since Commons IO 1.0
33 * @version $Revision: 490425 $ $Date: 2006-12-27 02:25:43 +0100 (Mi, 27 Dez 2006) $
34 *
35 * @author Steven Caswell
36 */
37 public class OrFileFilter
38 extends AbstractFileFilter
39 implements ConditionalFileFilter {
40
41 /** The list of file filters. */
42 private List fileFilters;
43
44 /**
45 * Constructs a new instance of <code>OrFileFilter</code>.
46 *
47 * @since Commons IO 1.1
48 */
49 public OrFileFilter() {
50 this.fileFilters = new ArrayList();
51 }
52
53 /**
54 * Constructs a new instance of <code>OrFileFilter</code>
55 * with the specified filters.
56 *
57 * @param fileFilters the file filters for this filter, copied, null ignored
58 * @since Commons IO 1.1
59 */
60 public OrFileFilter(final List fileFilters) {
61 if (fileFilters == null) {
62 this.fileFilters = new ArrayList();
63 } else {
64 this.fileFilters = new ArrayList(fileFilters);
65 }
66 }
67
68 /**
69 * Constructs a new file filter that ORs the result of two other filters.
70 *
71 * @param filter1 the first filter, must not be null
72 * @param filter2 the second filter, must not be null
73 * @throws IllegalArgumentException if either filter is null
74 */
75 public OrFileFilter(IOFileFilter filter1, IOFileFilter filter2) {
76 if (filter1 == null || filter2 == null) {
77 throw new IllegalArgumentException("The filters must not be null");
78 }
79 this.fileFilters = new ArrayList();
80 addFileFilter(filter1);
81 addFileFilter(filter2);
82 }
83
84 /**
85 * {@inheritDoc}
86 */
87 public void addFileFilter(final IOFileFilter ioFileFilter) {
88 this.fileFilters.add(ioFileFilter);
89 }
90
91 /**
92 * {@inheritDoc}
93 */
94 public List getFileFilters() {
95 return Collections.unmodifiableList(this.fileFilters);
96 }
97
98 /**
99 * {@inheritDoc}
100 */
101 public boolean removeFileFilter(IOFileFilter ioFileFilter) {
102 return this.fileFilters.remove(ioFileFilter);
103 }
104
105 /**
106 * {@inheritDoc}
107 */
108 public void setFileFilters(final List fileFilters) {
109 this.fileFilters = fileFilters;
110 }
111
112 /**
113 * {@inheritDoc}
114 */
115 public boolean accept(final File file) {
116 for (Iterator iter = this.fileFilters.iterator(); iter.hasNext();) {
117 IOFileFilter fileFilter = (IOFileFilter) iter.next();
118 if (fileFilter.accept(file)) {
119 return true;
120 }
121 }
122 return false;
123 }
124
125 /**
126 * {@inheritDoc}
127 */
128 public boolean accept(final File file, final String name) {
129 for (Iterator iter = this.fileFilters.iterator(); iter.hasNext();) {
130 IOFileFilter fileFilter = (IOFileFilter) iter.next();
131 if (fileFilter.accept(file, name)) {
132 return true;
133 }
134 }
135 return false;
136 }
137
138 }