Package ch.hortis.sonar.core.service

Source Code of ch.hortis.sonar.core.service.AvgCalculator

/*
* 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 ch.hortis.sonar.model.File;
import ch.hortis.sonar.model.MetricMeasure;
import ch.hortis.sonar.model.Metrics;
import ch.hortis.sonar.service.MeasureKey;

import java.util.List;

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 ) {
    avgModule( module, null );
  }
 
  private void executeFileMeasures( Module module ) {
    for ( File file : module.getFiles() ) {
      avgModule( module, file );
    }
  }
 
  private void avgModule( Module module, File file ) {
    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();
        module.createMeasure(targetMetricKey, avgValue);
      } else if (avgMetricMeasure != null && !isNotZero(avgMetricMeasure.getValue())) {
        MeasureKey missingMeasureKey = avgTargetMeasure == null ? avgTargetMetricKey : avgMetricKey;
        String message = "Unable to create avg metric " + targetMetricKey.getMetric() + " for project " +
        module.getMavenProject().getArtifactId() + " missing metric " + missingMeasureKey.getMetric();
        if ( missingMeasureKey.getFile() != null ) {
          message += " for file " + missingMeasureKey.getFile();
        }
        log.info(message);
      }
    }
  }

  private static final double BETA = 0.00000000001;
  private boolean isNotZero(Double d) {
    return (d <= -BETA) || (d>= BETA);
  }
}
TOP

Related Classes of ch.hortis.sonar.core.service.AvgCalculator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.