Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package exam;
- import java.util.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class GUnit {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Map<String, Map<String, List<String>>> data = new HashMap<>();
- Pattern pattern = Pattern.compile("^([A-Z][A-Za-z\\d]+) \\| ([A-Z][A-Za-z\\d]+) \\| ([A-Z][A-Za-z\\d]+)$");
- String inputLine = scanner.nextLine();
- while (!inputLine.equals("It's testing time!")) {
- Matcher matcher = pattern.matcher(inputLine);
- if (matcher.find()) {
- String className = matcher.group(1);
- String methodName = matcher.group(2);
- String unitTest = matcher.group(3);
- if (!data.containsKey(className)) {
- data.put(className, new HashMap<>());
- }
- if (!data.get(className).containsKey(methodName)) {
- data.get(className).put(methodName, new ArrayList<>());
- }
- if (!data.get(className).get(methodName).contains(unitTest)) {
- data.get(className).get(methodName).add(unitTest);
- }
- }
- inputLine = scanner.nextLine();
- }
- SortedSet<Map.Entry<String, Map<String, List<String>>>> sortedData = new TreeSet<>((o1, o2) -> {
- int unitTestsCount1 = 0; //.stream().map(e -> )
- for (Map.Entry<String, List<String>> stringListEntry : o1.getValue().entrySet()) {
- unitTestsCount1 += stringListEntry.getValue().size();
- }
- int unitTestsCount2 = 0;
- for (Map.Entry<String, List<String>> stringListEntry : o2.getValue().entrySet()) {
- unitTestsCount2 += stringListEntry.getValue().size();
- }
- if (unitTestsCount1 != unitTestsCount2) {
- return Integer.compare(unitTestsCount2, unitTestsCount1);
- }
- int methodsCount1 = o1.getValue().size();
- int methodsCount2 = o2.getValue().size();
- if (methodsCount1 != methodsCount2) {
- return Integer.compare(methodsCount1, methodsCount2);
- }
- return o1.getKey().compareTo(o2.getKey());
- });
- sortedData.addAll(data.entrySet());
- for (Map.Entry<String, Map<String, List<String>>> classMethodsEntry : sortedData) {
- System.out.println(classMethodsEntry.getKey() + ":");
- SortedSet<Map.Entry<String, List<String>>> sortedMethods = new TreeSet<>((o1, o2) -> {
- int testsCountO1 = o1.getValue().size();
- int testsCountO2 = o2.getValue().size();
- if (testsCountO1 != testsCountO2) {
- return Integer.compare(testsCountO2, testsCountO1);
- }
- return o1.getKey().compareTo(o2.getKey());
- });
- sortedMethods.addAll(data.get(classMethodsEntry.getKey()).entrySet());
- for (Map.Entry<String, List<String>> methodUnitTestsPair : sortedMethods) {
- System.out.println("##" + methodUnitTestsPair.getKey());
- Collections.sort(methodUnitTestsPair.getValue(), new MyComparator());
- for (String unitTest : methodUnitTestsPair.getValue()) {
- System.out.println("####" + unitTest);
- }
- }
- }
- }
- }
- class MyComparator implements Comparator<String> {
- public int compare(String o1, String o2) {
- int firstLength = o1.length();
- int secondLength = o2.length();
- if (firstLength != secondLength) {
- return Integer.compare(firstLength, secondLength);
- }
- return o1.compareTo(o2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment