001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar 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 * Sonar 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
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019 */
020package org.sonar.api.workflow.internal;
021
022import com.google.common.annotations.Beta;
023import com.google.common.base.Preconditions;
024import com.google.common.base.Strings;
025import com.google.common.collect.*;
026import org.sonar.api.workflow.Workflow;
027import org.sonar.api.workflow.condition.Condition;
028import org.sonar.api.workflow.condition.ProjectPropertyCondition;
029import org.sonar.api.workflow.function.Function;
030import org.sonar.api.workflow.screen.Screen;
031
032import java.util.List;
033import java.util.Map;
034import java.util.Set;
035
036/**
037 * @since 3.1
038 */
039@Beta
040public final class DefaultWorkflow implements Workflow {
041
042  private Set<String> commands = Sets.newLinkedHashSet();
043  private ListMultimap<String, Condition> conditionsByCommand = ArrayListMultimap.create();
044  private ListMultimap<String, Function> functionsByCommand = ArrayListMultimap.create();
045  private Map<String, Screen> screensByCommand = Maps.newLinkedHashMap();
046
047  /**
048   * Keys of all the properties that are required by conditions (see {@link org.sonar.api.workflow.condition.ProjectPropertyCondition}
049   */
050  private List<String> projectPropertyKeys = Lists.newArrayList();
051
052  /**
053   * Optimization: fast way to get all context conditions
054   */
055  private ListMultimap<String, Condition> contextConditionsByCommand = ArrayListMultimap.create();
056
057  /**
058   * Optimization: fast way to get all review conditions
059   */
060  private ListMultimap<String, Condition> reviewConditionsByCommand = ArrayListMultimap.create();
061
062
063  public Workflow addCommand(String key) {
064    Preconditions.checkArgument(!Strings.isNullOrEmpty(key), "Empty command key");
065    commands.add(key);
066    return this;
067  }
068
069  public Set<String> getCommands() {
070    return commands;
071  }
072
073  public boolean hasCommand(String key) {
074    return commands.contains(key);
075  }
076
077  public List<String> getProjectPropertyKeys() {
078    return projectPropertyKeys;
079  }
080
081  /**
082   * Shortcut for: getReviewConditions(commandKey) + getContextConditions(commandKey)
083   */
084  public List<Condition> getConditions(String commandKey) {
085    return conditionsByCommand.get(commandKey);
086  }
087
088  public List<Condition> getReviewConditions(String commandKey) {
089    return reviewConditionsByCommand.get(commandKey);
090  }
091
092  public List<Condition> getContextConditions(String commandKey) {
093    return contextConditionsByCommand.get(commandKey);
094  }
095
096  public Workflow addCondition(String commandKey, Condition condition) {
097    Preconditions.checkArgument(hasCommand(commandKey), "Unknown command: " + commandKey);
098    Preconditions.checkNotNull(condition);
099    conditionsByCommand.put(commandKey, condition);
100    if (condition instanceof ProjectPropertyCondition) {
101      projectPropertyKeys.add(((ProjectPropertyCondition) condition).getPropertyKey());
102    }
103    if (condition.isOnContext()) {
104      contextConditionsByCommand.put(commandKey, condition);
105    } else {
106      reviewConditionsByCommand.put(commandKey, condition);
107    }
108    return this;
109  }
110
111  public List<Function> getFunctions(String commandKey) {
112    return functionsByCommand.get(commandKey);
113  }
114
115  public Workflow addFunction(String commandKey, Function function) {
116    Preconditions.checkArgument(hasCommand(commandKey), "Unknown command: " + commandKey);
117    Preconditions.checkNotNull(function);
118    functionsByCommand.put(commandKey, function);
119    return this;
120  }
121
122  public Screen getScreen(String commandKey) {
123    return screensByCommand.get(commandKey);
124  }
125
126  public Workflow setScreen(String commandKey, Screen screen) {
127    Preconditions.checkArgument(hasCommand(commandKey), "Unknown command: " + commandKey);
128    Preconditions.checkNotNull(screen);
129    Preconditions.checkState(Strings.isNullOrEmpty(screen.getCommandKey()), "Screen is already associated with command: " + screen.getCommandKey());
130    screen.setCommandKey(commandKey);
131    screensByCommand.put(commandKey, screen);
132    return this;
133  }
134
135  public Map<String, Screen> getScreensByCommand() {
136    return screensByCommand;
137  }
138}