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; ...@@ -14,6 +14,7 @@ import azgracompress.utilities.Utils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Optional;
public class Benchmark extends BenchmarkBase { public class Benchmark extends BenchmarkBase {
...@@ -59,8 +60,8 @@ public class Benchmark extends BenchmarkBase { ...@@ -59,8 +60,8 @@ public class Benchmark extends BenchmarkBase {
decompressOps.setOutputFilePath(decompressedFile); decompressOps.setOutputFilePath(decompressedFile);
ImageDecompressor decompressor = new ImageDecompressor(decompressOps); ImageDecompressor decompressor = new ImageDecompressor(decompressOps);
final ImageU16Dataset decompressedDataset = decompressor.decompressInMemory(); final Optional<ImageU16Dataset> maybeDataset = decompressor.decompressInMemory();
if (decompressedDataset == null) { if (!maybeDataset.isPresent()) {
System.err.println("Errors occurred during decompression."); System.err.println("Errors occurred during decompression.");
return; return;
} }
...@@ -75,7 +76,7 @@ public class Benchmark extends BenchmarkBase { ...@@ -75,7 +76,7 @@ public class Benchmark extends BenchmarkBase {
return; 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 int[] diffArray = Utils.getDifference(originalData, quantizedData);
final double mse = Utils.calculateMse(diffArray); final double mse = Utils.calculateMse(diffArray);
......
...@@ -8,6 +8,7 @@ import azgracompress.utilities.Stopwatch; ...@@ -8,6 +8,7 @@ import azgracompress.utilities.Stopwatch;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.io.*; import java.io.*;
import java.util.Optional;
@SuppressWarnings("DuplicatedCode") @SuppressWarnings("DuplicatedCode")
...@@ -233,22 +234,22 @@ public class ImageDecompressor extends CompressorDecompressorBase { ...@@ -233,22 +234,22 @@ public class ImageDecompressor extends CompressorDecompressorBase {
} }
// TODO(Moravec): Return optional to get rid of null check. // 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()); try (FileInputStream fileInputStream = new FileInputStream(options.getInputDataInfo().getFilePath());
DataInputStream dataInputStream = new DataInputStream(fileInputStream)) { DataInputStream dataInputStream = new DataInputStream(fileInputStream)) {
final QCMPFileHeader header = decompressQcmpHeader(dataInputStream); final QCMPFileHeader header = decompressQcmpHeader(dataInputStream);
if (header == null) if (header == null)
return null; return Optional.empty();
IImageDecompressor imageDecompressor = getImageDecompressor(header); IImageDecompressor imageDecompressor = getImageDecompressor(header);
if (imageDecompressor == null) { if (imageDecompressor == null) {
System.err.println("Unable to create correct decompressor."); System.err.println("Unable to create correct decompressor.");
return null; return Optional.empty();
} }
if (!checkInputFileSize(header, imageDecompressor)) { if (!checkInputFileSize(header, imageDecompressor)) {
return null; return Optional.empty();
} }
final int planePixelCount = header.getImageSizeX() * header.getImageSizeY(); final int planePixelCount = header.getImageSizeX() * header.getImageSizeY();
...@@ -259,14 +260,14 @@ public class ImageDecompressor extends CompressorDecompressorBase { ...@@ -259,14 +260,14 @@ public class ImageDecompressor extends CompressorDecompressorBase {
imageDecompressor.decompressToBuffer(dataInputStream, decompressedData, header); imageDecompressor.decompressToBuffer(dataInputStream, decompressedData, header);
} catch (ImageDecompressionException ex) { } catch (ImageDecompressionException ex) {
System.err.println(ex.getMessage()); 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) { } catch (IOException ioEx) {
ioEx.printStackTrace(); 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