Skip to content

Commit 8c7328c

Browse files
authored
Merge pull request #3 from AlexSorb/fix-one
Fix one
2 parents b1c656d + 0941430 commit 8c7328c

File tree

17 files changed

+263
-455
lines changed

17 files changed

+263
-455
lines changed

app/src/main/java/hexlet/code/Formatter.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
package hexlet.code;
22

33
import com.fasterxml.jackson.core.JsonProcessingException;
4-
import hexlet.code.source.Difference;
54
import hexlet.code.source.formatters.Json;
65
import hexlet.code.source.formatters.Plain;
76
import hexlet.code.source.formatters.Stylish;
87

98
import java.util.List;
9+
import java.util.Map;
1010

1111
public class Formatter {
1212

13-
public static String getOrder(String format, List<Difference> difference) throws IllegalArgumentException,
14-
JsonProcessingException {
13+
public static String getOrder(String format, Map<String, List<Object>> differenceMap)
14+
throws IllegalArgumentException, JsonProcessingException {
1515
var normalizeFormat = format.trim().toLowerCase();
1616
var result = "";
1717

1818
switch (normalizeFormat) {
1919
case "plain":
20-
result += Plain.plain(difference);
20+
result += Plain.plain(differenceMap);
2121
break;
2222
case "stylish":
23-
result += Stylish.stylish(difference);
23+
result += Stylish.stylish(differenceMap);
2424
break;
2525
case "json":
26-
result += Json.json(difference);
26+
result += Json.json(differenceMap);
2727
break;
2828
default:
2929
throw new IllegalArgumentException("Не найден формат");
Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
package hexlet.code.source;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
35
import hexlet.code.Formatter;
4-
import hexlet.code.source.parsers.Parser;
5-
import hexlet.code.source.parsers.ParserJson;
6-
import hexlet.code.source.parsers.ParserYAML;
76

87
import java.io.FileNotFoundException;
98
import java.io.IOException;
109
import java.nio.file.Files;
1110
import java.nio.file.Path;
12-
import java.util.ArrayList;
11+
import java.util.List;
12+
import java.util.Set;
1313
import java.util.Map;
1414
import java.util.TreeMap;
15+
import java.util.TreeSet;
16+
import java.util.ArrayList;
1517

1618

1719
public class Differ {
1820

19-
public static final String ADD = "ADD";
20-
public static final String DEL = "DEL";
21-
public static final String UNC = "UNC";
22-
public static final String CHN = "CHN";
21+
public static final String ADDED = "ADDED";
22+
public static final String DELETED = "DELETED";
23+
public static final String CHANGED = "CHANGED";
24+
public static final String UNCHANGED = "UNCHANGED";
2325

2426
public static String generate(String filePath1, String filePath2, String format) throws IOException {
2527
var normalizedPath1 = FileManager.normaolizePath(Path.of(filePath1));
@@ -30,41 +32,38 @@ public static String generate(String filePath1, String filePath2, String format)
3032
throw new FileNotFoundException("Файл для чтения не найден");
3133
}
3234

33-
Parser parser = (FileManager.isJsonFile(normalizedPath1)) ? new ParserJson() : new ParserYAML();
35+
ObjectMapper mapper = (FileManager.isJsonFile(normalizedPath1)) ? new ObjectMapper() : new YAMLMapper();
36+
var dataFirst = mapper.readValue(normalizedPath1.toFile(), Map.class);
37+
var dataSecond = mapper.readValue(normalizedPath2.toFile(), Map.class);
3438

3539

36-
var dataFile1 = parser.parsFile(normalizedPath1);
37-
var dataFile2 = parser.parsFile(normalizedPath2);
3840

39-
Map<String, Difference> differenceTreeMap = new TreeMap<>();
41+
Map<String, List<Object>> differenceMap = new TreeMap<>();
42+
Set<String> keySet = new TreeSet<>(dataFirst.keySet());
43+
keySet.addAll(dataSecond.keySet());
4044

41-
dataFile1.forEach((key, value) -> {
42-
var currentDifference = new Difference();
43-
currentDifference.setKey(key);
44-
var addValue = value == null ? "null" : value;
45-
currentDifference.setOldValue(addValue);
46-
currentDifference.setState(DEL);
47-
differenceTreeMap.put(key, currentDifference);
48-
});
45+
keySet.forEach(key -> {
46+
String stage = "";
47+
var dataFirstValue = dataFirst.get(key) == null ? "null" : dataFirst.get(key);
48+
var dataSecondValue = dataSecond.get(key) == null ? "null" : dataSecond.get(key);
49+
50+
if (dataFirst.containsKey(key) && dataSecond.containsKey(key)) {
4951

50-
dataFile2.forEach((key, value) -> {
51-
var currentDifference = differenceTreeMap.getOrDefault(key, new Difference());
52-
var addValue = value == null ? "null" : value;
53-
currentDifference.setKey(key);
54-
currentDifference.setNewValue(addValue);
52+
stage = (dataFirstValue.equals(dataSecondValue)) ? UNCHANGED : CHANGED;
5553

56-
var addStage = ADD;
57-
if (currentDifference.getOldValue() != null && currentDifference.getNewValue() != null) {
58-
addStage = (currentDifference.getOldValue().equals(currentDifference.getNewValue()))
59-
? UNC : CHN;
54+
} else {
55+
stage = dataSecond.containsKey(key) ? ADDED : DELETED;
6056
}
61-
currentDifference.setState(addStage);
62-
differenceTreeMap.put(key, currentDifference);
63-
});
6457

58+
List<Object> date = new ArrayList<>(3);
59+
date.add(stage);
60+
date.add(dataFirstValue);
61+
date.add(dataSecondValue);
62+
63+
differenceMap.put(key, date);
64+
});
6565

66-
var listDifference = new ArrayList<>(differenceTreeMap.values());
67-
return Formatter.getOrder(format, listDifference);
66+
return Formatter.getOrder(format, differenceMap);
6867
}
6968

7069
}

app/src/main/java/hexlet/code/source/Difference.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

app/src/main/java/hexlet/code/source/formatters/Json.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
import com.fasterxml.jackson.core.JsonProcessingException;
44
import com.fasterxml.jackson.databind.ObjectMapper;
5-
import hexlet.code.source.Difference;
65

76
import java.util.List;
7+
import java.util.Map;
88

99
public class Json {
10-
public static String json(List<Difference> data) throws JsonProcessingException {
10+
public static String json(Map<String, List<Object>> differenceMap) throws JsonProcessingException {
1111
ObjectMapper objectMapper = new ObjectMapper();
12-
var result = objectMapper.writeValueAsString(data);
12+
var result = objectMapper.writeValueAsString(differenceMap);
1313
return result.trim();
1414
}
1515
}
Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,12 @@
11
package hexlet.code.source.formatters;
22

33
import hexlet.code.source.Differ;
4-
import hexlet.code.source.Difference;
54

65
import java.util.List;
6+
import java.util.Map;
77

8-
public class Plain {
9-
public static String plain(List<Difference> differences) {
10-
StringBuilder result = new StringBuilder();
11-
differences.forEach(value -> {
12-
String addString = "";
13-
var currentState = value.getState();
14-
15-
if (currentState.equals(Differ.ADD)) {
16-
addString = "Property '" + value.getKey() + "' was ";
17-
var newValue = getPrintString(value.getNewValue());
18-
addString += "added with value: " + newValue + "\n";
19-
}
20-
21-
if (currentState.equals(Differ.DEL)) {
22-
addString = "Property '" + value.getKey() + "' was ";
23-
addString += "removed" + "\n";
24-
}
25-
26-
if (currentState.equals(Differ.CHN)) {
27-
addString = "Property '" + value.getKey() + "' was ";
28-
var newValue = getPrintString(value.getNewValue());
29-
var oldValue = getPrintString(value.getOldValue());
30-
addString += "updated. From " + oldValue + " to " + newValue + "\n";
31-
}
328

33-
result.append(addString);
34-
});
35-
return result.toString().trim();
36-
}
9+
public class Plain {
3710

3811
public static String getPrintString(Object value) {
3912

@@ -52,4 +25,38 @@ public static String getPrintString(Object value) {
5225
}
5326
return result;
5427
}
28+
29+
public static String plain(Map<String, List<Object>> differenceMap) {
30+
StringBuilder result = new StringBuilder();
31+
differenceMap.forEach((key, value) -> {
32+
String addString = "Property '" + key + "' was ";
33+
if (value.size() != 3) {
34+
throw new IllegalArgumentException("Неподходящие данные");
35+
}
36+
37+
var currentState = value.getFirst();
38+
39+
if (currentState.equals(Differ.ADDED)) {
40+
Object newValue = getPrintString(value.getLast());
41+
addString += "added with value: " + newValue + "\n";
42+
}
43+
44+
if (currentState.equals(Differ.DELETED)) {
45+
addString += "removed" + "\n";
46+
}
47+
48+
if (currentState.equals(Differ.CHANGED)) {
49+
Object oldValue = getPrintString(value.get(1));
50+
Object newValue = getPrintString(value.getLast());
51+
addString += "updated. From " + oldValue + " to " + newValue + "\n";
52+
}
53+
54+
if (currentState.equals(Differ.UNCHANGED)) {
55+
addString = "";
56+
}
57+
58+
result.append(addString);
59+
});
60+
return result.toString().trim();
61+
}
5562
}

app/src/main/java/hexlet/code/source/formatters/Stylish.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
package hexlet.code.source.formatters;
22

33
import hexlet.code.source.Differ;
4-
import hexlet.code.source.Difference;
54

65
import java.util.List;
6+
import java.util.Map;
77

88
public class Stylish {
9-
public static String stylish(List<Difference> differences) {
9+
public static String stylish(Map<String, List<Object>> differenceMap) {
1010
StringBuilder result = new StringBuilder("{\n");
11-
differences.forEach(value -> {
12-
var currentState = value.getState();
13-
String addString = "";
11+
differenceMap.forEach((key, value) -> {
12+
String addedString = "";
1413

15-
if (currentState.equals(Differ.DEL) || currentState.equals(Differ.CHN)) {
16-
addString += "- " + value.getKey() + ": " + value.getOldValue() + "\n";
14+
if (value.size() != 3) {
15+
throw new IllegalArgumentException("Неподходящие данные");
1716
}
1817

19-
if (currentState.equals(Differ.ADD) || currentState.equals(Differ.CHN)) {
20-
addString += "+ " + value.getKey() + ": " + value.getNewValue() + "\n";
18+
if (value.getFirst().equals(Differ.DELETED) || value.getFirst().equals(Differ.CHANGED)) {
19+
addedString += "- " + key + ": " + value.get(1) + "\n";
2120
}
2221

23-
if (currentState.equals(Differ.UNC)) {
24-
addString += " " + value.getKey() + ": " + value.getOldValue() + "\n";
22+
if (value.getFirst().equals(Differ.ADDED) || value.getFirst().equals(Differ.CHANGED)) {
23+
addedString += "+ " + key + ": " + value.get(2) + "\n";
2524
}
26-
result.append(addString);
25+
26+
if (value.getFirst().equals(Differ.UNCHANGED)) {
27+
addedString += " " + key + ": " + value.get(1) + "\n";
28+
}
29+
30+
result.append(addedString);
2731
});
2832
result.append("}");
2933
return result.toString();

app/src/main/java/hexlet/code/source/parsers/Parser.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

app/src/main/java/hexlet/code/source/parsers/ParserJson.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

app/src/main/java/hexlet/code/source/parsers/ParserYAML.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

app/src/test/java/fixtures/Json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"chars1":["UNCHANGED",["a","b","c"],["a","b","c"]],"chars2":["CHANGED",["d","e","f"],false],"checked":["CHANGED",false,true],"default":["CHANGED","null",["value1","value2"]],"id":["CHANGED",45,"null"],"key1":["DELETED","value1","null"],"key2":["ADDED","null","value2"],"numbers1":["UNCHANGED",[1,2,3,4],[1,2,3,4]],"numbers2":["CHANGED",[2,3,4,5],[22,33,44,55]],"numbers3":["DELETED",[3,4,5],"null"],"numbers4":["ADDED","null",[4,5,6]],"obj1":["ADDED","null",{"nestedKey":"value","isNested":true}],"setting1":["CHANGED","Some value","Another value"],"setting2":["CHANGED",200,300],"setting3":["CHANGED",true,"none"]}

0 commit comments

Comments
 (0)