org.sonar.api.measures.MeasureUtils Maven / Gradle / Ivy
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2009 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* Sonar is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* Sonar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.api.measures;
import org.apache.commons.lang.StringUtils;
import java.util.Collection;
/**
* @since 1.10
*/
public final class MeasureUtils {
private MeasureUtils() {
}
/**
* Return true if all measures have numeric value.
*/
public static boolean haveValues(Measure... measures) {
if (measures == null || measures.length == 0) {
return false;
}
for (Measure measure : measures) {
if (!hasValue(measure)) {
return false;
}
}
return true;
}
/**
* Get the measure value. Return defaultValue
if measure is null or has no values.
*/
public static Double getValue(Measure measure, Double defaultValue) {
if (MeasureUtils.hasValue(measure)) {
return measure.getValue();
}
return defaultValue;
}
public static boolean hasValue(Measure measure) {
return measure != null && measure.getValue() != null;
}
public static boolean hasData(Measure measure) {
return measure != null && StringUtils.isNotBlank(measure.getData());
}
public static Double sum(boolean zeroIfNone, Collection measures) {
if (measures != null) {
return sum(zeroIfNone, measures.toArray(new Measure[measures.size()]));
}
return zeroIfNone(zeroIfNone);
}
public static Double sum(boolean zeroIfNone, Measure... measures) {
if (measures == null) {
return zeroIfNone(zeroIfNone);
}
Double sum = 0d;
boolean hasValue = false;
for (Measure measure : measures) {
if (measure != null && measure.getValue() != null) {
hasValue = true;
sum += measure.getValue();
}
}
if (hasValue) {
return sum;
}
return zeroIfNone(zeroIfNone);
}
private static Double zeroIfNone(boolean zeroIfNone) {
return zeroIfNone ? 0d : null;
}
}