001/* 002 * SonarQube 003 * Copyright (C) 2009-2018 SonarSource SA 004 * mailto:info 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.rules; 021 022import java.util.LinkedHashSet; 023import java.util.Set; 024 025import static java.lang.String.format; 026import static java.util.Arrays.stream; 027import static java.util.Collections.unmodifiableSet; 028import static java.util.stream.Collectors.toList; 029 030public enum RuleType { 031 CODE_SMELL(1), BUG(2), VULNERABILITY(3); 032 033 private static final Set<String> ALL_NAMES = unmodifiableSet(new LinkedHashSet<>(stream(values()) 034 .map(Enum::name) 035 .collect(toList()))); 036 037 private final int dbConstant; 038 039 RuleType(int dbConstant) { 040 this.dbConstant = dbConstant; 041 } 042 043 public int getDbConstant() { 044 return dbConstant; 045 } 046 047 public static Set<String> names() { 048 return ALL_NAMES; 049 } 050 051 /** 052 * Returns the enum constant of the specified DB column value. 053 */ 054 public static RuleType valueOf(int dbConstant) { 055 // iterating the array is fast-enough as size is small. No need for a map. 056 for (RuleType type : values()) { 057 if (type.getDbConstant() == dbConstant) { 058 return type; 059 } 060 } 061 throw new IllegalArgumentException(format("Unsupported type value : %d", dbConstant)); 062 } 063 064}