소스 검색

Pull out non-prefix functionality.

Move mode-only functionality into ModeManager, which can then
be used for other types of modes.

Change-Id: I8309a2f235f70c7a101266744995efcce24e9c1d
Reviewed-on: http://gerrit.dmdirc.com/3962
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
changes/62/3962/2
Chris Smith 9 년 전
부모
커밋
477509caba
3개의 변경된 파일159개의 추가작업 그리고 36개의 파일을 삭제
  1. 1
    1
      src/com/dmdirc/parser/irc/IRCChannelClientInfo.java
  2. 137
    0
      src/com/dmdirc/parser/irc/ModeManager.java
  3. 21
    35
      src/com/dmdirc/parser/irc/PrefixModeManager.java

+ 1
- 1
src/com/dmdirc/parser/irc/IRCChannelClientInfo.java 파일 보기

@@ -180,6 +180,6 @@ public class IRCChannelClientInfo implements ChannelClientInfo {
180 180
      * @param mode The mode to be removed.
181 181
      */
182 182
     public void removeMode(final char mode) {
183
-        modes = modes.replace(Character.toString(mode), "");
183
+        modes = modeManager.removeMode(modes, mode);
184 184
     }
185 185
 }

+ 137
- 0
src/com/dmdirc/parser/irc/ModeManager.java 파일 보기

@@ -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
+}

+ 21
- 35
src/com/dmdirc/parser/irc/PrefixModeManager.java 파일 보기

@@ -28,7 +28,7 @@ package com.dmdirc.parser.irc;
28 28
 public class PrefixModeManager {
29 29
 
30 30
     /** All known modes, in increasing order of importance. */
31
-    private String modes = "";
31
+    private final ModeManager modes = new ModeManager();
32 32
     /** All known prefixes, in increasing order of importance. */
33 33
     private String prefixes = "";
34 34
 
@@ -46,7 +46,7 @@ public class PrefixModeManager {
46 46
      * @param prefixes The corresponding new prefixes, in increasing order of importance.
47 47
      */
48 48
     public void setModes(final String modes, final String prefixes) {
49
-        this.modes = modes;
49
+        this.modes.set(modes);
50 50
         this.prefixes = prefixes;
51 51
     }
52 52
 
@@ -57,7 +57,7 @@ public class PrefixModeManager {
57 57
      * @return True if the mode is a prefix mode, false otherwise.
58 58
      */
59 59
     public boolean isPrefixMode(final char mode) {
60
-        return modes.indexOf(mode) > -1;
60
+        return modes.isMode(mode);
61 61
     }
62 62
 
63 63
     /**
@@ -77,7 +77,7 @@ public class PrefixModeManager {
77 77
      * @return The prefix corresponding to the mode.
78 78
      */
79 79
     public char getPrefixFor(final char mode) {
80
-        return prefixes.charAt(modes.indexOf(mode));
80
+        return prefixes.charAt(modes.getModes().indexOf(mode));
81 81
     }
82 82
 
83 83
     /**
@@ -102,7 +102,7 @@ public class PrefixModeManager {
102 102
      * @return The mode corresponding to the prefix.
103 103
      */
104 104
     public char getModeFor(final char prefix) {
105
-        return modes.charAt(prefixes.indexOf(prefix));
105
+        return modes.getModes().charAt(prefixes.indexOf(prefix));
106 106
     }
107 107
 
108 108
     /**
@@ -111,7 +111,7 @@ public class PrefixModeManager {
111 111
      * @return Set of known modes, in increasing order of importance.
112 112
      */
113 113
     public String getModes() {
114
-        return modes;
114
+        return modes.getModes();
115 115
     }
116 116
 
117 117
     /**
@@ -121,7 +121,7 @@ public class PrefixModeManager {
121 121
      * @param prefix The prefix that is used to show a user has the mode (e.g. '@')
122 122
      */
123 123
     public void add(final char mode, final char prefix) {
124
-        modes += mode;
124
+        modes.add(mode);
125 125
         prefixes += prefix;
126 126
     }
127 127
 
@@ -134,11 +134,7 @@ public class PrefixModeManager {
134 134
      * modes1 is more important than modes2; zero if the two are equivalent.
135 135
      */
136 136
     public int compareImportantModes(final String modes1, final String modes2) {
137
-        final char mode1 = modes1.isEmpty() ? ' ' : modes1.charAt(0);
138
-        final char mode2 = modes2.isEmpty() ? ' ' : modes2.charAt(0);
139
-        final int modeValue1 = modes.indexOf(mode1);
140
-        final int modeValue2 = modes.indexOf(mode2);
141
-        return modeValue1 - modeValue2;
137
+        return modes.compareImportantModes(modes1, modes2);
142 138
     }
143 139
 
144 140
     /**
@@ -150,7 +146,8 @@ public class PrefixModeManager {
150 146
      * @return True if the modes indicate the client is "opped", false otherwise.
151 147
      */
152 148
     public boolean isOpped(final String modeString) {
153
-        return !modeString.isEmpty() && modes.indexOf(modeString.charAt(0)) > modes.indexOf('v');
149
+        return !modeString.isEmpty()
150
+                && modes.getModes().indexOf(modeString.charAt(0)) > modes.getModes().indexOf('v');
154 151
     }
155 152
 
156 153
     /**
@@ -162,28 +159,17 @@ public class PrefixModeManager {
162 159
      * @return A mode string containing all the modes.
163 160
      */
164 161
     public String insertMode(final String modeString, final char mode) {
165
-        if (modeString.indexOf(mode) > -1) {
166
-            // Don't duplicate an existing mode
167
-            return modeString;
168
-        }
169
-
170
-        final StringBuilder result = new StringBuilder(modeString.length() + 1);
171
-        boolean missing = true;
172
-        final int value = modes.indexOf(mode);
173
-        for (char existingMode : modeString.toCharArray()) {
174
-            if (modes.indexOf(existingMode) < value && missing) {
175
-                // Our new mode is more important, insert it first.
176
-                result.append(mode);
177
-                missing = false;
178
-            }
179
-            result.append(existingMode);
180
-        }
181
-
182
-        if (missing) {
183
-            result.append(mode);
184
-        }
185
-
186
-        return result.toString();
162
+        return modes.insertMode(modeString, mode);
187 163
     }
188 164
 
165
+    /**
166
+     * Removes the specified mode from the mode string.
167
+     *
168
+     * @param modeString The mode string to modify.
169
+     * @param mode The mode to be removed.
170
+     * @return A copy of the mode string with the mode removed.
171
+     */
172
+    public String removeMode(final String modeString, final char mode) {
173
+        return modes.removeMode(modeString, mode);
174
+    }
189 175
 }

Loading…
취소
저장