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