View Javadoc

1   package com.atlassian.sal.api.features;
2   
3   import com.google.common.base.Predicate;
4   import com.google.common.collect.ImmutableMap;
5   import com.google.common.collect.ImmutableSet;
6   import com.google.common.collect.Iterables;
7   import com.google.common.collect.Maps;
8   
9   import javax.annotation.concurrent.Immutable;
10  
11  import static com.google.common.base.Preconditions.checkNotNull;
12  
13  /**
14   * Represents a set of enabled features.
15   *
16   * @since 2.10
17   */
18  @Immutable
19  public final class EnabledDarkFeatures
20  {
21      /**
22       * Shorthand in case there are no enabled dark features or they are disabled
23       */
24      public static final EnabledDarkFeatures NONE = new EnabledDarkFeatures(ImmutableMap.<FeatureKeyScope, ImmutableSet<String>>of());
25  
26      private final ImmutableMap<FeatureKeyScope, ImmutableSet<String>> enabledFeatures;
27  
28      /**
29       * Create an instance of enabled features based on the given map.
30       * @param enabledFeatures a map of enabled features
31       */
32      public EnabledDarkFeatures(final ImmutableMap<FeatureKeyScope, ImmutableSet<String>> enabledFeatures)
33      {
34          checkNotNull(enabledFeatures, "enabledFeatures");
35          this.enabledFeatures = ImmutableMap.copyOf(enabledFeatures);
36      }
37  
38      /**
39       * Return all enabled feature keys.
40       * @return all enabled feature keys
41       */
42      public ImmutableSet<String> getFeatureKeys()
43      {
44          return ImmutableSet.copyOf(Iterables.concat(enabledFeatures.values()));
45      }
46  
47      /**
48       * Returns all enabled feature keys matching the given criteria.
49       * @param criteria the filter condition to be applied on the set of enabled features
50       * @return the filtered set of enabled features
51       */
52      public ImmutableSet<String> getFeatureKeys(final Predicate<FeatureKeyScope> criteria)
53      {
54          checkNotNull(criteria, "criteria");
55          return ImmutableSet.copyOf(Iterables.concat(Maps.filterKeys(enabledFeatures, criteria).values()));
56      }
57  
58      /**
59       * Check that the given feature is enabled
60       * @param featureKey the feature key to be checked
61       * @return <code>true</code> if the given feature key is enabled, <code>false</code> otherwise
62       */
63      public boolean isFeatureEnabled(final String featureKey)
64      {
65          checkNotNull(featureKey, "featureKey");
66          return Iterables.contains(Iterables.concat(enabledFeatures.values()), featureKey);
67      }
68  
69      @Override
70      public String toString()
71      {
72          return "EnabledDarkFeatures{enabledFeatures=" + enabledFeatures + '}';
73      }
74  
75  }