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.

PropertyFieldDefinition.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.config;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import javax.annotation.Nullable;
  24. import org.apache.commons.lang.StringUtils;
  25. import org.sonar.api.PropertyField;
  26. import org.sonar.api.PropertyType;
  27. import static java.util.Arrays.asList;
  28. import static org.sonar.api.utils.Preconditions.checkArgument;
  29. /**
  30. * @since 3.3
  31. */
  32. public final class PropertyFieldDefinition {
  33. private final String key;
  34. private final String name;
  35. private final String description;
  36. private final PropertyType type;
  37. private final List<String> options;
  38. private PropertyFieldDefinition(Builder builder) {
  39. this.key = builder.key;
  40. this.name = builder.name;
  41. this.description = builder.description;
  42. this.type = builder.type;
  43. this.options = builder.options;
  44. }
  45. static List<PropertyFieldDefinition> create(PropertyField[] fields) {
  46. List<PropertyFieldDefinition> definitions = new ArrayList<>();
  47. for (PropertyField field : fields) {
  48. definitions.add(PropertyFieldDefinition.build(field.key())
  49. .name(field.name())
  50. .description(field.description())
  51. .type(field.type())
  52. .options(field.options())
  53. .build());
  54. }
  55. return definitions;
  56. }
  57. public static Builder build(String key) {
  58. return new Builder(key);
  59. }
  60. public String key() {
  61. return key;
  62. }
  63. public String name() {
  64. return name;
  65. }
  66. public String description() {
  67. return description;
  68. }
  69. public PropertyType type() {
  70. return type;
  71. }
  72. public List<String> options() {
  73. return options;
  74. }
  75. public PropertyDefinition.Result validate(@Nullable String value) {
  76. return PropertyDefinition.validate(type, value, options);
  77. }
  78. public static class Builder {
  79. private String key;
  80. private String name;
  81. private String description;
  82. private PropertyType type;
  83. private List<String> options;
  84. private Builder(String key) {
  85. this.key = key;
  86. this.name = "";
  87. this.description = "";
  88. this.type = PropertyType.STRING;
  89. this.options = new ArrayList<>();
  90. }
  91. public Builder name(String name) {
  92. this.name = name;
  93. return this;
  94. }
  95. public Builder description(String description) {
  96. this.description = description;
  97. return this;
  98. }
  99. public Builder type(PropertyType type) {
  100. this.type = type;
  101. return this;
  102. }
  103. public Builder options(String... options) {
  104. this.options.addAll(asList(options));
  105. return this;
  106. }
  107. public Builder options(List<String> options) {
  108. this.options.addAll(options);
  109. return this;
  110. }
  111. public PropertyFieldDefinition build() {
  112. checkArgument(!StringUtils.isEmpty(key), "Key must be set");
  113. checkArgument(!StringUtils.isEmpty(name), "Name must be set");
  114. return new PropertyFieldDefinition(this);
  115. }
  116. }
  117. }