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.vfs2.filter;
018
019import java.io.Serializable;
020import java.util.ArrayList;
021import java.util.Arrays;
022import java.util.Collections;
023import java.util.List;
024import java.util.Objects;
025
026import org.apache.commons.lang3.ArrayUtils;
027import org.apache.commons.vfs2.FileFilter;
028import org.apache.commons.vfs2.FileSelectInfo;
029import org.apache.commons.vfs2.FileSystemException;
030
031/**
032 * A filter providing conditional AND logic across a list of file filters. This
033 * filter returns {@code true} if all filters in the list return {@code true}.
034 * Otherwise, it returns {@code false}. Checking of the file filter list stops
035 * when the first filter returns {@code false}.
036 *
037 * @author This code was originally ported from Apache Commons IO File Filter
038 * @see "https://commons.apache.org/proper/commons-io/"
039 * @since 2.4
040 */
041public class AndFileFilter implements FileFilter, ConditionalFileFilter, Serializable {
042
043    private static final long serialVersionUID = 1L;
044
045    /** The list of file filters. */
046    private final List<FileFilter> fileFilters;
047
048    /**
049     * Default constructor.
050     */
051    public AndFileFilter() {
052        fileFilters = new ArrayList<>();
053    }
054
055    /**
056     * Constructs a new file filter that ANDs the result of other filters.
057     *
058     * @param filters array of filters, must not be null or empty
059     */
060    public AndFileFilter(final FileFilter... filters) {
061        if (ArrayUtils.isEmpty(filters)) {
062            throw new IllegalArgumentException("The filters must not be null or empty");
063        }
064        for (final FileFilter filter : filters) {
065            if (filter == null) {
066                throw new IllegalArgumentException("Null filters are not allowed");
067            }
068        }
069        fileFilters = new ArrayList<>(Arrays.asList(filters));
070    }
071
072    /**
073     * Constructs a new instance of {@code AndFileFilter} with the specified
074     * list of filters.
075     *
076     * @param fileFilters a List of FileFilter instances, copied, null ignored
077     */
078    public AndFileFilter(final List<FileFilter> fileFilters) {
079        if (fileFilters == null) {
080            this.fileFilters = new ArrayList<>();
081        } else {
082            this.fileFilters = new ArrayList<>(fileFilters);
083        }
084    }
085
086    @Override
087    public boolean accept(final FileSelectInfo fileSelectInfo) throws FileSystemException {
088        if (fileFilters.isEmpty()) {
089            return false;
090        }
091        for (final FileFilter fileFilter : fileFilters) {
092            if (!fileFilter.accept(fileSelectInfo)) {
093                return false;
094            }
095        }
096        return true;
097    }
098
099    @Override
100    public void addFileFilter(final FileFilter fileFilter) {
101        fileFilters.add(fileFilter);
102    }
103
104    @Override
105    public List<FileFilter> getFileFilters() {
106        return Collections.unmodifiableList(fileFilters);
107    }
108
109    @Override
110    public boolean removeFileFilter(final FileFilter fileFilter) {
111        return fileFilters.remove(fileFilter);
112    }
113
114    @Override
115    public void setFileFilters(final List<FileFilter> fileFilters) {
116        this.fileFilters.clear();
117        this.fileFilters.addAll(fileFilters);
118    }
119
120    /**
121     * Provide a String representation of this file filter.
122     *
123     * @return a String representation
124     */
125    @Override
126    public String toString() {
127        final StringBuilder buffer = new StringBuilder();
128        buffer.append(super.toString());
129        buffer.append("(");
130        if (fileFilters != null) {
131            for (int i = 0; i < fileFilters.size(); i++) {
132                if (i > 0) {
133                    buffer.append(",");
134                }
135                buffer.append(Objects.toString(fileFilters.get(i)));
136            }
137        }
138        buffer.append(")");
139        return buffer.toString();
140    }
141
142}