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.config;
021
022import com.google.common.base.Preconditions;
023import com.google.common.base.Strings;
024import com.google.common.collect.ImmutableList;
025import java.util.ArrayList;
026import java.util.List;
027import javax.annotation.Nullable;
028import org.sonar.api.PropertyField;
029import org.sonar.api.PropertyType;
030
031/**
032 * @since 3.3
033 */
034public final class PropertyFieldDefinition {
035  private final String key;
036  private final String name;
037  private final String description;
038  private final int indicativeSize;
039  private final PropertyType type;
040  private final List<String> options;
041
042  private PropertyFieldDefinition(Builder builder) {
043    this.key = builder.key;
044    this.name = builder.name;
045    this.description = builder.description;
046    this.indicativeSize = builder.indicativeSize;
047    this.type = builder.type;
048    this.options = builder.options;
049  }
050
051  static List<PropertyFieldDefinition> create(PropertyField[] fields) {
052    List<PropertyFieldDefinition> definitions = new ArrayList<>();
053    for (PropertyField field : fields) {
054      definitions.add(PropertyFieldDefinition.build(field.key())
055        .name(field.name())
056        .description(field.description())
057        .indicativeSize(field.indicativeSize())
058        .type(field.type())
059        .options(field.options())
060        .build()
061        );
062    }
063    return definitions;
064  }
065
066  public static Builder build(String key) {
067    return new Builder(key);
068  }
069
070  public String key() {
071    return key;
072  }
073
074  public String name() {
075    return name;
076  }
077
078  public String description() {
079    return description;
080  }
081
082  /**
083   * @deprecated since 6.1, as it was only used for UI.
084   */
085  @Deprecated
086  public int indicativeSize() {
087    return indicativeSize;
088  }
089
090  public PropertyType type() {
091    return type;
092  }
093
094  public List<String> options() {
095    return options;
096  }
097
098  public PropertyDefinition.Result validate(@Nullable String value) {
099    return PropertyDefinition.validate(type, value, options);
100  }
101
102  public static class Builder {
103    private String key;
104    private String name;
105    private String description;
106    private int indicativeSize;
107    private PropertyType type;
108    private List<String> options;
109
110    private Builder(String key) {
111      this.key = key;
112      this.name = "";
113      this.description = "";
114      this.indicativeSize = 20;
115      this.type = PropertyType.STRING;
116      this.options = new ArrayList<>();
117    }
118
119    public Builder name(String name) {
120      this.name = name;
121      return this;
122    }
123
124    public Builder description(String description) {
125      this.description = description;
126      return this;
127    }
128
129    /**
130     * @deprecated since 6.1, as it was only used for UI.
131     */
132    @Deprecated
133    public Builder indicativeSize(int indicativeSize) {
134      this.indicativeSize = indicativeSize;
135      return this;
136    }
137
138    public Builder type(PropertyType type) {
139      this.type = type;
140      return this;
141    }
142
143    public Builder options(String... options) {
144      this.options.addAll(ImmutableList.copyOf(options));
145      return this;
146    }
147
148    public Builder options(List<String> options) {
149      this.options.addAll(ImmutableList.copyOf(options));
150      return this;
151    }
152
153    public PropertyFieldDefinition build() {
154      Preconditions.checkArgument(!Strings.isNullOrEmpty(key), "Key must be set");
155      Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "Name must be set");
156      return new PropertyFieldDefinition(this);
157    }
158  }
159}