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