PageRenderTime 13ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://testability-explorer.googlecode.com/
Java | 159 lines | 127 code | 17 blank | 15 comment | 13 complexity | a65cc7a355d80694d52991a1c0c96ee6 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 static com.google.test.metric.report.chart.GoogleChartAPI.GREEN;
 19import static com.google.test.metric.report.chart.GoogleChartAPI.RED;
 20import static com.google.test.metric.report.chart.GoogleChartAPI.YELLOW;
 21import static java.lang.Integer.MAX_VALUE;
 22import static java.lang.Math.ceil;
 23import static java.lang.Math.log;
 24import static java.lang.Math.max;
 25import static java.lang.Math.min;
 26
 27import java.util.List;
 28
 29import com.google.common.base.Function;
 30import com.google.test.metric.report.chart.GoodnessChart;
 31import com.google.test.metric.report.chart.Histogram;
 32import com.google.test.metric.report.chart.HistogramChartUrl;
 33import com.google.test.metric.report.chart.PieChartUrl;
 34import com.google.test.metric.report.chart.Histogram.Linear;
 35
 36public class GradeCategories {
 37  private static final int MAX_HISTOGRAM_BINS = 30;
 38  private static final int HISTOGRAM_LEGEND_WIDTH = 130;
 39  private final int maxExcellentCost;
 40  private final int maxAcceptableCost;
 41
 42  public GradeCategories(int maxExcellentCost, int maxAcceptableCost) {
 43    this.maxExcellentCost = maxExcellentCost;
 44    this.maxAcceptableCost = maxAcceptableCost;
 45  }
 46
 47  public int getExcellentCount(List<Integer> costs) {
 48    int count = 0;
 49    for (int cost : costs) {
 50      if (cost <= maxExcellentCost) {
 51        count++;
 52      }
 53    }
 54    return count;
 55  }
 56
 57  public int getGoodCount(List<Integer> costs) {
 58    int count = 0;
 59    for (int cost : costs) {
 60      if (cost > maxExcellentCost && cost <= maxAcceptableCost) {
 61        count++;
 62      }
 63    }
 64    return count;
 65  }
 66
 67  public int getNeedsWorkCount(List<Integer> costs) {
 68    int count = 0;
 69    for (int cost : costs) {
 70      if (cost > maxAcceptableCost) {
 71        count++;
 72      }
 73    }
 74    return count;
 75  }
 76
 77  public GoodnessChart createOverallChart(int value) {
 78    GoodnessChart chart = new GoodnessChart(maxExcellentCost, maxAcceptableCost,
 79        10 * maxAcceptableCost, 100 * maxAcceptableCost);
 80    chart.setUnscaledValues(value);
 81    return chart;
 82  }
 83
 84  public PieChartUrl createDistributionChart(List<Integer> costs) {
 85    PieChartUrl chart = new PieChartUrl();
 86    chart.setItemLabel("Excellent", "Good", "Needs Work");
 87    chart.setColors(GREEN, YELLOW, RED);
 88    int excellentCount = getExcellentCount(costs);
 89    int goodCount = getGoodCount(costs);
 90    int needsWorkCount = getNeedsWorkCount(costs);
 91    chart.setValues(excellentCount, goodCount, needsWorkCount);
 92    return chart;
 93  }
 94
 95  public HistogramChartUrl createHistogram(int width, int height, List<Integer> costs) {
 96    return createHistogram(width,  height,  costs, new Linear());
 97  }
 98
 99  public HistogramChartUrl createHistogram(int width, int height, List<Integer> costs,
100                                           Function<Integer, Double> scalingFunction) {
101    int maxScale = 61;
102    MultiHistogramDataModel model = buildHistogramDataModel(costs, scalingFunction);
103    int[] excellent = model.getExcellent().getScaledBinRange(0, MAX_VALUE, maxScale);
104    int[] good = model.getGood().getScaledBinRange(0, MAX_VALUE, maxScale);
105    int[] needsWork = model.getNeedsWork().getScaledBinRange(0, MAX_VALUE, maxScale);
106    HistogramChartUrl chart = new HistogramChartUrl();
107    chart.setItemLabel(model.getOverallHistogram().getBinLabels(20));
108    chart.setValues(excellent, good, needsWork);
109    chart.setYMark(0, model.getOverallHistogram().getMaxBin(), scalingFunction);
110    chart.setSize(width, height);
111    chart.setBarWidth((width - HISTOGRAM_LEGEND_WIDTH) / model.getBinCount(), 0, 0);
112    chart.setChartLabel("Excellent", "Good", "Needs Work");
113    chart.setColors(GREEN, YELLOW, RED);
114    return chart;
115  }
116
117  public MultiHistogramDataModel buildHistogramDataModel(List<Integer> costs,
118      Function<Integer, Double> scalingFunction) {
119    int binCount = min(MAX_HISTOGRAM_BINS, 10 * (int) log(costs.size()) + 1);
120    int binWidth = (int) ceil((double) findMax(costs) / binCount);
121    Histogram overallHistogram = new Histogram(0, binWidth, binCount, scalingFunction);
122    Histogram excellentHistogram = new Histogram(0, binWidth, binCount, scalingFunction);
123    Histogram goodHistogram = new Histogram(0, binWidth, binCount, scalingFunction);
124    Histogram needsWorkHistogram = new Histogram(0, binWidth, binCount, scalingFunction);
125    for (int overallCost : costs) {
126      if (overallCost <= maxExcellentCost) {
127        excellentHistogram.value(overallCost);
128      } else if (overallCost <= maxAcceptableCost) {
129        goodHistogram.value(overallCost);
130      } else {
131        needsWorkHistogram.value(overallCost);
132      }
133      overallHistogram.value(overallCost);
134    }
135    int maxBin = overallHistogram.getMaxBin();
136    excellentHistogram.setMaxBin(maxBin);
137    goodHistogram.setMaxBin(maxBin);
138    needsWorkHistogram.setMaxBin(maxBin);
139
140    return new MultiHistogramDataModel(excellentHistogram, goodHistogram, needsWorkHistogram,
141        overallHistogram, binCount, binWidth);
142  }
143
144  private int findMax(List<Integer> values) {
145    int maxValue = Integer.MIN_VALUE;
146    for (int value : values) {
147      maxValue = max(maxValue, value);
148    }
149    return maxValue;
150  }
151
152  public int getMaxExcellentCost() {
153    return maxExcellentCost;
154  }
155
156  public int getMaxAcceptableCost() {
157    return maxAcceptableCost;
158  }
159}