    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 *
    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<>();
    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<>();
    063        } else {
    064            this.fileFilters = new ArrayList<>(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<>(2);
    080        addFileFilter(filter1);
    081        addFileFilter(filter2);
    082    }
    083
    084    /**
    085     * {@inheritDoc}
    086     */
    087    @Override
    088    public void addFileFilter(final IOFileFilter ioFileFilter) {
    089        this.fileFilters.add(ioFileFilter);
    090    }
    091
    092    /**
    093     * {@inheritDoc}
    094     */
    095    @Override
    096    public List<IOFileFilter> getFileFilters() {
    097        return Collections.unmodifiableList(this.fileFilters);
    098    }
    099
    100    /**
    101     * {@inheritDoc}
    102     */
    103    @Override
    104    public boolean removeFileFilter(final IOFileFilter ioFileFilter) {
    105        return this.fileFilters.remove(ioFileFilter);
    106    }
    107
    108    /**
    109     * {@inheritDoc}
    110     */
    111    @Override
    112    public void setFileFilters(final List<IOFileFilter> fileFilters) {
    113        this.fileFilters.clear();
    114        this.fileFilters.addAll(fileFilters);
    115    }
    116
    117    /**
    118     * {@inheritDoc}
    119     */
    120    @Override
    121    public boolean accept(final File file) {
    122        for (final IOFileFilter fileFilter : fileFilters) {
    123            if (fileFilter.accept(file)) {
    124                return true;
    125            }
    126        }
    127        return false;
    128    }
    129
    130    /**
    131     * {@inheritDoc}
    132     */
    133    @Override
    134    public boolean accept(final File file, final String name) {
    135        for (final IOFileFilter fileFilter : fileFilters) {
    136            if (fileFilter.accept(file, name)) {
    137                return true;
    138            }
    139        }
    140        return false;
    141    }
    142
    143    /**
    144     * Provide a String representation of this file filter.
    145     *
    146     * @return a String representation
    147     */
    148    @Override
    149    public String toString() {
    150        final StringBuilder buffer = new StringBuilder();
    151        buffer.append(super.toString());
    152        buffer.append("(");
    153        if (fileFilters != null) {
    154            for (int i = 0; i < fileFilters.size(); i++) {
    155                if (i > 0) {
    156                    buffer.append(",");
    157                }
    158                final Object filter = fileFilters.get(i);
    159                buffer.append(filter == null ? "null" : filter.toString());
    160            }
    161        }
    162        buffer.append(")");
    163        return buffer.toString();
    164    }
    165
    166}