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 021 package org.sonar.api.measures; 022 023 import org.sonar.api.resources.ResourceUtils; 024 025 import java.util.List; 026 import java.util.Arrays; 027 028 /** 029 * @since 2.1 030 */ 031 public class AverageComplexityFormula implements Formula { 032 033 private Metric byMetric; 034 035 /** 036 * @param byMetric The metric on which average complexity should be calculated : complexity by file, by method... 037 */ 038 public AverageComplexityFormula(Metric byMetric) { 039 this.byMetric = byMetric; 040 } 041 042 public List<Metric> dependsUponMetrics() { 043 return Arrays.asList(CoreMetrics.COMPLEXITY, byMetric); 044 } 045 046 public Measure calculate(FormulaData data, FormulaContext context) { 047 if (!shouldDecorateResource(data, context)) { 048 return null; 049 } 050 if (ResourceUtils.isFile(context.getResource())) { 051 Double byMeasure = MeasureUtils.getValue(data.getMeasure(byMetric), null); 052 Double complexity = MeasureUtils.getValue(data.getMeasure(CoreMetrics.COMPLEXITY), null); 053 if (complexity != null && byMeasure != null && byMeasure > 0.0) { 054 return new Measure(context.getTargetMetric(), (complexity / byMeasure)); 055 } 056 } else { 057 double totalByMeasure = 0; 058 double totalComplexity = 0; 059 boolean hasApplicableChildren = false; 060 061 for (FormulaData childrenData : data.getChildren()) { 062 Double childrenByMeasure = MeasureUtils.getValue(childrenData.getMeasure(byMetric), null); 063 Double childrenComplexity = MeasureUtils.getValue(childrenData.getMeasure(CoreMetrics.COMPLEXITY), null); 064 if (childrenComplexity != null && childrenByMeasure != null && childrenByMeasure > 0.0) { 065 totalByMeasure += childrenByMeasure; 066 totalComplexity += childrenComplexity; 067 hasApplicableChildren = true; 068 } 069 } 070 if (hasApplicableChildren) { 071 return new Measure(context.getTargetMetric(), (totalComplexity / totalByMeasure)); 072 } 073 } 074 return null; 075 } 076 077 private boolean shouldDecorateResource(FormulaData data, FormulaContext context) { 078 return !MeasureUtils.hasValue(data.getMeasure(context.getTargetMetric())); 079 } 080 }