11

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.

tbodt
  • 16,609
  • 6
  • 58
  • 83
aalku
  • 2,860
  • 2
  • 23
  • 44
  • Well...lambdas are the *implementation* of a Functional Interface, not abstract classes. Therefore, you can't do this with your `SimpleFileVisitor`. – Konstantin Yovkov Jun 24 '14 at 10:36
  • Thanks @kocko but I already said that in the question body. – aalku Jun 24 '14 at 10:54
  • Slight correction, they are not cast to a functional interface -- they have a target type, and a cast is *one way* to provide a target type for a lambda expression. – Brian Goetz Jun 25 '14 at 22:45

1 Answers1

17

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;
  });
Holger
  • 285,553
  • 42
  • 434
  • 765
  • Great idea. I wish that kind of helper classes were part of JRE. – aalku Jun 24 '14 at 10:57
  • 6
    In principle, it would be even possible to add these `on…` methods to the `SimpleFileVisitor` class itself without breaking compatibility with code using it without lambdas. But there are tons of classes in the JRE; the JRE developers simply have not enough time to think about useful evolution for every existing class… – Holger Jun 24 '14 at 11:05
  • @Holger You are right adding hooks for each class in the JRE be a tedious exercise. With that in mind it would be better if the language supported lamda override directly. Perhaps it would be a good feature request for Java X (if it isn't already requested). – Dev Jul 04 '16 at 21:27