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     */
020    package org.sonar.api.task;
021    
022    import com.google.common.base.Preconditions;
023    import com.google.common.base.Strings;
024    
025    import java.util.regex.Pattern;
026    
027    /**
028     * Register and describe a {@link TaskExtension}.
029     *
030     * @since 3.6
031     */
032    public class TaskDefinition implements TaskExtension, Comparable<TaskDefinition> {
033      static final String KEY_PATTERN = "[a-zA-Z0-9\\-\\_]+";
034    
035      private final String key;
036      private final String description;
037      private final Class<? extends Task> taskClass;
038    
039      private TaskDefinition(Builder builder) {
040        this.key = builder.key;
041        this.description = builder.description;
042        this.taskClass = builder.taskClass;
043      }
044    
045      public String description() {
046        return description;
047      }
048    
049      public String key() {
050        return key;
051      }
052    
053      public Class<? extends Task> taskClass() {
054        return taskClass;
055      }
056    
057      @Override
058      public String toString() {
059        return "Task " + key + "[class=" + taskClass.getName() + ", desc=" + description + "]";
060      }
061    
062      public static Builder builder() {
063        return new Builder();
064      }
065    
066      @Override
067      public boolean equals(Object o) {
068        if (this == o) {
069          return true;
070        }
071        if (o == null || getClass() != o.getClass()) {
072          return false;
073        }
074    
075        TaskDefinition that = (TaskDefinition) o;
076        if (!key.equals(that.key)) {
077          return false;
078        }
079        return true;
080      }
081    
082      @Override
083      public int hashCode() {
084        return key.hashCode();
085      }
086    
087      @Override
088      public int compareTo(TaskDefinition o) {
089        return key.compareTo(o.key);
090      }
091    
092      public static class Builder {
093        private String key;
094        private String description;
095        private Class<? extends Task> taskClass;
096    
097        private Builder() {
098        }
099    
100        public Builder key(String key) {
101          this.key = key;
102          return this;
103        }
104    
105        public Builder description(String s) {
106          this.description = s;
107          return this;
108        }
109    
110        public Builder taskClass(Class<? extends Task> taskClass) {
111          this.taskClass = taskClass;
112          return this;
113        }
114    
115        public TaskDefinition build() {
116          Preconditions.checkArgument(!Strings.isNullOrEmpty(key), "Task key must be set");
117          Preconditions.checkArgument(Pattern.matches(KEY_PATTERN, key), "Task key '" + key + "' must match " + KEY_PATTERN);
118          Preconditions.checkArgument(!Strings.isNullOrEmpty(description), "Description must be set for task '" + key + "'");
119          Preconditions.checkArgument(taskClass != null, "Class must be set for task '" + key + "'");
120          return new TaskDefinition(this);
121        }
122      }
123    }