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

http://testability-explorer.googlecode.com/ · Java · 120 lines · 82 code · 19 blank · 19 comment · 10 complexity · 0b958b8d3f0679e7b994e852c1ed2159 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;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. public class ClassCost {
  21. public static final String CLASS_NAME = "class";
  22. public static class CostComparator implements java.util.Comparator<ClassCost> {
  23. private final CostModel costModel;
  24. public CostComparator(CostModel costModel) {
  25. this.costModel = costModel;
  26. }
  27. public int compare(ClassCost c1, ClassCost c2) {
  28. int diff = (costModel.computeClass(c2) - costModel.computeClass(c1));
  29. return diff == 0 ? c1.className.compareTo(c2.className) : diff;
  30. }
  31. }
  32. private final List<MethodCost> methods;
  33. private final String className;
  34. public ClassCost(String className, List<MethodCost> methods) {
  35. this.className = className;
  36. this.methods = methods;
  37. }
  38. public MethodCost getMethodCost(String methodName) {
  39. for (MethodCost cost : methods) {
  40. if (cost.getMethodName().equals(methodName)) {
  41. return cost;
  42. }
  43. }
  44. throw new IllegalArgumentException("Method '" + methodName
  45. + "' does not exist.");
  46. }
  47. @Override
  48. public String toString() {
  49. return className;
  50. }
  51. public String getClassName() {
  52. return className;
  53. }
  54. public String getPackageName() {
  55. return getClassName().lastIndexOf('.') == -1 ? "" : getClassName().substring(0, getClassName().lastIndexOf('.'));
  56. }
  57. public List<MethodCost> getMethods() {
  58. return methods;
  59. }
  60. // TODO: delete
  61. public long getTotalComplexityCost() {
  62. long totalCost = 0;
  63. for (MethodCost methodCost : getMethods()) {
  64. totalCost += methodCost.getTotalCost().getCyclomaticComplexityCost();
  65. }
  66. return totalCost;
  67. }
  68. // TODO: delete
  69. public long getHighestMethodComplexityCost() {
  70. long cost = 0;
  71. for (MethodCost methodCost : getMethods()) {
  72. if (methodCost.getTotalCost().getCyclomaticComplexityCost() > cost) {
  73. cost = methodCost.getTotalCost().getCyclomaticComplexityCost();
  74. }
  75. }
  76. return cost;
  77. }
  78. // TODO: delete
  79. public long getTotalGlobalCost() {
  80. long totalCost = 0;
  81. for (MethodCost methodCost : getMethods()) {
  82. totalCost += methodCost.getTotalCost().getGlobalCost();
  83. }
  84. return totalCost;
  85. }
  86. // TODO: delete
  87. public long getHighestMethodGlobalCost() {
  88. long cost = 0;
  89. for (MethodCost methodCost : getMethods()) {
  90. if (methodCost.getTotalCost().getGlobalCost() > cost) {
  91. cost = methodCost.getTotalCost().getGlobalCost();
  92. }
  93. }
  94. return cost;
  95. }
  96. public Map<String, Object> getAttributes() {
  97. HashMap<String, Object> map = new HashMap<String, Object>();
  98. map.put(CLASS_NAME, className);
  99. return map;
  100. }
  101. }