/testability-explorer/src/main/java/com/google/test/metric/report/SummaryReportModel.java
Java | 88 lines | 61 code | 12 blank | 15 comment | 5 complexity | 5b8efb460cb0a27fbd97c922f53c681f 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.report; 17 18import java.util.ArrayList; 19import java.util.List; 20import java.util.SortedSet; 21import java.util.TreeSet; 22 23import com.google.test.metric.ClassCost; 24import com.google.test.metric.CostModel; 25import com.google.test.metric.WeightedAverage; 26 27public abstract class SummaryReportModel extends ReportModel { 28 29 protected final WeightedAverage weightedAverage = new WeightedAverage(); 30 protected SortedSet<ClassCost> worstOffenders; 31 protected final List<Integer> costs = new ArrayList<Integer>(); 32 33 public int getExcellentCount() { 34 return excellentCount; 35 } 36 37 public int getGoodCount() { 38 return goodCount; 39 } 40 41 public int getNeedsWorkCount() { 42 return needsWorkCount; 43 } 44 45 protected final int maxExcellentCost; 46 protected final int maxAcceptableCost; 47 protected final int worstOffenderCount; 48 protected int excellentCount = 0; 49 protected int goodCount = 0; 50 protected int needsWorkCount = 0; 51 protected int worstCost = 1; 52 private final CostModel costModel; 53 54 public SummaryReportModel(CostModel costModel, int maxExcellentCost, int maxAcceptableCost, int worstOffenderCount) { 55 this.costModel = costModel; 56 this.worstOffenders = new TreeSet<ClassCost>(new ClassCost.CostComparator(costModel)); 57 this.maxExcellentCost = maxExcellentCost; 58 this.maxAcceptableCost = maxAcceptableCost; 59 this.worstOffenderCount = worstOffenderCount; 60 } 61 62 @Override 63 public void addClassCost(ClassCost classCost) { 64 int cost = costModel.computeClass(classCost); 65 if (cost < maxExcellentCost) { 66 excellentCount++; 67 } else if (cost < maxAcceptableCost) { 68 goodCount++; 69 } else { 70 needsWorkCount++; 71 } 72 costs.add(cost); 73 worstOffenders.add(classCost); 74 if (worstOffenders.size() > worstOffenderCount) { 75 worstOffenders.remove(worstOffenders.last()); 76 } 77 worstCost = Math.max(worstCost, cost); 78 weightedAverage.addValue(cost); 79 } 80 81 public int getClassCount() { 82 return costs.size(); 83 } 84 85 public int getOverall() { 86 return (int) weightedAverage.getAverage(); 87 } 88}