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