    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 AND logic across a list of
    027 * file filters. This filter returns {@code true} if all 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 false}.
    031 *
    032 * @since 1.0
    033 *
    034 *
    035 * @see FileFilterUtils#and(IOFileFilter...)
    036 */
    037public class AndFileFilter
    038        extends AbstractFileFilter
    039        implements ConditionalFileFilter, Serializable {
    040
    041    private static final long serialVersionUID = 7215974688563965257L;
    042
    043    /** The list of file filters. */
    044    private final List<IOFileFilter> fileFilters;
    045
    046    /**
    047     * Constructs a new instance of <code>AndFileFilter</code>.
    048     *
    049     * @since 1.1
    050     */
    051    public AndFileFilter() {
    052        this.fileFilters = new ArrayList<>();
    053    }
    054
    055    /**
    056     * Constructs a new instance of <code>AndFileFilter</code>
    057     * with the specified list of filters.
    058     *
    059     * @param fileFilters  a List of IOFileFilter instances, copied, null ignored
    060     * @since 1.1
    061     */
    062    public AndFileFilter(final List<IOFileFilter> fileFilters) {
    063        if (fileFilters == null) {
    064            this.fileFilters = new ArrayList<>();
    065        } else {
    066            this.fileFilters = new ArrayList<>(fileFilters);
    067        }
    068    }
    069
    070    /**
    071     * Constructs a new file filter that ANDs the result of two other filters.
    072     *
    073     * @param filter1  the first filter, must not be null
    074     * @param filter2  the second filter, must not be null
    075     * @throws IllegalArgumentException if either filter is null
    076     */
    077    public AndFileFilter(final IOFileFilter filter1, final IOFileFilter filter2) {
    078        if (filter1 == null || filter2 == null) {
    079            throw new IllegalArgumentException("The filters must not be null");
    080        }
    081        this.fileFilters = new ArrayList<>(2);
    082        addFileFilter(filter1);
    083        addFileFilter(filter2);
    084    }
    085
    086    /**
    087     * {@inheritDoc}
    088     */
    089    @Override
    090    public void addFileFilter(final IOFileFilter ioFileFilter) {
    091        this.fileFilters.add(ioFileFilter);
    092    }
    093
    094    /**
    095     * {@inheritDoc}
    096     */
    097    @Override
    098    public List<IOFileFilter> getFileFilters() {
    099        return Collections.unmodifiableList(this.fileFilters);
    100    }
    101
    102    /**
    103     * {@inheritDoc}
    104     */
    105    @Override
    106    public boolean removeFileFilter(final IOFileFilter ioFileFilter) {
    107        return this.fileFilters.remove(ioFileFilter);
    108    }
    109
    110    /**
    111     * {@inheritDoc}
    112     */
    113    @Override
    114    public void setFileFilters(final List<IOFileFilter> fileFilters) {
    115        this.fileFilters.clear();
    116        this.fileFilters.addAll(fileFilters);
    117    }
    118
    119    /**
    120     * {@inheritDoc}
    121     */
    122    @Override
    123    public boolean accept(final File file) {
    124        if (this.fileFilters.isEmpty()) {
    125            return false;
    126        }
    127        for (final IOFileFilter fileFilter : fileFilters) {
    128            if (!fileFilter.accept(file)) {
    129                return false;
    130            }
    131        }
    132        return true;
    133    }
    134
    135    /**
    136     * {@inheritDoc}
    137     */
    138    @Override
    139    public boolean accept(final File file, final String name) {
    140        if (this.fileFilters.isEmpty()) {
    141            return false;
    142        }
    143        for (final IOFileFilter fileFilter : fileFilters) {
    144            if (!fileFilter.accept(file, name)) {
    145                return false;
    146            }
    147        }
    148        return true;
    149    }
    150
    151    /**
    152     * Provide a String representation of this file filter.
    153     *
    154     * @return a String representation
    155     */
    156    @Override
    157    public String toString() {
    158        final StringBuilder buffer = new StringBuilder();
    159        buffer.append(super.toString());
    160        buffer.append("(");
    161        if (fileFilters != null) {
    162            for (int i = 0; i < fileFilters.size(); i++) {
    163                if (i > 0) {
    164                    buffer.append(",");
    165                }
    166                final Object filter = fileFilters.get(i);
    167                buffer.append(filter == null ? "null" : filter.toString());
    168            }
    169        }
    170        buffer.append(")");
    171        return buffer.toString();
    172    }
    173
    174}