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.annotations.Beta; 023import com.google.common.base.Function; 024import com.google.common.base.Objects; 025import com.google.common.base.Preconditions; 026import com.google.common.base.Predicate; 027import com.google.common.collect.Collections2; 028import com.google.common.collect.ImmutableList; 029import com.google.common.collect.ImmutableMap; 030import com.google.common.collect.Lists; 031import com.google.common.collect.Maps; 032import com.google.common.collect.Sets; 033import org.sonar.api.ServerComponent; 034import org.sonar.api.task.TaskComponent; 035 036import javax.annotation.Nullable; 037 038import java.util.Collection; 039import java.util.Collections; 040import java.util.List; 041import java.util.Map; 042 043/** 044 * @since 2.14 045 */ 046@Beta 047public class ResourceTypes implements TaskComponent, ServerComponent { 048 049 public static final Predicate<ResourceType> AVAILABLE_FOR_FILTERS = new Predicate<ResourceType>() { 050 @Override 051 public boolean apply(@Nullable ResourceType input) { 052 return input != null && input.getBooleanProperty("supportsMeasureFilters"); 053 } 054 }; 055 056 private final Map<String, ResourceTypeTree> treeByQualifier; 057 private final Map<String, ResourceType> typeByQualifier; 058 private final Collection<ResourceType> rootTypes; 059 060 public ResourceTypes(ResourceTypeTree[] trees) { 061 Preconditions.checkNotNull(trees); 062 063 Map<String, ResourceTypeTree> treeMap = Maps.newHashMap(); 064 Map<String, ResourceType> typeMap = Maps.newLinkedHashMap(); 065 Collection<ResourceType> rootsSet = Sets.newHashSet(); 066 067 for (ResourceTypeTree tree : trees) { 068 rootsSet.add(tree.getRootType()); 069 for (ResourceType type : tree.getTypes()) { 070 if (treeMap.containsKey(type.getQualifier())) { 071 throw new IllegalStateException("Qualifier " + type.getQualifier() + " is defined in several trees"); 072 } 073 treeMap.put(type.getQualifier(), tree); 074 typeMap.put(type.getQualifier(), type); 075 } 076 } 077 treeByQualifier = ImmutableMap.copyOf(treeMap); 078 typeByQualifier = ImmutableMap.copyOf(typeMap); 079 rootTypes = ImmutableList.copyOf(rootsSet); 080 } 081 082 public ResourceType get(String qualifier) { 083 ResourceType type = typeByQualifier.get(qualifier); 084 return type != null ? type : ResourceType.builder(qualifier).build(); 085 } 086 087 public Collection<ResourceType> getAll() { 088 return typeByQualifier.values(); 089 } 090 091 public Collection<ResourceType> getRoots() { 092 return rootTypes; 093 } 094 095 public Collection<ResourceType> getAll(Predicate<ResourceType> predicate) { 096 return Collections2.filter(typeByQualifier.values(), predicate); 097 } 098 099 private static class PropertyKeyPredicate implements Predicate<ResourceType> { 100 private final String propertyKey; 101 102 public PropertyKeyPredicate(String propertyKey) { 103 this.propertyKey = propertyKey; 104 } 105 106 @Override 107 public boolean apply(@Nullable ResourceType input) { 108 return input != null && input.hasProperty(propertyKey); 109 } 110 } 111 112 public Collection<ResourceType> getAllWithPropertyKey(final String propertyKey) { 113 return Collections2.filter(typeByQualifier.values(), new PropertyKeyPredicate(propertyKey)); 114 } 115 116 private static class StringPropertyValuePredicate implements Predicate<ResourceType> { 117 private final String propertyValue; 118 private final String propertyKey; 119 120 public StringPropertyValuePredicate(String propertyValue, String propertyKey) { 121 this.propertyValue = propertyValue; 122 this.propertyKey = propertyKey; 123 } 124 125 @Override 126 public boolean apply(@Nullable ResourceType input) { 127 return input != null && Objects.equal(propertyValue, input.getStringProperty(propertyKey)); 128 } 129 } 130 131 public Collection<ResourceType> getAllWithPropertyValue(final String propertyKey, final String propertyValue) { 132 return Collections2.filter(typeByQualifier.values(), new StringPropertyValuePredicate(propertyValue, propertyKey)); 133 } 134 135 private static class BooleanPropertyValuePredicate implements Predicate<ResourceType> { 136 private final String propertyKey; 137 private final boolean propertyValue; 138 139 public BooleanPropertyValuePredicate(String propertyKey, boolean propertyValue) { 140 this.propertyKey = propertyKey; 141 this.propertyValue = propertyValue; 142 } 143 144 @Override 145 public boolean apply(@Nullable ResourceType input) { 146 return input != null && input.getBooleanProperty(propertyKey) == propertyValue; 147 } 148 } 149 150 public Collection<ResourceType> getAllWithPropertyValue(final String propertyKey, final boolean propertyValue) { 151 return Collections2.filter(typeByQualifier.values(), new BooleanPropertyValuePredicate(propertyKey, propertyValue)); 152 } 153 154 public List<String> getChildrenQualifiers(String qualifier) { 155 ResourceTypeTree tree = getTree(qualifier); 156 if (tree != null) { 157 return tree.getChildren(qualifier); 158 } 159 return Collections.emptyList(); 160 } 161 162 public List<ResourceType> getChildren(String qualifier) { 163 return Lists.transform(getChildrenQualifiers(qualifier), new Function<String, ResourceType>() { 164 @Override 165 public ResourceType apply(String s) { 166 return typeByQualifier.get(s); 167 } 168 }); 169 } 170 171 public List<String> getLeavesQualifiers(String qualifier) { 172 ResourceTypeTree tree = getTree(qualifier); 173 if (tree != null) { 174 return tree.getLeaves(); 175 } 176 return Collections.emptyList(); 177 } 178 179 public ResourceTypeTree getTree(String qualifier) { 180 return treeByQualifier.get(qualifier); 181 } 182 183 public ResourceType getRoot(String qualifier){ 184 return getTree(qualifier).getRootType(); 185 } 186 187}