PageRenderTime 18ms CodeModel.GetById 1ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://testability-explorer.googlecode.com/
Java | 102 lines | 73 code | 14 blank | 15 comment | 7 complexity | 707d4fc7aba17094fc8b934a8642bf22 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.chart;
 17
 18import static java.lang.Math.ceil;
 19import static java.lang.Math.log10;
 20import static java.lang.Math.max;
 21import static java.lang.Math.min;
 22
 23import com.google.common.base.Function;
 24import com.google.common.base.Nullable;
 25
 26public class Histogram {
 27
 28  private final int min;
 29  private final int binWidth;
 30  private final int[] bins;
 31  private final Function<Integer, Double> scalingFunction;
 32  private int maxBin;
 33  public static class Linear implements Function<Integer, Double> {
 34    public Double apply(@Nullable Integer integer) {
 35      return Double.valueOf(integer);
 36    }
 37  }
 38  public static class Logarithmic implements Function<Integer, Double> {
 39    public Double apply(@Nullable Integer integer) {
 40      return log10(integer) + 1;
 41    }
 42  }
 43
 44  public Histogram(int min, int binWidth, int binCount, Function<Integer, Double> scalingFunction) {
 45    this.min = min;
 46    this.binWidth = binWidth;
 47    this.scalingFunction = scalingFunction;
 48    this.bins = new int[binCount];
 49  }
 50
 51  public void value(int value) {
 52    maxBin = max(maxBin, ++bins[Math.min(bins.length -1, bin(value))]);
 53  }
 54
 55  private int bin(int value) {
 56    if (binWidth == 0) {
 57      return 0;
 58    }
 59    return ((value - min) / binWidth);
 60  }
 61
 62  public int[] getBins() {
 63    return bins;
 64  }
 65
 66  public int getMaxBin() {
 67    return maxBin;
 68  }
 69
 70  public void setMaxBin(int maxBin) {
 71    this.maxBin = maxBin;
 72  }
 73
 74  public int[] getBinRange() {
 75    return getScaledBinRange(0, Integer.MAX_VALUE, maxBin);
 76  }
 77
 78  public int[] getScaledBinRange(int from, int to, int maxScale) {
 79    double scale = (double) maxScale / scalingFunction.apply(maxBin);
 80    int[] scaledBins = new int[bins.length];
 81    int firstBin = max(0, bin(from));
 82    int lastBin = min(bins.length, bin(to));
 83    for (int binNumber = firstBin; binNumber < lastBin; binNumber++) {
 84      scaledBins[binNumber] = (int) ceil(scale * scalingFunction.apply(bins[binNumber]));
 85    }
 86    return scaledBins;
 87  }
 88
 89  public String[] getBinLabels(int maxLabels) {
 90    String[] labels = new String[bins.length];
 91    int everyN = (int) Math.ceil((float) bins.length / maxLabels);
 92    for (int i = 0; i < bins.length; i++) {
 93      if (i % everyN == 0) {
 94        labels[i] = Integer.toString(i * binWidth + binWidth / 2);
 95      } else {
 96        labels[i] = "";
 97      }
 98    }
 99    return labels;
100  }
101
102}