Skip to content

Commit 1fa690f

Browse files
committed
bugfix
1 parent 0a9b10e commit 1fa690f

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package hexlet.code.source;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
5+
import hexlet.code.Formatter;
6+
7+
import java.io.FileNotFoundException;
8+
import java.io.IOException;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.util.List;
12+
import java.util.Map;
13+
import java.util.Set;
14+
import java.util.TreeMap;
15+
import java.util.TreeSet;
16+
import java.util.ArrayList;
17+
18+
19+
public class Differ {
20+
public static final String ADDED = "ADDED";
21+
public static final String DELETED = "DELETED";
22+
public static final String CHANGED = "CHANGED";
23+
public static final String UNCHANGED = "UNCHANGED";
24+
25+
public static String generate(String filePath1, String filePath2, String format) throws IOException {
26+
var normalizedPath1 = FileManager.normaolizePath(Path.of(filePath1));
27+
var normalizedPath2 = FileManager.normaolizePath(Path.of(filePath2));
28+
29+
30+
if (Files.notExists(normalizedPath1) || Files.notExists(normalizedPath2)) {
31+
throw new FileNotFoundException("Файл для чтения не найден");
32+
}
33+
34+
ObjectMapper mapper = (FileManager.isJsonFile(normalizedPath1)) ? new ObjectMapper() : new YAMLMapper();
35+
var dataFirst = mapper.readValue(normalizedPath1.toFile(), Map.class);
36+
var dataSecond = mapper.readValue(normalizedPath2.toFile(), Map.class);
37+
38+
39+
40+
Map<String, List<Object>> differenceMap = new TreeMap<>();
41+
Set<String> keySet = new TreeSet<>(dataFirst.keySet());
42+
keySet.addAll(dataSecond.keySet());
43+
44+
keySet.forEach(key -> {
45+
String stage = "";
46+
var dataFirstValue = dataFirst.get(key) == null ? "null" : dataFirst.get(key);
47+
var dataSecondValue = dataSecond.get(key) == null ? "null" : dataSecond.get(key);
48+
49+
if (dataFirst.containsKey(key) && dataSecond.containsKey(key)) {
50+
51+
stage = (dataFirstValue.equals(dataSecondValue)) ? UNCHANGED : CHANGED;
52+
53+
} else {
54+
stage = dataSecond.containsKey(key) ? ADDED : DELETED;
55+
}
56+
57+
List<Object> date = new ArrayList<>(3);
58+
date.add(stage);
59+
date.add(dataFirstValue);
60+
date.add(dataSecondValue);
61+
62+
differenceMap.put(key, date);
63+
});
64+
65+
return Formatter.getOrder(format, differenceMap);
66+
}
67+
}

0 commit comments

Comments
 (0)