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 return key.equals(that.key); 079 } 080 081 @Override 082 public int hashCode() { 083 return key.hashCode(); 084 } 085 086 @Override 087 public int compareTo(TaskDefinition o) { 088 return key.compareTo(o.key); 089 } 090 091 public static class Builder { 092 private String key; 093 private String description; 094 private Class<? extends Task> taskClass; 095 096 private Builder() { 097 } 098 099 public Builder key(String key) { 100 this.key = key; 101 return this; 102 } 103 104 public Builder description(String s) { 105 this.description = s; 106 return this; 107 } 108 109 public Builder taskClass(Class<? extends Task> taskClass) { 110 this.taskClass = taskClass; 111 return this; 112 } 113 114 public TaskDefinition build() { 115 Preconditions.checkArgument(!Strings.isNullOrEmpty(key), "Task key must be set"); 116 Preconditions.checkArgument(Pattern.matches(KEY_PATTERN, key), "Task key '" + key + "' must match " + KEY_PATTERN); 117 Preconditions.checkArgument(!Strings.isNullOrEmpty(description), "Description must be set for task '" + key + "'"); 118 Preconditions.checkArgument(taskClass != null, "Class must be set for task '" + key + "'"); 119 return new TaskDefinition(this); 120 } 121 } 122}