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