Advertisement
valerielashvili

Commits

Aug 30th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | None | 0 0
  1. import java.math.BigDecimal;
  2. import java.text.Collator;
  3. import java.util.ArrayList;
  4. import java.util.LinkedHashMap;
  5. import java.util.Map;
  6. import java.util.Scanner;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9. import java.util.stream.Collectors;
  10.  
  11. public class p08_Commits {
  12.     public static void main(String[] args) {
  13.         Scanner scanner = new Scanner(System.in);
  14.         String inputLine = scanner.nextLine();
  15.  
  16.         LinkedHashMap<String, LinkedHashMap<String, ArrayList<Commit>>> gitUsers = new LinkedHashMap<>();
  17.  
  18.         Pattern pattern = Pattern.compile("^https://github\\.com/(?<user>[A-Za-z0-9\\-]+)/" +
  19.                 "(?<repo>[a-zA-Z\\-_]+)/commit/" +
  20.                 "(?<hash>[0-9a-f]{40})," +
  21.                 "(?<message>[^\\n]*)," +
  22.                 "(?<additions>\\d*)," +
  23.                 "(?<deletions>\\d*)$");
  24.  
  25.         while (!"git push".equals(inputLine)) {
  26.             Matcher matcher = pattern.matcher(inputLine);
  27.  
  28.             if (matcher.find()) {
  29.                 String username = matcher.group("user");
  30.                 String repo = matcher.group("repo");
  31.                 String hash = matcher.group("hash");
  32.                 String message = matcher.group("message");
  33.                 BigDecimal additions = new BigDecimal(matcher.group("additions"));
  34.                 BigDecimal deletions = new BigDecimal(matcher.group("deletions"));
  35.  
  36.                 Commit commit = new Commit(hash, message, additions, deletions);
  37.  
  38.                 gitUsers.putIfAbsent(username, new LinkedHashMap<>());
  39.                 gitUsers.get(username).putIfAbsent(repo, new ArrayList<>());
  40.                 gitUsers.get(username).get(repo).add(commit);
  41.             }
  42.             inputLine = scanner.nextLine();
  43.         }
  44.         Collator collator = Collator.getInstance();
  45.  
  46.         /*LinkedHashMap<String, LinkedHashMap<String, ArrayList<Commit>>> test = gitUsers.entrySet()
  47.                 .stream()
  48.                 .sorted((u1, u2) -> collator.compare(u1.getKey(), u2.getKey()))
  49.                 .map(u -> u.getValue().entrySet()
  50.                         .stream()
  51.                         .sorted((r1, r2) -> collator.compare(r1.getKey(), r2.getKey())))
  52.                 .collect(Collectors.toMap())*/
  53.  
  54.  
  55.         // Printing the data
  56.         gitUsers.entrySet()
  57.                 .stream()
  58.                 .sorted((u1, u2) -> collator.compare(u1.getKey(), u2.getKey()))
  59.                 .forEach(user -> {
  60.                     System.out.printf("%s:%n", user.getKey());
  61.  
  62.                     user.getValue().entrySet()
  63.                             .stream()
  64.                             .sorted((r1, r2) -> collator.compare(r1.getKey(), r2.getKey()))
  65.                             .forEach(repo -> {
  66.                                 System.out.printf("  %s:%n", repo.getKey());
  67.  
  68.                                 repo.getValue().forEach(c -> {
  69.                                     System.out.printf("    commit %s: %s (%s additions, %s deletions)%n",
  70.                                             c.getHash(), c.getMessage(), c.getAdditions(), c.getDeletions());
  71.                                 });
  72.  
  73.                                 BigDecimal totalAdditions = repo.getValue()
  74.                                         .stream()
  75.                                         .map(e -> e.getAdditions())
  76.                                         .reduce(BigDecimal.ZERO, BigDecimal::add);
  77.  
  78.                                 BigDecimal totalDeletions = repo.getValue()
  79.                                         .stream()
  80.                                         .map(e -> e.getDeletions())
  81.                                         .reduce(BigDecimal.ZERO, BigDecimal::add);
  82.  
  83.                                 System.out.printf("    Total: %s additions, %s deletions%n", totalAdditions, totalDeletions);
  84.                             });
  85.                 });
  86.     }
  87. }
  88.  
  89. class Commit {
  90.     private String hash;
  91.     private String message;
  92.     private BigDecimal additions;
  93.     private BigDecimal deletions;
  94.  
  95.     // Creating constructor
  96.     public Commit(String hash, String message, BigDecimal additions, BigDecimal deletions) {
  97.         this.hash = hash;
  98.         this.message = message;
  99.         this.additions = additions;
  100.         this.deletions = deletions;
  101.     }
  102.  
  103.     public String getHash() {
  104.         return this.hash;
  105.     }
  106.     public String getMessage() {
  107.         return this.message;
  108.     }
  109.     public BigDecimal getAdditions() {
  110.         return this.additions;
  111.     }
  112.     public BigDecimal getDeletions() {
  113.         return this.deletions;
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement