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.issue.action;
021    
022    import com.google.common.annotations.Beta;
023    import com.google.common.base.Preconditions;
024    import com.google.common.base.Strings;
025    import com.google.common.collect.ImmutableList;
026    import org.sonar.api.issue.Issue;
027    import org.sonar.api.issue.condition.Condition;
028    
029    import java.util.List;
030    
031    import static com.google.common.collect.Lists.newArrayList;
032    
033    /**
034     * @since 3.6
035     */
036    @Beta
037    public class Action {
038    
039      private final String key;
040      private final List<Condition> conditions;
041      private final List<Function> functions;
042    
043      Action(String key) {
044        Preconditions.checkArgument(!Strings.isNullOrEmpty(key), "Action key must be set");
045        this.key = key;
046        this.conditions = newArrayList();
047        this.functions = newArrayList();
048      }
049    
050      public String key() {
051        return key;
052      }
053    
054      public Action setConditions(Condition... conditions) {
055        this.conditions.addAll(ImmutableList.copyOf(conditions));
056        return this;
057      }
058    
059      public List<Condition> conditions() {
060        return conditions;
061      }
062    
063      public Action setFunctions(Function... functions) {
064        this.functions.addAll(ImmutableList.copyOf(functions));
065        return this;
066      }
067    
068      public List<Function> functions() {
069        return functions;
070      }
071    
072      public boolean supports(Issue issue) {
073        for (Condition condition : conditions) {
074          if (!condition.matches(issue)) {
075            return false;
076          }
077        }
078        return true;
079      }
080    
081      @Override
082      public boolean equals(Object o) {
083        if (this == o) {
084          return true;
085        }
086        if (o == null || getClass() != o.getClass()) {
087          return false;
088        }
089        Action that = (Action) o;
090        if (!key.equals(that.key)) {
091          return false;
092        }
093        return true;
094      }
095    
096      @Override
097      public int hashCode() {
098        return key.hashCode();
099      }
100    
101      @Override
102      public String toString() {
103        return key;
104      }
105    
106    }