001/* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2012 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar 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 * Sonar 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 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020package org.sonar.api.resources; 021 022import com.google.common.annotations.Beta; 023import com.google.common.base.Function; 024import com.google.common.base.Preconditions; 025import com.google.common.base.Predicate; 026import com.google.common.collect.Collections2; 027import com.google.common.collect.ImmutableMap; 028import com.google.common.collect.Lists; 029import com.google.common.collect.Maps; 030import org.sonar.api.BatchComponent; 031import org.sonar.api.ServerComponent; 032 033import javax.annotation.Nullable; 034import java.util.Collection; 035import java.util.Collections; 036import java.util.List; 037import java.util.Map; 038 039/** 040 * @since 2.14 041 */ 042@Beta 043public final class ResourceTypes implements BatchComponent, ServerComponent { 044 045 public static final Predicate<ResourceType> AVAILABLE_FOR_FILTERS = new Predicate<ResourceType>() { 046 public boolean apply(@Nullable ResourceType input) { 047 return input != null && Boolean.TRUE.equals(input.getBooleanProperty("availableForFilters")); 048 } 049 }; 050 051 private final Map<String, ResourceTypeTree> treeByQualifier; 052 private final Map<String, ResourceType> typeByQualifier; 053 054 public ResourceTypes(ResourceTypeTree[] trees) { 055 Preconditions.checkNotNull(trees); 056 057 Map<String, ResourceTypeTree> treeMap = Maps.newHashMap(); 058 Map<String, ResourceType> typeMap = Maps.newLinkedHashMap(); 059 060 for (ResourceTypeTree tree : trees) { 061 for (ResourceType type : tree.getTypes()) { 062 if (treeMap.containsKey(type.getQualifier())) { 063 throw new IllegalStateException("Qualifier " + type.getQualifier() + " is defined in several trees"); 064 } 065 treeMap.put(type.getQualifier(), tree); 066 typeMap.put(type.getQualifier(), type); 067 } 068 } 069 treeByQualifier = ImmutableMap.copyOf(treeMap); 070 typeByQualifier = ImmutableMap.copyOf(typeMap); 071 } 072 073 public ResourceType get(String qualifier) { 074 ResourceType type = typeByQualifier.get(qualifier); 075 return type != null ? type : ResourceType.builder(qualifier).build(); 076 } 077 078 public Collection<ResourceType> getAll() { 079 return typeByQualifier.values(); 080 } 081 082 public Collection<ResourceType> getAll(Predicate<ResourceType> predicate) { 083 return Collections2.filter(typeByQualifier.values(), predicate); 084 } 085 086 public List<String> getChildrenQualifiers(String qualifier) { 087 ResourceTypeTree tree = getTree(qualifier); 088 if (tree != null) { 089 return tree.getChildren(qualifier); 090 } 091 return Collections.emptyList(); 092 } 093 094 public List<ResourceType> getChildren(String qualifier) { 095 return Lists.transform(getChildrenQualifiers(qualifier), new Function<String, ResourceType>() { 096 public ResourceType apply(String s) { 097 return typeByQualifier.get(s); 098 } 099 }); 100 } 101 102 public List<String> getLeavesQualifiers(String qualifier) { 103 ResourceTypeTree tree = getTree(qualifier); 104 if (tree != null) { 105 return tree.getLeaves(); 106 } 107 return Collections.emptyList(); 108 } 109 110 public ResourceTypeTree getTree(String qualifier) { 111 return treeByQualifier.get(qualifier); 112 } 113}