AndFileFilter.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.io.filefilter;

  18. import java.io.File;
  19. import java.io.FileFilter;
  20. import java.io.Serializable;
  21. import java.nio.file.FileVisitResult;
  22. import java.nio.file.Path;
  23. import java.nio.file.attribute.BasicFileAttributes;
  24. import java.util.ArrayList;
  25. import java.util.Collections;
  26. import java.util.List;
  27. import java.util.Objects;
  28. import java.util.stream.Stream;

  29. /**
  30.  * A {@link FileFilter} providing conditional AND logic across a list of
  31.  * file filters. This filter returns {@code true} if all filters in the
  32.  * list return {@code true}. Otherwise, it returns {@code false}.
  33.  * Checking of the file filter list stops when the first filter returns
  34.  * {@code false}.
  35.  * <h2>Deprecating Serialization</h2>
  36.  * <p>
  37.  * <em>Serialization is deprecated and will be removed in 3.0.</em>
  38.  * </p>
  39.  *
  40.  * @since 1.0
  41.  * @see FileFilterUtils#and(IOFileFilter...)
  42.  */
  43. public class AndFileFilter extends AbstractFileFilter implements ConditionalFileFilter, Serializable {

  44.     private static final long serialVersionUID = 7215974688563965257L;

  45.     /** The list of file filters. */
  46.     private final List<IOFileFilter> fileFilters;

  47.     /**
  48.      * Constructs a new empty instance.
  49.      *
  50.      * @since 1.1
  51.      */
  52.     public AndFileFilter() {
  53.         this(0);
  54.     }

  55.     /**
  56.      * Constructs a new instance with the given initial list.
  57.      *
  58.      * @param initialList the initial list.
  59.      */
  60.     private AndFileFilter(final ArrayList<IOFileFilter> initialList) {
  61.         this.fileFilters = Objects.requireNonNull(initialList, "initialList");
  62.     }

  63.     /**
  64.      * Constructs a new instance with the given initial capacity.
  65.      *
  66.      * @param initialCapacity the initial capacity.
  67.      */
  68.     private AndFileFilter(final int initialCapacity) {
  69.         this(new ArrayList<>(initialCapacity));
  70.     }

  71.     /**
  72.      * Constructs a new instance for the give filters.
  73.      *
  74.      * @param fileFilters filters to OR.
  75.      * @since 2.9.0
  76.      */
  77.     public AndFileFilter(final IOFileFilter... fileFilters) {
  78.         this(Objects.requireNonNull(fileFilters, "fileFilters").length);
  79.         addFileFilter(fileFilters);
  80.     }

  81.     /**
  82.      * Constructs a new file filter that ANDs the result of other filters.
  83.      *
  84.      * @param filter1  the first filter, must second be null
  85.      * @param filter2  the first filter, must not be null
  86.      * @throws IllegalArgumentException if either filter is null
  87.      */
  88.     public AndFileFilter(final IOFileFilter filter1, final IOFileFilter filter2) {
  89.         this(2);
  90.         addFileFilter(filter1);
  91.         addFileFilter(filter2);
  92.     }

  93.     /**
  94.      * Constructs a new instance of {@link AndFileFilter}
  95.      * with the specified list of filters.
  96.      *
  97.      * @param fileFilters  a List of IOFileFilter instances, copied.
  98.      * @since 1.1
  99.      */
  100.     public AndFileFilter(final List<IOFileFilter> fileFilters) {
  101.         this(new ArrayList<>(Objects.requireNonNull(fileFilters, "fileFilters")));
  102.     }

  103.     /**
  104.      * {@inheritDoc}
  105.      */
  106.     @Override
  107.     public boolean accept(final File file) {
  108.         return !isEmpty() && fileFilters.stream().allMatch(fileFilter -> fileFilter.accept(file));
  109.     }

  110.     /**
  111.      * {@inheritDoc}
  112.      */
  113.     @Override
  114.     public boolean accept(final File file, final String name) {
  115.         return !isEmpty() && fileFilters.stream().allMatch(fileFilter -> fileFilter.accept(file, name));
  116.     }

  117.     /**
  118.      * {@inheritDoc}
  119.      * @since 2.9.0
  120.      */
  121.     @Override
  122.     public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
  123.         return isEmpty() ? FileVisitResult.TERMINATE
  124.                 : toDefaultFileVisitResult(fileFilters.stream().allMatch(fileFilter -> fileFilter.accept(file, attributes) == FileVisitResult.CONTINUE));
  125.     }

  126.     /**
  127.      * {@inheritDoc}
  128.      */
  129.     @Override
  130.     public void addFileFilter(final IOFileFilter fileFilter) {
  131.         fileFilters.add(Objects.requireNonNull(fileFilter, "fileFilter"));
  132.     }

  133.     /**
  134.      * Adds the given file filters.
  135.      *
  136.      * @param fileFilters the filters to add.
  137.      * @since 2.9.0
  138.      */
  139.     public void addFileFilter(final IOFileFilter... fileFilters) {
  140.         Stream.of(Objects.requireNonNull(fileFilters, "fileFilters")).forEach(this::addFileFilter);
  141.     }

  142.     /**
  143.      * {@inheritDoc}
  144.      */
  145.     @Override
  146.     public List<IOFileFilter> getFileFilters() {
  147.         return Collections.unmodifiableList(fileFilters);
  148.     }

  149.     private boolean isEmpty() {
  150.         return fileFilters.isEmpty();
  151.     }

  152.     /**
  153.      * {@inheritDoc}
  154.      */
  155.     @Override
  156.     public boolean removeFileFilter(final IOFileFilter ioFileFilter) {
  157.         return fileFilters.remove(ioFileFilter);
  158.     }

  159.     /**
  160.      * {@inheritDoc}
  161.      */
  162.     @Override
  163.     public void setFileFilters(final List<IOFileFilter> fileFilters) {
  164.         this.fileFilters.clear();
  165.         this.fileFilters.addAll(fileFilters);
  166.     }

  167.     /**
  168.      * Builds a String representation of this file filter.
  169.      *
  170.      * @return a String representation
  171.      */
  172.     @Override
  173.     public String toString() {
  174.         final StringBuilder buffer = new StringBuilder();
  175.         buffer.append(super.toString());
  176.         buffer.append("(");
  177.         append(fileFilters, buffer);
  178.         buffer.append(")");
  179.         return buffer.toString();
  180.     }

  181. }