001/* 002 * SonarQube 003 * Copyright (C) 2009-2016 SonarSource SA 004 * mailto:contact AT sonarsource DOT com 005 * 006 * This program 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 * This program 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 com.google.common.collect.Multiset; 023import com.google.common.collect.TreeMultiset; 024import java.util.Map; 025import org.apache.commons.lang.StringUtils; 026import org.apache.commons.lang.math.NumberUtils; 027import org.sonar.api.utils.KeyValueFormat; 028import org.sonar.api.utils.SonarException; 029 030/** 031 * Utility to build a distribution based on discrete values 032 * 033 * <p>An example of usage : you wish to record the number of violations for each level of rules priority 034 * 035 * @since 1.10 036 */ 037public class CountDistributionBuilder implements MeasureBuilder { 038 039 private final Metric metric; 040 private final Multiset countBag = TreeMultiset.create(); 041 042 /** 043 * Creates an empty CountDistributionBuilder for a specified metric 044 * 045 * @param metric the metric 046 */ 047 public CountDistributionBuilder(Metric metric) { 048 if (metric == null || !metric.isDataType()) { 049 throw new SonarException("Metric is null or has invalid type"); 050 } 051 this.metric = metric; 052 } 053 054 /** 055 * Increments an entry 056 * 057 * @param value the value that should be incremented 058 * @param count the number by which to increment 059 * @return the current object 060 */ 061 public CountDistributionBuilder add(Object value, int count) { 062 if (count == 0) { 063 addZero(value); 064 065 } else { 066 if (this.countBag.add(value, count) == 0) { 067 // hack 068 this.countBag.add(value, 1); 069 } 070 } 071 return this; 072 } 073 074 /** 075 * Increments an entry by one 076 * 077 * @param value the value that should be incremented 078 * @return the current object 079 */ 080 public CountDistributionBuilder add(Object value) { 081 return add(value, 1); 082 } 083 084 /** 085 * Adds an entry without a zero count if it does not exist 086 * 087 * @param value the entry to be added 088 * @return the current object 089 */ 090 public CountDistributionBuilder addZero(Object value) { 091 if (!countBag.contains(value)) { 092 countBag.add(value, 1); 093 } 094 return this; 095 } 096 097 /** 098 * Adds an existing Distribution to the current one. 099 * It will create the entries if they don't exist. 100 * Can be used to add the values of children resources for example 101 * 102 * @param measure the measure to add to the current one 103 * @return the current object 104 */ 105 public CountDistributionBuilder add(Measure measure) { 106 if (measure != null && measure.getData() != null) { 107 Map<String, String> map = KeyValueFormat.parse(measure.getData()); 108 for (Map.Entry<String, String> entry : map.entrySet()) { 109 String key = entry.getKey(); 110 int value = StringUtils.isBlank(entry.getValue()) ? 0 : Integer.parseInt(entry.getValue()); 111 if (NumberUtils.isNumber(key)) { 112 add(NumberUtils.toInt(key), value); 113 } else { 114 add(key, value); 115 } 116 } 117 } 118 return this; 119 } 120 121 /** 122 * @return whether the current object is empty or not 123 */ 124 public boolean isEmpty() { 125 return countBag.isEmpty(); 126 } 127 128 /** 129 * Resets all entries to zero 130 * 131 * @return the current object 132 */ 133 public CountDistributionBuilder clear() { 134 countBag.clear(); 135 return this; 136 } 137 138 /** 139 * Shortcut for <code>build(true)</code> 140 * 141 * @return the built measure 142 */ 143 @Override 144 public Measure build() { 145 return build(true); 146 } 147 148 /** 149 * Used to build a measure from the current object 150 * 151 * @param allowEmptyData should be built if current object is empty 152 * @return the built measure 153 */ 154 public Measure build(boolean allowEmptyData) { 155 if (!isEmpty() || allowEmptyData) { 156 return new Measure(metric, MultisetDistributionFormat.format(countBag)); 157 } 158 return null; 159 } 160}