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