package problems04; import jdk.nashorn.internal.objects.annotations.Function; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; public class P04_LogParser { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Pattern pat = Pattern.compile("\\{\"Project\": \\[\"(.+|\\s+)\"\\], \"Type\": \\[\"(.+|\\s+)\"\\], \"Message\": \\[\"(.+|\\s+)\"]}"); TreeMap>> database = new TreeMap<>(); String inputLine = sc.nextLine(); while (!inputLine.equals("END")) { Matcher matcher = pat.matcher(inputLine); while (matcher.find()) { String projectName = matcher.group(1); String reportLevel = matcher.group(2); String message = matcher.group(3); if (!database.containsKey(projectName)) { database.put(projectName, new TreeMap<>()); database.get(projectName).put("Critical", new ArrayList<>()); database.get(projectName).put("Warning", new ArrayList<>()); } database.get(projectName).get(reportLevel).add(message); } inputLine = sc.nextLine(); } TreeMap>> databaseNew = new TreeMap<>(); database.entrySet().stream() .sorted((e1, e2) -> Integer.compare( e2.getValue().get("Warning").size() + e2.getValue().get("Critical").size(), e1.getValue().get("Warning").size()+ e1.getValue().get("Critical").size())) .forEach(x1 -> { System.out.println(x1.getKey() + ":"); int totalCriticalErrors = x1.getValue().get("Critical").size(); int totalWarningErrors = x1.getValue().get("Warning").size(); int totalErrors = totalCriticalErrors + totalWarningErrors; System.out.printf("Total Errors: %d\n", totalErrors); System.out.printf("Critical: %d\n", totalCriticalErrors); System.out.printf("Warnings: %d\n", totalWarningErrors); System.out.println("Critical Messages:"); if (x1.getValue().get("Critical").size() == 0) { System.out.println("--->None"); } else { x1.getValue().get("Critical") .stream() .sorted((x, y) -> x.compareTo(y)) .sorted((x, y) -> Integer.compare(x.length(), y.length())) .forEach(x -> System.out.println("--->" + x)); } System.out.println("Warning Messages:"); if (x1.getValue().get("Warning").size() == 0) { System.out.println("--->None"); } else { x1.getValue().get("Warning") .stream() .sorted((x, y) -> x.compareTo(y)) .sorted((x, y) -> Integer.compare(x.length(), y.length())) .forEach(x -> System.out.println("--->" + x)); } System.out.println(); }); } }