From 0a9b10e2ca6edafda679312d0ace92696ac1176f Mon Sep 17 00:00:00 2001 From: Alexandr Ryabinin Date: Tue, 15 Oct 2024 01:06:34 +0300 Subject: [PATCH 1/2] bugfix --- .../main/java/hexlet/code/source/Differ.java | 69 ------------------- 1 file changed, 69 deletions(-) delete mode 100644 app/src/main/java/hexlet/code/source/Differ.java diff --git a/app/src/main/java/hexlet/code/source/Differ.java b/app/src/main/java/hexlet/code/source/Differ.java deleted file mode 100644 index f2ccdca..0000000 --- a/app/src/main/java/hexlet/code/source/Differ.java +++ /dev/null @@ -1,69 +0,0 @@ -package hexlet.code.source; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; -import hexlet.code.Formatter; - -import java.io.FileNotFoundException; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.List; -import java.util.Set; -import java.util.Map; -import java.util.TreeMap; -import java.util.TreeSet; -import java.util.ArrayList; - - -public class Differ { - - public static final String ADDED = "ADDED"; - public static final String DELETED = "DELETED"; - public static final String CHANGED = "CHANGED"; - public static final String UNCHANGED = "UNCHANGED"; - - public static String generate(String filePath1, String filePath2, String format) throws IOException { - var normalizedPath1 = FileManager.normaolizePath(Path.of(filePath1)); - var normalizedPath2 = FileManager.normaolizePath(Path.of(filePath2)); - - - if (Files.notExists(normalizedPath1) || Files.notExists(normalizedPath2)) { - throw new FileNotFoundException("Файл для чтения не найден"); - } - - ObjectMapper mapper = (FileManager.isJsonFile(normalizedPath1)) ? new ObjectMapper() : new YAMLMapper(); - var dataFirst = mapper.readValue(normalizedPath1.toFile(), Map.class); - var dataSecond = mapper.readValue(normalizedPath2.toFile(), Map.class); - - - - Map> differenceMap = new TreeMap<>(); - Set keySet = new TreeSet<>(dataFirst.keySet()); - keySet.addAll(dataSecond.keySet()); - - keySet.forEach(key -> { - String stage = ""; - var dataFirstValue = dataFirst.get(key) == null ? "null" : dataFirst.get(key); - var dataSecondValue = dataSecond.get(key) == null ? "null" : dataSecond.get(key); - - if (dataFirst.containsKey(key) && dataSecond.containsKey(key)) { - - stage = (dataFirstValue.equals(dataSecondValue)) ? UNCHANGED : CHANGED; - - } else { - stage = dataSecond.containsKey(key) ? ADDED : DELETED; - } - - List date = new ArrayList<>(3); - date.add(stage); - date.add(dataFirstValue); - date.add(dataSecondValue); - - differenceMap.put(key, date); - }); - - return Formatter.getOrder(format, differenceMap); - } - -} From 1fa690fdeeb206f6e61833751527801d572484ec Mon Sep 17 00:00:00 2001 From: Alexandr Ryabinin Date: Tue, 15 Oct 2024 01:07:31 +0300 Subject: [PATCH 2/2] bugfix --- .../main/java/hexlet/code/source/Differ.java | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 app/src/main/java/hexlet/code/source/Differ.java diff --git a/app/src/main/java/hexlet/code/source/Differ.java b/app/src/main/java/hexlet/code/source/Differ.java new file mode 100644 index 0000000..25f09a2 --- /dev/null +++ b/app/src/main/java/hexlet/code/source/Differ.java @@ -0,0 +1,67 @@ +package hexlet.code.source; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; +import hexlet.code.Formatter; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.ArrayList; + + +public class Differ { + public static final String ADDED = "ADDED"; + public static final String DELETED = "DELETED"; + public static final String CHANGED = "CHANGED"; + public static final String UNCHANGED = "UNCHANGED"; + + public static String generate(String filePath1, String filePath2, String format) throws IOException { + var normalizedPath1 = FileManager.normaolizePath(Path.of(filePath1)); + var normalizedPath2 = FileManager.normaolizePath(Path.of(filePath2)); + + + if (Files.notExists(normalizedPath1) || Files.notExists(normalizedPath2)) { + throw new FileNotFoundException("Файл для чтения не найден"); + } + + ObjectMapper mapper = (FileManager.isJsonFile(normalizedPath1)) ? new ObjectMapper() : new YAMLMapper(); + var dataFirst = mapper.readValue(normalizedPath1.toFile(), Map.class); + var dataSecond = mapper.readValue(normalizedPath2.toFile(), Map.class); + + + + Map> differenceMap = new TreeMap<>(); + Set keySet = new TreeSet<>(dataFirst.keySet()); + keySet.addAll(dataSecond.keySet()); + + keySet.forEach(key -> { + String stage = ""; + var dataFirstValue = dataFirst.get(key) == null ? "null" : dataFirst.get(key); + var dataSecondValue = dataSecond.get(key) == null ? "null" : dataSecond.get(key); + + if (dataFirst.containsKey(key) && dataSecond.containsKey(key)) { + + stage = (dataFirstValue.equals(dataSecondValue)) ? UNCHANGED : CHANGED; + + } else { + stage = dataSecond.containsKey(key) ? ADDED : DELETED; + } + + List date = new ArrayList<>(3); + date.add(stage); + date.add(dataFirstValue); + date.add(dataSecondValue); + + differenceMap.put(key, date); + }); + + return Formatter.getOrder(format, differenceMap); + } +}