I was trying to refactor an old code to use streams, and my first approach was this:
public void run() throws IOException {
Files.list(this.source)
.filter(Images::isImage)
.map(Image::new)
.filter(image -> image.isProportional(this.height, this.width))
.map(image -> image.resize(this.height, this.width))
.forEach(image -> Images.write(image, this.destination));
}
This is not compiling since new Image() and Images.write() throws IOExceptions.
Wrapping those exceptions with UncheckedIOException wouldn't do the trick as I don't want to stop other images to be processed if one of them fails.
So I ended writing 2 private methods:
private Optional<Image> createImage(Path imagePath) {
try {
return Optional.of(new Image(imagePath));
} catch (IOException e) {
return Optional.empty();
}
}
private void write(Image image) {
try {
Images.write(image, this.destination);
} catch (IOException e) {
// log error
}
}
createImage() returns an Optional since this seems sensible. However after this my code got really ugly:
public void run() throws IOException {
Files.list(source)
.filter(Images::isImage)
.map(this::createImage)
.filter(image -> image.isPresent() && image.get().isProportional(this.height, this.width))
.map(image -> image.get().resize(this.height, this.width))
.forEach(this::write);
}
Is there a way to avoid using get() and isPresent() on that code?
Thanks!