|
@@ -22,32 +22,22 @@
|
22
|
22
|
|
23
|
23
|
package com.dmdirc.parser.irc;
|
24
|
24
|
|
25
|
|
-import java.util.HashMap;
|
26
|
|
-import java.util.Map;
|
27
|
|
-import java.util.Set;
|
28
|
|
-
|
29
|
25
|
/**
|
30
|
26
|
* Handles prefix modes (those that can be applied to a user in a channel, such as +ohv).
|
31
|
27
|
*/
|
32
|
28
|
public class PrefixModeManager {
|
33
|
29
|
|
34
|
|
- /** Map storing known prefix modes (ohv). */
|
35
|
|
- private final Map<Character, Long> prefixModes = new HashMap<>();
|
36
|
|
- /**
|
37
|
|
- * Map of known prefix modes (ohv) to prefixes (@%+) - Both ways.
|
38
|
|
- * Prefix map contains 2 pairs for each mode. (eg @ => o and o => @)
|
39
|
|
- */
|
40
|
|
- private final Map<Character, Character> prefixMap = new HashMap<>();
|
41
|
|
- /** The next available number for a prefix mode. */
|
42
|
|
- private long nextKeyPrefix = 1;
|
|
30
|
+ /** All known modes, in increasing order of importance. */
|
|
31
|
+ private String modes = "";
|
|
32
|
+ /** All known prefixes, in increasing order of importance. */
|
|
33
|
+ private String prefixes = "";
|
43
|
34
|
|
44
|
35
|
/**
|
45
|
36
|
* Resets the state of this manager, clearing all known modes.
|
46
|
37
|
*/
|
47
|
38
|
public void clear() {
|
48
|
|
- prefixMap.clear();
|
49
|
|
- prefixModes.clear();
|
50
|
|
- nextKeyPrefix = 1;
|
|
39
|
+ modes = "";
|
|
40
|
+ prefixes = "";
|
51
|
41
|
}
|
52
|
42
|
|
53
|
43
|
/**
|
|
@@ -57,7 +47,7 @@ public class PrefixModeManager {
|
57
|
47
|
* @return True if the mode is a prefix mode, false otherwise.
|
58
|
48
|
*/
|
59
|
49
|
public boolean isPrefixMode(final char mode) {
|
60
|
|
- return prefixModes.containsKey(mode);
|
|
50
|
+ return modes.indexOf(mode) > -1;
|
61
|
51
|
}
|
62
|
52
|
|
63
|
53
|
/**
|
|
@@ -67,7 +57,7 @@ public class PrefixModeManager {
|
67
|
57
|
* @return True if the character is a prefix, false otherwise.
|
68
|
58
|
*/
|
69
|
59
|
public boolean isPrefix(final char prefix) {
|
70
|
|
- return !isPrefixMode(prefix) && prefixMap.containsKey(prefix);
|
|
60
|
+ return prefixes.indexOf(prefix) > -1;
|
71
|
61
|
}
|
72
|
62
|
|
73
|
63
|
/**
|
|
@@ -77,19 +67,19 @@ public class PrefixModeManager {
|
77
|
67
|
* @return The prefix corresponding to the mode.
|
78
|
68
|
*/
|
79
|
69
|
public char getPrefixFor(final char mode) {
|
80
|
|
- return prefixMap.get(mode);
|
|
70
|
+ return prefixes.charAt(modes.indexOf(mode));
|
81
|
71
|
}
|
82
|
72
|
|
83
|
73
|
/**
|
84
|
74
|
* Converts a string containing prefix modes into a string containing the corresponding
|
85
|
75
|
* prefixes (e.g. 'ov' becomes '@+').
|
86
|
76
|
*
|
87
|
|
- * @param modes The modes to retrieve prefixes for.
|
|
77
|
+ * @param modeString The modes to retrieve prefixes for.
|
88
|
78
|
* @return The prefixes corresponding to the modes.
|
89
|
79
|
*/
|
90
|
|
- public String getPrefixesFor(final String modes) {
|
91
|
|
- final StringBuilder builder = new StringBuilder(modes.length());
|
92
|
|
- for (char mode : modes.toCharArray()) {
|
|
80
|
+ public String getPrefixesFor(final String modeString) {
|
|
81
|
+ final StringBuilder builder = new StringBuilder(modeString.length());
|
|
82
|
+ for (char mode : modeString.toCharArray()) {
|
93
|
83
|
builder.append(getPrefixFor(mode));
|
94
|
84
|
}
|
95
|
85
|
return builder.toString();
|
|
@@ -102,16 +92,16 @@ public class PrefixModeManager {
|
102
|
92
|
* @return The mode corresponding to the prefix.
|
103
|
93
|
*/
|
104
|
94
|
public char getModeFor(final char prefix) {
|
105
|
|
- return prefixMap.get(prefix);
|
|
95
|
+ return modes.charAt(prefixes.indexOf(prefix));
|
106
|
96
|
}
|
107
|
97
|
|
108
|
98
|
/**
|
109
|
99
|
* Gets the set of all known prefix modes.
|
110
|
100
|
*
|
111
|
|
- * @return Set of known modes.
|
|
101
|
+ * @return Set of known modes, in increasing order of importance.
|
112
|
102
|
*/
|
113
|
|
- public Set<Character> getModes() {
|
114
|
|
- return prefixModes.keySet();
|
|
103
|
+ public CharSequence getModes() {
|
|
104
|
+ return modes;
|
115
|
105
|
}
|
116
|
106
|
|
117
|
107
|
/**
|
|
@@ -121,23 +111,8 @@ public class PrefixModeManager {
|
121
|
111
|
* @param prefix The prefix that is used to show a user has the mode (e.g. '@')
|
122
|
112
|
*/
|
123
|
113
|
public void add(final char mode, final char prefix) {
|
124
|
|
- prefixModes.put(mode, nextKeyPrefix);
|
125
|
|
- prefixMap.put(mode, prefix);
|
126
|
|
- prefixMap.put(prefix, mode);
|
127
|
|
- nextKeyPrefix *= 2;
|
128
|
|
- }
|
129
|
|
-
|
130
|
|
- /**
|
131
|
|
- * Gets a unique numerical value for the specified mode. More important modes have higher
|
132
|
|
- * values.
|
133
|
|
- *
|
134
|
|
- * @param mode The mode to return the value of.
|
135
|
|
- * @return The value of that mode.
|
136
|
|
- * @deprecated These values are an implementation detail, and shouldn't be exposed.
|
137
|
|
- */
|
138
|
|
- @Deprecated
|
139
|
|
- private long getValueOf(final char mode) {
|
140
|
|
- return prefixModes.get(mode);
|
|
114
|
+ modes += mode;
|
|
115
|
+ prefixes += prefix;
|
141
|
116
|
}
|
142
|
117
|
|
143
|
118
|
/**
|
|
@@ -151,9 +126,9 @@ public class PrefixModeManager {
|
151
|
126
|
public int compareImportantModes(final String modes1, final String modes2) {
|
152
|
127
|
final char mode1 = modes1.isEmpty() ? ' ' : modes1.charAt(0);
|
153
|
128
|
final char mode2 = modes2.isEmpty() ? ' ' : modes2.charAt(0);
|
154
|
|
- final long modeValue1 = isPrefixMode(mode1) ? getValueOf(mode1) : 0;
|
155
|
|
- final long modeValue2 = isPrefixMode(mode2) ? getValueOf(mode2) : 0;
|
156
|
|
- return (int) (modeValue1 - modeValue2);
|
|
129
|
+ final int modeValue1 = modes.indexOf(mode1);
|
|
130
|
+ final int modeValue2 = modes.indexOf(mode2);
|
|
131
|
+ return modeValue1 - modeValue2;
|
157
|
132
|
}
|
158
|
133
|
|
159
|
134
|
/**
|
|
@@ -161,45 +136,40 @@ public class PrefixModeManager {
|
161
|
136
|
* considered one who has any mode greater than 'v' (voice), or if voice doesn't exist then
|
162
|
137
|
* any mode at all.
|
163
|
138
|
*
|
164
|
|
- * @param modes The modes to test
|
|
139
|
+ * @param modeString The modes to test
|
165
|
140
|
* @return True if the modes indicate the client is "opped", false otherwise.
|
166
|
141
|
*/
|
167
|
|
- public boolean isOpped(final String modes) {
|
168
|
|
- if (modes.isEmpty()) {
|
169
|
|
- return false;
|
170
|
|
- }
|
171
|
|
-
|
172
|
|
- final long voiceValue = isPrefixMode('v') ? prefixModes.get('v') : 0;
|
173
|
|
- return getValueOf(modes.charAt(0)) > voiceValue;
|
|
142
|
+ public boolean isOpped(final String modeString) {
|
|
143
|
+ return !modeString.isEmpty() && modes.indexOf(modeString.charAt(0)) > modes.indexOf('v');
|
174
|
144
|
}
|
175
|
145
|
|
176
|
146
|
/**
|
177
|
147
|
* Inserts the specified mode into the correct place in the mode string, maintaining importance
|
178
|
148
|
* order.
|
179
|
149
|
*
|
180
|
|
- * @param modes The existing modes to add the new one to.
|
|
150
|
+ * @param modeString The existing modes to add the new one to.
|
181
|
151
|
* @param mode The new mode to be added.
|
182
|
152
|
* @return A mode string containing all the modes.
|
183
|
153
|
*/
|
184
|
|
- public String insertMode(final String modes, final char mode) {
|
185
|
|
- if (modes.indexOf(mode) > -1) {
|
|
154
|
+ public String insertMode(final String modeString, final char mode) {
|
|
155
|
+ if (modeString.indexOf(mode) > -1) {
|
186
|
156
|
// Don't duplicate an existing mode
|
187
|
|
- return modes;
|
|
157
|
+ return modeString;
|
188
|
158
|
}
|
189
|
159
|
|
190
|
|
- final StringBuilder result = new StringBuilder(modes.length() + 1);
|
191
|
|
- boolean found = false;
|
192
|
|
- final long value = getValueOf(mode);
|
193
|
|
- for (char existingMode : modes.toCharArray()) {
|
194
|
|
- if (getValueOf(existingMode) < value && !found) {
|
|
160
|
+ final StringBuilder result = new StringBuilder(modeString.length() + 1);
|
|
161
|
+ boolean missing = true;
|
|
162
|
+ final int value = modes.indexOf(mode);
|
|
163
|
+ for (char existingMode : modeString.toCharArray()) {
|
|
164
|
+ if (modes.indexOf(existingMode) < value && missing) {
|
195
|
165
|
// Our new mode is more important, insert it first.
|
196
|
166
|
result.append(mode);
|
197
|
|
- found = true;
|
|
167
|
+ missing = false;
|
198
|
168
|
}
|
199
|
169
|
result.append(existingMode);
|
200
|
170
|
}
|
201
|
171
|
|
202
|
|
- if (!found) {
|
|
172
|
+ if (missing) {
|
203
|
173
|
result.append(mode);
|
204
|
174
|
}
|
205
|
175
|
|