瀏覽代碼

Tidy up some common parser files.

Change-Id: I3ed34a6e2dc899c24ba39b198b748be7c0f1812c
Reviewed-on: http://gerrit.dmdirc.com/3951
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
changes/51/3951/2
Chris Smith 9 年之前
父節點
當前提交
20f3e950cb

+ 1
- 4
src/com/dmdirc/parser/common/AwayState.java 查看文件

@@ -24,9 +24,6 @@ package com.dmdirc.parser.common;
24 24
 
25 25
 /**
26 26
  * Away states.
27
- *
28
- * @author Shane
29
- * @since 0.6.3
30 27
  */
31 28
 public enum AwayState {
32 29
 
@@ -42,7 +39,7 @@ public enum AwayState {
42 39
 
43 40
     /**
44 41
      * Adds a friendly name to the away state.
45
-     * <p/>
42
+     *
46 43
      * @param friendlyName Friendly name
47 44
      */
48 45
     AwayState(final String friendlyName) {

+ 2
- 1
src/com/dmdirc/parser/common/BaseChannelClientInfo.java 查看文件

@@ -66,13 +66,14 @@ public abstract class BaseChannelClientInfo implements ChannelClientInfo {
66 66
     }
67 67
 
68 68
     @Override
69
+    @SuppressWarnings("ReturnOfCollectionOrArrayField")
69 70
     public Map<Object, Object> getMap() {
70 71
         return map;
71 72
     }
72 73
 
73 74
     @Override
74 75
     public String toString() {
75
-        return getImportantMode() + client.toString();
76
+        return getImportantMode() + client;
76 77
     }
77 78
 
78 79
 }

+ 1
- 0
src/com/dmdirc/parser/common/BaseChannelInfo.java 查看文件

@@ -53,6 +53,7 @@ public abstract class BaseChannelInfo implements ChannelInfo {
53 53
     }
54 54
 
55 55
     @Override
56
+    @SuppressWarnings("ReturnOfCollectionOrArrayField")
56 57
     public Map<Object, Object> getMap() {
57 58
         return map;
58 59
     }

+ 6
- 1
src/com/dmdirc/parser/common/BaseClientInfo.java 查看文件

@@ -40,7 +40,11 @@ public abstract class BaseClientInfo implements ClientInfo {
40 40
     private final Map<Object, Object> map = new HashMap<>();
41 41
 
42 42
     /** The user's details. */
43
-    private String nick, user, host, realname, account = null;
43
+    private String nick;
44
+    private String user;
45
+    private String host;
46
+    private String realname;
47
+    private String account;
44 48
 
45 49
     /**
46 50
      * Creates a new base client info for the specified parser with the
@@ -130,6 +134,7 @@ public abstract class BaseClientInfo implements ClientInfo {
130 134
     }
131 135
 
132 136
     @Override
137
+    @SuppressWarnings("ReturnOfCollectionOrArrayField")
133 138
     public Map<Object, Object> getMap() {
134 139
         return map;
135 140
     }

+ 8
- 6
src/com/dmdirc/parser/common/BaseParser.java 查看文件

@@ -22,6 +22,8 @@
22 22
 
23 23
 package com.dmdirc.parser.common;
24 24
 
25
+import com.dmdirc.parser.interfaces.ChannelInfo;
26
+import com.dmdirc.parser.interfaces.ClientInfo;
25 27
 import com.dmdirc.parser.interfaces.callbacks.CallbackInterface;
26 28
 
27 29
 import java.net.InetSocketAddress;
@@ -35,11 +37,10 @@ import java.util.List;
35 37
 import java.util.Map;
36 38
 
37 39
 /**
38
- * Implements common base functionality for parsers.<p>
39
- * Implementations of this class must be annotated with
40
- * {@link ChildImplementations} to define the implementations to use for
41
- * instances of {@link com.dmdirc.parser.interfaces.ClientInfo},
42
- * {@link com.dmdirc.parser.interfaces.ChannelInfo}, etc.
40
+ * Implements common base functionality for parsers.
41
+ * <p>
42
+ * Implementations of this class must be annotated with {@link ChildImplementations} to define
43
+ * the implementations to use for instances of {@link ClientInfo}, {@link ChannelInfo}, etc.
43 44
  */
44 45
 public abstract class BaseParser extends ThreadedParser {
45 46
 
@@ -83,7 +84,7 @@ public abstract class BaseParser extends ThreadedParser {
83 84
 
84 85
         final Map<Class<?>, Class<?>> implementations = new HashMap<>();
85 86
 
86
-        for (Class<?> child : this.getClass().getAnnotation(ChildImplementations.class).value()) {
87
+        for (Class<?> child : getClass().getAnnotation(ChildImplementations.class).value()) {
87 88
             for (Class<?> iface : child.getInterfaces()) {
88 89
                 implementations.put(iface, child);
89 90
             }
@@ -177,6 +178,7 @@ public abstract class BaseParser extends ThreadedParser {
177 178
     }
178 179
 
179 180
     @Override
181
+    @SuppressWarnings("ReturnOfCollectionOrArrayField")
180 182
     public Map<Object, Object> getMap() {
181 183
         return map;
182 184
     }

+ 0
- 2
src/com/dmdirc/parser/common/CallbackNotFoundException.java 查看文件

@@ -24,8 +24,6 @@ package com.dmdirc.parser.common;
24 24
 
25 25
 /**
26 26
  * IRC Parser Callback Exception.
27
- *
28
- * @author Shane Mc Cormack
29 27
  */
30 28
 public class CallbackNotFoundException extends RuntimeException {
31 29
     /**

+ 0
- 3
src/com/dmdirc/parser/common/ChannelJoinRequest.java 查看文件

@@ -24,9 +24,6 @@ package com.dmdirc.parser.common;
24 24
 
25 25
 /**
26 26
  * Describes the information needed to try and join a channel.
27
- *
28
- * @author chris
29
- * @since 0.6.4
30 27
  */
31 28
 public class ChannelJoinRequest {
32 29
 

+ 3
- 3
src/com/dmdirc/parser/common/ChannelListModeItem.java 查看文件

@@ -22,12 +22,12 @@
22 22
 
23 23
 package com.dmdirc.parser.common;
24 24
 
25
+import com.dmdirc.parser.interfaces.Parser;
26
+
25 27
 /**
26 28
  * Contains Channel List Mode information.
27 29
  *
28
- * @author Shane Mc Cormack
29
- * @author Chris Smith
30
- * @see com.dmdirc.parser.interfaces.Parser
30
+ * @see Parser
31 31
  */
32 32
 public class ChannelListModeItem {
33 33
 

+ 0
- 2
src/com/dmdirc/parser/common/CompositionState.java 查看文件

@@ -24,8 +24,6 @@ package com.dmdirc.parser.common;
24 24
 
25 25
 /**
26 26
  * Possible composition states for remote users.
27
- *
28
- * @since 0.6.6
29 27
  */
30 28
 public enum CompositionState {
31 29
 

+ 0
- 3
src/com/dmdirc/parser/common/DefaultStringConverter.java 查看文件

@@ -26,9 +26,6 @@ import com.dmdirc.parser.interfaces.StringConverter;
26 26
 
27 27
 /**
28 28
  * A {@link StringConverter} that uses the system's default charset.
29
- *
30
- * @since 0.6.3m2
31
- * @author chris
32 29
  */
33 30
 public class DefaultStringConverter implements StringConverter {
34 31
 

+ 103
- 51
src/com/dmdirc/parser/common/IgnoreList.java 查看文件

@@ -46,7 +46,7 @@ public class IgnoreList {
46 46
      *
47 47
      * @param items Items to add to this RegexStringList
48 48
      */
49
-    public IgnoreList(final List<String> items) {
49
+    public IgnoreList(final Iterable<String> items) {
50 50
         addAll(items);
51 51
     }
52 52
 
@@ -70,7 +70,7 @@ public class IgnoreList {
70 70
      *
71 71
      * @param patterns A list of patterns to be added
72 72
      */
73
-    public void addAll(final List<String> patterns) {
73
+    public void addAll(final Iterable<String> patterns) {
74 74
         for (String pattern : patterns) {
75 75
             add(pattern);
76 76
         }
@@ -82,7 +82,7 @@ public class IgnoreList {
82 82
      * @param position Position in the list to remove
83 83
      */
84 84
     public void remove(final int position) {
85
-        if (position < this.count()) {
85
+        if (position < count()) {
86 86
             ignoreInfo.remove(position);
87 87
         }
88 88
     }
@@ -102,8 +102,8 @@ public class IgnoreList {
102 102
      * @throws PatternSyntaxException if one of the items in the list is an invalid regex
103 103
      */
104 104
     public int matches(final String check) throws PatternSyntaxException {
105
-        for (int i = 0; i < this.count(); ++i) {
106
-            if (check.matches("(?i)" + this.get(i))) {
105
+        for (int i = 0; i < count(); i++) {
106
+            if (check.matches("(?i)" + get(i))) {
107 107
                 return i;
108 108
             }
109 109
         }
@@ -120,11 +120,7 @@ public class IgnoreList {
120 120
      */
121 121
     public boolean matches(final int position, final String check) throws
122 122
             PatternSyntaxException {
123
-        if (position < this.count()) {
124
-            return check.matches("(?i)" + this.get(position));
125
-        } else {
126
-            return false;
127
-        }
123
+        return position < count() && check.matches("(?i)" + get(position));
128 124
     }
129 125
 
130 126
     /**
@@ -134,7 +130,7 @@ public class IgnoreList {
134 130
      * @return String showing the pattern. ("" if position isn't valid)
135 131
      */
136 132
     public String get(final int position) {
137
-        if (position < this.count()) {
133
+        if (position < count()) {
138 134
             return ignoreInfo.get(position);
139 135
         } else {
140 136
             return "";
@@ -148,7 +144,7 @@ public class IgnoreList {
148 144
      * @param pattern New pattern
149 145
      */
150 146
     public void set(final int position, final String pattern) {
151
-        if (position < this.count()) {
147
+        if (position < count()) {
152 148
             ignoreInfo.set(position, pattern);
153 149
         }
154 150
     }
@@ -220,62 +216,87 @@ public class IgnoreList {
220 216
     protected static String regexToSimple(final String regex)
221 217
             throws UnsupportedOperationException {
222 218
         final StringBuilder res = new StringBuilder(regex.length());
223
-        boolean escaped = false;
224
-        boolean inchar = false;
219
+        final ConversionState state = new ConversionState();
225 220
 
226 221
         for (char part : regex.toCharArray()) {
227
-            if (inchar) {
228
-                inchar = false;
229
-
230
-                if (part == '*') {
231
-                    res.append(part);
232
-                    continue;
233
-                } else {
234
-                    res.append('?');
235
-                }
236
-            }
237
-
238
-            if (escaped) {
239
-                if (part == '?' || part == '*') {
240
-                    throw new UnsupportedOperationException("Cannot convert to"
241
-                            + " simple expression: ? or * is escaped.");
242
-                }
243
-
244
-                res.append(part);
245
-                escaped = false;
246
-            } else if (part == '\\') {
247
-                escaped = true;
248
-            } else if (part == '.') {
249
-                inchar = true;
250
-            } else if (part == '.' || part == '^' || part == '$' || part
251
-                    == '['
252
-                    || part == ']' || part == '\\' || part == '(' || part == ')'
253
-                    || part == '{' || part == '}' || part == '|' || part == '+'
254
-                    || part == '*' || part == '?') {
255
-                throw new UnsupportedOperationException("Cannot convert to"
256
-                        + " simple expression: unescaped special char: " + part);
222
+            if (state.getAndResetLastCharWasDot()) {
223
+                handleCharFollowingDot(state, res, part);
224
+            } else if (state.getAndResetEscaped()) {
225
+                handleEscapedChar(res, part);
257 226
             } else {
258
-                res.append(part);
227
+                handleNormalChar(state, res, part);
259 228
             }
260 229
         }
261 230
 
262
-        if (escaped) {
231
+        if (state.getAndResetEscaped()) {
263 232
             throw new UnsupportedOperationException("Cannot convert to "
264 233
                     + "simple expression: trailing backslash");
265
-        } else if (inchar) {
234
+        } else if (state.getAndResetLastCharWasDot()) {
266 235
             res.append('?');
267 236
         }
268 237
 
269 238
         return res.toString();
270 239
     }
271 240
 
241
+    /**
242
+     * Handles a single char that was preceded by a '.' when converting from a regex.
243
+     *
244
+     * @param state The current state of conversion.
245
+     * @param builder The builder to append data to.
246
+     * @param character The character in question.
247
+     */
248
+    private static void handleCharFollowingDot(final ConversionState state,
249
+            final StringBuilder builder, final char character) {
250
+        if (character == '*') {
251
+            builder.append('*');
252
+        } else {
253
+            builder.append('?');
254
+            handleNormalChar(state, builder, character);
255
+        }
256
+    }
257
+
258
+    /**
259
+     * Handles a single char that was escaped when converting from a regex.
260
+     *
261
+     * @param builder The builder to append data to.
262
+     * @param character The character in question.
263
+     */
264
+    private static void handleEscapedChar(final StringBuilder builder, final char character) {
265
+        if (character == '?' || character == '*') {
266
+            throw new UnsupportedOperationException("Cannot convert to"
267
+                    + " simple expression: ? or * is escaped.");
268
+        }
269
+
270
+        builder.append(character);
271
+    }
272
+
273
+    /**
274
+     * Handles a single normal character when converting from a regex.
275
+     *
276
+     * @param state The current state of conversion.
277
+     * @param builder The builder to append data to.
278
+     * @param character The character in question.
279
+     */
280
+    private static void handleNormalChar(final ConversionState state,
281
+            final StringBuilder builder, final char character) {
282
+        if (character == '\\') {
283
+            state.setEscaped();
284
+        } else if (character == '.') {
285
+            state.setLastCharWasDot();
286
+        } else if ("^$[](){}|+*?".indexOf(character) > -1) {
287
+            throw new UnsupportedOperationException("Cannot convert to"
288
+                    + " simple expression: unescaped special char: " + character);
289
+        } else {
290
+            builder.append(character);
291
+        }
292
+    }
293
+
272 294
     /**
273 295
      * Converts a simple expression to a regular expression.
274 296
      *
275 297
      * @param regex The simple expression to be converted
276 298
      * @return A corresponding regular expression
277 299
      */
278
-    @SuppressWarnings("fallthrough")
279 300
     protected static String simpleToRegex(final String regex) {
280 301
         final StringBuilder res = new StringBuilder(regex.length());
281 302
 
@@ -293,14 +314,14 @@ public class IgnoreList {
293 314
                 case '}':
294 315
                 case '|':
295 316
                 case '+':
296
-                    res.append('\\');
297
-                    res.append(part);
317
+                    res.append('\\').append(part);
298 318
                     break;
299 319
                 case '?':
300 320
                     res.append('.');
301 321
                     break;
302 322
                 case '*':
303
-                    res.append('.');
323
+                    res.append(".*");
324
+                    break;
304 325
                 default:
305 326
                     res.append(part);
306 327
                     break;
@@ -309,4 +330,35 @@ public class IgnoreList {
309 330
 
310 331
         return res.toString();
311 332
     }
333
+
334
+    /**
335
+     * Utility class to represent state while converting a regex to a simple form.
336
+     */
337
+    private static final class ConversionState {
338
+
339
+        private boolean escaped;
340
+        private boolean lastCharWasDot;
341
+
342
+        public boolean getAndResetLastCharWasDot() {
343
+            final boolean oldValue = lastCharWasDot;
344
+            lastCharWasDot = false;
345
+            return oldValue;
346
+        }
347
+
348
+        public void setLastCharWasDot() {
349
+            lastCharWasDot = true;
350
+        }
351
+
352
+        public boolean getAndResetEscaped() {
353
+            final boolean oldValue = escaped;
354
+            escaped = false;
355
+            return oldValue;
356
+        }
357
+
358
+        public void setEscaped() {
359
+            escaped = true;
360
+        }
361
+
362
+    }
363
+
312 364
 }

+ 4
- 4
src/com/dmdirc/parser/common/MyInfo.java 查看文件

@@ -22,12 +22,12 @@
22 22
 
23 23
 package com.dmdirc.parser.common;
24 24
 
25
+import com.dmdirc.parser.interfaces.Parser;
26
+
25 27
 /**
26 28
  * Contains User information.
27 29
  *
28
- * @author Shane Mc Cormack
29
- * @author Chris Smith
30
- * @see com.dmdirc.parser.interfaces.Parser
30
+ * @see Parser
31 31
  */
32 32
 public class MyInfo {
33 33
 
@@ -71,7 +71,7 @@ public class MyInfo {
71 71
             nickname = result;
72 72
             username = nickname;
73 73
             realname = nickname + " - DMDIrc";
74
-            altNickname = nickname + "-";
74
+            altNickname = nickname + '-';
75 75
         }
76 76
     }
77 77
 

+ 0
- 2
src/com/dmdirc/parser/common/ParserError.java 查看文件

@@ -24,8 +24,6 @@ package com.dmdirc.parser.common;
24 24
 
25 25
 /**
26 26
  * IRC Parser Error.
27
- *
28
- * @author Shane Mc Cormack
29 27
  */
30 28
 public final class ParserError {
31 29
     /** Error is potentially Fatal, Desync 99% Guaranteed! */

+ 0
- 3
src/com/dmdirc/parser/common/SystemEncoder.java 查看文件

@@ -27,9 +27,6 @@ import com.dmdirc.parser.interfaces.Encoder;
27 27
 /**
28 28
  * A basic {@link Encoder} implementation that just encodes messages using
29 29
  * the system's default charset.
30
- *
31
- * @since 0.6.5
32
- * @author chris
33 30
  */
34 31
 public class SystemEncoder implements Encoder {
35 32
 

+ 10
- 8
src/com/dmdirc/parser/common/ThreadedParser.java 查看文件

@@ -32,29 +32,31 @@ public abstract class ThreadedParser implements Parser {
32 32
 
33 33
     /** Parser Control Thread. */
34 34
     protected Thread controlThread;
35
+    /** Object to use to lock access to {@link #controlThread}. */
36
+    protected final Object controlThreadLock = new Object();
35 37
 
36 38
     @Override
37 39
     public void connect() {
38
-        synchronized (this) {
39
-            if (controlThread != null) {
40
-                // To ensure correct internal state, parsers must be recreated for
41
-                // new connections rather than being recycled.
42
-                throw new UnsupportedOperationException("This parser has already been running.");
43
-            } else {
44
-                controlThread = new Thread(new Runnable(){
40
+        synchronized (controlThreadLock) {
41
+            if (controlThread == null) {
42
+                controlThread = new Thread(new Runnable() {
45 43
                     @Override
46 44
                     public void run() {
47 45
                         ThreadedParser.this.run();
48 46
                     }
49 47
                 }, "Parser Thread");
50 48
                 controlThread.start();
49
+            } else {
50
+                // To ensure correct internal state, parsers must be recreated for
51
+                // new connections rather than being recycled.
52
+                throw new UnsupportedOperationException("This parser has already been running.");
51 53
             }
52 54
         }
53 55
     }
54 56
 
55 57
     @Override
56 58
     public void disconnect(final String message) {
57
-        synchronized (this) {
59
+        synchronized (controlThreadLock) {
58 60
             if (controlThread != null) {
59 61
                 controlThread.interrupt();
60 62
             }

Loading…
取消
儲存