You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RuleType.java 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.api.rules;
  21. import java.util.LinkedHashSet;
  22. import java.util.Set;
  23. import javax.annotation.CheckForNull;
  24. import static java.lang.String.format;
  25. import static java.util.Arrays.stream;
  26. import static java.util.Collections.unmodifiableSet;
  27. import static java.util.stream.Collectors.toList;
  28. public enum RuleType {
  29. CODE_SMELL(1), BUG(2), VULNERABILITY(3), SECURITY_HOTSPOT(4);
  30. private static final Set<String> ALL_NAMES = unmodifiableSet(new LinkedHashSet<>(stream(values())
  31. .map(Enum::name)
  32. .collect(toList())));
  33. private final int dbConstant;
  34. RuleType(int dbConstant) {
  35. this.dbConstant = dbConstant;
  36. }
  37. public int getDbConstant() {
  38. return dbConstant;
  39. }
  40. public static Set<String> names() {
  41. return ALL_NAMES;
  42. }
  43. /**
  44. * Returns the enum constant of the specified DB column value.
  45. */
  46. public static RuleType valueOf(int dbConstant) {
  47. // iterating the array is fast-enough as size is small. No need for a map.
  48. for (RuleType type : values()) {
  49. if (type.getDbConstant() == dbConstant) {
  50. return type;
  51. }
  52. }
  53. throw new IllegalArgumentException(format("Unsupported type value : %d", dbConstant));
  54. }
  55. @CheckForNull
  56. public static RuleType valueOfNullable(int dbConstant) {
  57. // iterating the array is fast-enough as size is small. No need for a map.
  58. for (RuleType type : values()) {
  59. if (type.getDbConstant() == dbConstant) {
  60. return type;
  61. }
  62. }
  63. if (dbConstant == 0) {
  64. return null;
  65. }
  66. throw new IllegalArgumentException(format("Unsupported type value : %d", dbConstant));
  67. }
  68. }