PageRenderTime 46ms CodeModel.GetById 25ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

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