001/* 002 * SonarQube, open source software quality management tool. 003 * Copyright (C) 2008-2013 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 */ 032public 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 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}