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 @FunctionalInterface 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}