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       * Shorthand in case there are no enabled dark features or they are disabled
22       */
23      public static final EnabledDarkFeatures NONE = new EnabledDarkFeatures(ImmutableMap.<FeatureKeyScope, ImmutableSet<String>>of());
24  
25      private final ImmutableMap<FeatureKeyScope, ImmutableSet<String>> enabledFeatures;
26  
27      /**
28       * Create an instance of enabled features based on the given map.
29       *
30       * @param enabledFeatures a map of enabled features
31       */
32      public EnabledDarkFeatures(final ImmutableMap<FeatureKeyScope, ImmutableSet<String>> enabledFeatures) {
33          checkNotNull(enabledFeatures, "enabledFeatures");
34          this.enabledFeatures = ImmutableMap.copyOf(enabledFeatures);
35      }
36  
37      /**
38       * Return all enabled feature keys.
39       *
40       * @return all enabled feature keys
41       */
42      public ImmutableSet<String> getFeatureKeys() {
43          return ImmutableSet.copyOf(Iterables.concat(enabledFeatures.values()));
44      }
45  
46      /**
47       * Returns all enabled feature keys matching the given criteria.
48       *
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          checkNotNull(criteria, "criteria");
54          return ImmutableSet.copyOf(Iterables.concat(Maps.filterKeys(enabledFeatures, criteria).values()));
55      }
56  
57      /**
58       * Check that the given feature is enabled
59       *
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          checkNotNull(featureKey, "featureKey");
65          return Iterables.contains(Iterables.concat(enabledFeatures.values()), featureKey);
66      }
67  
68      @Override
69      public String toString() {
70          return "EnabledDarkFeatures{enabledFeatures=" + enabledFeatures + '}';
71      }
72  
73  }