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