001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar 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 * Sonar 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
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019 */
020package org.sonar.api.config;
021
022import com.google.common.collect.Lists;
023import org.sonar.api.PropertyField;
024import org.sonar.api.PropertyType;
025
026import javax.annotation.Nullable;
027
028import java.util.List;
029
030/**
031 * @since 3.3
032 */
033public final class PropertyFieldDefinition {
034  private final String key;
035  private final String name;
036  private final String description;
037  private final int indicativeSize;
038  private final PropertyType type;
039  private final String[] options;
040
041  private PropertyFieldDefinition(PropertyField annotation) {
042    this.key = annotation.key();
043    this.name = annotation.name();
044    this.description = annotation.description();
045    this.indicativeSize = annotation.indicativeSize();
046    this.type = annotation.type();
047    this.options = annotation.options();
048  }
049
050  public static List<PropertyFieldDefinition> create(PropertyField[] fields) {
051    List<PropertyFieldDefinition> definitions = Lists.newArrayList();
052    for (PropertyField field : fields) {
053      definitions.add(new PropertyFieldDefinition(field));
054    }
055    return definitions;
056  }
057
058  public String getKey() {
059    return key;
060  }
061
062  public String getName() {
063    return name;
064  }
065
066  public String getDescription() {
067    return description;
068  }
069
070  public int getIndicativeSize() {
071    return indicativeSize;
072  }
073
074  public PropertyType getType() {
075    return type;
076  }
077
078  public String[] getOptions() {
079    return options.clone();
080  }
081
082  public PropertyDefinition.Result validate(@Nullable String value) {
083    return PropertyDefinition.validate(type, value, options);
084  }
085}