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 * @deprecated since 5.6. Scanner side is not responsible to aggregate measures since 5.2.
037 */
038@Deprecated
039public class CountDistributionBuilder implements MeasureBuilder {
040
041  private final Metric metric;
042  private final Multiset countBag = TreeMultiset.create();
043
044  /**
045   * Creates an empty CountDistributionBuilder for a specified metric
046   *
047   * @param metric the metric
048   */
049  public CountDistributionBuilder(Metric metric) {
050    if (metric == null || !metric.isDataType()) {
051      throw new SonarException("Metric is null or has invalid type");
052    }
053    this.metric = metric;
054  }
055
056  /**
057   * Increments an entry
058   *
059   * @param value the value that should be incremented
060   * @param count the number by which to increment
061   * @return the current object
062   */
063  public CountDistributionBuilder add(Object value, int count) {
064    if (count == 0) {
065      addZero(value);
066
067    } else {
068      if (this.countBag.add(value, count) == 0) {
069        // hack
070        this.countBag.add(value, 1);
071      }
072    }
073    return this;
074  }
075
076  /**
077   * Increments an entry by one
078   *
079   * @param value the value that should be incremented
080   * @return the current object
081   */
082  public CountDistributionBuilder add(Object value) {
083    return add(value, 1);
084  }
085
086  /**
087   * Adds an entry without a zero count if it does not exist
088   *
089   * @param value the entry to be added
090   * @return the current object
091   */
092  public CountDistributionBuilder addZero(Object value) {
093    if (!countBag.contains(value)) {
094      countBag.add(value, 1);
095    }
096    return this;
097  }
098
099  /**
100   * Adds an existing Distribution to the current one.
101   * It will create the entries if they don't exist.
102   * Can be used to add the values of children resources for example
103   *
104   * @param measure the measure to add to the current one
105   * @return the current object
106   */
107  public CountDistributionBuilder add(Measure measure) {
108    if (measure != null && measure.getData() != null) {
109      Map<String, String> map = KeyValueFormat.parse(measure.getData());
110      for (Map.Entry<String, String> entry : map.entrySet()) {
111        String key = entry.getKey();
112        int value = StringUtils.isBlank(entry.getValue()) ? 0 : Integer.parseInt(entry.getValue());
113        if (NumberUtils.isNumber(key)) {
114          add(NumberUtils.toInt(key), value);
115        } else {
116          add(key, value);
117        }
118      }
119    }
120    return this;
121  }
122
123  /**
124   * @return whether the current object is empty or not
125   */
126  public boolean isEmpty() {
127    return countBag.isEmpty();
128  }
129
130  /**
131   * Resets all entries to zero
132   *
133   * @return the current object
134   */
135  public CountDistributionBuilder clear() {
136    countBag.clear();
137    return this;
138  }
139
140  /**
141   * Shortcut for <code>build(true)</code>
142   *
143   * @return the built measure
144   */
145  @Override
146  public Measure build() {
147    return build(true);
148  }
149
150  /**
151   * Used to build a measure from the current object
152   *
153   * @param allowEmptyData should be built if current object is empty
154   * @return the built measure
155   */
156  public Measure build(boolean allowEmptyData) {
157    if (!isEmpty() || allowEmptyData) {
158      return new Measure(metric, MultisetDistributionFormat.format(countBag));
159    }
160    return null;
161  }
162}