AndFileFilter.java revision 96c5af40d639d629267794f4f0338a267ff94ce5
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 */
17package org.apache.commons.io.filefilter;
18
19import java.io.File;
20import java.io.Serializable;
21import java.util.ArrayList;
22import java.util.Collections;
23import java.util.Iterator;
24import java.util.List;
25
26/**
27 * A {@link java.io.FileFilter} providing conditional AND logic across a list of
28 * file filters. This filter returns <code>true</code> if all filters in the
29 * list return <code>true</code>. Otherwise, it returns <code>false</code>.
30 * Checking of the file filter list stops when the first filter returns
31 * <code>false</code>.
32 *
33 * @since Commons IO 1.0
34 * @version $Revision: 606381 $ $Date: 2007-12-22 02:03:16 +0000 (Sat, 22 Dec 2007) $
35 *
36 * @author Steven Caswell
37 */
38public class AndFileFilter
39        extends AbstractFileFilter
40        implements ConditionalFileFilter, Serializable {
41
42    /** The list of file filters. */
43    private List fileFilters;
44
45    /**
46     * Constructs a new instance of <code>AndFileFilter</code>.
47     *
48     * @since Commons IO 1.1
49     */
50    public AndFileFilter() {
51        this.fileFilters = new ArrayList();
52    }
53
54    /**
55     * Constructs a new instance of <code>AndFileFilter</code>
56     * with the specified list of filters.
57     *
58     * @param fileFilters  a List of IOFileFilter instances, copied, null ignored
59     * @since Commons IO 1.1
60     */
61    public AndFileFilter(final List fileFilters) {
62        if (fileFilters == null) {
63            this.fileFilters = new ArrayList();
64        } else {
65            this.fileFilters = new ArrayList(fileFilters);
66        }
67    }
68
69    /**
70     * Constructs a new file filter that ANDs the result of two other filters.
71     *
72     * @param filter1  the first filter, must not be null
73     * @param filter2  the second filter, must not be null
74     * @throws IllegalArgumentException if either filter is null
75     */
76    public AndFileFilter(IOFileFilter filter1, IOFileFilter filter2) {
77        if (filter1 == null || filter2 == null) {
78            throw new IllegalArgumentException("The filters must not be null");
79        }
80        this.fileFilters = new ArrayList();
81        addFileFilter(filter1);
82        addFileFilter(filter2);
83    }
84
85    /**
86     * {@inheritDoc}
87     */
88    public void addFileFilter(final IOFileFilter ioFileFilter) {
89        this.fileFilters.add(ioFileFilter);
90    }
91
92    /**
93     * {@inheritDoc}
94     */
95    public List getFileFilters() {
96        return Collections.unmodifiableList(this.fileFilters);
97    }
98
99    /**
100     * {@inheritDoc}
101     */
102    public boolean removeFileFilter(final IOFileFilter ioFileFilter) {
103        return this.fileFilters.remove(ioFileFilter);
104    }
105
106    /**
107     * {@inheritDoc}
108     */
109    public void setFileFilters(final List fileFilters) {
110        this.fileFilters = new ArrayList(fileFilters);
111    }
112
113    /**
114     * {@inheritDoc}
115     */
116    public boolean accept(final File file) {
117        if (this.fileFilters.size() == 0) {
118            return false;
119        }
120        for (Iterator iter = this.fileFilters.iterator(); iter.hasNext();) {
121            IOFileFilter fileFilter = (IOFileFilter) iter.next();
122            if (!fileFilter.accept(file)) {
123                return false;
124            }
125        }
126        return true;
127    }
128
129    /**
130     * {@inheritDoc}
131     */
132    public boolean accept(final File file, final String name) {
133        if (this.fileFilters.size() == 0) {
134            return false;
135        }
136        for (Iterator iter = this.fileFilters.iterator(); iter.hasNext();) {
137            IOFileFilter fileFilter = (IOFileFilter) iter.next();
138            if (!fileFilter.accept(file, name)) {
139                return false;
140            }
141        }
142        return true;
143    }
144
145    /**
146     * Provide a String representaion of this file filter.
147     *
148     * @return a String representaion
149     */
150    public String toString() {
151        StringBuffer buffer = new StringBuffer();
152        buffer.append(super.toString());
153        buffer.append("(");
154        if (fileFilters != null) {
155            for (int i = 0; i < fileFilters.size(); i++) {
156                if (i > 0) {
157                    buffer.append(",");
158                }
159                Object filter = fileFilters.get(i);
160                buffer.append(filter == null ? "null" : filter.toString());
161            }
162        }
163        buffer.append(")");
164        return buffer.toString();
165    }
166
167}
168