SimplePathVisitor.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.file;

  18. import java.io.IOException;
  19. import java.nio.file.FileVisitResult;
  20. import java.nio.file.Path;
  21. import java.nio.file.SimpleFileVisitor;
  22. import java.util.Objects;

  23. import org.apache.commons.io.build.AbstractSupplier;
  24. import org.apache.commons.io.function.IOBiFunction;

  25. /**
  26.  * A {@link SimpleFileVisitor} typed to a {@link Path}.
  27.  *
  28.  * @since 2.7
  29.  */
  30. public abstract class SimplePathVisitor extends SimpleFileVisitor<Path> implements PathVisitor {

  31.     /**
  32.      * Abstracts builder for subclasses.
  33.      *
  34.      * @param <T> The SimplePathVisitor type.
  35.      * @param <B> The builder type.
  36.      * @since 2.19.0
  37.      */
  38.     protected abstract static class AbstractBuilder<T, B extends AbstractSupplier<T, B>> extends AbstractSupplier<T, B> {

  39.         private IOBiFunction<Path, IOException, FileVisitResult> visitFileFailedFunction;

  40.         /**
  41.          * Constructs a new builder for subclasses.
  42.          */
  43.         public AbstractBuilder() {
  44.             // empty.
  45.         }

  46.         IOBiFunction<Path, IOException, FileVisitResult> getVisitFileFailedFunction() {
  47.             return visitFileFailedFunction;
  48.         }

  49.         /**
  50.          * Sets the function to call on {@link #visitFileFailed(Path, IOException)}.
  51.          * <p>
  52.          * Defaults to {@link SimpleFileVisitor#visitFileFailed(Object, IOException)} on construction.
  53.          * </p>
  54.          *
  55.          * @param visitFileFailedFunction the function to call on {@link #visitFileFailed(Path, IOException)}.
  56.          * @return this instance.
  57.          */
  58.         public B setVisitFileFailedFunction(final IOBiFunction<Path, IOException, FileVisitResult> visitFileFailedFunction) {
  59.             this.visitFileFailedFunction = visitFileFailedFunction;
  60.             return asThis();
  61.         }

  62.     }

  63.     private final IOBiFunction<Path, IOException, FileVisitResult> visitFileFailedFunction;

  64.     /**
  65.      * Constructs a new instance.
  66.      */
  67.     protected SimplePathVisitor() {
  68.         this.visitFileFailedFunction = super::visitFileFailed;
  69.     }

  70.     /**
  71.      * Constructs a new instance.
  72.      *
  73.      * @param builder The builder provided by a subclass.
  74.      */
  75.     SimplePathVisitor(final AbstractBuilder<?, ?> builder) {
  76.         this.visitFileFailedFunction = builder.visitFileFailedFunction != null ? builder.visitFileFailedFunction : super::visitFileFailed;
  77.     }

  78.     /**
  79.      * Constructs a new instance.
  80.      *
  81.      * @param visitFileFailedFunction Called on {@link #visitFileFailed(Path, IOException)}.
  82.      */
  83.     protected SimplePathVisitor(final IOBiFunction<Path, IOException, FileVisitResult> visitFileFailedFunction) {
  84.         this.visitFileFailedFunction = Objects.requireNonNull(visitFileFailedFunction, "visitFileFailedFunction");
  85.     }

  86.     @Override
  87.     public FileVisitResult visitFileFailed(final Path file, final IOException exc) throws IOException {
  88.         return visitFileFailedFunction.apply(file, exc);
  89.     }
  90. }