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