Advertisement
ivanov_ivan

HelpingCommits

Aug 31st, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.33 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.Map;
  6. import java.util.TreeMap;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9.  
  10. public class p08_Commits {
  11.     public static void main(String[] args) throws IOException {
  12.         BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
  13.         String inputLine = scanner.readLine();
  14.  
  15.         Map<String, Map<String, ArrayList<Commit>>> gitUsers = new TreeMap<>();
  16.  
  17.         Pattern pattern = Pattern.compile("^https://github\\.com/(?<user>[A-Za-z0-9\\-]+)/" +
  18.                 "(?<repo>[a-zA-Z\\-_]+)/commit/" +
  19.                 "(?<hash>[0-9a-f]{40})," +
  20.                 "(?<message>[^\\n]*)," +
  21.                 "(?<additions>\\d*)," +
  22.                 "(?<deletions>\\d*)$");
  23.  
  24.         while (!"git push".equals(inputLine)) {
  25.             Matcher matcher = pattern.matcher(inputLine);
  26.  
  27.             if (matcher.find()) {
  28.                 String username = matcher.group("user");
  29.                 String repo = matcher.group("repo");
  30.                 String hash = matcher.group("hash");
  31.                 String message = matcher.group("message");
  32.                 long additions = Long.parseLong(matcher.group("additions"));
  33.                 long deletions = Long.parseLong(matcher.group("deletions"));
  34.  
  35.                 Commit commit = new Commit(hash, message, additions, deletions);
  36.  
  37.                 gitUsers.putIfAbsent(username, new TreeMap<>());
  38.                 gitUsers.get(username).putIfAbsent(repo, new ArrayList<>());
  39.                 gitUsers.get(username).get(repo).add(commit);
  40.             }
  41.             inputLine = scanner.readLine();
  42.         }
  43.  
  44.         // Printing the data
  45.         gitUsers.forEach((key, value) -> {
  46.             System.out.printf("%s:%n", key);
  47.  
  48.             value.forEach((key1, value1) -> {
  49.                 System.out.printf("  %s:%n", key1);
  50.  
  51.                 value1.forEach(c -> {
  52.                     System.out.printf("    commit %s: %s (%s additions, %s deletions)%n",
  53.                             c.getHash(), c.getMessage(), c.getAdditions(), c.getDeletions());
  54.                 });
  55.  
  56.                 long totalAdditions = value1
  57.                         .stream()
  58.                         .mapToLong(Commit::getAdditions)
  59.                         .sum();
  60.  
  61.                 long totalDeletions = value1
  62.                         .stream()
  63.                         .mapToLong(Commit::getDeletions)
  64.                         .sum();
  65.  
  66.                 System.out.printf("    Total: %s additions, %s deletions%n", totalAdditions, totalDeletions);
  67.             });
  68.         });
  69.     }
  70. }
  71.  
  72. class Commit {
  73.     private String hash;
  74.     private String message;
  75.     private long additions;
  76.     private long deletions;
  77.  
  78.     // Creating constructor
  79.     public Commit(String hash, String message, long additions, long deletions) {
  80.         this.hash = hash;
  81.         this.message = message;
  82.         this.additions = additions;
  83.         this.deletions = deletions;
  84.     }
  85.  
  86.     public String getHash() {
  87.         return this.hash;
  88.     }
  89.  
  90.     public String getMessage() {
  91.         return this.message;
  92.     }
  93.  
  94.     public long getAdditions() {
  95.         return this.additions;
  96.     }
  97.  
  98.     public long getDeletions() {
  99.         return this.deletions;
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement