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.resources;
021
022import com.google.common.collect.ArrayListMultimap;
023import com.google.common.collect.ImmutableListMultimap;
024import com.google.common.collect.ListMultimap;
025import java.util.ArrayList;
026import java.util.Arrays;
027import java.util.Collection;
028import java.util.List;
029import java.util.stream.Collectors;
030import org.sonar.api.batch.ScannerSide;
031import org.sonar.api.ce.ComputeEngineSide;
032import org.sonar.api.server.ServerSide;
033
034import static com.google.common.base.Preconditions.checkArgument;
035import static java.util.Collections.unmodifiableList;
036import static java.util.Objects.requireNonNull;
037
038/**
039 * @since 2.14
040 */
041@ScannerSide
042@ServerSide
043@ComputeEngineSide
044public class ResourceTypeTree {
045
046  private final List<ResourceType> types;
047  private final ListMultimap<String, String> relations;
048  private final ResourceType root;
049
050  private ResourceTypeTree(Builder builder) {
051    this.types = unmodifiableList(new ArrayList<>(builder.types));
052    this.relations = ImmutableListMultimap.copyOf(builder.relations);
053    this.root = builder.root;
054  }
055
056  public List<ResourceType> getTypes() {
057    return types;
058  }
059
060  public List<String> getChildren(String qualifier) {
061    return relations.get(qualifier);
062  }
063
064  public ResourceType getRootType() {
065    return root;
066  }
067
068  public List<String> getLeaves() {
069    return unmodifiableList(relations.values()
070      .stream()
071      .filter(qualifier -> relations.get(qualifier).isEmpty())
072      .collect(Collectors.toList()));
073  }
074
075  @Override
076  public String toString() {
077    return root.getQualifier();
078  }
079
080  public static Builder builder() {
081    return new Builder();
082  }
083
084  public static final class Builder {
085    private List<ResourceType> types = new ArrayList<>();
086    private ListMultimap<String, String> relations = ArrayListMultimap.create();
087    private ResourceType root;
088
089    private Builder() {
090    }
091
092    public Builder addType(ResourceType type) {
093      requireNonNull(type);
094      checkArgument(!types.contains(type), String.format("%s is already registered", type.getQualifier()));
095      types.add(type);
096      return this;
097    }
098
099    public Builder addRelations(String parentQualifier, String... childrenQualifiers) {
100      requireNonNull(parentQualifier);
101      requireNonNull(childrenQualifiers);
102      checkArgument(childrenQualifiers.length > 0, "childrenQualifiers can't be empty");
103      relations.putAll(parentQualifier, Arrays.asList(childrenQualifiers));
104      return this;
105    }
106
107    public ResourceTypeTree build() {
108      Collection<String> children = relations.values();
109      for (ResourceType type : types) {
110        if (!children.contains(type.getQualifier())) {
111          root = type;
112          break;
113        }
114      }
115      return new ResourceTypeTree(this);
116    }
117  }
118
119}