001/* 002 * SonarQube, open source software quality management tool. 003 * Copyright (C) 2008-2014 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * SonarQube is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * SonarQube is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public License 017 * along with this program; if not, write to the Free Software Foundation, 018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 019 */ 020package org.sonar.api.measures; 021 022import org.apache.commons.collections.SortedBag; 023import org.apache.commons.collections.Transformer; 024import org.apache.commons.collections.bag.TransformedSortedBag; 025import org.apache.commons.collections.bag.TreeBag; 026import org.apache.commons.lang.NumberUtils; 027import org.sonar.api.utils.KeyValueFormat; 028import org.sonar.api.utils.SonarException; 029 030import javax.annotation.Nullable; 031 032import java.util.Arrays; 033import java.util.Collections; 034import java.util.Map; 035import java.util.Set; 036 037/** 038 * Utility to build a distribution based on defined ranges 039 * <p/> 040 * <p>An example of usage : you wish to record the percentage of lines of code that belong to method 041 * with pre-defined ranges of complexity.</p> 042 * 043 * @since 1.10 044 */ 045public class RangeDistributionBuilder implements MeasureBuilder { 046 047 private Metric<String> metric; 048 private SortedBag countBag; 049 private boolean isEmpty = true; 050 private Number[] bottomLimits; 051 private boolean isValid = true; 052 053 /** 054 * RangeDistributionBuilder for a metric and a defined range 055 * Each entry is initialized at zero 056 * 057 * @param metric the metric to record the measure against 058 * @param bottomLimits the bottom limits of ranges to be used 059 */ 060 public RangeDistributionBuilder(Metric<String> metric, Number[] bottomLimits) { 061 setMetric(metric); 062 init(bottomLimits); 063 } 064 065 private void init(Number[] bottomLimits) { 066 this.bottomLimits = new Number[bottomLimits.length]; 067 System.arraycopy(bottomLimits, 0, this.bottomLimits, 0, this.bottomLimits.length); 068 Arrays.sort(this.bottomLimits); 069 changeDoublesToInts(); 070 countBag = TransformedSortedBag.decorate(new TreeBag(), new RangeTransformer()); 071 doClear(); 072 } 073 074 private void changeDoublesToInts() { 075 boolean onlyInts = true; 076 for (Number bottomLimit : bottomLimits) { 077 if (NumberUtils.compare(bottomLimit.intValue(), bottomLimit.doubleValue()) != 0) { 078 onlyInts = false; 079 } 080 } 081 if (onlyInts) { 082 for (int i = 0; i < bottomLimits.length; i++) { 083 bottomLimits[i] = bottomLimits[i].intValue(); 084 } 085 } 086 } 087 088 public RangeDistributionBuilder(Metric<String> metric) { 089 this.metric = metric; 090 } 091 092 /** 093 * Gives the bottom limits of ranges used 094 * 095 * @return the bottom limits of defined range for the distribution 096 */ 097 public Number[] getBottomLimits() { 098 return bottomLimits; 099 } 100 101 /** 102 * Increments an entry by 1 103 * 104 * @param value the value to use to pick the entry to increment 105 * @return the current object 106 */ 107 public RangeDistributionBuilder add(Number value) { 108 return add(value, 1); 109 } 110 111 /** 112 * Increments an entry 113 * 114 * @param value the value to use to pick the entry to increment 115 * @param count the number by which to increment 116 * @return the current object 117 */ 118 public RangeDistributionBuilder add(Number value, int count) { 119 if (value != null && greaterOrEqualsThan(value, bottomLimits[0])) { 120 this.countBag.add(value, count); 121 isEmpty = false; 122 } 123 return this; 124 } 125 126 private RangeDistributionBuilder addLimitCount(Number limit, int count) { 127 for (Number bottomLimit : bottomLimits) { 128 if (NumberUtils.compare(bottomLimit.doubleValue(), limit.doubleValue()) == 0) { 129 this.countBag.add(limit, count); 130 isEmpty = false; 131 return this; 132 } 133 } 134 isValid = false; 135 return this; 136 } 137 138 /** 139 * Adds an existing Distribution to the current one. 140 * It will create the entries if they don't exist. 141 * Can be used to add the values of children resources for example 142 * <p/> 143 * Since 2.2, the distribution returned will be invalidated in case the 144 * measure given does not use the same bottom limits 145 * 146 * @param measure the measure to add to the current one 147 * @return the current object 148 */ 149 public RangeDistributionBuilder add(@Nullable Measure<String> measure) { 150 if (measure != null && measure.getData() != null) { 151 Map<Double, Double> map = KeyValueFormat.parse(measure.getData(), KeyValueFormat.newDoubleConverter(), KeyValueFormat.newDoubleConverter()); 152 Number[] limits = map.keySet().toArray(new Number[map.size()]); 153 if (bottomLimits == null) { 154 init(limits); 155 156 } else if (!areSameLimits(bottomLimits, map.keySet())) { 157 isValid = false; 158 } 159 160 if (isValid) { 161 for (Map.Entry<Double, Double> entry : map.entrySet()) { 162 addLimitCount(entry.getKey(), entry.getValue().intValue()); 163 } 164 } 165 } 166 return this; 167 } 168 169 private boolean areSameLimits(Number[] bottomLimits, Set<Double> limits) { 170 if (limits.size() == bottomLimits.length) { 171 for (Number l : bottomLimits) { 172 if (!limits.contains(l.doubleValue())) { 173 return false; 174 } 175 } 176 return true; 177 } 178 return false; 179 } 180 181 /** 182 * Resets all entries to zero 183 * 184 * @return the current object 185 */ 186 public RangeDistributionBuilder clear() { 187 doClear(); 188 return this; 189 } 190 191 private void doClear() { 192 if (countBag != null) { 193 countBag.clear(); 194 } 195 if (bottomLimits != null) { 196 Collections.addAll(countBag, bottomLimits); 197 } 198 isEmpty = true; 199 } 200 201 /** 202 * @return whether the current object is empty or not 203 */ 204 public boolean isEmpty() { 205 return isEmpty; 206 } 207 208 /** 209 * Shortcut for <code>build(true)</code> 210 * 211 * @return the built measure 212 */ 213 public Measure<String> build() { 214 return build(true); 215 } 216 217 /** 218 * Used to build a measure from the current object 219 * 220 * @param allowEmptyData should be built if current object is empty 221 * @return the built measure 222 */ 223 public Measure<String> build(boolean allowEmptyData) { 224 if (isValid && (!isEmpty || allowEmptyData)) { 225 return new Measure<String>(metric, KeyValueFormat.format(countBag, -1)); 226 } 227 return null; 228 } 229 230 private class RangeTransformer implements Transformer { 231 public Object transform(Object o) { 232 Number n = (Number) o; 233 for (int i = bottomLimits.length - 1; i >= 0; i--) { 234 if (greaterOrEqualsThan(n, bottomLimits[i])) { 235 return bottomLimits[i]; 236 } 237 } 238 return null; 239 } 240 } 241 242 private static boolean greaterOrEqualsThan(Number n1, Number n2) { 243 return NumberUtils.compare(n1.doubleValue(), n2.doubleValue()) >= 0; 244 } 245 246 private void setMetric(Metric metric) { 247 if (metric == null || !metric.isDataType()) { 248 throw new SonarException("Metric is null or has unvalid type"); 249 } 250 this.metric = metric; 251 } 252}