Lambda expressions must be cast to a functional interface. They cannot extend a class as far as I know but I want to know if there is a way to get something similar.
I have java.nio.file.SimpleFileVisitor<Path>
as base class and I want to override a method of it but I wish to do so inside another method. I can do it with an anonymous class this way:
public static void printContent(Path path) throws IOException {
FileVisitor<Path> visitor = new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
System.out.println(file);
return super.visitFile(file, attrs);
}
};
Files.walkFileTree(path, visitor);
}
Is there a way to remove that load of code with the help of a lambda?
I think the lambda would be (f) -> System.out.println(f);
I thought about forgetting SimpleFileVisitor and creating a equivalent interface with default methods but, how could I select what method to override? Would I need to leave the method I want to override without default implementation? In that case I would need several Interfaces for different cases with different not implemented methods.
Thank you.
Use Delegation. For this task you need a helper class that has to be implemented only once:
interface IoBiFunction<T, U, R> {
R apply(T t, U u) throws IOException;
}
class LambdaFileVisitor<T> extends SimpleFileVisitor<T> {
IoBiFunction<T, BasicFileAttributes, FileVisitResult> preVisitDir=super::preVisitDirectory;
IoBiFunction<T, BasicFileAttributes, FileVisitResult> visitFile=super::visitFile;
IoBiFunction<T, IOException, FileVisitResult> visitFailed=super::visitFileFailed;
IoBiFunction<T, IOException, FileVisitResult> postVisitDir=super::postVisitDirectory;
public LambdaFileVisitor<T> onVisitFile(IoBiFunction<T, BasicFileAttributes, FileVisitResult> f) {
this.visitFile = Objects.requireNonNull(f);
return this;
}
public LambdaFileVisitor<T> onVisitFailed(IoBiFunction<T, IOException, FileVisitResult> f) {
this.visitFailed = Objects.requireNonNull(f);
return this;
}
public LambdaFileVisitor<T> onPreVisitDir(IoBiFunction<T, BasicFileAttributes, FileVisitResult> f) {
this.preVisitDir = Objects.requireNonNull(f);
return this;
}
public LambdaFileVisitor<T> onPostVisitDir(IoBiFunction<T, IOException, FileVisitResult> f) {
this.postVisitDir = Objects.requireNonNull(f);
return this;
}
@Override
public FileVisitResult visitFile(T file, BasicFileAttributes attrs) throws IOException {
return visitFile.apply(file, attrs);
}
@Override
public FileVisitResult visitFileFailed(T file, IOException exc) throws IOException {
return visitFailed.apply(file, exc);
}
@Override
public FileVisitResult preVisitDirectory(T dir, BasicFileAttributes attrs) throws IOException {
return preVisitDir.apply(dir, attrs);
}
@Override
public FileVisitResult postVisitDirectory(T dir, IOException exc) throws IOException {
return postVisitDir.apply(dir, exc);
}
}
Once you have your helper class you can use it together with lambda expressions, e.g.
FileVisitor<Path> fv=new LambdaFileVisitor<Path>()
.onVisitFile((f,a)->{System.out.println(f); return CONTINUE; })
.onVisitFailed((f,e)->{ throw e; });
or
FileVisitor<Path> fv2=new LambdaFileVisitor<Path>()
.onPreVisitDir((f,a)->{System.out.println("ENTER "+f); return CONTINUE; })
.onPostVisitDir((f,e)->{
System.out.println("LEAVE "+f);
if(e!=null) throw e; else return CONTINUE;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With