001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info 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.batch.bootstrap;
021
022import org.sonar.api.batch.ScannerSide;
023
024import java.util.ArrayList;
025import java.util.List;
026
027/**
028 * Mutable project definitions that can be modified by {@link ProjectBuilder} extensions.
029 * 
030 * @deprecated since 6.5 plugins should no longer modify the project's structure
031 * @since 2.9
032 */
033@Deprecated
034@ScannerSide
035public class ProjectReactor implements ProjectKey {
036
037  private ProjectDefinition root;
038
039  public ProjectReactor(ProjectDefinition root) {
040    if (root.getParent() != null) {
041      throw new IllegalArgumentException("Not a root project: " + root);
042    }
043    this.root = root;
044  }
045
046  public List<ProjectDefinition> getProjects() {
047    return collectProjects(root, new ArrayList<>());
048  }
049
050  /**
051   * Populates list of projects from hierarchy.
052   */
053  private static List<ProjectDefinition> collectProjects(ProjectDefinition def, List<ProjectDefinition> collected) {
054    collected.add(def);
055    for (ProjectDefinition child : def.getSubProjects()) {
056      collectProjects(child, collected);
057    }
058    return collected;
059  }
060
061  public ProjectDefinition getRoot() {
062    return root;
063  }
064
065  public ProjectDefinition getProject(String key) {
066    for (ProjectDefinition p : getProjects()) {
067      if (key.equals(p.getKey())) {
068        return p;
069      }
070    }
071    return null;
072  }
073
074  @Override
075  public String get() {
076    if (root != null) {
077      return root.getKeyWithBranch();
078    }
079    return null;
080  }
081}