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