Skip to content
Snippets Groups Projects
Commit d971b564 authored by Vojtěch Moravec's avatar Vojtěch Moravec
Browse files

Make decompressInMemory return Optional<T> instead of maybe null.

parent 01d5185f
No related branches found
No related tags found
No related merge requests found
......@@ -14,6 +14,7 @@ import azgracompress.utilities.Utils;
import java.io.File;
import java.io.IOException;
import java.util.Optional;
public class Benchmark extends BenchmarkBase {
......@@ -59,8 +60,8 @@ public class Benchmark extends BenchmarkBase {
decompressOps.setOutputFilePath(decompressedFile);
ImageDecompressor decompressor = new ImageDecompressor(decompressOps);
final ImageU16Dataset decompressedDataset = decompressor.decompressInMemory();
if (decompressedDataset == null) {
final Optional<ImageU16Dataset> maybeDataset = decompressor.decompressInMemory();
if (!maybeDataset.isPresent()) {
System.err.println("Errors occurred during decompression.");
return;
}
......@@ -75,7 +76,7 @@ public class Benchmark extends BenchmarkBase {
return;
}
final int[] quantizedData = TypeConverter.shortArrayToIntArray(decompressedDataset.getPlaneData(0));
final int[] quantizedData = TypeConverter.shortArrayToIntArray(maybeDataset.get().getPlaneData(0));
final int[] diffArray = Utils.getDifference(originalData, quantizedData);
final double mse = Utils.calculateMse(diffArray);
......
......@@ -8,6 +8,7 @@ import azgracompress.utilities.Stopwatch;
import org.jetbrains.annotations.Nullable;
import java.io.*;
import java.util.Optional;
@SuppressWarnings("DuplicatedCode")
......@@ -233,22 +234,22 @@ public class ImageDecompressor extends CompressorDecompressorBase {
}
// TODO(Moravec): Return optional to get rid of null check.
public ImageU16Dataset decompressInMemory() {
public Optional<ImageU16Dataset> decompressInMemory() {
try (FileInputStream fileInputStream = new FileInputStream(options.getInputDataInfo().getFilePath());
DataInputStream dataInputStream = new DataInputStream(fileInputStream)) {
final QCMPFileHeader header = decompressQcmpHeader(dataInputStream);
if (header == null)
return null;
return Optional.empty();
IImageDecompressor imageDecompressor = getImageDecompressor(header);
if (imageDecompressor == null) {
System.err.println("Unable to create correct decompressor.");
return null;
return Optional.empty();
}
if (!checkInputFileSize(header, imageDecompressor)) {
return null;
return Optional.empty();
}
final int planePixelCount = header.getImageSizeX() * header.getImageSizeY();
......@@ -259,14 +260,14 @@ public class ImageDecompressor extends CompressorDecompressorBase {
imageDecompressor.decompressToBuffer(dataInputStream, decompressedData, header);
} catch (ImageDecompressionException ex) {
System.err.println(ex.getMessage());
return null;
return Optional.empty();
}
return new ImageU16Dataset(header.getImageDims().toV2i(), header.getImageSizeZ(), decompressedData);
return Optional.of(new ImageU16Dataset(header.getImageDims().toV2i(), header.getImageSizeZ(), decompressedData));
} catch (IOException ioEx) {
ioEx.printStackTrace();
return null;
return Optional.empty();
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment