From 264c0020e0e0045de7e266bf1f504e45da8de0bd Mon Sep 17 00:00:00 2001 From: Andrei Punko Date: Sat, 26 Oct 2024 22:59:42 +0300 Subject: [PATCH] Remove ending space on each row from result of saving Matrix --- src/main/java/by/andd3dfx/util/FileUtil.java | 27 ++++++++++--------- .../java/by/andd3dfx/util/FileUtilTest.java | 16 +++++------ src/test/resources/matrix-not-original.txt | 5 ++-- src/test/resources/matrix-original.txt | 4 +-- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/main/java/by/andd3dfx/util/FileUtil.java b/src/main/java/by/andd3dfx/util/FileUtil.java index 16d91ad..4f0275d 100644 --- a/src/main/java/by/andd3dfx/util/FileUtil.java +++ b/src/main/java/by/andd3dfx/util/FileUtil.java @@ -2,11 +2,13 @@ import by.andd3dfx.math.Interval; import by.andd3dfx.math.Matrix; -import lombok.SneakyThrows; import java.io.FileWriter; import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; import java.util.function.Function; +import java.util.stream.Collectors; /** * Util to serialize data into files @@ -42,24 +44,25 @@ public static void saveFunc(Interval timeInterval, Function xFun serialize(fileName, sb); } - @SneakyThrows - public static void save(Matrix m, String fileName, boolean rowsToColumns) { - var file = new FileWriter(fileName); - if (rowsToColumns) { + public static void save(Matrix m, String fileName, boolean rotate) { + var sb = new StringBuilder(); + if (rotate) { for (int j = 0; j < m.getN(); j++) { + var list = new ArrayList(); for (int i = 0; i < m.getM(); i++) { - file.write(m.get(i, j) + " "); + list.add(m.get(i, j)); } - file.write("\n"); + sb.append(list.stream().map(String::valueOf) + .collect(Collectors.joining(" ")) + "\n"); } } else { for (int i = 0; i < m.getM(); i++) { - for (int j = 0; j < m.getN(); j++) { - file.write(m.get(i, j) + " "); - } - file.write("\n"); + var string = Arrays.stream(m.get(i)) + .mapToObj(String::valueOf) + .collect(Collectors.joining(" ")); + sb.append(string + "\n"); } } - file.close(); + serialize(fileName, sb); } } diff --git a/src/test/java/by/andd3dfx/util/FileUtilTest.java b/src/test/java/by/andd3dfx/util/FileUtilTest.java index 84c372e..143c0b8 100644 --- a/src/test/java/by/andd3dfx/util/FileUtilTest.java +++ b/src/test/java/by/andd3dfx/util/FileUtilTest.java @@ -46,11 +46,9 @@ void saveFuncForParameterizedXAndY() throws IOException { @Test void saveMatrixInOriginalView() throws IOException { final var fileName = "matrix-original.txt"; - var m = new Matrix(2, 2); - m.set(0, 0, 5); - m.set(0, 1, 6); - m.set(1, 0, 9); - m.set(1, 1, 1); + var m = new Matrix(2, 3); + m.set(0, new double[]{50, 51, 52}); + m.set(1, new double[]{65, 66, 67}); FileUtil.save(m, BUILD_PATH + fileName, false); @@ -60,11 +58,9 @@ void saveMatrixInOriginalView() throws IOException { @Test void saveMatrixWithConversionRowsToColumns() throws IOException { final var fileName = "matrix-not-original.txt"; - var m = new Matrix(2, 2); - m.set(0, 0, 5); - m.set(0, 1, 6); - m.set(1, 0, 9); - m.set(1, 1, 1); + var m = new Matrix(2, 3); + m.set(0, new double[]{50, 51, 52}); + m.set(1, new double[]{65, 66, 67}); FileUtil.save(m, BUILD_PATH + fileName, true); diff --git a/src/test/resources/matrix-not-original.txt b/src/test/resources/matrix-not-original.txt index 5836c44..e1ab380 100644 --- a/src/test/resources/matrix-not-original.txt +++ b/src/test/resources/matrix-not-original.txt @@ -1,2 +1,3 @@ -5.0 9.0 -6.0 1.0 +50.0 65.0 +51.0 66.0 +52.0 67.0 diff --git a/src/test/resources/matrix-original.txt b/src/test/resources/matrix-original.txt index b2d478f..d322f05 100644 --- a/src/test/resources/matrix-original.txt +++ b/src/test/resources/matrix-original.txt @@ -1,2 +1,2 @@ -5.0 6.0 -9.0 1.0 +50.0 51.0 52.0 +65.0 66.0 67.0