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

http://testability-explorer.googlecode.com/ · Java · 69 lines · 44 code · 11 blank · 14 comment · 4 complexity · fe31c8d41730f00947196e50c6b4051c MD5 · raw file

  1. // Copyright 2009 Google Inc. All Rights Reserved.
  2. package com.google.test.metric.report.issues;
  3. import com.google.inject.Inject;
  4. import com.google.test.metric.ClassCost;
  5. import com.google.test.metric.Cost;
  6. import com.google.test.metric.CostModel;
  7. import com.google.test.metric.MethodCost;
  8. import com.google.test.metric.ViolationCost;
  9. import com.google.test.metric.WeightedAverage;
  10. /**
  11. * A cost model that can answer the hypothetical question "what would be the cost of this class
  12. * if I made a certain change to it?"
  13. *
  14. * The values returned are only rough approximations. Figuring out the real cost requires
  15. * munging the ClassInfo to produce the class with the hypothetical change, then re-running the
  16. * analysis, which is far too CPU intensive.
  17. *
  18. * @author alexeagle@google.com (Alex Eagle)
  19. */
  20. public class HypotheticalCostModel {
  21. private final CostModel costModel;
  22. @Inject
  23. public HypotheticalCostModel(CostModel costModel) {
  24. this.costModel = costModel;
  25. }
  26. private int computeClassWithoutMethod(ClassCost classCost, MethodCost adjustedMethod,
  27. Cost replacementCost) {
  28. WeightedAverage average = costModel.createWeighedAverage();
  29. for (MethodCost methodCost : classCost.getMethods()) {
  30. Cost cost = (adjustedMethod == methodCost ? replacementCost : methodCost.getTotalCost());
  31. average.addValue(costModel.computeOverall(cost));
  32. }
  33. return (int) average.getAverage();
  34. }
  35. public float computeContributionFromIssue(ClassCost classCost, MethodCost violationMethodCost,
  36. ViolationCost violationCost) {
  37. Cost adjustedCost = violationMethodCost.getTotalCost().add(violationCost.getCost().negate());
  38. WeightedAverage average = costModel.createWeighedAverage();
  39. for (MethodCost methodCost : classCost.getMethods()) {
  40. Cost cost = (violationMethodCost == methodCost ? adjustedCost : methodCost.getTotalCost());
  41. average.addValue(costModel.computeOverall(cost));
  42. }
  43. return 1 - (int) average.getAverage() / (float) computeClass(classCost);
  44. }
  45. public float computeContributionFromMethod(ClassCost classCost, MethodCost violationMethodCost) {
  46. final float costWithoutIssue =
  47. computeClassWithoutMethod(classCost, violationMethodCost,
  48. violationMethodCost.getDependentCost());
  49. final float totalCost = (float) computeClass(classCost);
  50. return 1 - costWithoutIssue / totalCost;
  51. }
  52. /**
  53. * Provided for passthrough to the delegate {@link CostModel}.
  54. */
  55. public int computeClass(ClassCost classCost) {
  56. return costModel.computeClass(classCost);
  57. }
  58. }