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
9
10
11
12
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
24
25
26
27
28 public ModuleCompleteKey(final String completeKey) {
29 this(pluginKeyFromCompleteKey(completeKey), moduleKeyFromCompleteKey(completeKey));
30 }
31
32
33
34
35
36
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))
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
59
60
61
62
63 public String getModuleKey() {
64 return moduleKey;
65 }
66
67
68
69
70
71
72
73 public String getPluginKey() {
74 return pluginKey;
75 }
76
77
78
79
80
81
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 }