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 */
020 package org.sonar.api.measures;
021
022 import org.apache.commons.collections.SortedBag;
023 import org.apache.commons.collections.bag.TreeBag;
024 import org.apache.commons.lang.StringUtils;
025 import org.apache.commons.lang.math.NumberUtils;
026 import org.sonar.api.utils.KeyValueFormat;
027 import org.sonar.api.utils.SonarException;
028
029 import java.util.Map;
030
031 /**
032 * Utility to build a distribution based on discrete values
033 *
034 * <p>An example of usage : you wish to record the number of violations for each level of rules priority</p>
035 *
036 * @since 1.10
037 */
038 public class CountDistributionBuilder implements MeasureBuilder {
039
040 private Metric metric;
041 // TODO to be replaced by com.google.common.collect.SortedMultiset while upgrading to Guava 11+
042 private SortedBag countBag;
043
044 /**
045 * Creates an empty CountDistributionBuilder for a specified metric
046 *
047 * @param metric the metric
048 */
049 public CountDistributionBuilder(Metric metric) {
050 setMetric(metric);
051 this.countBag = new TreeBag();
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)) {
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 public Measure build() {
144 return build(true);
145 }
146
147 /**
148 * Used to build a measure from the current object
149 *
150 * @param allowEmptyData should be built if current object is empty
151 * @return the built measure
152 */
153 public Measure build(boolean allowEmptyData) {
154 if (!isEmpty() || allowEmptyData) {
155 //-1 is a hack to include zero values
156 return new Measure(metric, KeyValueFormat.format(countBag, -1));
157 }
158 return null;
159 }
160
161 private void setMetric(Metric metric) {
162 if (metric == null || !metric.isDataType()) {
163 throw new SonarException("Metric is null or has unvalid type");
164 }
165 this.metric = metric;
166 }
167 }