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 OR logic across a list of
28 * file filters. This filter returns <code>true</code> if any 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>true</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 OrFileFilter
39        extends AbstractFileFilter
40        implements ConditionalFileFilter, Serializable {
41
42    /** The list of file filters. */
43    private List<IOFileFilter> fileFilters;
44
45    /**
46     * Constructs a new instance of <code>OrFileFilter</code>.
47     *
48     * @since Commons IO 1.1
49     */
50    public OrFileFilter() {
51        this.fileFilters = new ArrayList<IOFileFilter>();
52    }
53
54    /**
55     * Constructs a new instance of <code>OrFileFilter</code>
56     * with the specified filters.
57     *
58     * @param fileFilters  the file filters for this filter, copied, null ignored
59     * @since Commons IO 1.1
60     */
61    public OrFileFilter(final List<IOFileFilter> fileFilters) {
62        if (fileFilters == null) {
63            this.fileFilters = new ArrayList<IOFileFilter>();
64        } else {
65            this.fileFilters = new ArrayList<IOFileFilter>(fileFilters);
66        }
67    }
68
69    /**
70     * Constructs a new file filter that ORs 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 OrFileFilter(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<IOFileFilter>();
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<IOFileFilter> getFileFilters() {
96        return Collections.unmodifiableList(this.fileFilters);
97    }
98
99    /**
100     * {@inheritDoc}
101     */
102    public boolean removeFileFilter(IOFileFilter ioFileFilter) {
103        return this.fileFilters.remove(ioFileFilter);
104    }
105
106    /**
107     * {@inheritDoc}
108     */
109    public void setFileFilters(final List<IOFileFilter> fileFilters) {
110        this.fileFilters = fileFilters;
111    }
112
113    /**
114     * {@inheritDoc}
115     */
116    @Override
117    public boolean accept(final File file) {
118        for (Iterator<IOFileFilter> iter = this.fileFilters.iterator(); iter.hasNext();) {
119            IOFileFilter fileFilter = iter.next();
120            if (fileFilter.accept(file)) {
121                return true;
122            }
123        }
124        return false;
125    }
126
127    /**
128     * {@inheritDoc}
129     */
130    @Override
131    public boolean accept(final File file, final String name) {
132        for (Iterator<IOFileFilter> iter = this.fileFilters.iterator(); iter.hasNext();) {
133            IOFileFilter fileFilter = iter.next();
134            if (fileFilter.accept(file, name)) {
135                return true;
136            }
137        }
138        return false;
139    }
140
141    /**
142     * Provide a String representaion of this file filter.
143     *
144     * @return a String representaion
145     */
146    @Override
147    public String toString() {
148        StringBuffer buffer = new StringBuffer();
149        buffer.append(super.toString());
150        buffer.append("(");
151        if (fileFilters != null) {
152            for (int i = 0; i < fileFilters.size(); i++) {
153                if (i > 0) {
154                    buffer.append(",");
155                }
156                Object filter = fileFilters.get(i);
157                buffer.append(filter == null ? "null" : filter.toString());
158            }
159        }
160        buffer.append(")");
161        return buffer.toString();
162    }
163
164}
165