001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.io.filefilter;
018    
019    import java.io.File;
020    import java.io.Serializable;
021    import java.util.ArrayList;
022    import java.util.Collections;
023    import java.util.List;
024    
025    /**
026     * A {@link java.io.FileFilter} providing conditional OR logic across a list of
027     * file filters. This filter returns <code>true</code> if any filters in the
028     * list return <code>true</code>. Otherwise, it returns <code>false</code>.
029     * Checking of the file filter list stops when the first filter returns
030     * <code>true</code>.
031     *
032     * @since Commons IO 1.0
033     * @version $Revision: 1005099 $ $Date: 2010-10-06 12:13:01 -0400 (Wed, 06 Oct 2010) $
034     *
035     * @author Steven Caswell
036     * @see FileFilterUtils#or(IOFileFilter...)
037     */
038    public class OrFileFilter
039            extends AbstractFileFilter
040            implements ConditionalFileFilter, Serializable {
041    
042        /** The list of file filters. */
043        private final List<IOFileFilter> fileFilters;
044    
045        /**
046         * Constructs a new instance of <code>OrFileFilter</code>.
047         *
048         * @since Commons IO 1.1
049         */
050        public OrFileFilter() {
051            this.fileFilters = new ArrayList<IOFileFilter>();
052        }
053    
054        /**
055         * Constructs a new instance of <code>OrFileFilter</code>
056         * with the specified filters.
057         *
058         * @param fileFilters  the file filters for this filter, copied, null ignored
059         * @since Commons IO 1.1
060         */
061        public OrFileFilter(final List<IOFileFilter> fileFilters) {
062            if (fileFilters == null) {
063                this.fileFilters = new ArrayList<IOFileFilter>();
064            } else {
065                this.fileFilters = new ArrayList<IOFileFilter>(fileFilters);
066            }
067        }
068    
069        /**
070         * Constructs a new file filter that ORs the result of two other filters.
071         * 
072         * @param filter1  the first filter, must not be null
073         * @param filter2  the second filter, must not be null
074         * @throws IllegalArgumentException if either filter is null
075         */
076        public OrFileFilter(IOFileFilter filter1, IOFileFilter filter2) {
077            if (filter1 == null || filter2 == null) {
078                throw new IllegalArgumentException("The filters must not be null");
079            }
080            this.fileFilters = new ArrayList<IOFileFilter>(2);
081            addFileFilter(filter1);
082            addFileFilter(filter2);
083        }
084    
085        /**
086         * {@inheritDoc}
087         */
088        public void addFileFilter(final IOFileFilter ioFileFilter) {
089            this.fileFilters.add(ioFileFilter);
090        }
091    
092        /**
093         * {@inheritDoc}
094         */
095        public List<IOFileFilter> getFileFilters() {
096            return Collections.unmodifiableList(this.fileFilters);
097        }
098    
099        /**
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.clear();
111            this.fileFilters.addAll(fileFilters);
112        }
113    
114        /**
115         * {@inheritDoc}
116         */
117        @Override
118        public boolean accept(final File file) {
119            for (IOFileFilter fileFilter : fileFilters) {
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 (IOFileFilter fileFilter : fileFilters) {
133                if (fileFilter.accept(file, name)) {
134                    return true;
135                }
136            }
137            return false;
138        }
139    
140        /**
141         * Provide a String representaion of this file filter.
142         *
143         * @return a String representaion
144         */
145        @Override
146        public String toString() {
147            StringBuilder buffer = new StringBuilder();
148            buffer.append(super.toString());
149            buffer.append("(");
150            if (fileFilters != null) {
151                for (int i = 0; i < fileFilters.size(); i++) {
152                    if (i > 0) {
153                        buffer.append(",");
154                    }
155                    Object filter = fileFilters.get(i);
156                    buffer.append(filter == null ? "null" : filter.toString());
157                }
158            }
159            buffer.append(")");
160            return buffer.toString();
161        }
162    
163    }