Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigDecimal;
- import java.text.Collator;
- import java.util.ArrayList;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import java.util.stream.Collectors;
- public class p08_Commits {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String inputLine = scanner.nextLine();
- LinkedHashMap<String, LinkedHashMap<String, ArrayList<Commit>>> gitUsers = new LinkedHashMap<>();
- Pattern pattern = Pattern.compile("^https://github\\.com/(?<user>[A-Za-z0-9\\-]+)/" +
- "(?<repo>[a-zA-Z\\-_]+)/commit/" +
- "(?<hash>[0-9a-f]{40})," +
- "(?<message>[^\\n]*)," +
- "(?<additions>\\d*)," +
- "(?<deletions>\\d*)$");
- while (!"git push".equals(inputLine)) {
- Matcher matcher = pattern.matcher(inputLine);
- if (matcher.find()) {
- String username = matcher.group("user");
- String repo = matcher.group("repo");
- String hash = matcher.group("hash");
- String message = matcher.group("message");
- BigDecimal additions = new BigDecimal(matcher.group("additions"));
- BigDecimal deletions = new BigDecimal(matcher.group("deletions"));
- Commit commit = new Commit(hash, message, additions, deletions);
- gitUsers.putIfAbsent(username, new LinkedHashMap<>());
- gitUsers.get(username).putIfAbsent(repo, new ArrayList<>());
- gitUsers.get(username).get(repo).add(commit);
- }
- inputLine = scanner.nextLine();
- }
- Collator collator = Collator.getInstance();
- /*LinkedHashMap<String, LinkedHashMap<String, ArrayList<Commit>>> test = gitUsers.entrySet()
- .stream()
- .sorted((u1, u2) -> collator.compare(u1.getKey(), u2.getKey()))
- .map(u -> u.getValue().entrySet()
- .stream()
- .sorted((r1, r2) -> collator.compare(r1.getKey(), r2.getKey())))
- .collect(Collectors.toMap())*/
- // Printing the data
- gitUsers.entrySet()
- .stream()
- .sorted((u1, u2) -> collator.compare(u1.getKey(), u2.getKey()))
- .forEach(user -> {
- System.out.printf("%s:%n", user.getKey());
- user.getValue().entrySet()
- .stream()
- .sorted((r1, r2) -> collator.compare(r1.getKey(), r2.getKey()))
- .forEach(repo -> {
- System.out.printf(" %s:%n", repo.getKey());
- repo.getValue().forEach(c -> {
- System.out.printf(" commit %s: %s (%s additions, %s deletions)%n",
- c.getHash(), c.getMessage(), c.getAdditions(), c.getDeletions());
- });
- BigDecimal totalAdditions = repo.getValue()
- .stream()
- .map(e -> e.getAdditions())
- .reduce(BigDecimal.ZERO, BigDecimal::add);
- BigDecimal totalDeletions = repo.getValue()
- .stream()
- .map(e -> e.getDeletions())
- .reduce(BigDecimal.ZERO, BigDecimal::add);
- System.out.printf(" Total: %s additions, %s deletions%n", totalAdditions, totalDeletions);
- });
- });
- }
- }
- class Commit {
- private String hash;
- private String message;
- private BigDecimal additions;
- private BigDecimal deletions;
- // Creating constructor
- public Commit(String hash, String message, BigDecimal additions, BigDecimal deletions) {
- this.hash = hash;
- this.message = message;
- this.additions = additions;
- this.deletions = deletions;
- }
- public String getHash() {
- return this.hash;
- }
- public String getMessage() {
- return this.message;
- }
- public BigDecimal getAdditions() {
- return this.additions;
- }
- public BigDecimal getDeletions() {
- return this.deletions;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement