On Tue, 1 Jun 2021 17:20:38 GMT, Patrick Concannon <pconcan...@openjdk.org> 
wrote:

>> Hi,
>> 
>> Could someone please review my code for updating the code in the `java.net` 
>> and `java.nio` packages to make use of the switch expressions?
>> 
>> Kind regards,
>> Patrick
>
> Patrick Concannon has updated the pull request incrementally with one 
> additional commit since the last revision:
> 
>   8268056: Reverted changes to URLDecoder; reformatted change to FileTime

src/java.base/share/classes/java/nio/file/Files.java line 2832:

> 2830:                             result = FileVisitResult.CONTINUE;
> 2831:                     }
> 2832:                     default -> throw new AssertionError("Should not get 
> here");

This is subjective, and we're still finding our way with how best to construct 
some of the more complex switch expressions.

Where possible, I think it is best to remove assignments from the individual 
case branches. The use of `yield` makes it very clear what is going on, and 
ensures that each case branch, well... yields something. So how about:


  FileVisitResult result = switch (ev.type()) {
      case ENTRY -> {
          IOException ioe = ev.ioeException();
          if (ioe == null) {
              assert ev.attributes() != null;
              yield visitor.visitFile(ev.file(), ev.attributes());
          } else {
              yield visitor.visitFileFailed(ev.file(), ioe);
          }
      }
      case START_DIRECTORY -> {
          var r = visitor.preVisitDirectory(ev.file(), ev.attributes());

          // if SKIP_SIBLINGS and SKIP_SUBTREE is returned then
          // there shouldn't be any more events for the current
          // directory.
          if (r == FileVisitResult.SKIP_SUBTREE ||
              r == FileVisitResult.SKIP_SIBLINGS)
              walker.pop();
          yield r;
      }
      case END_DIRECTORY -> {
          var r = visitor.postVisitDirectory(ev.file(), ev.ioeException());
          // SKIP_SIBLINGS is a no-op for postVisitDirectory
          if (r == FileVisitResult.SKIP_SIBLINGS)
              r = FileVisitResult.CONTINUE;
          yield r;
      }
      default -> throw new AssertionError("Should not get here");
  };

-------------

PR: https://git.openjdk.java.net/jdk/pull/4285

Reply via email to