AbstractFileFilter.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.FilenameFilter;
  21. import java.io.IOException;
  22. import java.nio.file.FileVisitResult;
  23. import java.nio.file.Path;
  24. import java.nio.file.attribute.BasicFileAttributes;
  25. import java.util.List;
  26. import java.util.Objects;

  27. import org.apache.commons.io.file.PathFilter;
  28. import org.apache.commons.io.file.PathVisitor;
  29. import org.apache.commons.io.function.IOSupplier;

  30. /**
  31.  * Abstracts the implementation of the {@link FileFilter} (IO), {@link FilenameFilter} (IO), {@link PathFilter} (NIO)
  32.  * interfaces via our own {@link IOFileFilter} interface.
  33.  * <p>
  34.  * Note that a subclass MUST override one of the {@code accept} methods, otherwise that subclass will infinitely loop.
  35.  * </p>
  36.  *
  37.  * @since 1.0
  38.  */
  39. public abstract class AbstractFileFilter implements IOFileFilter, PathVisitor {

  40.     static FileVisitResult toDefaultFileVisitResult(final boolean accept) {
  41.         return accept ? FileVisitResult.CONTINUE : FileVisitResult.TERMINATE;
  42.     }

  43.     /**
  44.      * What to do when this filter accepts.
  45.      */
  46.     private final FileVisitResult onAccept;

  47.     /**
  48.      * What to do when this filter rejects.
  49.      */
  50.     private final FileVisitResult onReject;

  51.     /**
  52.      * Constructs a new instance.
  53.      */
  54.     public AbstractFileFilter() {
  55.         this(FileVisitResult.CONTINUE, FileVisitResult.TERMINATE);
  56.     }

  57.     /**
  58.      * Constructs a new instance.
  59.      *
  60.      * @param onAccept What to do on acceptance.
  61.      * @param onReject What to do on rejection.
  62.      * @since 2.12.0.
  63.      */
  64.     protected AbstractFileFilter(final FileVisitResult onAccept, final FileVisitResult onReject) {
  65.         this.onAccept = onAccept;
  66.         this.onReject = onReject;
  67.     }

  68.     /**
  69.      * Checks to see if the File should be accepted by this filter.
  70.      *
  71.      * @param file the File to check
  72.      * @return true if this file matches the test
  73.      */
  74.     @Override
  75.     public boolean accept(final File file) {
  76.         Objects.requireNonNull(file, "file");
  77.         return accept(file.getParentFile(), file.getName());
  78.     }

  79.     /**
  80.      * Checks to see if the File should be accepted by this filter.
  81.      *
  82.      * @param dir the directory File to check
  83.      * @param name the file name within the directory to check
  84.      * @return true if this file matches the test
  85.      */
  86.     @Override
  87.     public boolean accept(final File dir, final String name) {
  88.         Objects.requireNonNull(name, "name");
  89.         return accept(new File(dir, name));
  90.     }

  91.     void append(final List<?> list, final StringBuilder buffer) {
  92.         for (int i = 0; i < list.size(); i++) {
  93.             if (i > 0) {
  94.                 buffer.append(",");
  95.             }
  96.             buffer.append(list.get(i));
  97.         }
  98.     }

  99.     void append(final Object[] array, final StringBuilder buffer) {
  100.         for (int i = 0; i < array.length; i++) {
  101.             if (i > 0) {
  102.                 buffer.append(",");
  103.             }
  104.             buffer.append(array[i]);
  105.         }
  106.     }

  107.     FileVisitResult get(final IOSupplier<FileVisitResult> supplier) {
  108.         try {
  109.             return supplier.get();
  110.         } catch (final IOException e) {
  111.             return handle(e);
  112.         }
  113.     }

  114.     /**
  115.      * Handles exceptions caught while accepting.
  116.      *
  117.      * @param t the caught Throwable.
  118.      * @return the given Throwable.
  119.      * @since 2.9.0
  120.      */
  121.     protected FileVisitResult handle(final Throwable t) {
  122.         return FileVisitResult.TERMINATE;
  123.     }

  124.     @Override
  125.     public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
  126.         return FileVisitResult.CONTINUE;
  127.     }

  128.     @Override
  129.     public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attributes) throws IOException {
  130.         return accept(dir, attributes);
  131.     }

  132.     /**
  133.      * Converts a boolean into a FileVisitResult.
  134.      *
  135.      * @param accept accepted or rejected.
  136.      * @return a FileVisitResult.
  137.      */
  138.     FileVisitResult toFileVisitResult(final boolean accept) {
  139.         return accept ? onAccept : onReject;
  140.     }

  141.     /**
  142.      * Provides a String representation of this file filter.
  143.      *
  144.      * @return a String representation
  145.      */
  146.     @Override
  147.     public String toString() {
  148.         return getClass().getSimpleName();
  149.     }

  150.     @Override
  151.     public FileVisitResult visitFile(final Path file, final BasicFileAttributes attributes) throws IOException {
  152.         return accept(file, attributes);
  153.     }

  154.     @Override
  155.     public FileVisitResult visitFileFailed(final Path file, final IOException exc) throws IOException {
  156.         return FileVisitResult.CONTINUE;
  157.     }

  158. }