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