    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.nio.file.FileVisitResult;
    022import java.nio.file.Path;
    023import java.nio.file.attribute.BasicFileAttributes;
    024import java.util.ArrayList;
    025import java.util.Collections;
    026import java.util.List;
    027import java.util.Objects;
    028import java.util.stream.Stream;
    029
    030/**
    031 * A {@link java.io.FileFilter} providing conditional AND logic across a list of
    032 * file filters. This filter returns {@code true} if all filters in the
    033 * list return {@code true}. Otherwise, it returns {@code false}.
    034 * Checking of the file filter list stops when the first filter returns
    035 * {@code false}.
    036 * <h2>Deprecating Serialization</h2>
    037 * <p>
    038 * <em>Serialization is deprecated and will be removed in 3.0.</em>
    039 * </p>
    040 *
    041 * @since 1.0
    042 * @see FileFilterUtils#and(IOFileFilter...)
    043 */
    044public class AndFileFilter extends AbstractFileFilter implements ConditionalFileFilter, Serializable {
    045
    046    private static final long serialVersionUID = 7215974688563965257L;
    047
    048    /** The list of file filters. */
    049    private final List<IOFileFilter> fileFilters;
    050
    051    /**
    052     * Constructs a new empty instance.
    053     *
    054     * @since 1.1
    055     */
    056    public AndFileFilter() {
    057        this(0);
    058    }
    059
    060    /**
    061     * Constructs a new instance with the given initial list.
    062     *
    063     * @param initialList the initial list.
    064     */
    065    private AndFileFilter(final ArrayList<IOFileFilter> initialList) {
    066        this.fileFilters = Objects.requireNonNull(initialList, "initialList");
    067    }
    068
    069    /**
    070     * Constructs a new instance with the given initial capacity.
    071     *
    072     * @param initialCapacity the initial capacity.
    073     */
    074    private AndFileFilter(final int initialCapacity) {
    075        this(new ArrayList<>(initialCapacity));
    076    }
    077
    078    /**
    079     * Constructs a new instance for the give filters.
    080     *
    081     * @param fileFilters filters to OR.
    082     * @since 2.9.0
    083     */
    084    public AndFileFilter(final IOFileFilter... fileFilters) {
    085        this(Objects.requireNonNull(fileFilters, "fileFilters").length);
    086        addFileFilter(fileFilters);
    087    }
    088
    089    /**
    090     * Constructs a new file filter that ANDs the result of other filters.
    091     *
    092     * @param filter1  the first filter, must second be null
    093     * @param filter2  the first filter, must not be null
    094     * @throws IllegalArgumentException if either filter is null
    095     */
    096    public AndFileFilter(final IOFileFilter filter1, final IOFileFilter filter2) {
    097        this(2);
    098        addFileFilter(filter1);
    099        addFileFilter(filter2);
    100    }
    101
    102    /**
    103     * Constructs a new instance of {@link AndFileFilter}
    104     * with the specified list of filters.
    105     *
    106     * @param fileFilters  a List of IOFileFilter instances, copied.
    107     * @since 1.1
    108     */
    109    public AndFileFilter(final List<IOFileFilter> fileFilters) {
    110        this(new ArrayList<>(Objects.requireNonNull(fileFilters, "fileFilters")));
    111    }
    112
    113    /**
    114     * {@inheritDoc}
    115     */
    116    @Override
    117    public boolean accept(final File file) {
    118        return !isEmpty() && fileFilters.stream().allMatch(fileFilter -> fileFilter.accept(file));
    119    }
    120
    121    /**
    122     * {@inheritDoc}
    123     */
    124    @Override
    125    public boolean accept(final File file, final String name) {
    126        return !isEmpty() && fileFilters.stream().allMatch(fileFilter -> fileFilter.accept(file, name));
    127    }
    128
    129    /**
    130     * {@inheritDoc}
    131     * @since 2.9.0
    132     */
    133    @Override
    134    public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
    135        return isEmpty() ? FileVisitResult.TERMINATE
    136                : toDefaultFileVisitResult(fileFilters.stream().allMatch(fileFilter -> fileFilter.accept(file, attributes) == FileVisitResult.CONTINUE));
    137    }
    138
    139    /**
    140     * {@inheritDoc}
    141     */
    142    @Override
    143    public void addFileFilter(final IOFileFilter fileFilter) {
    144        fileFilters.add(Objects.requireNonNull(fileFilter, "fileFilter"));
    145    }
    146
    147    /**
    148     * Adds the given file filters.
    149     *
    150     * @param fileFilters the filters to add.
    151     * @since 2.9.0
    152     */
    153    public void addFileFilter(final IOFileFilter... fileFilters) {
    154        Stream.of(Objects.requireNonNull(fileFilters, "fileFilters")).forEach(this::addFileFilter);
    155    }
    156
    157    /**
    158     * {@inheritDoc}
    159     */
    160    @Override
    161    public List<IOFileFilter> getFileFilters() {
    162        return Collections.unmodifiableList(fileFilters);
    163    }
    164
    165    private boolean isEmpty() {
    166        return fileFilters.isEmpty();
    167    }
    168
    169    /**
    170     * {@inheritDoc}
    171     */
    172    @Override
    173    public boolean removeFileFilter(final IOFileFilter ioFileFilter) {
    174        return fileFilters.remove(ioFileFilter);
    175    }
    176
    177    /**
    178     * {@inheritDoc}
    179     */
    180    @Override
    181    public void setFileFilters(final List<IOFileFilter> fileFilters) {
    182        this.fileFilters.clear();
    183        this.fileFilters.addAll(fileFilters);
    184    }
    185
    186    /**
    187     * Builds a String representation of this file filter.
    188     *
    189     * @return a String representation
    190     */
    191    @Override
    192    public String toString() {
    193        final StringBuilder buffer = new StringBuilder();
    194        buffer.append(super.toString());
    195        buffer.append("(");
    196        append(fileFilters, buffer);
    197        buffer.append(")");
    198        return buffer.toString();
    199    }
    200
    201}