]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6730 Measure Computer API definition
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 3 Aug 2015 13:26:19 +0000 (15:26 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 7 Aug 2015 12:08:48 +0000 (14:08 +0200)
sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/Component.java [new file with mode: 0644]
sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/Measure.java [new file with mode: 0644]
sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/MeasureComputer.java [new file with mode: 0644]
sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/MeasureComputerProvider.java [new file with mode: 0644]
sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/Settings.java [new file with mode: 0644]
sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/package-info.java [new file with mode: 0644]

diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/Component.java b/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/Component.java
new file mode 100644 (file)
index 0000000..bd8746a
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.api.ce.measure;
+
+import javax.annotation.CheckForNull;
+
+public interface Component {
+
+  enum Type {
+    PROJECT, MODULE, DIRECTORY, FILE
+  }
+
+  Type getType();
+
+  /**
+   * The attributes of the Component if it's type is File.
+   *
+   * @throws IllegalStateException if the Component's type is not {@link Type#FILE}
+   */
+  FileAttributes getFileAttributes();
+
+  interface FileAttributes {
+
+    boolean isUnitTest();
+
+    @CheckForNull
+    String getLanguageKey();
+
+  }
+}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/Measure.java b/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/Measure.java
new file mode 100644 (file)
index 0000000..fea604b
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.api.ce.measure;
+
+public interface Measure {
+
+  /**
+   * The value of this measure as a integer.
+   *
+   * @throws IllegalStateException if the value type of the metric is not a integer {see @link org.sonar.api.measures.Metric.ValueType}
+   */
+  int getIntValue();
+
+  /**
+   * The value of this measure as a long.
+   *
+   * @throws IllegalStateException if the value type of the metric is not a long {see @link org.sonar.api.measures.Metric.ValueType}
+   */
+  long getLongValue();
+
+  /**
+   * The value of this measure as a double.
+   *
+   * @throws IllegalStateException if the value type of the metric is not a double {see @link org.sonar.api.measures.Metric.ValueType}
+   */
+  double getDoubleValue();
+
+  /**
+   * The value of this measure as a string.
+   *
+   * @throws IllegalStateException if the value type of the metric is not a string {see @link org.sonar.api.measures.Metric.ValueType}
+   */
+  String getStringValue();
+
+}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/MeasureComputer.java b/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/MeasureComputer.java
new file mode 100644 (file)
index 0000000..0d1e2c8
--- /dev/null
@@ -0,0 +1,147 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.api.ce.measure;
+
+import java.util.Set;
+import javax.annotation.CheckForNull;
+
+/**
+ * This class is used to define which metrics are required to compute some measures on some given metrics, and to define the implementation of the measures computation
+ */
+public interface MeasureComputer {
+
+  /**
+   * Return the metric keys that can be read using {@link Implementation.Context}.
+   *
+   * Can never be empty as it's checked in the builder
+   */
+  Set<String> getInputMetrics();
+
+  /**
+   * Return the metric keys that can be create using {@link Implementation.Context}.
+   *
+   * Can never ne empty as it's checked om the builder
+   */
+  Set<String> getOutputMetrics();
+
+  Implementation getImplementation();
+
+  interface MeasureComputerBuilder {
+
+    /**
+     * @throws IllegalStateException if there's not at least one input metrics
+     */
+    MeasureComputerBuilder setInputMetrics(String... inputMetrics);
+
+    /**
+     * @throws IllegalStateException if there's not at least one output metrics
+     */
+    MeasureComputerBuilder setOutputMetrics(String... outMetrics);
+
+    /**
+     * @throws IllegalStateException if there's no implementation
+     */
+    MeasureComputerBuilder setImplementation(Implementation impl);
+
+    /**
+     * @throws IllegalStateException if there's not at least one input metrics
+     * @throws IllegalStateException if there's not at least one output metrics
+     * @throws IllegalStateException if there's no implementation
+     */
+    MeasureComputer build();
+  }
+
+  /**
+   * This interface must be implemented to define how the measures are computed.
+   */
+  interface Implementation {
+
+    /**
+     * This method will be called on each component of the projects.
+     */
+    void compute(Context ctx);
+
+    /**
+     * Context specific to the computation of the measure(s) of a given component
+     */
+    interface Context {
+
+      /**
+       * Returns the current component.
+       */
+      Component getComponent();
+
+      /**
+       * Returns settings of the current component.
+       */
+      Settings getSettings();
+
+      /**
+       * Returns the measure from a given metric on the current component.
+       *
+       * @throws IllegalArgumentException if the metric is not listed in {@link MeasureComputer#getInputMetrics()}
+       */
+      @CheckForNull
+      Measure getMeasure(String metric);
+
+      /**
+       * Returns measures from a given metric on children of the current component.
+       * It no measure is found for a child, this measure is ignored
+       *
+       * @throws IllegalArgumentException if the metric is not listed in {@link MeasureComputer#getInputMetrics()} or in {@link MeasureComputer#getOutputMetrics()}
+       */
+      Iterable<Measure> getChildrenMeasures(String metric);
+
+      /**
+       * Add a new measure of a given metric which measure type will be int
+       *
+       * @throws IllegalArgumentException if the metric is not listed in {@link MeasureComputer#getOutputMetrics()}
+       * @throws UnsupportedOperationException if a measure for the specified metric already exists for the current component
+       */
+      void addMeasure(String metric, int value);
+
+      /**
+       * Add a new measure of a given metric which measure type will be double
+       *
+       * @throws IllegalArgumentException if the metric is not listed in {@link MeasureComputer#getOutputMetrics()}
+       * @throws UnsupportedOperationException if a measure for the specified metric already exists for the current component
+       */
+      void addMeasure(String metric, double value);
+
+      /**
+       * Add a new measure of a given metric which measure type will be long
+       *
+       * @throws IllegalArgumentException if the metric is not listed in {@link MeasureComputer#getOutputMetrics()}
+       * @throws UnsupportedOperationException if a measure for the specified metric already exists for the current component
+       */
+      void addMeasure(String metric, long value);
+
+      /**
+       * Add a new measure of a given metric which measure type will be string
+       *
+       * @throws IllegalArgumentException if the metric is not listed in {@link MeasureComputer#getOutputMetrics()}
+       * @throws UnsupportedOperationException if a measure for the specified metric already exists for the current component
+       */
+      void addMeasure(String metric, String value);
+
+    }
+  }
+}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/MeasureComputerProvider.java b/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/MeasureComputerProvider.java
new file mode 100644 (file)
index 0000000..6eb62df
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.api.ce.measure;
+
+import org.sonar.api.ExtensionPoint;
+import org.sonar.api.server.ServerSide;
+
+/**
+ * This extension point can be used to register {@link MeasureComputer}(s) that will be able to compute measures when a batch report is processed by the Compute Engine
+ */
+@ServerSide
+@ExtensionPoint
+public interface MeasureComputerProvider {
+
+  /**
+   *  Use this method to register a new measure computer.
+   */
+  void register(Context ctx);
+
+  interface Context {
+
+    /**
+     * Add a new computer to the context.
+     *
+     * @throws UnsupportedOperationException when trying to add a computer providing some measures on metrics already defined by another {@link MeasureComputer}
+     */
+    Context add(MeasureComputer measureComputer);
+
+    /**
+     * Use this method to build a MeasureComputer to be used in the {@link #add(MeasureComputer)} method
+     */
+    MeasureComputer.MeasureComputerBuilder newMeasureComputerBuilder();
+
+  }
+}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/Settings.java b/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/Settings.java
new file mode 100644 (file)
index 0000000..b704d6c
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.api.ce.measure;
+
+import javax.annotation.CheckForNull;
+
+/**
+ * Settings of the current component.
+ */
+public interface Settings {
+
+  /**
+   * Returns the property as a string
+   * Matching on key is case sensitive
+   */
+  @CheckForNull
+  String getString(String key);
+
+  /**
+   * Returns the property as a an array
+   * Returns an empty array if no property is found for this key
+   * Matching on key is case sensitive
+   */
+  String[] getStringArray(String key);
+
+}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/package-info.java b/sonar-plugin-api/src/main/java/org/sonar/api/ce/measure/package-info.java
new file mode 100644 (file)
index 0000000..0e5ec01
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+@ParametersAreNonnullByDefault
+package org.sonar.api.ce.measure;
+
+import javax.annotation.ParametersAreNonnullByDefault;