001/* 002 * SonarQube 003 * Copyright (C) 2009-2017 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 com.google.common.base.Function; 023import java.util.Arrays; 024import java.util.Set; 025import javax.annotation.Nonnull; 026 027import static com.google.common.collect.FluentIterable.from; 028import static java.lang.String.format; 029 030public enum RuleType { 031 CODE_SMELL(1), BUG(2), VULNERABILITY(3); 032 033 private static final Set<String> ALL_NAMES = from(Arrays.asList(values())).transform(ToName.INSTANCE).toSet(); 034 035 private final int dbConstant; 036 037 RuleType(int dbConstant) { 038 this.dbConstant = dbConstant; 039 } 040 041 public int getDbConstant() { 042 return dbConstant; 043 } 044 045 public static Set<String> names() { 046 return ALL_NAMES; 047 } 048 049 /** 050 * Returns the enum constant of the specified DB column value. 051 */ 052 public static RuleType valueOf(int dbConstant) { 053 // iterating the array is fast-enough as size is small. No need for a map. 054 for (RuleType type : values()) { 055 if (type.getDbConstant() == dbConstant) { 056 return type; 057 } 058 } 059 throw new IllegalArgumentException(format("Unsupported type value : %d", dbConstant)); 060 } 061 062 private enum ToName implements Function<RuleType, String> { 063 INSTANCE; 064 065 @Override 066 public String apply(@Nonnull RuleType input) { 067 return input.name(); 068 } 069 } 070 071}