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 */ 017 018package org.apache.commons.io.file; 019 020import java.io.IOException; 021import java.nio.file.FileVisitResult; 022import java.nio.file.Path; 023import java.nio.file.SimpleFileVisitor; 024import java.util.Objects; 025 026import org.apache.commons.io.build.AbstractSupplier; 027import org.apache.commons.io.function.IOBiFunction; 028 029/** 030 * A {@link SimpleFileVisitor} typed to a {@link Path}. 031 * 032 * @since 2.7 033 */ 034public abstract class SimplePathVisitor extends SimpleFileVisitor<Path> implements PathVisitor { 035 036 /** 037 * Abstracts builder for subclasses. 038 * 039 * @param <T> The SimplePathVisitor type. 040 * @param <B> The builder type. 041 * @since 2.19.0 042 */ 043 protected abstract static class AbstractBuilder<T, B extends AbstractSupplier<T, B>> extends AbstractSupplier<T, B> { 044 045 private IOBiFunction<Path, IOException, FileVisitResult> visitFileFailedFunction; 046 047 /** 048 * Constructs a new builder for subclasses. 049 */ 050 public AbstractBuilder() { 051 // empty. 052 } 053 054 IOBiFunction<Path, IOException, FileVisitResult> getVisitFileFailedFunction() { 055 return visitFileFailedFunction; 056 } 057 058 /** 059 * Sets the function to call on {@link #visitFileFailed(Path, IOException)}. 060 * <p> 061 * Defaults to {@link SimpleFileVisitor#visitFileFailed(Object, IOException)} on construction. 062 * </p> 063 * 064 * @param visitFileFailedFunction the function to call on {@link #visitFileFailed(Path, IOException)}. 065 * @return this instance. 066 */ 067 public B setVisitFileFailedFunction(final IOBiFunction<Path, IOException, FileVisitResult> visitFileFailedFunction) { 068 this.visitFileFailedFunction = visitFileFailedFunction; 069 return asThis(); 070 } 071 072 } 073 074 private final IOBiFunction<Path, IOException, FileVisitResult> visitFileFailedFunction; 075 076 /** 077 * Constructs a new instance. 078 */ 079 protected SimplePathVisitor() { 080 this.visitFileFailedFunction = super::visitFileFailed; 081 } 082 083 /** 084 * Constructs a new instance. 085 * 086 * @param builder The builder provided by a subclass. 087 */ 088 SimplePathVisitor(final AbstractBuilder<?, ?> builder) { 089 this.visitFileFailedFunction = builder.visitFileFailedFunction != null ? builder.visitFileFailedFunction : super::visitFileFailed; 090 } 091 092 /** 093 * Constructs a new instance. 094 * 095 * @param visitFileFailedFunction Called on {@link #visitFileFailed(Path, IOException)}. 096 */ 097 protected SimplePathVisitor(final IOBiFunction<Path, IOException, FileVisitResult> visitFileFailedFunction) { 098 this.visitFileFailedFunction = Objects.requireNonNull(visitFileFailedFunction, "visitFileFailedFunction"); 099 } 100 101 @Override 102 public FileVisitResult visitFileFailed(final Path file, final IOException exc) throws IOException { 103 return visitFileFailedFunction.apply(file, exc); 104 } 105}