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