View Javadoc

1   /*
2    * Created by IntelliJ IDEA.
3    * User: Mike
4    * Date: Jul 29, 2004
5    * Time: 3:19:33 PM
6    */
7   package com.atlassian.plugin;
8   
9   import com.google.common.annotations.VisibleForTesting;
10  import org.apache.commons.lang.StringUtils;
11  
12  import static org.apache.commons.lang.StringUtils.isEmpty;
13  
14  public class ModuleCompleteKey
15  {
16      @VisibleForTesting
17      protected static final String SEPARATOR = ":";
18  
19      private final String pluginKey;
20      private final String moduleKey;
21  
22      public ModuleCompleteKey(String completeKey)
23      {
24          this(StringUtils.substringBefore(completeKey, SEPARATOR), StringUtils.substringAfter(completeKey, SEPARATOR));
25      }
26  
27      public ModuleCompleteKey(String pluginKey, String moduleKey)
28      {
29          this.pluginKey = StringUtils.trimToEmpty(pluginKey);
30  
31          if (!isValidKey(this.pluginKey))
32          {
33              throw new IllegalArgumentException("Invalid plugin key specified: " + this.pluginKey);
34          }
35  
36          this.moduleKey = StringUtils.trimToEmpty(moduleKey);
37  
38          if (isEmpty(this.moduleKey)) // just validate that we have a non-empty module key
39          {
40              throw new IllegalArgumentException("Invalid module key specified: " + this.moduleKey);
41          }
42      }
43  
44      private boolean isValidKey(String key)
45      {
46          return StringUtils.isNotBlank(key) && !key.contains(SEPARATOR);
47      }
48  
49      public String getModuleKey()
50      {
51          return moduleKey;
52      }
53  
54      public String getPluginKey()
55      {
56          return pluginKey;
57      }
58  
59      public String getCompleteKey()
60      {
61          return pluginKey + SEPARATOR + moduleKey;
62      }
63  
64      @Override
65      public boolean equals(Object o)
66      {
67          if (this == o)
68          {
69              return true;
70          }
71          if (o == null || getClass() != o.getClass())
72          {
73              return false;
74          }
75  
76          ModuleCompleteKey that = (ModuleCompleteKey) o;
77  
78          if (!moduleKey.equals(that.moduleKey))
79          {
80              return false;
81          }
82          if (!pluginKey.equals(that.pluginKey))
83          {
84              return false;
85          }
86  
87          return true;
88      }
89  
90      @Override
91      public int hashCode()
92      {
93          int result = pluginKey.hashCode();
94          result = 31 * result + moduleKey.hashCode();
95          return result;
96      }
97  
98      @Override
99      public String toString()
100     {
101         return getCompleteKey();
102     }
103 }