View Javadoc
1   package com.atlassian.plugin;
2   
3   import com.atlassian.annotations.PublicApi;
4   import com.google.common.annotations.VisibleForTesting;
5   import com.google.common.base.Strings;
6   
7   /**
8    * Represents the fully qualified key of a plugin module.
9    * <p>
10   * The complete key is the combination of the plugin key and the "simple" module key.
11   * For example if the plugin key is "com.acme.myplugin" and the module key is "my-foo-module", then the complete key is
12   * "com.acme.myplugin:my-foo-module".
13   */
14  @PublicApi
15  public final class ModuleCompleteKey {
16      @VisibleForTesting
17      protected static final String SEPARATOR = ":";
18  
19      private final String pluginKey;
20      private final String moduleKey;
21  
22      /**
23       * Constructs a ModuleCompleteKey given the String representation of the complete key.
24       * The String representation includes the plugin key, followed by a ':', followed by the simple module key.
25       *
26       * @param completeKey the String representation of the complete key
27       */
28      public ModuleCompleteKey(final String completeKey) {
29          this(pluginKeyFromCompleteKey(completeKey), moduleKeyFromCompleteKey(completeKey));
30      }
31  
32      /**
33       * Constructs a ModuleCompleteKey given the separate plugin key and module key.
34       *
35       * @param pluginKey the plugin key
36       * @param moduleKey the module key
37       */
38      public ModuleCompleteKey(final String pluginKey, final String moduleKey) {
39          this.pluginKey = Strings.nullToEmpty(pluginKey).trim();
40  
41          if (!isValidKey(this.pluginKey)) {
42              throw new IllegalArgumentException("Invalid plugin key specified: " + this.pluginKey);
43          }
44  
45          this.moduleKey = Strings.nullToEmpty(moduleKey).trim();
46  
47          if (Strings.isNullOrEmpty(this.moduleKey)) // just validate that we have a non-empty module key
48          {
49              throw new IllegalArgumentException("Invalid module key specified: " + this.moduleKey);
50          }
51      }
52  
53      private boolean isValidKey(final String key) {
54          return !Strings.isNullOrEmpty(Strings.nullToEmpty(key).trim()) && !key.contains(SEPARATOR);
55      }
56  
57      /**
58       * Returns the "simple" module key.
59       * This is the second half of the "complete" key.
60       *
61       * @return the simple module key.
62       */
63      public String getModuleKey() {
64          return moduleKey;
65      }
66  
67      /**
68       * Returns the plugin key.
69       * This is the first half of the "complete" key.
70       *
71       * @return the plugin key.
72       */
73      public String getPluginKey() {
74          return pluginKey;
75      }
76  
77      /**
78       * Returns the String representation of the complete key.
79       * This includes the plugin key, followed by a ':', followed by the simple module key.
80       *
81       * @return the String representation of the complete key.
82       */
83      public String getCompleteKey() {
84          return pluginKey + SEPARATOR + moduleKey;
85      }
86  
87      @SuppressWarnings("RedundantIfStatement")
88      @Override
89      public boolean equals(final Object o) {
90          if (this == o) {
91              return true;
92          }
93          if (o == null || getClass() != o.getClass()) {
94              return false;
95          }
96  
97          final ModuleCompleteKey that = (ModuleCompleteKey) o;
98  
99          if (!moduleKey.equals(that.moduleKey)) {
100             return false;
101         }
102         if (!pluginKey.equals(that.pluginKey)) {
103             return false;
104         }
105 
106         return true;
107     }
108 
109     @Override
110     public int hashCode() {
111         int result = pluginKey.hashCode();
112         result = 31 * result + moduleKey.hashCode();
113         return result;
114     }
115 
116     @Override
117     public String toString() {
118         return getCompleteKey();
119     }
120 
121     @VisibleForTesting
122     static String pluginKeyFromCompleteKey(final String completeKey) {
123         if (completeKey != null) {
124             return completeKey.split(SEPARATOR)[0];
125         }
126         return "";
127     }
128 
129     @VisibleForTesting
130     static String moduleKeyFromCompleteKey(final String completeKey) {
131         if (completeKey != null) {
132             final String[] split = completeKey.split(SEPARATOR, 2);
133             if (split.length == 2) {
134                 return split[1];
135             }
136         }
137         return "";
138     }
139 }