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