001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 SonarSource
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.lang.StringUtils;
023    
024    import java.util.Collection;
025    
026    /**
027     * An utility class to manipulate measures
028     *
029     * @since 1.10
030     */
031    public final class MeasureUtils {
032    
033      /**
034       * Class cannot be instantiated, it should only be access through static methods
035       */
036      private MeasureUtils() {
037      }
038    
039      /**
040       * Return true if all measures have numeric value
041       *
042       * @param measures the measures
043       * @return true if all measures numeric values
044       */
045      public static boolean haveValues(Measure... measures) {
046        if (measures == null || measures.length == 0) {
047          return false;
048        }
049        for (Measure measure : measures) {
050          if (!hasValue(measure)) {
051            return false;
052          }
053        }
054        return true;
055      }
056    
057      /**
058       * Get the value of a measure, or alternatively a default value
059       *
060       * @param measure      the measure
061       * @param defaultValue the default value
062       * @return <code>defaultValue</code> if measure is null or has no values.
063       */
064    
065      public static Double getValue(Measure measure, Double defaultValue) {
066        if (MeasureUtils.hasValue(measure)) {
067          return measure.getValue();
068        }
069        return defaultValue;
070      }
071    
072      public static Long getValueAsLong(Measure measure, Long defaultValue) {
073        if (MeasureUtils.hasValue(measure)) {
074          return measure.getValue().longValue();
075        }
076        return defaultValue;
077      }
078    
079    
080      public static Double getVariation(Measure measure, int periodIndex) {
081        return getVariation(measure, periodIndex, null);
082      }
083    
084      public static Double getVariation(Measure measure, int periodIndex, Double defaultValue) {
085        Double result = null;
086        if (measure != null) {
087          result = measure.getVariation(periodIndex);
088        }
089        return (result != null ? result : defaultValue);
090      }
091    
092      public static Long getVariationAsLong(Measure measure, int periodIndex) {
093        return getVariationAsLong(measure, periodIndex, null);
094      }
095    
096      public static Long getVariationAsLong(Measure measure, int periodIndex, Long defaultValue) {
097        Double result = null;
098        if (measure != null) {
099          result = measure.getVariation(periodIndex);
100        }
101        return result == null ? defaultValue : Long.valueOf(result.longValue());
102      }
103    
104      /**
105       * Tests if a measure has a value
106       *
107       * @param measure the measure
108       * @return whether the measure has a value
109       */
110      public static boolean hasValue(Measure measure) {
111        return measure != null && measure.getValue() != null;
112      }
113    
114      /**
115       * Tests if a measure has a data field
116       *
117       * @param measure the measure
118       * @return whether the measure has a data field
119       */
120      public static boolean hasData(Measure measure) {
121        return measure != null && StringUtils.isNotBlank(measure.getData());
122      }
123    
124      /**
125       * Sums a series of measures
126       *
127       * @param zeroIfNone whether to return 0 or null in case measures is null
128       * @param measures   the series of measures
129       * @return the sum of the measure series
130       */
131      public static Double sum(boolean zeroIfNone, Collection<Measure> measures) {
132        if (measures != null) {
133          return sum(zeroIfNone, measures.toArray(new Measure[measures.size()]));
134        }
135        return zeroIfNone(zeroIfNone);
136      }
137    
138      /**
139       * Sums a series of measures
140       *
141       * @param zeroIfNone whether to return 0 or null in case measures is null
142       * @param measures   the series of measures
143       * @return the sum of the measure series
144       */
145      public static Double sum(boolean zeroIfNone, Measure... measures) {
146        if (measures == null) {
147          return zeroIfNone(zeroIfNone);
148        }
149        Double sum = 0d;
150        boolean hasValue = false;
151        for (Measure measure : measures) {
152          if (measure != null && measure.getValue() != null) {
153            hasValue = true;
154            sum += measure.getValue();
155          }
156        }
157    
158        if (hasValue) {
159          return sum;
160        }
161        return zeroIfNone(zeroIfNone);
162      }
163    
164      private static Double zeroIfNone(boolean zeroIfNone) {
165        return zeroIfNone ? 0d : null;
166      }
167    }