From 90b73848134de312bb558141b31b8acc9b56b7fb Mon Sep 17 00:00:00 2001 From: Fabrice Bellingard Date: Thu, 5 Apr 2012 09:41:23 +0200 Subject: [PATCH] Fix violations --- .../measures/AverageComplexityFormula.java | 1 - .../sonar/api/measures/AverageFormula.java | 59 ++++++++++++------- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/AverageComplexityFormula.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/AverageComplexityFormula.java index 297b7062a5f..3a22d656741 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/AverageComplexityFormula.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/AverageComplexityFormula.java @@ -29,7 +29,6 @@ package org.sonar.api.measures; @Deprecated public class AverageComplexityFormula extends AverageFormula { - @Deprecated public AverageComplexityFormula(Metric byMetric) { super(CoreMetrics.COMPLEXITY, byMetric); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/AverageFormula.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/AverageFormula.java index 7ed369837bf..861c67daf0b 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/AverageFormula.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/AverageFormula.java @@ -62,31 +62,48 @@ public class AverageFormula implements Formula { if (!shouldDecorateResource(data, context)) { return null; } + + Measure result = null; if (ResourceUtils.isFile(context.getResource())) { - Double byMeasure = MeasureUtils.getValue(data.getMeasure(byMetric), null); - Double mainMeasure = MeasureUtils.getValue(data.getMeasure(mainMetric), null); - if (mainMeasure != null && byMeasure != null && byMeasure > 0.0) { - return new Measure(context.getTargetMetric(), (mainMeasure / byMeasure)); - } + result = calculateForFile(data, context); } else { - double totalByMeasure = 0; - double totalMainMeasure = 0; - boolean hasApplicableChildren = false; - - for (FormulaData childrenData : data.getChildren()) { - Double childrenByMeasure = MeasureUtils.getValue(childrenData.getMeasure(byMetric), null); - Double childrenMainMeasure = MeasureUtils.getValue(childrenData.getMeasure(mainMetric), null); - if (childrenMainMeasure != null && childrenByMeasure != null && childrenByMeasure > 0.0) { - totalByMeasure += childrenByMeasure; - totalMainMeasure += childrenMainMeasure; - hasApplicableChildren = true; - } - } - if (hasApplicableChildren) { - return new Measure(context.getTargetMetric(), (totalMainMeasure / totalByMeasure)); + result = calculateOnChildren(data, context); + } + return result; + } + + private Measure calculateOnChildren(FormulaData data, FormulaContext context) { + Measure result = null; + + double totalByMeasure = 0; + double totalMainMeasure = 0; + boolean hasApplicableChildren = false; + + for (FormulaData childrenData : data.getChildren()) { + Double childrenByMeasure = MeasureUtils.getValue(childrenData.getMeasure(byMetric), null); + Double childrenMainMeasure = MeasureUtils.getValue(childrenData.getMeasure(mainMetric), null); + if (childrenMainMeasure != null && childrenByMeasure != null && childrenByMeasure > 0.0) { + totalByMeasure += childrenByMeasure; + totalMainMeasure += childrenMainMeasure; + hasApplicableChildren = true; } } - return null; + if (hasApplicableChildren) { + result = new Measure(context.getTargetMetric(), (totalMainMeasure / totalByMeasure)); + } + return result; + } + + private Measure calculateForFile(FormulaData data, FormulaContext context) { + Measure result = null; + + Double byMeasure = MeasureUtils.getValue(data.getMeasure(byMetric), null); + Double mainMeasure = MeasureUtils.getValue(data.getMeasure(mainMetric), null); + if (mainMeasure != null && byMeasure != null && byMeasure > 0.0) { + result = new Measure(context.getTargetMetric(), (mainMeasure / byMeasure)); + } + + return result; } private boolean shouldDecorateResource(FormulaData data, FormulaContext context) { -- 2.39.5