Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.esa.snap.core.dataio;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ProductReaderUtils {

public static final Class<?>[] IO_TYPES = new Class[]{
Path.class,
File.class,
String.class
};
public static final OutputConverter[] IO_CONVERTERS = new OutputConverter[]{
output -> (Path) output,
output -> ((File) output).toPath(),
output -> Paths.get((String) output)
};

public static Path convertToPath(final Object object) {
for (int i = 0; i < IO_TYPES.length; i++) {
if (IO_TYPES[i].isInstance(object)) {
return IO_CONVERTERS[i].convertOutput(object);
}
}
return null;
}

private interface OutputConverter {

Path convertOutput(Object output);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.esa.snap.core.dataio;

import org.junit.Test;

import java.awt.*;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;

public class ProductReaderUtilsTest {

@Test
public void testThatIO_TypesContains_Path_File_and_String_classes() {
assertThat(ProductReaderUtils.IO_TYPES, is(new Class[]{Path.class, File.class, String.class}));
}

@Test
public void testThatInputObjectsAreConvertedToPath_String_to_Path() {
Path path = ProductReaderUtils.convertToPath("/some/path/Filename"); // StringCase
assertThat(path, is(notNullValue()));
assertThat(path.toString(), matchesPattern("[/\\\\]{1}some[\\\\/]{1}path[\\\\/]{1}Filename"));
}

@Test
public void testThatInputObjectsAreConvertedToPath_File_to_Path() {
Path path = ProductReaderUtils.convertToPath(new File("/some/other/path/Filename"));
assertThat(path, is(notNullValue()));
assertThat(path.toString(), matchesPattern("[/\\\\]{1}some[\\\\/]{1}other[\\\\/]{1}path[\\\\/]{1}Filename"));
}

@Test
public void testThatInputObjectsAreConvertedToPath_Path_to_Path() {
Path path = ProductReaderUtils.convertToPath(Paths.get("/some/path/Pathname"));
assertThat(path, is(notNullValue()));
assertThat(path.toString(), matchesPattern("[/\\\\]{1}some[\\\\/]{1}path[\\\\/]{1}Pathname"));
}

@Test
public void testThatInvalidObjectsToBeConverted_ReturnNull() {
Path path = ProductReaderUtils.convertToPath(new Color(2, 3, 4));
assertThat(path, is(nullValue()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,13 @@
import org.esa.snap.core.datamodel.ProductData;
import org.esa.snap.dataio.znap.preferences.ZnapPreferencesConstants;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -132,27 +130,6 @@ final class ZnapConstantsAndUtils {
public static final String NAME_MASKS = "Masks";
public static final String NAME_FILTER_BANDS = "FilterBands";

static final Class<?>[] IO_TYPES = new Class[]{
Path.class,
File.class,
String.class
};

private static final OutputConverter[] IO_CONVERTERS = new OutputConverter[]{
output -> (Path) output,
output -> ((File) output).toPath(),
output -> Paths.get((String) output)
};

static Path convertToPath(final Object object) {
for (int i = 0; i < IO_TYPES.length; i++) {
if (IO_TYPES[i].isInstance(object)) {
return IO_CONVERTERS[i].convertOutput(object);
}
}
return null;
}

static boolean isExistingEmptyDirectory(Path path) {
try {
return Files.isDirectory(path) && Files.list(path).count() == 0;
Expand All @@ -162,11 +139,6 @@ static boolean isExistingEmptyDirectory(Path path) {
return false;
}

private interface OutputConverter {

Path convertOutput(Object output);
}

static SnapDataType getSnapDataType(DataType zarrDataType) {
if (zarrDataType == DataType.f8) {
return SnapDataType.TYPE_FLOAT64;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
import static org.esa.snap.dataio.znap.ZnapConstantsAndUtils.VIRTUAL_BAND_EXPRESSION;
import static org.esa.snap.dataio.znap.ZnapConstantsAndUtils.WAVELENGTH;
import static org.esa.snap.dataio.znap.ZnapConstantsAndUtils.cast;
import static org.esa.snap.dataio.znap.ZnapConstantsAndUtils.convertToPath;
import static org.esa.snap.core.dataio.ProductReaderUtils.convertToPath;
import static org.esa.snap.dataio.znap.ZnapConstantsAndUtils.getSnapDataType;
import static ucar.nc2.constants.ACDD.TIME_END;
import static ucar.nc2.constants.ACDD.TIME_START;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.esa.snap.core.dataio.ProductReader;
import org.esa.snap.core.dataio.ProductReaderPlugIn;
import org.esa.snap.core.util.io.SnapFileFilter;
import org.esa.snap.core.dataio.ProductReaderUtils;

import java.io.File;
import java.io.IOException;
Expand All @@ -42,7 +43,7 @@ public class ZnapProductReaderPlugIn implements ProductReaderPlugIn {

@Override
public DecodeQualification getDecodeQualification(Object input) {
final Path inputPath = convertToPath(input);
final Path inputPath = ProductReaderUtils.convertToPath(input);
if (inputPath == null) {
return DecodeQualification.UNABLE;
}
Expand Down Expand Up @@ -94,7 +95,7 @@ public DecodeQualification getDecodeQualification(Object input) {

@Override
public Class<?>[] getInputTypes() {
return IO_TYPES;
return ProductReaderUtils.IO_TYPES;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
import static org.esa.snap.dataio.znap.ZnapConstantsAndUtils.WAVELENGTH_UNIT;
import static org.esa.snap.dataio.znap.ZnapConstantsAndUtils.ZNAP_CONTAINER_EXTENSION;
import static org.esa.snap.dataio.znap.ZnapConstantsAndUtils.ZNAP_ZIP_CONTAINER_EXTENSION;
import static org.esa.snap.dataio.znap.ZnapConstantsAndUtils.convertToPath;
import static org.esa.snap.core.dataio.ProductReaderUtils.convertToPath;
import static org.esa.snap.dataio.znap.ZnapConstantsAndUtils.getSnapDataType;
import static org.esa.snap.dataio.znap.ZnapConstantsAndUtils.writeProductMetadata;
import static org.esa.snap.dataio.znap.preferences.ZnapPreferencesConstants.DEFAULT_COMPRESSION_LEVEL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.esa.snap.core.dataio.ProductWriterPlugIn;
import org.esa.snap.core.datamodel.Product;
import org.esa.snap.core.util.io.SnapFileFilter;
import org.esa.snap.core.dataio.ProductReaderUtils;
import org.esa.snap.runtime.Config;

import java.util.Locale;
Expand All @@ -39,7 +40,7 @@ public EncodeQualification getEncodeQualification(Product product) {
}

public Class<?>[] getOutputTypes() {
return IO_TYPES;
return ProductReaderUtils.IO_TYPES;
}

public ProductWriter createWriterInstance() {
Expand Down