View Javadoc
1   package com.atlassian.plugin;
2   
3   import com.google.common.collect.ImmutableListMultimap;
4   import com.google.common.collect.ImmutableSet;
5   import com.google.common.collect.Multimap;
6   
7   import java.util.Set;
8   
9   import static com.atlassian.plugin.PluginDependencies.Type.DYNAMIC;
10  import static com.atlassian.plugin.PluginDependencies.Type.MANDATORY;
11  import static com.atlassian.plugin.PluginDependencies.Type.OPTIONAL;
12  
13  /**
14   * All plugin keys which are dependents of another plugin, divided into OSGi style import resolutions: mandatory
15   * (default), optional and dynamic.
16   *
17   * @see Plugin#getDependencies()
18   * @since 4.0
19   */
20  public class PluginDependencies {
21      // dependency types in the order of significance
22      public enum Type {
23          MANDATORY,
24          OPTIONAL,
25          DYNAMIC;
26  
27          public boolean lessSignificant(final Type other) {
28              return ordinal() > other.ordinal();
29          }
30      }
31  
32      // Plugin keys of mandatory, optional and dynamic dependencies
33      // keys can appear in multiple sets
34      private final Set<String> mandatory;
35      private final Set<String> optional;
36      private final Set<String> dynamic;
37      /**
38       * Plugin keys of all dependencies
39       */
40      private final Set<String> all;
41      /**
42       * A list of dependency types by plugin key
43       */
44      private final Multimap<String, Type> byPluginKey;
45  
46      public PluginDependencies() {
47          this(null, null, null);
48      }
49  
50      public PluginDependencies(final Set<String> mandatory, final Set<String> optional, final Set<String> dynamic) {
51          this.mandatory = mandatory == null ? ImmutableSet.of() : ImmutableSet.copyOf(mandatory);
52          this.optional = optional == null ? ImmutableSet.of() : ImmutableSet.copyOf(optional);
53          this.dynamic = dynamic == null ? ImmutableSet.of() : ImmutableSet.copyOf(dynamic);
54  
55          this.all = ImmutableSet.<String>builder()
56                  .addAll(this.mandatory)
57                  .addAll(this.optional)
58                  .addAll(this.dynamic)
59                  .build();
60  
61          final ImmutableListMultimap.Builder<String, Type> byPluginKey = ImmutableListMultimap.builder();
62  
63          for (final String key : this.mandatory) {
64              byPluginKey.put(key, MANDATORY);
65          }
66          for (final String key : this.optional) {
67              byPluginKey.put(key, OPTIONAL);
68          }
69          for (final String key : this.dynamic) {
70              byPluginKey.put(key, DYNAMIC);
71          }
72  
73          this.byPluginKey = byPluginKey.build();
74      }
75  
76      public Set<String> getMandatory() {
77          return mandatory;
78      }
79  
80      public Set<String> getOptional() {
81          return optional;
82      }
83  
84      public Set<String> getDynamic() {
85          return dynamic;
86      }
87  
88      public Set<String> getAll() {
89          return all;
90      }
91  
92      public Multimap<String, Type> getByPluginKey() {
93          return byPluginKey;
94      }
95  
96      public static Builder builder() {
97          return new Builder();
98      }
99  
100     public static class Builder {
101         final ImmutableSet.Builder<String> mandatory = new ImmutableSet.Builder<>();
102         final ImmutableSet.Builder<String> optional = new ImmutableSet.Builder<>();
103         final ImmutableSet.Builder<String> dynamic = new ImmutableSet.Builder<>();
104 
105         private Builder() {
106         }
107 
108         public Builder withMandatory(String... pluginKey) {
109             mandatory.add(pluginKey);
110             return this;
111         }
112 
113         public Builder withOptional(String... pluginKey) {
114             optional.add(pluginKey);
115             return this;
116         }
117 
118         public Builder withDynamic(String... pluginKey) {
119             dynamic.add(pluginKey);
120             return this;
121         }
122 
123         public PluginDependencies build() {
124             return new PluginDependencies(mandatory.build(), optional.build(), dynamic.build());
125         }
126     }
127 
128 }