|
@@ -0,0 +1,137 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2014 DMDirc Developers
|
|
3
|
+ *
|
|
4
|
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+ * of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+ * in the Software without restriction, including without limitation the rights
|
|
7
|
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+ * copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+ * furnished to do so, subject to the following conditions:
|
|
10
|
+ *
|
|
11
|
+ * The above copyright notice and this permission notice shall be included in
|
|
12
|
+ * all copies or substantial portions of the Software.
|
|
13
|
+ *
|
|
14
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+ * SOFTWARE.
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+package com.dmdirc.parser.irc;
|
|
24
|
+
|
|
25
|
+/**
|
|
26
|
+ * Generic mode manager.
|
|
27
|
+ */
|
|
28
|
+public class ModeManager {
|
|
29
|
+
|
|
30
|
+ /** All known modes, in increasing order of importance. */
|
|
31
|
+ private String modes = "";
|
|
32
|
+
|
|
33
|
+ /**
|
|
34
|
+ * Resets the state of this manager, clearing all known modes.
|
|
35
|
+ */
|
|
36
|
+ public void clear() {
|
|
37
|
+ modes = "";
|
|
38
|
+ }
|
|
39
|
+
|
|
40
|
+ /**
|
|
41
|
+ * Replaces all existing modes with the specified ones.
|
|
42
|
+ *
|
|
43
|
+ * @param modes The new modes, in increasing order of importance.
|
|
44
|
+ */
|
|
45
|
+ public void set(final String modes) {
|
|
46
|
+ this.modes = modes;
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ /**
|
|
50
|
+ * Adds a new mode. Modes must be added in increasing order of importance.
|
|
51
|
+ *
|
|
52
|
+ * @param mode The mode that appears in mode strings (e.g. 'o').
|
|
53
|
+ */
|
|
54
|
+ public void add(final char mode) {
|
|
55
|
+ modes += mode;
|
|
56
|
+ }
|
|
57
|
+
|
|
58
|
+ /**
|
|
59
|
+ * Determines if the specified character is a mode (e.g. 'o', 'v').
|
|
60
|
+ *
|
|
61
|
+ * @param mode The mode to be tested
|
|
62
|
+ * @return True if the mode is a mode, false otherwise.
|
|
63
|
+ */
|
|
64
|
+ public boolean isMode(final char mode) {
|
|
65
|
+ return modes.indexOf(mode) > -1;
|
|
66
|
+ }
|
|
67
|
+
|
|
68
|
+ /**
|
|
69
|
+ * Gets the set of all known prefix modes.
|
|
70
|
+ *
|
|
71
|
+ * @return Set of known modes, in increasing order of importance.
|
|
72
|
+ */
|
|
73
|
+ public String getModes() {
|
|
74
|
+ return modes;
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ /**
|
|
78
|
+ * Compares the most important mode of the given mode lists.
|
|
79
|
+ *
|
|
80
|
+ * @param modes1 The first set of modes to compare. Must be ordered by importance.
|
|
81
|
+ * @param modes2 The second set of modes to compare. Must be ordered by importance.
|
|
82
|
+ * @return A negative number of modes2 is more important than modes1; a positive number if
|
|
83
|
+ * modes1 is more important than modes2; zero if the two are equivalent.
|
|
84
|
+ */
|
|
85
|
+ public int compareImportantModes(final String modes1, final String modes2) {
|
|
86
|
+ final char mode1 = modes1.isEmpty() ? ' ' : modes1.charAt(0);
|
|
87
|
+ final char mode2 = modes2.isEmpty() ? ' ' : modes2.charAt(0);
|
|
88
|
+ final int modeValue1 = modes.indexOf(mode1);
|
|
89
|
+ final int modeValue2 = modes.indexOf(mode2);
|
|
90
|
+ return modeValue1 - modeValue2;
|
|
91
|
+ }
|
|
92
|
+
|
|
93
|
+ /**
|
|
94
|
+ * Inserts the specified mode into the correct place in the mode string, maintaining importance
|
|
95
|
+ * order.
|
|
96
|
+ *
|
|
97
|
+ * @param modeString The existing modes to add the new one to.
|
|
98
|
+ * @param mode The new mode to be added.
|
|
99
|
+ * @return A mode string containing all the modes.
|
|
100
|
+ */
|
|
101
|
+ public String insertMode(final String modeString, final char mode) {
|
|
102
|
+ if (modeString.indexOf(mode) > -1) {
|
|
103
|
+ // Don't duplicate an existing mode
|
|
104
|
+ return modeString;
|
|
105
|
+ }
|
|
106
|
+
|
|
107
|
+ final StringBuilder result = new StringBuilder(modeString.length() + 1);
|
|
108
|
+ boolean missing = true;
|
|
109
|
+ final int value = modes.indexOf(mode);
|
|
110
|
+ for (char existingMode : modeString.toCharArray()) {
|
|
111
|
+ if (modes.indexOf(existingMode) < value && missing) {
|
|
112
|
+ // Our new mode is more important, insert it first.
|
|
113
|
+ result.append(mode);
|
|
114
|
+ missing = false;
|
|
115
|
+ }
|
|
116
|
+ result.append(existingMode);
|
|
117
|
+ }
|
|
118
|
+
|
|
119
|
+ if (missing) {
|
|
120
|
+ result.append(mode);
|
|
121
|
+ }
|
|
122
|
+
|
|
123
|
+ return result.toString();
|
|
124
|
+ }
|
|
125
|
+
|
|
126
|
+ /**
|
|
127
|
+ * Removes the specified mode from the mode string.
|
|
128
|
+ *
|
|
129
|
+ * @param modeString The mode string to modify.
|
|
130
|
+ * @param mode The mode to be removed.
|
|
131
|
+ * @return A copy of the mode string with the mode removed.
|
|
132
|
+ */
|
|
133
|
+ public String removeMode(final String modeString, final char mode) {
|
|
134
|
+ return modeString.replace(Character.toString(mode), "");
|
|
135
|
+ }
|
|
136
|
+
|
|
137
|
+}
|