Refactorings FileLoader

This commit is contained in:
ngb
2022-07-15 22:27:44 +02:00
parent 22c0547caa
commit 91805f7794
3 changed files with 71 additions and 12 deletions

View File

@@ -29,7 +29,7 @@ public final class FileLoader {
try { try {
return Files.readAllLines(Paths.get(ResourceStreamProvider.getResourceURL(source).toURI()), charset); return Files.readAllLines(Paths.get(ResourceStreamProvider.getResourceURL(source).toURI()), charset);
} catch( IOException | URISyntaxException ex ) { } catch( IOException | URISyntaxException ex ) {
LOG.warn(ex, "Error while loading lines from source <%s>", source); LOG.error(ex, "Error while loading lines from source <%s>", source);
} }
return Collections.EMPTY_LIST; return Collections.EMPTY_LIST;
@@ -43,17 +43,37 @@ public final class FileLoader {
try { try {
return Files.readString(Paths.get(ResourceStreamProvider.getResourceURL(source).toURI()), charset); return Files.readString(Paths.get(ResourceStreamProvider.getResourceURL(source).toURI()), charset);
} catch( IOException | URISyntaxException ex ) { } catch( IOException | URISyntaxException ex ) {
LOG.warn(ex, "Error while loading text from source <%s>", source); LOG.error(ex, "Error while loading text from source <%s>", source);
} }
return ""; return "";
} }
public static double[][] loadDoubles( String source, char separator, boolean skipFirst ) { public static String[][] loadCsv( String source, boolean skipFirst ) {
return loadDoubles(source, separator, skipFirst, UTF8); return loadCsv(source, ',', skipFirst, UTF8);
} }
public static double[][] loadDoubles( String source, char separator, boolean skipFirst, Charset charset ) { public static String[][] loadCsv( String source, char separator, boolean skipFirst, Charset charset ) {
try {
int n = skipFirst ? 1 : 0;
return Files
.lines(Paths.get(ResourceStreamProvider.getResourceURL(source).toURI()), charset)
.skip(n)
.map(
( line ) -> line.split(Character.toString(separator))
).toArray(String[][]::new);
} catch( IOException | URISyntaxException ex ) {
LOG.error(ex, "Error while loading csv source <%s>", source);
}
return new String[0][0];
}
public static double[][] loadValues( String source, char separator, boolean skipFirst ) {
return loadValues(source, separator, skipFirst, UTF8);
}
public static double[][] loadValues( String source, char separator, boolean skipFirst, Charset charset ) {
try { try {
int n = skipFirst ? 1 : 0; int n = skipFirst ? 1 : 0;
return Files return Files
@@ -73,7 +93,7 @@ public final class FileLoader {
).toArray() ).toArray()
).toArray(double[][]::new); ).toArray(double[][]::new);
} catch( IOException | URISyntaxException ex ) { } catch( IOException | URISyntaxException ex ) {
LOG.warn(ex, "Error while loading double values from csv source <%s>", source); LOG.error(ex, "Error while loading double values from csv source <%s>", source);
} }
return new double[0][0]; return new double[0][0];

View File

@@ -5,6 +5,7 @@ import schule.ngb.zm.Zeichenmaschine;
import java.io.*; import java.io.*;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.nio.file.Files;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
/** /**
@@ -149,11 +150,17 @@ public class ResourceStreamProvider {
return new URL(source); return new URL(source);
} }
/**
* Ver
* @param source
* @return
* @throws IOException
*/
public static OutputStream getOutputStream( String source ) throws IOException { public static OutputStream getOutputStream( String source ) throws IOException {
Validator.requireNotNull(source, "Resource source may not be null"); Validator.requireNotNull(source, "Resource source may not be null");
Validator.requireNotEmpty(source, "Resource source may not be empty."); Validator.requireNotEmpty(source, "Resource source may not be empty.");
URL url = getResourceURL(source);
return getOutputStream(new File(source)); return getOutputStream(new File(url.getPath()));
} }
public static OutputStream getOutputStream( File file ) throws IOException { public static OutputStream getOutputStream( File file ) throws IOException {

View File

@@ -2,7 +2,6 @@ package schule.ngb.zm.util;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@@ -62,7 +61,7 @@ class FileLoaderTest {
} }
@Test @Test
void loadCsv() { void loadValues() {
double[][] data; double[][] data;
double[][] csv; double[][] csv;
@@ -71,10 +70,43 @@ class FileLoaderTest {
{2.1,2.2,2.3}, {2.1,2.2,2.3},
{3.1,3.2,3.3} {3.1,3.2,3.3}
}; };
csv = FileLoader.loadDoubles("data_comma.csv", ',', true); csv = FileLoader.loadValues("data_comma.csv", ',', true);
assertArrayEquals(data, csv); assertArrayEquals(data, csv);
csv = FileLoader.loadDoubles("data_semicolon_latin.csv", ';', true, FileLoader.ISO_8859_1); csv = FileLoader.loadValues("data_semicolon_latin.csv", ';', true, FileLoader.ISO_8859_1);
assertArrayEquals(data, csv);
}
@Test
void loadCsv() {
String[][] data;
String[][] csv;
data = new String[][]{
{"Header1","Header2","Header3"},
{"1.1","1.2","1.3"},
{"2.1","2.2","2.3"},
{"3.1","3.2","3.3"}
};
csv = FileLoader.loadCsv("data_comma.csv", false);
assertArrayEquals(data, csv);
data = new String[][]{
{"1.1","1.2","1.3"},
{"2.1","2.2","2.3"},
{"3.1","3.2","3.3"}
};
csv = FileLoader.loadCsv("data_semicolon.csv", ';', true, FileLoader.UTF8);
assertArrayEquals(data, csv);
data = new String[][]{
{"Nöme","Häder2","Straße"},
{"1.1","1.2","1.3"},
{"2.1","2.2","2.3"},
{"3.1","3.2","3.3"}
};
csv = FileLoader.loadCsv("data_semicolon_latin.csv", ';', false, FileLoader.ISO_8859_1);
assertArrayEquals(data, csv); assertArrayEquals(data, csv);
} }