PageRenderTime 29ms CodeModel.GetById 17ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

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