PageRenderTime 79ms CodeModel.GetById 50ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://testability-explorer.googlecode.com/
Java | 160 lines | 107 code | 31 blank | 22 comment | 4 complexity | fe8f2341712befbe64d187f935f61392 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 */
 16package com.google.test.metric;
 17
 18import com.google.test.metric.report.RemovePackageFormatter;
 19
 20import java.util.ArrayList;
 21import java.util.List;
 22import java.util.Map;
 23
 24public class MethodCost {
 25
 26  private final String className;
 27  private final String methodName;
 28  private final int lineNumber;
 29  private final boolean constructor;
 30  private final boolean aStatic;
 31  private final boolean staticInit;
 32
 33  private final List<ViolationCost> costSources = new ArrayList<ViolationCost>();
 34
 35  public boolean isConstructor() {
 36    return constructor;
 37  }
 38
 39  public boolean isStatic() {
 40    return aStatic;
 41  }
 42
 43  public Cost getDirectCost() {
 44    return directCost;
 45  }
 46
 47  public Cost getDependentCost() {
 48    return dependentCost;
 49  }
 50
 51  public Cost getConstructorDependentCost() {
 52    return constructorDependentCost;
 53  }
 54
 55  private final Cost directCost = new Cost();
 56  private final Cost dependentCost = new Cost();
 57  private final Cost constructorDependentCost = new Cost();
 58
 59  public static final String METHOD_NAME_ATTRIBUTE = "name";
 60
 61  /**
 62   * @param className
 63   * @param methodName
 64   *          name of the method, such as {@code void myMethod()}.
 65   * @param lineNumber
 66   * @param isStaticInit
 67   */
 68  public MethodCost(String className, String methodName, int lineNumber, boolean isConstructor,
 69                    boolean isStatic,
 70                    boolean isStaticInit) {
 71    this.className = className;
 72    this.methodName = methodName;
 73    this.lineNumber = lineNumber;
 74    constructor = isConstructor;
 75    aStatic = isStatic;
 76    staticInit = isStaticInit;
 77  }
 78
 79  public Cost getTotalCost() {
 80    return new Cost().add(directCost).add(dependentCost).add(constructorDependentCost);
 81  }
 82
 83  public Cost getNonConstructorCost() {
 84    return new Cost().add(directCost).add(dependentCost);
 85  }
 86
 87  public String getMethodName() {
 88    return methodName;
 89  }
 90
 91  public void addCostSource(ViolationCost costSource) {
 92    costSource.link(directCost, dependentCost, constructorDependentCost);
 93    costSources.add(costSource);
 94  }
 95
 96  @Override
 97  public String toString() {
 98    return getMethodName() + toCostsString();
 99  }
100
101  public String toCostsString() {
102    return " [" + getTotalCost() + " / " + directCost + "]";
103  }
104
105  public int getMethodLineNumber() {
106    return lineNumber;
107  }
108
109  public String getClassName() {
110    return className;
111  }
112
113  public List<ViolationCost> getViolationCosts() {
114    return costSources;
115  }
116
117  public List<ViolationCost> getImplicitViolationCosts() {
118    return filterViolationCosts(true);
119  }
120
121  public List<ViolationCost> getExplicitViolationCosts() {
122    return filterViolationCosts(false);
123  }
124
125  private List<ViolationCost> filterViolationCosts(boolean implicit) {
126    List<ViolationCost> result = new ArrayList<ViolationCost>();
127    for (ViolationCost cost : getViolationCosts()) {
128      if (cost.isImplicit() == implicit) {
129        result.add(cost);
130      }
131    }
132    return result;
133  }
134
135  public Cost getCost() {
136    return directCost;
137  }
138
139  public Map<String, Object> getAttributes() {
140    Map<String, Object> map = getTotalCost().getAttributes();
141    map.put("line", lineNumber);
142    map.put(METHOD_NAME_ATTRIBUTE, methodName);
143    return map;
144  }
145
146  public void link() {
147  }
148
149  public boolean isMainMethod() {
150    return isStatic() && "void main(java.lang.String[])".equals(getMethodName());
151  }
152
153  public String getDescription() {
154    return new RemovePackageFormatter().format(methodName);
155  }
156
157  public boolean isStaticInit() {
158    return staticInit;
159  }
160}