Guest User

Untitled

a guest
Apr 24th, 2016
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. package exam;
  2.  
  3. import java.util.*;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class GUnit {
  8.  
  9. public static void main(String[] args) {
  10. Scanner scanner = new Scanner(System.in);
  11.  
  12. Map<String, Map<String, List<String>>> data = new HashMap<>();
  13.  
  14. Pattern pattern = Pattern.compile("^([A-Z][A-Za-z\\d]+) \\| ([A-Z][A-Za-z\\d]+) \\| ([A-Z][A-Za-z\\d]+)$");
  15.  
  16. String inputLine = scanner.nextLine();
  17. while (!inputLine.equals("It's testing time!")) {
  18. Matcher matcher = pattern.matcher(inputLine);
  19.  
  20. if (matcher.find()) {
  21. String className = matcher.group(1);
  22. String methodName = matcher.group(2);
  23. String unitTest = matcher.group(3);
  24.  
  25. if (!data.containsKey(className)) {
  26. data.put(className, new HashMap<>());
  27. }
  28.  
  29. if (!data.get(className).containsKey(methodName)) {
  30. data.get(className).put(methodName, new ArrayList<>());
  31. }
  32.  
  33. if (!data.get(className).get(methodName).contains(unitTest)) {
  34. data.get(className).get(methodName).add(unitTest);
  35. }
  36. }
  37.  
  38. inputLine = scanner.nextLine();
  39. }
  40.  
  41.  
  42. SortedSet<Map.Entry<String, Map<String, List<String>>>> sortedData = new TreeSet<>((o1, o2) -> {
  43. int unitTestsCount1 = 0; //.stream().map(e -> )
  44. for (Map.Entry<String, List<String>> stringListEntry : o1.getValue().entrySet()) {
  45. unitTestsCount1 += stringListEntry.getValue().size();
  46. }
  47.  
  48. int unitTestsCount2 = 0;
  49. for (Map.Entry<String, List<String>> stringListEntry : o2.getValue().entrySet()) {
  50. unitTestsCount2 += stringListEntry.getValue().size();
  51. }
  52.  
  53. if (unitTestsCount1 != unitTestsCount2) {
  54. return Integer.compare(unitTestsCount2, unitTestsCount1);
  55. }
  56.  
  57. int methodsCount1 = o1.getValue().size();
  58. int methodsCount2 = o2.getValue().size();
  59. if (methodsCount1 != methodsCount2) {
  60. return Integer.compare(methodsCount1, methodsCount2);
  61. }
  62.  
  63. return o1.getKey().compareTo(o2.getKey());
  64. });
  65.  
  66. sortedData.addAll(data.entrySet());
  67.  
  68. for (Map.Entry<String, Map<String, List<String>>> classMethodsEntry : sortedData) {
  69. System.out.println(classMethodsEntry.getKey() + ":");
  70.  
  71. SortedSet<Map.Entry<String, List<String>>> sortedMethods = new TreeSet<>((o1, o2) -> {
  72. int testsCountO1 = o1.getValue().size();
  73. int testsCountO2 = o2.getValue().size();
  74. if (testsCountO1 != testsCountO2) {
  75. return Integer.compare(testsCountO2, testsCountO1);
  76. }
  77.  
  78. return o1.getKey().compareTo(o2.getKey());
  79. });
  80.  
  81. sortedMethods.addAll(data.get(classMethodsEntry.getKey()).entrySet());
  82.  
  83. for (Map.Entry<String, List<String>> methodUnitTestsPair : sortedMethods) {
  84. System.out.println("##" + methodUnitTestsPair.getKey());
  85.  
  86. Collections.sort(methodUnitTestsPair.getValue(), new MyComparator());
  87. for (String unitTest : methodUnitTestsPair.getValue()) {
  88. System.out.println("####" + unitTest);
  89. }
  90. }
  91. }
  92. }
  93. }
  94.  
  95. class MyComparator implements Comparator<String> {
  96. public int compare(String o1, String o2) {
  97. int firstLength = o1.length();
  98. int secondLength = o2.length();
  99.  
  100. if (firstLength != secondLength) {
  101. return Integer.compare(firstLength, secondLength);
  102. }
  103.  
  104. return o1.compareTo(o2);
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment