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