OrFileFilter.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 OR logic across a list of file filters. This filter returns
  31.  * {@code true} if any filters in the list return {@code true}. Otherwise, it returns {@code false}. Checking of the
  32.  * file filter list stops when the first filter returns {@code true}.
  33.  * <h2>Deprecating Serialization</h2>
  34.  * <p>
  35.  * <em>Serialization is deprecated and will be removed in 3.0.</em>
  36.  * </p>
  37.  *
  38.  * @since 1.0
  39.  * @see FileFilterUtils#or(IOFileFilter...)
  40.  */
  41. public class OrFileFilter extends AbstractFileFilter implements ConditionalFileFilter, Serializable {

  42.     private static final long serialVersionUID = 5767770777065432721L;

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

  45.     /**
  46.      * Constructs a new instance of {@link OrFileFilter}.
  47.      *
  48.      * @since 1.1
  49.      */
  50.     public OrFileFilter() {
  51.         this(0);
  52.     }

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

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

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

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

  90.     /**
  91.      * Constructs a new instance of {@link OrFileFilter} with the specified filters.
  92.      *
  93.      * @param fileFilters the file filters for this filter, copied.
  94.      * @since 1.1
  95.      */
  96.     public OrFileFilter(final List<IOFileFilter> fileFilters) {
  97.         this(new ArrayList<>(Objects.requireNonNull(fileFilters, "fileFilters")));
  98.     }

  99.     /**
  100.      * {@inheritDoc}
  101.      */
  102.     @Override
  103.     public boolean accept(final File file) {
  104.         return fileFilters.stream().anyMatch(fileFilter -> fileFilter.accept(file));
  105.     }

  106.     /**
  107.      * {@inheritDoc}
  108.      */
  109.     @Override
  110.     public boolean accept(final File file, final String name) {
  111.         return fileFilters.stream().anyMatch(fileFilter -> fileFilter.accept(file, name));
  112.     }

  113.     /**
  114.      * {@inheritDoc}
  115.      */
  116.     @Override
  117.     public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
  118.         return toDefaultFileVisitResult(fileFilters.stream().anyMatch(fileFilter -> fileFilter.accept(file, attributes) == FileVisitResult.CONTINUE));
  119.     }

  120.     /**
  121.      * {@inheritDoc}
  122.      */
  123.     @Override
  124.     public void addFileFilter(final IOFileFilter fileFilter) {
  125.         this.fileFilters.add(Objects.requireNonNull(fileFilter, "fileFilter"));
  126.     }

  127.     /**
  128.      * Adds the given file filters.
  129.      *
  130.      * @param fileFilters the filters to add.
  131.      * @since 2.9.0
  132.      */
  133.     public void addFileFilter(final IOFileFilter... fileFilters) {
  134.         Stream.of(Objects.requireNonNull(fileFilters, "fileFilters")).forEach(this::addFileFilter);
  135.     }

  136.     /**
  137.      * {@inheritDoc}
  138.      */
  139.     @Override
  140.     public List<IOFileFilter> getFileFilters() {
  141.         return Collections.unmodifiableList(this.fileFilters);
  142.     }

  143.     /**
  144.      * {@inheritDoc}
  145.      */
  146.     @Override
  147.     public boolean removeFileFilter(final IOFileFilter fileFilter) {
  148.         return this.fileFilters.remove(fileFilter);
  149.     }

  150.     /**
  151.      * {@inheritDoc}
  152.      */
  153.     @Override
  154.     public void setFileFilters(final List<IOFileFilter> fileFilters) {
  155.         this.fileFilters.clear();
  156.         this.fileFilters.addAll(Objects.requireNonNull(fileFilters, "fileFilters"));
  157.     }

  158.     /**
  159.      * Provide a String representation of this file filter.
  160.      *
  161.      * @return a String representation
  162.      */
  163.     @Override
  164.     public String toString() {
  165.         final StringBuilder buffer = new StringBuilder();
  166.         buffer.append(super.toString());
  167.         buffer.append("(");
  168.         append(fileFilters, buffer);
  169.         buffer.append(")");
  170.         return buffer.toString();
  171.     }

  172. }