Skip to content

Commit 9558903

Browse files
authored
Merge pull request #6 from AlexSorb/fix-2
Fix 2
2 parents a42444a + fe52706 commit 9558903

File tree

16 files changed

+122
-92
lines changed

16 files changed

+122
-92
lines changed

app/Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ run-dist: build install
1919
run-help: build install
2020
./build/install/app/bin/app -h
2121

22-
run-json: build install
22+
run-stylish: build install
2323
./build/install/app/bin/app -f=stylish ./src/main/java/resources/json/FirstJsonFile.json ./src/main/java/resources/json/SecondJsonFile.json
2424

25+
run-plain: build install
26+
./build/install/app/bin/app -f=plain ./src/main/java/resources/json/FirstJsonFile.json ./src/main/java/resources/json/SecondJsonFile.json
27+
28+
run-json: build install
29+
./build/install/app/bin/app -f=json ./src/main/java/resources/json/FirstJsonFile.json ./src/main/java/resources/json/SecondJsonFile.json
30+
31+
2532
.PHONY: build

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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
public static final String DEFAULT_FORMAT = "stylish";
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.generateFormatString(format, differenceMap);
32+
}
33+
34+
public static String generate(String filePath1, String filePath2) throws IOException {
35+
return Differ.generate(filePath1, filePath2, DEFAULT_FORMAT);
36+
}
37+
}

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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
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;
1010

1111
public class Formatter {
1212

13-
public static String getOrder(String format, Map<String, List<Object>> differenceMap)
13+
public static String generateFormatString(String format, Map<String, List<Object>> differenceMap)
1414
throws IllegalArgumentException, JsonProcessingException {
1515
var normalizeFormat = format.trim().toLowerCase();
1616
var result = "";
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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package hexlet.code.source.formatters;
1+
package hexlet.code.formatters;
22

3-
import hexlet.code.source.Differ;
3+
import hexlet.code.TreeBuilder;
44

55
import java.util.List;
66
import java.util.Map;
@@ -33,22 +33,22 @@ public static String plain(Map<String, List<Object>> differenceMap) {
3333

3434
var currentState = value.getFirst();
3535

36-
if (currentState.equals(Differ.ADDED)) {
36+
if (currentState.equals(TreeBuilder.ADDED)) {
3737
Object newValue = getPrintString(value.getLast());
3838
addString += "added with value: " + newValue + "\n";
3939
}
4040

41-
if (currentState.equals(Differ.DELETED)) {
41+
if (currentState.equals(TreeBuilder.DELETED)) {
4242
addString += "removed" + "\n";
4343
}
4444

45-
if (currentState.equals(Differ.CHANGED)) {
45+
if (currentState.equals(TreeBuilder.CHANGED)) {
4646
Object oldValue = getPrintString(value.get(1));
4747
Object newValue = getPrintString(value.getLast());
4848
addString += "updated. From " + oldValue + " to " + newValue + "\n";
4949
}
5050

51-
if (currentState.equals(Differ.UNCHANGED)) {
51+
if (currentState.equals(TreeBuilder.UNCHANGED)) {
5252
addString = "";
5353
}
5454

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

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

3-
import hexlet.code.source.Differ;
3+
import hexlet.code.TreeBuilder;
44

55
import java.util.List;
66
import java.util.Map;
@@ -11,15 +11,15 @@ public static String stylish(Map<String, List<Object>> differenceMap) {
1111
differenceMap.forEach((key, value) -> {
1212
String addedString = "";
1313

14-
if (value.getFirst().equals(Differ.DELETED) || value.getFirst().equals(Differ.CHANGED)) {
14+
if (value.getFirst().equals(TreeBuilder.DELETED) || value.getFirst().equals(TreeBuilder.CHANGED)) {
1515
addedString += "- " + key + ": " + value.get(1) + "\n";
1616
}
1717

18-
if (value.getFirst().equals(Differ.ADDED) || value.getFirst().equals(Differ.CHANGED)) {
18+
if (value.getFirst().equals(TreeBuilder.ADDED) || value.getFirst().equals(TreeBuilder.CHANGED)) {
1919
addedString += "+ " + key + ": " + value.get(2) + "\n";
2020
}
2121

22-
if (value.getFirst().equals(Differ.UNCHANGED)) {
22+
if (value.getFirst().equals(TreeBuilder.UNCHANGED)) {
2323
addedString += " " + key + ": " + value.get(1) + "\n";
2424
}
2525

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

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

0 commit comments

Comments
 (0)