001/* 002 * SonarQube, open source software quality management tool. 003 * Copyright (C) 2008-2014 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * SonarQube 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 * SonarQube 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 License 017 * along with this program; if not, write to the Free Software Foundation, 018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 019 */ 020 021package org.sonar.api.ce.measure; 022 023import java.util.List; 024import java.util.Set; 025import javax.annotation.CheckForNull; 026import org.sonar.api.ExtensionPoint; 027import org.sonar.api.ce.measure.MeasureComputer.MeasureComputerDefinition.Builder; 028import org.sonar.api.server.ServerSide; 029 030/** 031 * Define how to compute new measures on some metrics declared by {@link org.sonar.api.measures.Metrics}. 032 * <p/> 033 * This interface replaces the deprecated class org.sonar.api.batch.Decorator. 034 * <p/> 035 * <h3>How to use</h3> 036 * <pre> 037 * public class MyMeasureComputer implements MeasureComputer { 038 * 039 * {@literal @}Override 040 * public MeasureComputerDefinition define(MeasureComputerDefinitionContext defContext) { 041 * return defContext.newDefinitionBuilder() 042 * 043 * // Input metrics can be empty, for instance if only issues will be read 044 * .setInputMetrics("ncloc") 045 * 046 * // Output metrics must contains at least one metric 047 * .setOutputMetrics("my_new_metric") 048 * 049 * .build(); 050 * } 051 * 052 * {@literal @}Override 053 * public void compute(MeasureComputerContext context) { 054 * int ncloc = context.getMeasure("ncloc"); 055 * List<Issue> issues = context.getIssues(); 056 * if (ncloc != null && !issues.isEmpty()) { 057 * double value = issues.size() / ncloc; 058 * context.addMeasure("my_new_metric", value); 059 * } 060 * } 061 * } 062 * </pre> 063 * <p/> 064 * <h3>How to test</h3> 065 * <pre> 066 * public class MyMeasureComputerTest { 067 * 068 * MyMeasureComputer underTest = new MyMeasureComputer(); 069 * 070 * {@literal @}Test 071 * public void test_definition() { 072 * TestMeasureComputerDefinitionContext defContext = new TestMeasureComputerDefinitionContext(); 073 * MeasureComputerDefinition def = underTest.define(defContext); 074 * assertThat(def).isNotNull(); 075 * assertThat(def.getInputMetrics()).containsOnly("ncloc"); 076 * assertThat(def.getOutputMetrics()).containsOnly("my_new_metric"); 077 * } 078 * 079 * {@literal @}Test 080 * public void sum_ncloc_and_issues() { 081 * TestMeasureComputerContext context = new TestMeasureComputerContext(underTest); 082 * context.addMeasure("ncloc", 2); 083 * context.setIssues(Arrays.asList(new TestIssue.Builder().setKey("ABCD").build())); 084 * underTest.compute(context); 085 * 086 * assertThat(context.getMeasureValue("my_new_metric")).isEqualTo(0.5); 087 * } 088 * </pre> 089 * 090 * @since 5.2 091 */ 092@ServerSide 093@ExtensionPoint 094public interface MeasureComputer { 095 096 /** 097 * Use to define which metrics are required to compute some measures on some given metrics 098 */ 099 MeasureComputerDefinition define(MeasureComputerDefinitionContext defContext); 100 101 /** 102 * Context specific to the definition of the measure computer 103 */ 104 interface MeasureComputerDefinitionContext { 105 Builder newDefinitionBuilder(); 106 } 107 108 interface MeasureComputerDefinition { 109 /** 110 * Return the metric keys that can be read using {@link MeasureComputerContext}. 111 * Can be empty for instance when the computer only need to access to issues. 112 */ 113 Set<String> getInputMetrics(); 114 115 /** 116 * Return the metric keys that can be create using {@link MeasureComputerContext}. 117 * Can never ne empty. 118 */ 119 Set<String> getOutputMetrics(); 120 121 interface Builder { 122 123 /** 124 * List of metric keys of the measures that will be loaded by this computer. It can be empty (for instance when only issues are needed). 125 * A metric must be either a {@link org.sonar.api.measures.CoreMetrics} or a metric provided by {@link org.sonar.api.measures.Metrics} 126 * 127 * @throws NullPointerException if inputMetrics is null 128 * @throws NullPointerException if the metrics contains a {@code null} 129 * */ 130 Builder setInputMetrics(String... inputMetrics); 131 132 /** 133 * List of metric keys of the measures that can be added by this computer. At least one metric key must be defined. 134 * 135 * At runtime, the following conditions will be validated : 136 * <ul> 137 * <li>A metric must be defined by {@link org.sonar.api.measures.Metrics}</li> 138 * <li>A metric cannot be a {@link org.sonar.api.measures.CoreMetrics}</li> 139 * <li>A metric must be generated by only one {@link MeasureComputer}</li> 140 * </ul> 141 * 142 * @throws NullPointerException if outputMetrics is null 143 * @throws IllegalArgumentException if there's not at least one output metrics 144 * @throws NullPointerException if the metrics contains a {@code null} 145 */ 146 Builder setOutputMetrics(String... outputMetrics); 147 148 /** 149 * @throws NullPointerException if inputMetrics is null 150 * @throws NullPointerException if inputs metrics contains a {@code null} 151 * @throws NullPointerException if outputMetrics is null 152 * @throws IllegalArgumentException if there's not at least one output metrics 153 * @throws NullPointerException if outputs metrics contains a {@code null} 154 */ 155 MeasureComputerDefinition build(); 156 } 157 } 158 159 /** 160 * This method will be called on each component of the projects. 161 */ 162 void compute(MeasureComputerContext context); 163 164 /** 165 * Context specific to the computation of the measure(s) of a given component 166 */ 167 interface MeasureComputerContext { 168 /** 169 * Returns the current component. 170 */ 171 Component getComponent(); 172 173 /** 174 * Returns settings of the current component. 175 */ 176 Settings getSettings(); 177 178 /** 179 * Returns the measure from a given metric on the current component. 180 * 181 * @throws IllegalArgumentException if the metric is not listed in {@link MeasureComputerDefinition#getInputMetrics()} 182 */ 183 @CheckForNull 184 Measure getMeasure(String metric); 185 186 /** 187 * Returns measures from a given metric on children of the current component. 188 * It no measure is found for a child, this measure is ignored 189 * 190 * @throws IllegalArgumentException if the metric is not listed in {@link MeasureComputerDefinition#getInputMetrics()} 191 * or in {@link MeasureComputerDefinition#getOutputMetrics()} 192 */ 193 Iterable<Measure> getChildrenMeasures(String metric); 194 195 /** 196 * Add a new measure of a given metric which measure type will be int 197 * 198 * @throws IllegalArgumentException if the metric is not listed in {@link MeasureComputerDefinition#getOutputMetrics()} 199 * @throws UnsupportedOperationException if a measure for the specified metric already exists for the current component 200 */ 201 void addMeasure(String metric, int value); 202 203 /** 204 * Add a new measure of a given metric which measure type will be double 205 * 206 * @throws IllegalArgumentException if the metric is not listed in {@link MeasureComputerDefinition#getOutputMetrics()} 207 * @throws UnsupportedOperationException if a measure for the specified metric already exists for the current component 208 */ 209 void addMeasure(String metric, double value); 210 211 /** 212 * Add a new measure of a given metric which measure type will be long 213 * 214 * @throws IllegalArgumentException if the metric is not listed in {@link MeasureComputerDefinition#getOutputMetrics()} 215 * @throws UnsupportedOperationException if a measure for the specified metric already exists for the current component 216 */ 217 void addMeasure(String metric, long value); 218 219 /** 220 * Add a new measure of a given metric which measure type will be string 221 * 222 * @throws IllegalArgumentException if the metric is not listed in {@link MeasureComputerDefinition#getOutputMetrics()} 223 * @throws UnsupportedOperationException if a measure for the specified metric already exists for the current component 224 */ 225 void addMeasure(String metric, String value); 226 227 /** 228 * Add a new measure of a given metric which measure type will be boolean 229 * 230 * @throws IllegalArgumentException if the metric is not listed in {@link MeasureComputerDefinition#getOutputMetrics()} 231 * @throws UnsupportedOperationException if a measure for the specified metric already exists for the current component 232 */ 233 void addMeasure(String metric, boolean value); 234 235 /** 236 * Return list of all issues (open, closed, etc.) of current component. 237 */ 238 List<? extends Issue> getIssues(); 239 } 240}