kidroca

Log Prase

Nov 16th, 2015
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.53 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6. import java.util.stream.Collectors;
  7.  
  8. public class Problem4 {
  9.  
  10.     private static HashMap<String, Project> projectMap = new HashMap<>();
  11.     private static Pattern mainPat = // Pattern.compile("(?<=\\[\")[^\"]+");
  12.             Pattern.compile("\\{\"Project\": \\[\"(?<name>.+)\"\\], \"Type\": \\[\"(?<type>.+)\"\\], \"Message\": \\[\"(?<message>.+)\"]}");
  13.    
  14.     public static void main(String[] args) {
  15.         readInput();
  16.  
  17.         List<Project> projects = projectMap.values().stream().collect(Collectors.toList());
  18.         Collections.sort(projects);
  19.  
  20.         StringBuilder sb = new StringBuilder();
  21.  
  22.         for (Project project : projects) {
  23.             if (project.getErrorCount() > 0) {
  24.                 sb.append(project.toString());
  25.                 sb.append("\n");
  26.             }
  27.         }
  28.  
  29.         if (sb.length() >= 2) {
  30.             sb.delete(sb.length() - 2, sb.length());
  31.         }
  32.  
  33.         System.out.println(sb.toString());
  34.         // System.out.println("svyrshi");
  35.     }
  36.  
  37.     private static void readInput() {
  38.         Scanner scanducan = new Scanner(System.in);
  39.        // {\"Project\": [\"Project1\"], \"Type\": [\"Critical\"], \"Message\": [\"File not found\"]}";
  40.  
  41.         String jstring;
  42.         while (!(jstring = scanducan.nextLine()).equals("END")) {
  43.             Matcher macho = mainPat.matcher(jstring);
  44.  
  45.             if(macho.find()) {
  46.                 List<String> parts = new ArrayList<>();
  47.  
  48.                 parts.add(macho.group("name"));
  49.                 parts.add(macho.group("type"));
  50.                 parts.add(macho.group("message"));
  51.  
  52.                 addError(parts);
  53.             }
  54.         }
  55.     }
  56.  
  57.     private static void addError(List<String> parts) {
  58.         String projectName = parts.get(0);
  59.         ErrorType type = ErrorType.valueOf(parts.get(1));
  60.         String message = parts.get(2);
  61.  
  62.         Project project;
  63.         if (projectMap.containsKey(projectName)) {
  64.             project = projectMap.get(projectName);
  65.         } else {
  66.             project = new Project(projectName);
  67.             projectMap.put(projectName, project);
  68.         }
  69.  
  70.         AppError currentError = new AppError(message, type, project);
  71.         project.addError(currentError);
  72.     }
  73. }
  74.  
  75. class Project implements Comparable<Project> {
  76.  
  77.     public String name;
  78.     public List<AppError> critical;
  79.     public List<AppError> warning;
  80.  
  81.     public Project(String name) {
  82.         this.name = name;
  83.         this.critical = new ArrayList<>();
  84.         this.warning = new ArrayList<>();
  85.     }
  86.  
  87.     @Override
  88.     public String toString() {
  89.             //        ProjectName:
  90.             //        Total Errors: {total number of errors}
  91.             //        Critical: {total number of critical critical}
  92.             //        Warnings: {total number of warnings}
  93.             //        Critical Messages:
  94.             //        --->{message of critical error No. 1}
  95.             //        --->{message of critical error No.2}
  96.             //        ...
  97.             //        Warning Messages:
  98.             //        --->{message of warning No. 1}
  99.             //        --->{message of warning No. 2}
  100.             //        ...
  101.         StringBuilder sb = new StringBuilder();
  102.         sb.append(String.format("%s:\n", this.name))
  103.                 .append(String.format("Total Errors: %d\n", this.getErrorCount()))
  104.                 .append(String.format("Critical: %d\n", this.critical.size()))
  105.                 .append(String.format("Warnings: %d\n", this.warning.size()))
  106.                 .append("Critical Messages:\n");
  107.  
  108.         if (this.critical.size() > 0) {
  109.             Collections.sort(this.critical);
  110.  
  111.             for (AppError c : this.critical) {
  112.                 sb.append(String.format("--->%s\n", c.message));
  113.             }
  114.         } else {
  115.             sb.append("--->None\n");
  116.         }
  117.  
  118.         sb.append("Warning Messages:\n");
  119.  
  120.         if (this.warning.size() > 0) {
  121.             Collections.sort(this.warning);
  122.  
  123.             for (AppError w : this.warning) {
  124.                 sb.append(String.format("--->%s\n", w.message));
  125.             }
  126.         } else {
  127.             sb.append("--->None\n");
  128.         }
  129.  
  130.         return sb.toString();
  131.     }
  132.  
  133.     @Override
  134.     public int compareTo(Project o) {
  135.         int errors = this.getErrorCount();
  136.         int otherErorrs = o.getErrorCount();
  137.  
  138.         if (errors != otherErorrs) {
  139.             return otherErorrs - errors;
  140.         } else {
  141.             return this.name.compareTo(o.name);
  142.         }
  143.     }
  144.  
  145.     public void addError(AppError err) {
  146.         if (err.type == ErrorType.Critical) {
  147.             this.critical.add(err);
  148.         } else {
  149.             this.warning.add(err);
  150.         }
  151.     }
  152.  
  153.     public int getErrorCount() {
  154.         return this.critical.size() + this.warning.size();
  155.     }
  156. }
  157.  
  158. class AppError implements Comparable<AppError> {
  159.  
  160.     public String message;
  161.     public ErrorType type;
  162.     public Project project;
  163.  
  164.     public AppError(String message, ErrorType type, Project project) {
  165.         this.message = message;
  166.         this.type = type;
  167.         this.project = project;
  168.     }
  169.  
  170.     @Override
  171.     public int compareTo(AppError other) {
  172.         int lengthCompare = this.message.length() - other.message.length();
  173.         if (lengthCompare != 0) {
  174.             return lengthCompare;
  175.         } else {
  176.             return this.message.compareTo(other.message);
  177.         }
  178.     }
  179. }
  180.  
  181. enum ErrorType {
  182.     Warning,
  183.     Critical
  184. }
Advertisement
Add Comment
Please, Sign In to add comment