001/*
002 * SonarQube
003 * Copyright (C) 2009-2016 SonarSource SA
004 * mailto:contact 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 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.ImmutableSet;
031import com.google.common.collect.Lists;
032import com.google.common.collect.Maps;
033import com.google.common.collect.Sets;
034import java.util.ArrayList;
035import java.util.Collection;
036import java.util.Collections;
037import java.util.List;
038import java.util.Map;
039import javax.annotation.Nullable;
040import org.sonar.api.server.ServerSide;
041
042/**
043 * @since 2.14
044 */
045@Beta
046@ServerSide
047public class ResourceTypes {
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> orderedTypes;
059  private final Collection<ResourceType> rootTypes;
060
061  public ResourceTypes() {
062    this(new ResourceTypeTree[0]);
063  }
064
065  public ResourceTypes(ResourceTypeTree[] trees) {
066    Preconditions.checkNotNull(trees);
067
068    Map<String, ResourceTypeTree> treeMap = Maps.newHashMap();
069    Map<String, ResourceType> typeMap = Maps.newLinkedHashMap();
070    Collection<ResourceType> rootsSet = Sets.newHashSet();
071
072    for (ResourceTypeTree tree : trees) {
073      rootsSet.add(tree.getRootType());
074      for (ResourceType type : tree.getTypes()) {
075        if (treeMap.containsKey(type.getQualifier())) {
076          throw new IllegalStateException("Qualifier " + type.getQualifier() + " is defined in several trees");
077        }
078        treeMap.put(type.getQualifier(), tree);
079        typeMap.put(type.getQualifier(), type);
080      }
081    }
082    treeByQualifier = ImmutableMap.copyOf(treeMap);
083    typeByQualifier = ImmutableMap.copyOf(typeMap);
084    rootTypes = ImmutableList.copyOf(rootsSet);
085
086    List<ResourceType> mutableOrderedTypes = new ArrayList<>();
087    ResourceType view = null;
088    ResourceType subView = null;
089    for (ResourceType resourceType : typeByQualifier.values()) {
090      if (Qualifiers.VIEW.equals(resourceType.getQualifier())) {
091        view = resourceType;
092      } else if (Qualifiers.SUBVIEW.equals(resourceType.getQualifier())) {
093        subView = resourceType;
094      } else {
095        mutableOrderedTypes.add(resourceType);
096      }
097    }
098    if (subView != null) {
099      mutableOrderedTypes.add(0, subView);
100    }
101    if (view != null) {
102      mutableOrderedTypes.add(0, view);
103    }
104
105    orderedTypes = ImmutableSet.copyOf(mutableOrderedTypes);
106  }
107
108  public ResourceType get(String qualifier) {
109    ResourceType type = typeByQualifier.get(qualifier);
110    return type != null ? type : ResourceType.builder(qualifier).build();
111  }
112
113  public Collection<ResourceType> getAll() {
114    return typeByQualifier.values();
115  }
116
117  public Collection<ResourceType> getAllOrdered() {
118    return orderedTypes;
119  }
120
121  public Collection<ResourceType> getRoots() {
122    return rootTypes;
123  }
124
125  public Collection<ResourceType> getAll(Predicate<ResourceType> predicate) {
126    return Collections2.filter(typeByQualifier.values(), predicate);
127  }
128
129  private static class PropertyKeyPredicate implements Predicate<ResourceType> {
130    private final String propertyKey;
131
132    public PropertyKeyPredicate(String propertyKey) {
133      this.propertyKey = propertyKey;
134    }
135
136    @Override
137    public boolean apply(@Nullable ResourceType input) {
138      return input != null && input.hasProperty(propertyKey);
139    }
140  }
141
142  public Collection<ResourceType> getAllWithPropertyKey(final String propertyKey) {
143    return Collections2.filter(typeByQualifier.values(), new PropertyKeyPredicate(propertyKey));
144  }
145
146  private static class StringPropertyValuePredicate implements Predicate<ResourceType> {
147    private final String propertyValue;
148    private final String propertyKey;
149
150    public StringPropertyValuePredicate(String propertyValue, String propertyKey) {
151      this.propertyValue = propertyValue;
152      this.propertyKey = propertyKey;
153    }
154
155    @Override
156    public boolean apply(@Nullable ResourceType input) {
157      return input != null && Objects.equal(propertyValue, input.getStringProperty(propertyKey));
158    }
159  }
160
161  public Collection<ResourceType> getAllWithPropertyValue(final String propertyKey, final String propertyValue) {
162    return Collections2.filter(typeByQualifier.values(), new StringPropertyValuePredicate(propertyValue, propertyKey));
163  }
164
165  private static class BooleanPropertyValuePredicate implements Predicate<ResourceType> {
166    private final String propertyKey;
167    private final boolean propertyValue;
168
169    public BooleanPropertyValuePredicate(String propertyKey, boolean propertyValue) {
170      this.propertyKey = propertyKey;
171      this.propertyValue = propertyValue;
172    }
173
174    @Override
175    public boolean apply(@Nullable ResourceType input) {
176      return input != null && input.getBooleanProperty(propertyKey) == propertyValue;
177    }
178  }
179
180  public Collection<ResourceType> getAllWithPropertyValue(final String propertyKey, final boolean propertyValue) {
181    return Collections2.filter(typeByQualifier.values(), new BooleanPropertyValuePredicate(propertyKey, propertyValue));
182  }
183
184  public List<String> getChildrenQualifiers(String qualifier) {
185    ResourceTypeTree tree = getTree(qualifier);
186    if (tree != null) {
187      return tree.getChildren(qualifier);
188    }
189    return Collections.emptyList();
190  }
191
192  public List<ResourceType> getChildren(String qualifier) {
193    return Lists.transform(getChildrenQualifiers(qualifier), new Function<String, ResourceType>() {
194      @Override
195      public ResourceType apply(String s) {
196        return typeByQualifier.get(s);
197      }
198    });
199  }
200
201  public List<String> getLeavesQualifiers(String qualifier) {
202    ResourceTypeTree tree = getTree(qualifier);
203    if (tree != null) {
204      return tree.getLeaves();
205    }
206    return Collections.emptyList();
207  }
208
209  public ResourceTypeTree getTree(String qualifier) {
210    return treeByQualifier.get(qualifier);
211  }
212
213  public ResourceType getRoot(String qualifier) {
214    return getTree(qualifier).getRootType();
215  }
216
217}