001/* 002 * SonarQube 003 * Copyright (C) 2009-2018 SonarSource SA 004 * mailto:info AT sonarsource DOT com 005 * 006 * This program 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 * This program 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.annotations.Beta; 023import java.util.ArrayList; 024import java.util.Collection; 025import java.util.Collections; 026import java.util.LinkedHashMap; 027import java.util.LinkedHashSet; 028import java.util.List; 029import java.util.Map; 030import org.sonar.api.ce.ComputeEngineSide; 031import org.sonar.api.server.ServerSide; 032 033import static java.util.Collections.unmodifiableList; 034import static java.util.Collections.unmodifiableMap; 035import static java.util.Objects.requireNonNull; 036 037/** 038 * @since 2.14 039 */ 040@Beta 041@ServerSide 042@ComputeEngineSide 043public class ResourceTypes { 044 045 private final Map<String, ResourceTypeTree> treeByQualifier; 046 private final Map<String, ResourceType> typeByQualifier; 047 private final Collection<ResourceType> rootTypes; 048 049 public ResourceTypes(ResourceTypeTree[] trees) { 050 requireNonNull(trees); 051 052 Map<String, ResourceTypeTree> treeMap = new LinkedHashMap<>(); 053 Map<String, ResourceType> typeMap = new LinkedHashMap<>(); 054 Collection<ResourceType> rootsSet = new LinkedHashSet<>(); 055 056 for (ResourceTypeTree tree : trees) { 057 rootsSet.add(tree.getRootType()); 058 for (ResourceType type : tree.getTypes()) { 059 if (treeMap.containsKey(type.getQualifier())) { 060 throw new IllegalStateException("Qualifier " + type.getQualifier() + " is defined in several trees"); 061 } 062 treeMap.put(type.getQualifier(), tree); 063 typeMap.put(type.getQualifier(), type); 064 } 065 } 066 treeByQualifier = unmodifiableMap(new LinkedHashMap<>(treeMap)); 067 typeByQualifier = unmodifiableMap(new LinkedHashMap<>(typeMap)); 068 rootTypes = unmodifiableList(new ArrayList<>(rootsSet)); 069 } 070 071 public ResourceType get(String qualifier) { 072 ResourceType type = typeByQualifier.get(qualifier); 073 return type != null ? type : ResourceType.builder(qualifier).build(); 074 } 075 076 public Collection<ResourceType> getAll() { 077 return typeByQualifier.values(); 078 } 079 080 public Collection<ResourceType> getRoots() { 081 return rootTypes; 082 } 083 084 public List<String> getLeavesQualifiers(String qualifier) { 085 ResourceTypeTree tree = getTree(qualifier); 086 if (tree != null) { 087 return tree.getLeaves(); 088 } 089 return Collections.emptyList(); 090 } 091 092 private ResourceTypeTree getTree(String qualifier) { 093 return treeByQualifier.get(qualifier); 094 } 095}