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