diff --git a/plugin-api/src/main/java/org/sonar/api/measures/SeverityValues.java b/plugin-api/src/main/java/org/sonar/api/measures/SeverityValues.java index 7b7d93c7..342115b5 100644 --- a/plugin-api/src/main/java/org/sonar/api/measures/SeverityValues.java +++ b/plugin-api/src/main/java/org/sonar/api/measures/SeverityValues.java @@ -19,6 +19,8 @@ */ package org.sonar.api.measures; +import org.sonar.api.issue.impact.Severity; + /** * Integer values that map to severity levels used by severity-based metrics. * Severities increase in numeric value; a higher value means a worse severity. @@ -43,4 +45,49 @@ public final class SeverityValues { private SeverityValues() { // constants only } + + /** + * Maps a rule severity string (see {@link org.sonar.api.rule.Severity}) to a {@link SeverityValues} integer. + * + * @throws IllegalArgumentException if the severity string is not recognised + * @since 13.6 + */ + public static int fromRuleSeverity(String severity) { + switch (severity) { + case org.sonar.api.rule.Severity.INFO: + return INFO; + case org.sonar.api.rule.Severity.MINOR: + return LOW; + case org.sonar.api.rule.Severity.MAJOR: + return MEDIUM; + case org.sonar.api.rule.Severity.CRITICAL: + return HIGH; + case org.sonar.api.rule.Severity.BLOCKER: + return BLOCKER; + default: + throw new IllegalArgumentException("Unknown rule severity: " + severity); + } + } + + /** + * Maps impact severity (see {@link org.sonar.api.issue.impact.Severity}) to a {@link SeverityValues} integer. + * + * @since 13.6 + */ + public static int fromImpactSeverity(Severity severity) { + switch (severity) { + case INFO: + return INFO; + case LOW: + return LOW; + case MEDIUM: + return MEDIUM; + case HIGH: + return HIGH; + case BLOCKER: + return BLOCKER; + default: + throw new IllegalArgumentException("Unknown impact severity: " + severity); + } + } }