Description
After JDK-8263411, the stream call inside jdk.jshell.SnippetMaps::fullClassNameAndPackageToClass looks confusing:
return Stream.concat(Stream.of("java.lang"), pkgs)
.filter(ipkg -> !ipkg.isEmpty() && ipkg.equals(pkg))
.map(ipkg -> full.substring(pkg.length() + 1))
.findFirst()
.orElse(full);
The `map` step ignores the input element, so the subsequent stream content doesn't depend on the previous stream content. This looks like a misuse of stream API. In fact, using anyMatch here would be better:
return Stream.concat(Stream.of("java.lang"), pkgs)
.anyMatch(ipkg -> !ipkg.isEmpty() && ipkg.equals(pkg))
? full.substring(pkg.length() + 1) : full;
Moreover, `!ipkg.isEmpty() && ipkg.equals(pkg)` is the same as `!pkg.isEmpty() && ipkg.equals(pkg)`, and as `!pkg.isEmpty()` check doesn't depend on the stream content, it could be extracted to a separate check. In case of empty package, stream is not necessary at all.
return Stream.concat(Stream.of("java.lang"), pkgs)
.filter(ipkg -> !ipkg.isEmpty() && ipkg.equals(pkg))
.map(ipkg -> full.substring(pkg.length() + 1))
.findFirst()
.orElse(full);
The `map` step ignores the input element, so the subsequent stream content doesn't depend on the previous stream content. This looks like a misuse of stream API. In fact, using anyMatch here would be better:
return Stream.concat(Stream.of("java.lang"), pkgs)
.anyMatch(ipkg -> !ipkg.isEmpty() && ipkg.equals(pkg))
? full.substring(pkg.length() + 1) : full;
Moreover, `!ipkg.isEmpty() && ipkg.equals(pkg)` is the same as `!pkg.isEmpty() && ipkg.equals(pkg)`, and as `!pkg.isEmpty()` check doesn't depend on the stream content, it could be extracted to a separate check. In case of empty package, stream is not necessary at all.
Attachments
Issue Links
- relates to
-
JDK-8263411 Convert jshell tool to use Stream.toList()
-
- Resolved
-