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