/*
* This program is copyright (c) 2007 Hortis-GRC SA.
*
* This file is part of Sonar.
* Sonar is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package ch.hortis.sonar.core.service;
import java.util.List;
import ch.hortis.sonar.model.File;
import ch.hortis.sonar.model.MetricMeasure;
import ch.hortis.sonar.model.Metrics;
import ch.hortis.sonar.service.MeasureKey;
public class AvgCalculator extends AbstractMetricCalculator {
private Metrics targetMetric;
private Metrics averageTargetMetric;
private Metrics averageMetric;
private boolean alsoOnFiles;
public AvgCalculator(Metrics targetMetric, Metrics averageTargetMetric, Metrics averageMetric) {
this(targetMetric,averageTargetMetric,averageMetric,true);
}
public AvgCalculator(Metrics targetMetric, Metrics averageTargetMetric, Metrics averageMetric, boolean alsoOnFiles) {
this.targetMetric = targetMetric;
this.averageTargetMetric = averageTargetMetric;
this.averageMetric = averageMetric;
this.alsoOnFiles = alsoOnFiles;
}
public Metrics getMetricKey() {
return targetMetric;
}
public void execute(Module module, List<Module> directSubmodules) {
executeProjectMeasures(module);
if (alsoOnFiles) {
executeFileMeasures(module);
}
}
private void executeProjectMeasures( Module module ) {
MeasureKey targetMetricKey = new MeasureKey(getMetric());
MeasureKey avgTargetMetricKey = new MeasureKey(getMetric(averageTargetMetric));
MeasureKey avgMetricKey = new MeasureKey(getMetric(averageMetric));
avgMeasure( module, targetMetricKey, avgTargetMetricKey, avgMetricKey );
}
private void executeFileMeasures( Module module ) {
for ( File file : module.getFiles() ) {
MeasureKey targetMetricKey = new MeasureKey(getMetric(), null, null, file );
MeasureKey avgTargetMetricKey = new MeasureKey(getMetric(averageTargetMetric), null, null, file);
MeasureKey avgMetricKey = new MeasureKey(getMetric(averageMetric), null, null, file);
avgMeasure( module, targetMetricKey, avgTargetMetricKey, avgMetricKey );
}
}
private void avgMeasure(Module module, MeasureKey targetMetricKey, MeasureKey avgTargetMetricKey, MeasureKey avgMetricKey) {
if (module.getMeasure(targetMetricKey)==null) {
MetricMeasure avgTargetMeasure = module.getMeasure( avgTargetMetricKey );
MetricMeasure avgMetricMeasure = module.getMeasure( avgMetricKey );
if ( avgTargetMeasure != null && avgMetricMeasure != null && isNotZero(avgMetricMeasure.getValue())) {
Double avgValue = avgTargetMeasure.getValue() / avgMetricMeasure.getValue();
if (log.isDebugEnabled()) log.debug( module.getMavenProject().getArtifactId() + " create average " + targetMetricKey.getMetric() + " : " + avgValue );
module.createMeasure(targetMetricKey, avgValue);
} else {
MeasureKey missingMeasureKey = avgTargetMeasure == null ? avgTargetMetricKey : avgMetricKey;
log.warn( "Unable to create avg metric " + targetMetricKey.getMetric() + " for project " +
module.getMavenProject().getArtifactId() + " missing metric " + missingMeasureKey.getMetric());
}
}
}
private static final double BETA = 0.00000000001;
private boolean isNotZero(Double d) {
return (d <= -BETA) || (d>= BETA);
}
}