/testability-explorer/src/main/java/com/google/test/metric/report/Diff.java

http://testability-explorer.googlecode.com/ · Java · 145 lines · 105 code · 20 blank · 20 comment · 13 complexity · 9cd46b9d936224082c706420a2ebeec8 MD5 · raw file

  1. /*
  2. * Copyright 2007 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.test.metric.report;
  17. import java.util.Collections;
  18. import java.util.Comparator;
  19. import java.util.List;
  20. /**
  21. * Simple bean to store a difference between two testability reports.
  22. * @author alexeagle@google.com (Alex Eagle)
  23. *
  24. */
  25. public class Diff {
  26. private final List<ClassDiff> classDiffs;
  27. public Diff(List<ClassDiff> classDiffs) {
  28. this.classDiffs = classDiffs;
  29. }
  30. public List<ClassDiff> getClassDiffs() {
  31. return classDiffs;
  32. }
  33. public void sort() {
  34. Collections.sort(classDiffs, new ClassDeltaComparator());
  35. for (ClassDiff classDiff : classDiffs) {
  36. Collections.sort(classDiff.methodDiffs, new MethodDeltaComparator());
  37. }
  38. }
  39. public static class ClassDiff {
  40. private final Integer oldMetric;
  41. private final Integer newMetric;
  42. private final List<MethodDiff> methodDiffs;
  43. private final String className;
  44. @SuppressWarnings("unchecked")
  45. public ClassDiff(String className, Integer oldMetric, Integer newMetric) {
  46. this.className = className;
  47. this.oldMetric = oldMetric;
  48. this.newMetric = newMetric;
  49. this.methodDiffs = Collections.EMPTY_LIST;
  50. }
  51. public ClassDiff(String className, Integer oldMetric, Integer newMetric, List<MethodDiff> methodDiffs) {
  52. this.className = className;
  53. this.oldMetric = oldMetric;
  54. this.newMetric = newMetric;
  55. this.methodDiffs = methodDiffs;
  56. }
  57. public String getClassName() {
  58. return className;
  59. }
  60. public Integer getNewMetric() {
  61. return newMetric;
  62. }
  63. public Integer getOldMetric() {
  64. return oldMetric;
  65. }
  66. public List<MethodDiff> getMethodDiffs() {
  67. return methodDiffs;
  68. }
  69. public Integer getDelta() {
  70. if (newMetric == null) {
  71. return -oldMetric;
  72. }
  73. if (oldMetric == null) {
  74. return newMetric;
  75. }
  76. return (newMetric - oldMetric);
  77. }
  78. }
  79. public static class MethodDiff {
  80. private final Integer oldMetric;
  81. private final Integer newMetric;
  82. private final String methodName;
  83. public MethodDiff(String methodName, Integer oldMetric, Integer newMetric) {
  84. this.oldMetric = oldMetric;
  85. this.newMetric = newMetric;
  86. this.methodName = methodName;
  87. }
  88. public Integer getOldMetric() {
  89. return oldMetric;
  90. }
  91. public Integer getNewMetric() {
  92. return newMetric;
  93. }
  94. public String getMethodName() {
  95. return methodName;
  96. }
  97. public Integer getDelta() {
  98. if (newMetric == null) {
  99. return -oldMetric;
  100. }
  101. if (oldMetric == null) {
  102. return newMetric;
  103. }
  104. return (newMetric - oldMetric);
  105. }
  106. }
  107. private static class ClassDeltaComparator implements Comparator<ClassDiff> {
  108. public int compare(ClassDiff classDiff, ClassDiff classDiff1) {
  109. int result = classDiff.getDelta().compareTo(classDiff1.getDelta());
  110. if (result == 0) {
  111. result = classDiff.getClassName().compareTo(classDiff1.getClassName());
  112. }
  113. return result;
  114. }
  115. }
  116. private static class MethodDeltaComparator implements Comparator<MethodDiff> {
  117. public int compare(MethodDiff methodDiff, MethodDiff methodDiff1) {
  118. int result = methodDiff.getDelta().compareTo(methodDiff1.getDelta());
  119. if (result == 0) {
  120. result = methodDiff.getMethodName().compareTo(methodDiff.getMethodName());
  121. }
  122. return result;
  123. }
  124. }
  125. }