-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
219 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package hexlet.code; | ||
|
||
import java.util.Map; | ||
import java.util.SortedSet; | ||
import java.util.TreeSet; | ||
import java.util.LinkedHashMap; | ||
import java.util.ArrayList; | ||
|
||
public class DiffBuilder { | ||
public static Map<String, Map<Object, Object>> build(Map<String, Object> mapFile, Map<String, Object> mapFile2) { | ||
SortedSet<String> keys = new TreeSet<>(mapFile.keySet()); | ||
keys.addAll(mapFile2.keySet()); | ||
Map<String, Map<Object, Object>> diffResult = new LinkedHashMap<>(); | ||
LinkedHashMap<Object, Object> deleteMap = new LinkedHashMap<>(); | ||
LinkedHashMap<Object, Object> notChangedMap = new LinkedHashMap<>(); | ||
LinkedHashMap<Object, Object> changedMap = new LinkedHashMap<>(); | ||
LinkedHashMap<Object, Object> addedMap = new LinkedHashMap<>(); | ||
for (String key : keys) { | ||
Object value = mapFile.get(key) == null ? "null" : mapFile.get(key); | ||
if (mapFile2.containsKey(key)) { | ||
Object differFileKeyValue = mapFile2.get(key) == null ? "null" : mapFile2.get(key); | ||
if (differFileKeyValue.equals(value)) { | ||
notChangedMap.put(key, value); | ||
} else if (!mapFile.containsKey(key)) { | ||
addedMap.put(key, differFileKeyValue); | ||
} else { | ||
ArrayList<Object> values = new ArrayList<>(); | ||
values.add(value); | ||
values.add(differFileKeyValue); | ||
changedMap.put(key, values); | ||
} | ||
mapFile2.remove(key); | ||
} else { | ||
deleteMap.put(key, value); | ||
} | ||
} | ||
diffResult.put("notchanged", notChangedMap); | ||
diffResult.put("add", addedMap); | ||
diffResult.put("changed", changedMap); | ||
diffResult.put("delete", deleteMap); | ||
return diffResult; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,21 @@ | ||
package hexlet.code; | ||
|
||
import hexlet.code.formatters.IFormatter; | ||
|
||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Map; | ||
import java.util.SortedSet; | ||
import java.util.TreeSet; | ||
import java.util.LinkedHashMap; | ||
|
||
|
||
public class Differ { | ||
public static Map<String, Object[]> generate(Path filepath, Path filepath2) { | ||
public static String generate(String pathToFile, String pathToFile2, String format) throws Exception { | ||
Path filepath = Paths.get(pathToFile).toAbsolutePath().normalize(); | ||
Path filepath2 = Paths.get(pathToFile2).toAbsolutePath().normalize(); | ||
|
||
Map<String, Object> mapFile = Parser.parse(filepath); | ||
Map<String, Object> mapFile2 = Parser.parse(filepath2); | ||
return compareFiles(mapFile, mapFile2); | ||
} | ||
|
||
private static Map<String, Object[]> compareFiles(Map<String, Object> mapFile, Map<String, Object> mapFile2) { | ||
SortedSet<String> keys = new TreeSet<>(mapFile.keySet()); | ||
keys.addAll(mapFile2.keySet()); | ||
Map<String, Object[]> diffResult = new LinkedHashMap<>(); | ||
for (String key : keys) { | ||
Object value = mapFile.get(key) == null ? "null" : mapFile.get(key); | ||
if (mapFile2.containsKey(key)) { | ||
Object differFileKeyValue = mapFile2.get(key) == null ? "null" : mapFile2.get(key); | ||
if (differFileKeyValue.equals(value)) { | ||
diffResult.put(key, new Object[] {"none", value}); | ||
} else if (!mapFile.containsKey(key)) { | ||
diffResult.put(key, new Object[] {"+", differFileKeyValue }); | ||
} else { | ||
diffResult.put(key, new Object[] {"-+", value, differFileKeyValue }); | ||
} | ||
mapFile2.remove(key); | ||
} else { | ||
diffResult.put(key, new Object[] {"-", value}); | ||
} | ||
} | ||
mapFile2.forEach((k, v) -> { | ||
if (!mapFile.containsKey(k)) { | ||
diffResult.put(k, new Object[] {"+", v}); | ||
} | ||
}); | ||
return diffResult; | ||
IFormatter formatter = null; | ||
formatter = FormatterFactory.getFormatter(format); | ||
return formatter.format(DiffBuilder.build(mapFile, mapFile2)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package hexlet.code; | ||
|
||
import hexlet.code.formatters.IFormatter; | ||
import hexlet.code.formatters.JsonFormatter; | ||
import hexlet.code.formatters.PlainFormatter; | ||
import hexlet.code.formatters.StylishFormatter; | ||
|
||
public class FormatterFactory { | ||
|
||
public static IFormatter getFormatter(String format) throws Exception { | ||
return switch (format) { | ||
case "stylish" -> new StylishFormatter(); | ||
case "plain" -> new PlainFormatter(); | ||
case "json" -> new JsonFormatter(); | ||
default -> throw new Exception("Unexpected format '%s'. Possible formats: [stylish, plain, json]" | ||
.formatted(format)); | ||
}; | ||
} | ||
|
||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
app/src/main/java/hexlet/code/formatters/FormatterFactory.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 18 additions & 11 deletions
29
app/src/main/java/hexlet/code/formatters/PlainFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 16 additions & 11 deletions
27
app/src/main/java/hexlet/code/formatters/StylishFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.