Skip to content

Commit e36855c

Browse files
committed
move files
1 parent a42444a commit e36855c

File tree

15 files changed

+101
-89
lines changed

15 files changed

+101
-89
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import picocli.CommandLine.Command;
55
import picocli.CommandLine.Parameters;
66
import picocli.CommandLine.Option;
7-
import hexlet.code.source.Differ;
87

98
import java.io.IOException;
109
import java.util.concurrent.Callable;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package hexlet.code;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
5+
6+
import java.io.FileNotFoundException;
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.util.Map;
11+
12+
13+
public class Differ {
14+
15+
16+
public static String generate(String filePath1, String filePath2, String format) throws IOException {
17+
var normalizedPath1 = FileManager.normaolizePath(Path.of(filePath1));
18+
var normalizedPath2 = FileManager.normaolizePath(Path.of(filePath2));
19+
20+
21+
if (Files.notExists(normalizedPath1) || Files.notExists(normalizedPath2)) {
22+
throw new FileNotFoundException("Файл для чтения не найден");
23+
}
24+
25+
ObjectMapper mapper = (FileManager.isJsonFile(normalizedPath1)) ? new ObjectMapper() : new YAMLMapper();
26+
var dataFirst = mapper.readValue(normalizedPath1.toFile(), Map.class);
27+
var dataSecond = mapper.readValue(normalizedPath2.toFile(), Map.class);
28+
29+
var differenceMap = TreeBuilder.getTreeDifference(dataFirst, dataSecond);
30+
31+
return Formatter.getOrder(format, differenceMap);
32+
}
33+
}

app/src/main/java/hexlet/code/source/FileManager.java renamed to app/src/main/java/hexlet/code/FileManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package hexlet.code.source;
1+
package hexlet.code;
22

33
import java.nio.file.Path;
44

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

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

33
import com.fasterxml.jackson.core.JsonProcessingException;
4-
import hexlet.code.source.formatters.Json;
5-
import hexlet.code.source.formatters.Plain;
6-
import hexlet.code.source.formatters.Stylish;
4+
import hexlet.code.formatters.Json;
5+
import hexlet.code.formatters.Plain;
6+
import hexlet.code.formatters.Stylish;
77

88
import java.util.List;
99
import java.util.Map;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package hexlet.code;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
import java.util.Set;
6+
import java.util.TreeMap;
7+
import java.util.TreeSet;
8+
import java.util.ArrayList;
9+
10+
public class TreeBuilder {
11+
public static final String ADDED = "ADDED";
12+
public static final String DELETED = "DELETED";
13+
public static final String CHANGED = "CHANGED";
14+
public static final String UNCHANGED = "UNCHANGED";
15+
16+
public static Map<String, List<Object>> getTreeDifference(Map<String, Object> firstDataMap,
17+
Map<String, Object> SecondDataMap) {
18+
19+
Map<String, List<Object>> result = new TreeMap<>();
20+
Set<String> keySet = new TreeSet<>(firstDataMap.keySet());
21+
keySet.addAll(SecondDataMap.keySet());
22+
23+
keySet.forEach(key -> {
24+
String stage;
25+
var dataFirstValue = firstDataMap.get(key) == null ? "null" : firstDataMap.get(key);
26+
var dataSecondValue = SecondDataMap.get(key) == null ? "null" : SecondDataMap.get(key);
27+
28+
if (firstDataMap.containsKey(key) && SecondDataMap.containsKey(key)) {
29+
30+
stage = (dataFirstValue.equals(dataSecondValue)) ? UNCHANGED : CHANGED;
31+
32+
} else {
33+
stage = SecondDataMap.containsKey(key) ? ADDED : DELETED;
34+
}
35+
36+
List<Object> date = new ArrayList<>();
37+
date.add(stage);
38+
date.add(dataFirstValue);
39+
date.add(dataSecondValue);
40+
41+
result.put(key, date);
42+
});
43+
return result;
44+
}
45+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package hexlet.code.source.formatters;
1+
package hexlet.code.formatters;
22

33
import com.fasterxml.jackson.core.JsonProcessingException;
44
import com.fasterxml.jackson.databind.ObjectMapper;

app/src/main/java/hexlet/code/source/formatters/Plain.java renamed to app/src/main/java/hexlet/code/formatters/Plain.java

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

3-
import hexlet.code.source.Differ;
3+
import hexlet.code.Differ;
4+
import hexlet.code.TreeBuilder;
45

56
import java.util.List;
67
import java.util.Map;
@@ -33,22 +34,22 @@ public static String plain(Map<String, List<Object>> differenceMap) {
3334

3435
var currentState = value.getFirst();
3536

36-
if (currentState.equals(Differ.ADDED)) {
37+
if (currentState.equals(TreeBuilder.ADDED)) {
3738
Object newValue = getPrintString(value.getLast());
3839
addString += "added with value: " + newValue + "\n";
3940
}
4041

41-
if (currentState.equals(Differ.DELETED)) {
42+
if (currentState.equals(TreeBuilder.DELETED)) {
4243
addString += "removed" + "\n";
4344
}
4445

45-
if (currentState.equals(Differ.CHANGED)) {
46+
if (currentState.equals(TreeBuilder.CHANGED)) {
4647
Object oldValue = getPrintString(value.get(1));
4748
Object newValue = getPrintString(value.getLast());
4849
addString += "updated. From " + oldValue + " to " + newValue + "\n";
4950
}
5051

51-
if (currentState.equals(Differ.UNCHANGED)) {
52+
if (currentState.equals(TreeBuilder.UNCHANGED)) {
5253
addString = "";
5354
}
5455

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
package hexlet.code.source.formatters;
1+
package hexlet.code.formatters;
22

3-
import hexlet.code.source.Differ;
3+
import hexlet.code.Differ;
4+
import hexlet.code.TreeBuilder;
45

56
import java.util.List;
67
import java.util.Map;
@@ -11,15 +12,15 @@ public static String stylish(Map<String, List<Object>> differenceMap) {
1112
differenceMap.forEach((key, value) -> {
1213
String addedString = "";
1314

14-
if (value.getFirst().equals(Differ.DELETED) || value.getFirst().equals(Differ.CHANGED)) {
15+
if (value.getFirst().equals(TreeBuilder.DELETED) || value.getFirst().equals(TreeBuilder.CHANGED)) {
1516
addedString += "- " + key + ": " + value.get(1) + "\n";
1617
}
1718

18-
if (value.getFirst().equals(Differ.ADDED) || value.getFirst().equals(Differ.CHANGED)) {
19+
if (value.getFirst().equals(TreeBuilder.ADDED) || value.getFirst().equals(TreeBuilder.CHANGED)) {
1920
addedString += "+ " + key + ": " + value.get(2) + "\n";
2021
}
2122

22-
if (value.getFirst().equals(Differ.UNCHANGED)) {
23+
if (value.getFirst().equals(TreeBuilder.UNCHANGED)) {
2324
addedString += " " + key + ": " + value.get(1) + "\n";
2425
}
2526

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

Lines changed: 0 additions & 67 deletions
This file was deleted.
File renamed without changes.

0 commit comments

Comments
 (0)