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.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 /**
073 * Tests if a measure has a value
074 *
075 * @param measure the measure
076 * @return whether the measure has a value
077 */
078 public static boolean hasValue(Measure measure) {
079 return measure != null && measure.getValue() != null;
080 }
081
082 /**
083 * Tests if a measure has a data field
084 *
085 * @param measure the measure
086 * @return whether the measure has a data field
087 */
088 public static boolean hasData(Measure measure) {
089 return measure != null && StringUtils.isNotBlank(measure.getData());
090 }
091
092 /**
093 * Sums a series of measures
094 *
095 * @param zeroIfNone whether to return 0 or null in case measures is null
096 * @param measures the series of measures
097 * @return the sum of the measure series
098 */
099 public static Double sum(boolean zeroIfNone, Collection<Measure> measures) {
100 if (measures != null) {
101 return sum(zeroIfNone, measures.toArray(new Measure[measures.size()]));
102 }
103 return zeroIfNone(zeroIfNone);
104 }
105
106 /**
107 * Sums a series of measures
108 *
109 * @param zeroIfNone whether to return 0 or null in case measures is null
110 * @param measures the series of measures
111 * @return the sum of the measure series
112 */
113 public static Double sum(boolean zeroIfNone, Measure... measures) {
114 if (measures == null) {
115 return zeroIfNone(zeroIfNone);
116 }
117 Double sum = 0d;
118 boolean hasValue = false;
119 for (Measure measure : measures) {
120 if (measure != null && measure.getValue() != null) {
121 hasValue = true;
122 sum += measure.getValue();
123 }
124 }
125
126 if (hasValue) {
127 return sum;
128 }
129 return zeroIfNone(zeroIfNone);
130 }
131
132 private static Double zeroIfNone(boolean zeroIfNone) {
133 return zeroIfNone ? 0d : null;
134 }
135 }