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 */
017package org.apache.commons.io.filefilter;
018
019import java.io.File;
020import java.io.Serializable;
021import java.util.ArrayList;
022import java.util.Collections;
023import 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} if any filters in the
028 * list return {@code true}. Otherwise, it returns {@code false}.
029 * Checking of the file filter list stops when the first filter returns
030 * {@code true}.
031 *
032 * @since 1.0
033 * @version $Id: OrFileFilter.java 1642757 2014-12-01 21:09:30Z sebb $
034 * @see FileFilterUtils#or(IOFileFilter...)
035 */
036public class OrFileFilter
037        extends AbstractFileFilter
038        implements ConditionalFileFilter, Serializable {
039
040    private static final long serialVersionUID = 5767770777065432721L;
041    /** The list of file filters. */
042    private final List<IOFileFilter> fileFilters;
043
044    /**
045     * Constructs a new instance of <code>OrFileFilter</code>.
046     *
047     * @since 1.1
048     */
049    public OrFileFilter() {
050        this.fileFilters = new ArrayList<IOFileFilter>();
051    }
052
053    /**
054     * Constructs a new instance of <code>OrFileFilter</code>
055     * with the specified filters.
056     *
057     * @param fileFilters  the file filters for this filter, copied, null ignored
058     * @since 1.1
059     */
060    public OrFileFilter(final List<IOFileFilter> fileFilters) {
061        if (fileFilters == null) {
062            this.fileFilters = new ArrayList<IOFileFilter>();
063        } else {
064            this.fileFilters = new ArrayList<IOFileFilter>(fileFilters);
065        }
066    }
067
068    /**
069     * Constructs a new file filter that ORs the result of two other filters.
070     * 
071     * @param filter1  the first filter, must not be null
072     * @param filter2  the second filter, must not be null
073     * @throws IllegalArgumentException if either filter is null
074     */
075    public OrFileFilter(final IOFileFilter filter1, final IOFileFilter filter2) {
076        if (filter1 == null || filter2 == null) {
077            throw new IllegalArgumentException("The filters must not be null");
078        }
079        this.fileFilters = new ArrayList<IOFileFilter>(2);
080        addFileFilter(filter1);
081        addFileFilter(filter2);
082    }
083
084    /**
085     * {@inheritDoc}
086     */
087    public void addFileFilter(final IOFileFilter ioFileFilter) {
088        this.fileFilters.add(ioFileFilter);
089    }
090
091    /**
092     * {@inheritDoc}
093     */
094    public List<IOFileFilter> getFileFilters() {
095        return Collections.unmodifiableList(this.fileFilters);
096    }
097
098    /**
099     * {@inheritDoc}
100     */
101    public boolean removeFileFilter(final IOFileFilter ioFileFilter) {
102        return this.fileFilters.remove(ioFileFilter);
103    }
104
105    /**
106     * {@inheritDoc}
107     */
108    public void setFileFilters(final List<IOFileFilter> fileFilters) {
109        this.fileFilters.clear();
110        this.fileFilters.addAll(fileFilters);
111    }
112
113    /**
114     * {@inheritDoc}
115     */
116    @Override
117    public boolean accept(final File file) {
118        for (final IOFileFilter fileFilter : fileFilters) {
119            if (fileFilter.accept(file)) {
120                return true;
121            }
122        }
123        return false;
124    }
125
126    /**
127     * {@inheritDoc}
128     */
129    @Override
130    public boolean accept(final File file, final String name) {
131        for (final IOFileFilter fileFilter : fileFilters) {
132            if (fileFilter.accept(file, name)) {
133                return true;
134            }
135        }
136        return false;
137    }
138
139    /**
140     * Provide a String representaion of this file filter.
141     *
142     * @return a String representaion
143     */
144    @Override
145    public String toString() {
146        final StringBuilder buffer = new StringBuilder();
147        buffer.append(super.toString());
148        buffer.append("(");
149        if (fileFilters != null) {
150            for (int i = 0; i < fileFilters.size(); i++) {
151                if (i > 0) {
152                    buffer.append(",");
153                }
154                final Object filter = fileFilters.get(i);
155                buffer.append(filter == null ? "null" : filter.toString());
156            }
157        }
158        buffer.append(")");
159        return buffer.toString();
160    }
161
162}