Advertisement
Guest User

Untitled

a guest
Mar 30th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.52 KB | None | 0 0
  1. package problems04;
  2.  
  3. import jdk.nashorn.internal.objects.annotations.Function;
  4.  
  5. import java.util.*;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8. import java.util.stream.Collectors;
  9.  
  10. public class P04_LogParser {
  11.     public static void main(String[] args) {
  12.         Scanner sc = new Scanner(System.in);
  13.  
  14.         Pattern pat = Pattern.compile("\\{\"Project\": \\[\"(.+|\\s+)\"\\], \"Type\": \\[\"(.+|\\s+)\"\\], \"Message\": \\[\"(.+|\\s+)\"]}");
  15.  
  16.         TreeMap<String, TreeMap<String, ArrayList<String>>> database = new TreeMap<>();
  17.  
  18.         String inputLine = sc.nextLine();
  19.         while (!inputLine.equals("END")) {
  20.             Matcher matcher = pat.matcher(inputLine);
  21.             while (matcher.find()) {
  22.                 String projectName = matcher.group(1);
  23.                 String reportLevel = matcher.group(2);
  24.                 String message = matcher.group(3);
  25.  
  26.                 if (!database.containsKey(projectName)) {
  27.                     database.put(projectName, new TreeMap<>());
  28.                     database.get(projectName).put("Critical", new ArrayList<>());
  29.                     database.get(projectName).put("Warning", new ArrayList<>());
  30.                 }
  31.  
  32.                 database.get(projectName).get(reportLevel).add(message);
  33.             }
  34.  
  35.             inputLine = sc.nextLine();
  36.         }
  37.  
  38.         TreeMap<String, TreeMap<String, ArrayList<String>>> databaseNew = new TreeMap<>();
  39.  
  40.         database.entrySet().stream()
  41.                 .sorted((e1, e2) ->
  42.                         Integer.compare(
  43.                                 e2.getValue().get("Warning").size() + e2.getValue().get("Critical").size(),
  44.                                 e1.getValue().get("Warning").size()+ e1.getValue().get("Critical").size()))
  45.                 .forEach(x1 -> {
  46.                     System.out.println(x1.getKey() + ":");
  47.  
  48.                     int totalCriticalErrors = x1.getValue().get("Critical").size();
  49.                     int totalWarningErrors = x1.getValue().get("Warning").size();
  50.                     int totalErrors = totalCriticalErrors + totalWarningErrors;
  51.  
  52.                     System.out.printf("Total Errors: %d\n", totalErrors);
  53.                     System.out.printf("Critical: %d\n", totalCriticalErrors);
  54.                     System.out.printf("Warnings: %d\n", totalWarningErrors);
  55.  
  56.                     System.out.println("Critical Messages:");
  57.                     if (x1.getValue().get("Critical").size() == 0) {
  58.                         System.out.println("--->None");
  59.                     } else {
  60.                         x1.getValue().get("Critical")
  61.                                 .stream()
  62.                                 .sorted((x, y) -> x.compareTo(y))
  63.                                 .sorted((x, y) -> Integer.compare(x.length(), y.length()))
  64.                                 .forEach(x -> System.out.println("--->" + x));
  65.                     }
  66.                     System.out.println("Warning Messages:");
  67.                     if (x1.getValue().get("Warning").size() == 0) {
  68.                         System.out.println("--->None");
  69.                     } else {
  70.                         x1.getValue().get("Warning")
  71.                                 .stream()
  72.                                 .sorted((x, y) -> x.compareTo(y))
  73.                                 .sorted((x, y) -> Integer.compare(x.length(), y.length()))
  74.                                 .forEach(x -> System.out.println("--->" + x));
  75.                     }
  76.                     System.out.println();
  77.                 });
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement