001/* 002 * SonarQube 003 * Copyright (C) 2009-2016 SonarSource SA 004 * mailto:contact AT sonarsource DOT com 005 * 006 * This program 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 * This program 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; 024import java.util.regex.Pattern; 025import org.sonar.api.ExtensionPoint; 026import org.sonar.api.batch.ScannerSide; 027import org.sonar.api.batch.InstantiationStrategy; 028 029/** 030 * Register and describe a {@link TaskExtension}. 031 * 032 * @since 3.6 033 */ 034@ExtensionPoint 035@ScannerSide 036@InstantiationStrategy(InstantiationStrategy.PER_TASK) 037public class TaskDefinition implements Comparable<TaskDefinition> { 038 static final String KEY_PATTERN = "[a-zA-Z0-9\\-\\_]+"; 039 040 private final String key; 041 private final String description; 042 private final Class<? extends Task> taskClass; 043 044 private TaskDefinition(Builder builder) { 045 this.key = builder.key; 046 this.description = builder.description; 047 this.taskClass = builder.taskClass; 048 } 049 050 public String description() { 051 return description; 052 } 053 054 public String key() { 055 return key; 056 } 057 058 public Class<? extends Task> taskClass() { 059 return taskClass; 060 } 061 062 @Override 063 public String toString() { 064 return "Task " + key + "[class=" + taskClass.getName() + ", desc=" + description + "]"; 065 } 066 067 public static Builder builder() { 068 return new Builder(); 069 } 070 071 @Override 072 public boolean equals(Object o) { 073 if (this == o) { 074 return true; 075 } 076 if (o == null || getClass() != o.getClass()) { 077 return false; 078 } 079 080 TaskDefinition that = (TaskDefinition) o; 081 return key.equals(that.key); 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}