001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar 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     * Sonar 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
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
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      private SortedBag countBag;
042    
043      /**
044       * Creates an empty CountDistributionBuilder for a specified metric
045       *
046       * @param metric the metric
047       */
048      public CountDistributionBuilder(Metric metric) {
049        setMetric(metric);
050        this.countBag = new TreeBag();
051      }
052    
053      /**
054       * Increments an entry
055       *
056       * @param value the value that should be incremented
057       * @param count the number by which to increment
058       * @return the current object
059       */
060      public CountDistributionBuilder add(Object value, int count) {
061        if (count == 0) {
062          addZero(value);
063    
064        } else {
065          if (this.countBag.add(value, count)) {
066            this.countBag.add(value, 1);//hack
067          }
068        }
069        return this;
070      }
071    
072      /**
073       * Increments an entry by one
074       *
075       * @param value the value that should be incremented
076       * @return the current object
077       */
078      public CountDistributionBuilder add(Object value) {
079        return add(value, 1);
080      }
081    
082      /**
083       * Adds an entry without a zero count if it does not exist
084       *
085       * @param value the entry to be added
086       * @return the current object
087       */
088      public CountDistributionBuilder addZero(Object value) {
089        if (!countBag.contains(value)) {
090          countBag.add(value, 1);
091        }
092        return this;
093      }
094    
095      /**
096       * Adds an existing Distribution to the current one.
097       * It will create the entries if they don't exist.
098       * Can be used to add the values of children resources for example
099       *
100       * @param measure the measure to add to the current one
101       * @return the current object
102       */
103      public CountDistributionBuilder add(Measure measure) {
104        if (measure != null && measure.getData() != null) {
105          Map<String, String> map = KeyValueFormat.parse(measure.getData());
106          for (Map.Entry<String, String> entry : map.entrySet()) {
107            String key = entry.getKey();
108            int value = (StringUtils.isBlank(entry.getValue()) ? 0 : Integer.parseInt(entry.getValue()));
109            if (NumberUtils.isNumber(key)) {
110              add(NumberUtils.toInt(key), value);
111            } else {
112              add(key, value);
113            }
114          }
115        }
116        return this;
117      }
118    
119      /**
120       * @return whether the current object is empty or not
121       */
122      public boolean isEmpty() {
123        return countBag.isEmpty();
124      }
125    
126      /**
127       * Resets all entries to zero
128       *
129       * @return the current object
130       */
131      public CountDistributionBuilder clear() {
132        countBag.clear();
133        return this;
134      }
135    
136      /**
137       * Shortcut for <code>build(true)</code>
138       *
139       * @return the built measure
140       */
141      public Measure build() {
142        return build(true);
143      }
144    
145      /**
146       * Used to build a measure from the current object
147       *
148       * @param allowEmptyData should be built if current object is empty
149       * @return the built measure
150       */
151      public Measure build(boolean allowEmptyData) {
152        if (!isEmpty() || allowEmptyData) {
153          return new Measure(metric, KeyValueFormat.format(countBag, -1)); //-1 is a hack to include zero values
154        }
155        return null;
156      }
157    
158      private void setMetric(Metric metric) {
159        if (metric == null || !metric.isDataType()) {
160          throw new SonarException("Metric is null or has unvalid type");
161        }
162        this.metric = metric;
163      }
164    }