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