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 java.util.Map;
023import org.apache.commons.collections.SortedBag;
024import org.apache.commons.collections.bag.TreeBag;
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</p>
034 *
035 * @since 1.10
036 */
037public class CountDistributionBuilder implements MeasureBuilder {
038
039  private Metric metric;
040  // TODO to be replaced by com.google.common.collect.SortedMultiset while upgrading to Guava 11+
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        //hack
067        this.countBag.add(value, 1);
068      }
069    }
070    return this;
071  }
072
073  /**
074   * Increments an entry by one
075   *
076   * @param value the value that should be incremented
077   * @return the current object
078   */
079  public CountDistributionBuilder add(Object value) {
080    return add(value, 1);
081  }
082
083  /**
084   * Adds an entry without a zero count if it does not exist
085   *
086   * @param value the entry to be added
087   * @return the current object
088   */
089  public CountDistributionBuilder addZero(Object value) {
090    if (!countBag.contains(value)) {
091      countBag.add(value, 1);
092    }
093    return this;
094  }
095
096  /**
097   * Adds an existing Distribution to the current one.
098   * It will create the entries if they don't exist.
099   * Can be used to add the values of children resources for example
100   *
101   * @param measure the measure to add to the current one
102   * @return the current object
103   */
104  public CountDistributionBuilder add(Measure measure) {
105    if (measure != null && measure.getData() != null) {
106      Map<String, String> map = KeyValueFormat.parse(measure.getData());
107      for (Map.Entry<String, String> entry : map.entrySet()) {
108        String key = entry.getKey();
109        int value = StringUtils.isBlank(entry.getValue()) ? 0 : Integer.parseInt(entry.getValue());
110        if (NumberUtils.isNumber(key)) {
111          add(NumberUtils.toInt(key), value);
112        } else {
113          add(key, value);
114        }
115      }
116    }
117    return this;
118  }
119
120  /**
121   * @return whether the current object is empty or not
122   */
123  public boolean isEmpty() {
124    return countBag.isEmpty();
125  }
126
127  /**
128   * Resets all entries to zero
129   *
130   * @return the current object
131   */
132  public CountDistributionBuilder clear() {
133    countBag.clear();
134    return this;
135  }
136
137  /**
138   * Shortcut for <code>build(true)</code>
139   *
140   * @return the built measure
141   */
142  @Override
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}