Skip to content

Commit 80ae655

Browse files
committed
added Plain format
1 parent e2f8fd0 commit 80ae655

File tree

7 files changed

+140
-73
lines changed

7 files changed

+140
-73
lines changed

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,7 @@
1111

1212
class App implements Runnable {
1313

14-
// @Option(names = { "-h", "--help" }, usageHelp = true, description = "Show this help message and exit.")
15-
// boolean usageHelpRequested;
16-
//
17-
// @Option(names = { "-V", "--version" }, versionHelp = true, description = "Print version information and exit.")
18-
// boolean versionInfoRequested;
19-
20-
@Option(names = { "-f", "--format " }, paramLabel = "format",
14+
@Option(names = { "-f", "--format" }, paramLabel = "format",
2115
description = "output format [default: stylish]", defaultValue = "stylish")
2216
private String format;
2317

@@ -31,7 +25,7 @@ class App implements Runnable {
3125
@Override
3226
public void run() {
3327
try {
34-
String diff = Differ.generate(filePath1, filePath2);
28+
String diff = Differ.generate(filePath1, filePath2, format);
3529
System.out.println(diff);
3630
} catch (IOException e) {
3731
System.err.println("Ошибка при чтении файла: " + e.getMessage());
@@ -42,3 +36,10 @@ public static void main(String[] args) {
4236
new CommandLine(new App()).execute(args);
4337
}
4438
}
39+
40+
41+
// @Option(names = { "-h", "--help" }, usageHelp = true, description = "Show this help message and exit.")
42+
// boolean usageHelpRequested;
43+
//
44+
// @Option(names = { "-V", "--version" }, versionHelp = true, description = "Print version information and exit.")
45+
// boolean versionInfoRequested;

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

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,59 @@
11
package hexlet.code;
22

3+
import hexlet.code.formatters.Formatter;
4+
35
import java.io.IOException;
4-
import java.util.Map;
5-
import java.util.Set;
6-
import java.util.TreeSet;
6+
import java.util.*;
77

88
public class Differ {
99

10-
public static String generate(String filePath1, String filePath2) throws IOException {
10+
public static String generate(String filePath1, String filePath2, String format) throws IOException {
1111
// Используем новый Parser для обработки разных форматов
1212
Map<String, Object> fileInfo1 = Parser.parse(filePath1);
1313
Map<String, Object> fileInfo2 = Parser.parse(filePath2);
1414

15-
return generateDiff(fileInfo1, fileInfo2);
15+
List<Map<String, Object>> diff = calculateDiff(fileInfo1, fileInfo2);
16+
17+
return Formatter.format(diff, format);
1618
}
1719

18-
public static String generateDiff(Map<String, Object> fileInfo1, Map<String, Object> fileInfo2) {
20+
public static List<Map<String, Object>> calculateDiff(Map<String, Object> fileInfo1, Map<String, Object> fileInfo2) {
1921
Set<String> allKeys = new TreeSet<>();
2022
allKeys.addAll(fileInfo1.keySet());
2123
allKeys.addAll(fileInfo2.keySet());
2224

23-
StringBuilder differences = new StringBuilder("\n{\n");
25+
List<Map<String, Object>> diff = new ArrayList<>();
2426

2527
for (String key : allKeys) {
26-
Object value1 = fileInfo1.getOrDefault(key, null);
27-
Object value2 = fileInfo2.getOrDefault(key, null);
28+
Object value1 = fileInfo1.get(key);
29+
Object value2 = fileInfo2.get(key);
2830

29-
if (value1 != null && value2 != null && value1.equals(value2)) {
30-
differences.append(" ").append(key).append(": ").append(value1).append("\n");
31+
Map<String, Object> diffEntry = new HashMap<>();
32+
diffEntry.put("key", key);
33+
34+
if (!fileInfo1.containsKey(key)) {
35+
// Ключ был добавлен во второй файл
36+
diffEntry.put("type", "added");
37+
diffEntry.put("newValue", value2);
38+
} else if (!fileInfo2.containsKey(key)) {
39+
// Ключ был удалён
40+
diffEntry.put("type", "removed");
41+
diffEntry.put("oldValue", value1);
42+
} else if (Objects.equals(value1, value2)) {
43+
// Значение осталось неизменным
44+
diffEntry.put("type", "unchanged");
45+
diffEntry.put("value", value1);
3146
} else {
32-
if (fileInfo1.containsKey(key)) { // Ключ был в первом файле
33-
differences.append(" - ").append(key).append(": ").append(value1).append("\n");
34-
}
35-
if (fileInfo2.containsKey(key)) { // Ключ был во втором файле
36-
differences.append(" + ").append(key).append(": ").append(value2).append("\n");
37-
}
47+
// Значение изменилось
48+
diffEntry.put("type", "updated");
49+
diffEntry.put("oldValue", value1);
50+
diffEntry.put("newValue", value2);
3851
}
52+
53+
diff.add(diffEntry);
3954
}
4055

41-
differences.append("}\n");
42-
return differences.toString();
56+
return diff;
4357
}
4458
}
4559

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

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package hexlet.code.formatters;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
public class Formatter {
7+
8+
public static String format(List<Map<String, Object>> diff, String format) {
9+
return switch (format) {
10+
case "plain" -> Plain.format(diff);
11+
case "stylish" -> Stylish.format(diff);
12+
default -> throw new IllegalArgumentException("Unsupported format: " + format);
13+
};
14+
}
15+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package hexlet.code.formatters;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
public class Plain {
7+
8+
public static String format(List<Map<String, Object>> diff) {
9+
StringBuilder result = new StringBuilder("\n");
10+
11+
for (Map<String, Object> entry : diff) {
12+
String key = (String) entry.get("key");
13+
String type = (String) entry.get("type");
14+
Object oldValue = entry.get("oldValue");
15+
Object newValue = entry.get("newValue");
16+
17+
switch (type) {
18+
case "added" -> result.append(String.format("Property '%s' was added with value: %s\n",
19+
key, formatValue(newValue)));
20+
case "removed" -> result.append(String.format("Property '%s' was removed\n", key));
21+
case "updated" -> result.append(String.format("Property '%s' was updated. From %s to %s\n",
22+
key, formatValue(oldValue), formatValue(newValue)));
23+
default -> {} // Игнорируем "unchanged" (оставленные без изменений)
24+
}
25+
}
26+
27+
return result.toString().trim();
28+
}
29+
30+
private static String formatValue(Object value) {
31+
if (value instanceof Map || value instanceof List) {
32+
return "[complex value]";
33+
} else if (value instanceof String) {
34+
return "'" + value + "'";
35+
}
36+
return String.valueOf(value);
37+
}
38+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package hexlet.code.formatters;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
public class Stylish {
7+
public static String format(List<Map<String, Object>> diff) {
8+
StringBuilder result = new StringBuilder("{\n");
9+
10+
for (Map<String, Object> entry : diff) {
11+
String type = (String) entry.get("type"); // Исправлено с "sign"
12+
String key = (String) entry.get("key");
13+
Object oldValue = entry.get("oldValue");
14+
Object newValue = entry.get("newValue");
15+
16+
switch (type) {
17+
case "added" -> result.append(" + ").append(key).append(": ").append(newValue).append("\n");
18+
case "removed" -> result.append(" - ").append(key).append(": ").append(oldValue).append("\n");
19+
case "updated" -> {
20+
result.append(" - ").append(key).append(": ").append(oldValue).append("\n");
21+
result.append(" + ").append(key).append(": ").append(newValue).append("\n");
22+
}
23+
case "unchanged" -> result.append(" ").append(key).append(": ").append(entry.get("value")).append("\n");
24+
}
25+
}
26+
27+
result.append("}\n");
28+
return result.toString();
29+
}
30+
}

app/src/test/java/hexlet/code/DifferTest.java

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,26 @@
66

77
class DifferTest {
88
private static final String EXPECTED_DIFF = """
9-
{
10-
chars1: [a, b, c]
11-
- chars2: [d, e, f]
12-
+ chars2: false
13-
- checked: false
14-
+ checked: true
15-
- default: null
16-
+ default: [value1, value2]
17-
- id: 45
18-
+ id: null
19-
- key1: value1
20-
+ key2: value2
21-
numbers1: [1, 2, 3, 4]
22-
- numbers2: [2, 3, 4, 5]
23-
+ numbers2: [22, 33, 44, 55]
24-
- numbers3: [3, 4, 5]
25-
+ numbers4: [4, 5, 6]
26-
+ obj1: {nestedKey=value, isNested=true}
27-
- setting1: Some value
28-
+ setting1: Another value
29-
- setting2: 200
30-
+ setting2: 300
31-
- setting3: true
32-
+ setting3: none
33-
}
9+
Property 'chars2' was updated. From [complex value] to false
10+
Property 'checked' was updated. From false to true
11+
Property 'default' was updated. From null to [complex value]
12+
Property 'id' was updated. From 45 to null
13+
Property 'key1' was removed
14+
Property 'key2' was added with value: 'value2'
15+
Property 'numbers2' was updated. From [complex value] to [complex value]
16+
Property 'numbers3' was removed
17+
Property 'numbers4' was added with value: [complex value]
18+
Property 'obj1' was added with value: [complex value]
19+
Property 'setting1' was updated. From 'Some value' to 'Another value'
20+
Property 'setting2' was updated. From 200 to 300
21+
Property 'setting3' was updated. From true to 'none'
3422
""";
3523

3624
@Test
3725
void testGenerateDiffForJson() throws IOException {
3826
String filePath1 = "file1.json";
3927
String filePath2 = "file2.json";
40-
String actualDiff = Differ.generate(filePath1, filePath2);
28+
String actualDiff = Differ.generate(filePath1, filePath2, "plain");
4129
assertEquals(EXPECTED_DIFF.trim(), actualDiff.trim());
4230
System.out.println("ВСЕ РАБОТАЕТ!");
4331
}
@@ -46,7 +34,7 @@ void testGenerateDiffForJson() throws IOException {
4634
void testGenerateDiffForYaml() throws IOException {
4735
String filePath1 = "file1.yml";
4836
String filePath2 = "file2.yml";
49-
String actualDiff = Differ.generate(filePath1, filePath2);
37+
String actualDiff = Differ.generate(filePath1, filePath2, "plain");
5038
assertEquals(EXPECTED_DIFF.trim(), actualDiff.trim());
5139
System.out.println("ВСЕ РАБОТАЕТ! лала!");
5240
}

0 commit comments

Comments
 (0)