Browse Source

Use a ModeManager for boolean chan modes.

Change-Id: Ie4986e34e3408a8906c99b0c318ccbe58c8d5019
Reviewed-on: http://gerrit.dmdirc.com/3964
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
changes/64/3964/2
Chris Smith 9 years ago
parent
commit
b72a5d72e8

+ 1
- 1
src/com/dmdirc/parser/interfaces/Parser.java View File

@@ -308,7 +308,7 @@ public interface Parser {
308 308
     int getMaxTopicLength();
309 309
 
310 310
     /**
311
-     * Retrieves an alphabetically-sorted list of boolean channel modes.
311
+     * Retrieves a list of boolean channel modes.
312 312
      * Boolean channel modes may only be set or unset, and do not take any
313 313
      * arguments.
314 314
      *

+ 14
- 22
src/com/dmdirc/parser/irc/IRCChannelInfo.java View File

@@ -61,7 +61,7 @@ public class IRCChannelInfo implements ChannelInfo {
61 61
     /** Has this channel ever had a topic? */
62 62
     private boolean hadTopic;
63 63
     /** Known boolean-modes for channel. */
64
-    private long modes;
64
+    private String modes;
65 65
     /** Reference to the parser object that owns this channel, Used for modes. */
66 66
     private final IRCParser parser; // Reference to parser object that owns this channel. Used for Modes
67 67
     /** Channel Name. */
@@ -76,9 +76,9 @@ public class IRCChannelInfo implements ChannelInfo {
76 76
      * LinkedList storing status of mode adding.
77 77
      * if an item is in this list for a mode, we are expecting new items for the list
78 78
      */
79
-    private final List<Character> addingModes = new LinkedList<>();
79
+    private final Collection<Character> addingModes = new LinkedList<>();
80 80
     /** Modes waiting to be sent to the server. */
81
-    private final List<String> modeQueue = new LinkedList<>();
81
+    private final Collection<String> modeQueue = new LinkedList<>();
82 82
     /** A Map to allow applications to attach misc data to this object. */
83 83
     private final Map<Object, Object> map;
84 84
     /** Queue of requested list modes. */
@@ -426,20 +426,20 @@ public class IRCChannelInfo implements ChannelInfo {
426 426
     }
427 427
 
428 428
     /**
429
-     * Set the channel modes (as an integer).
429
+     * Set the channel modes.
430 430
      *
431
-     * @param nNewMode new long representing channel modes. (Boolean only)
431
+     * @param nNewMode new boolean channel modes
432 432
      */
433
-    public void setMode(final long nNewMode) {
433
+    public void setMode(final String nNewMode) {
434 434
         modes = nNewMode;
435 435
     }
436 436
 
437 437
     /**
438
-     * Get the channel modes (as an integer).
438
+     * Get the channel modes.
439 439
      *
440
-     * @return long representing channel modes. (Boolean only)
440
+     * @return the boolean channel modes.
441 441
      */
442
-    public long getMode() {
442
+    public String getMode() {
443 443
         return modes;
444 444
     }
445 445
 
@@ -447,17 +447,9 @@ public class IRCChannelInfo implements ChannelInfo {
447 447
     public String getModes() {
448 448
         final StringBuilder sModes = new StringBuilder("+");
449 449
         final StringBuilder sModeParams = new StringBuilder();
450
-        String sTemp;
451
-        long nTemp;
452
-        final long nChanModes = this.getMode();
453
-        for (char cTemp : parser.chanModesBool.keySet()) {
454
-            nTemp = parser.chanModesBool.get(cTemp);
455
-            if ((nChanModes & nTemp) == nTemp) {
456
-                sModes.append(cTemp);
457
-            }
458
-        }
450
+        sModes.append(modes);
459 451
         for (char cTemp : paramModes.keySet()) {
460
-            sTemp = paramModes.get(cTemp);
452
+            final String sTemp = paramModes.get(cTemp);
461 453
             if (!sTemp.isEmpty()) {
462 454
                 sModes.append(cTemp);
463 455
                 sModeParams.append(' ').append(this.getMode(cTemp));
@@ -613,7 +605,7 @@ public class IRCChannelInfo implements ChannelInfo {
613 605
         }
614 606
 
615 607
         modestr = (add ? "+" : "-") + mode;
616
-        if (parser.chanModesBool.containsKey(mode)) {
608
+        if (parser.chanModesBool.isMode(mode)) {
617 609
             final String teststr = (add ? "-" : "+") + mode;
618 610
             if (modeQueue.contains(teststr)) {
619 611
                 modeQueue.remove(teststr);
@@ -629,9 +621,9 @@ public class IRCChannelInfo implements ChannelInfo {
629 621
                 modeint = parser.chanModesOther.get(mode);
630 622
                 if ((modeint & IRCParser.MODE_LIST) == IRCParser.MODE_LIST) {
631 623
                     modestr = modestr + " " + parameter;
632
-                } else if (!add && ((modeint & IRCParser.MODE_UNSET) == IRCParser.MODE_UNSET)) {
624
+                } else if (!add && (modeint & IRCParser.MODE_UNSET) == IRCParser.MODE_UNSET) {
633 625
                     modestr = modestr + " " + parameter;
634
-                } else if (add && ((modeint & IRCParser.MODE_SET) == IRCParser.MODE_SET)) {
626
+                } else if (add && (modeint & IRCParser.MODE_SET) == IRCParser.MODE_SET) {
635 627
                     // Does mode require a param to unset aswell?
636 628
                     // We might need to queue an unset first
637 629
                     if ((modeint & IRCParser.MODE_UNSET) == IRCParser.MODE_UNSET) {

+ 6
- 29
src/com/dmdirc/parser/irc/IRCParser.java View File

@@ -171,16 +171,11 @@ public class IRCParser extends BaseParser implements SecureParser, EncodingParse
171 171
     /** Manager used to handle user modes (owxis etc). */
172 172
     public final ModeManager userModes = new ModeManager();
173 173
     /**
174
-     * Hashtable storing known boolean chan modes (cntmi etc).
175
-     * Valid Boolean Modes are stored as Hashtable.pub('m',1); where 'm' is the mode and 1 is a numeric value.<br><br>
176
-     * Numeric values are powers of 2. This allows up to 32 modes at present (expandable to 64)<br><br>
177
-     * ChannelInfo/ChannelClientInfo etc provide methods to view the modes in a human way.<br><br>
178
-     * <br>
174
+     * Manager used to handle channel boolean modes.
175
+     * <p>
179 176
      * Channel modes discovered but not listed in 005 are stored as boolean modes automatically (and a ERROR_WARNING Error is called)
180 177
      */
181
-    public final Map<Character, Long> chanModesBool = new HashMap<>();
182
-    /** Integer representing the next avaliable integer value of a Boolean mode. */
183
-    public long nextKeyCMBool = 1;
178
+    public final ModeManager chanModesBool = new ModeManager();
184 179
     /**
185 180
      * Hashtable storing known non-boolean chan modes (klbeI etc).
186 181
      * Non Boolean Modes (for Channels) are stored together in this hashtable, the value param
@@ -712,8 +707,6 @@ public class IRCParser extends BaseParser implements SecureParser, EncodingParse
712 707
         if (out != null) {
713 708
             out.clearQueue();
714 709
         }
715
-        // Reset the mode indexes
716
-        nextKeyCMBool = 1;
717 710
         setServerName("");
718 711
         networkName = "";
719 712
         lastLine = null;
@@ -1523,8 +1516,6 @@ public class IRCParser extends BaseParser implements SecureParser, EncodingParse
1523 1516
 
1524 1517
         // resetState
1525 1518
         chanModesOther.clear();
1526
-        chanModesBool.clear();
1527
-        nextKeyCMBool = 1;
1528 1519
 
1529 1520
         // List modes.
1530 1521
         for (int i = 0; i < bits[0].length(); ++i) {
@@ -1555,14 +1546,8 @@ public class IRCParser extends BaseParser implements SecureParser, EncodingParse
1555 1546
         }
1556 1547
 
1557 1548
         // Boolean Mode
1558
-        for (int i = 0; i < bits[3].length(); ++i) {
1559
-            final Character cMode = bits[3].charAt(i);
1560
-            callDebugInfo(DEBUG_INFO, "Found Boolean Mode: %c [%d]", cMode, nextKeyCMBool);
1561
-            if (!chanModesBool.containsKey(cMode)) {
1562
-                chanModesBool.put(cMode, nextKeyCMBool);
1563
-                nextKeyCMBool *= 2;
1564
-            }
1565
-        }
1549
+        chanModesBool.set(bits[3]);
1550
+        callDebugInfo(DEBUG_INFO, "Found boolean modes: %s", bits[3]);
1566 1551
     }
1567 1552
 
1568 1553
     @Override
@@ -1572,15 +1557,7 @@ public class IRCParser extends BaseParser implements SecureParser, EncodingParse
1572 1557
 
1573 1558
     @Override
1574 1559
     public String getBooleanChannelModes() {
1575
-        final char[] modes = new char[chanModesBool.size()];
1576
-        int i = 0;
1577
-        for (char mode : chanModesBool.keySet()) {
1578
-            modes[i] = mode;
1579
-            i++;
1580
-        }
1581
-        // Alphabetically sort the array
1582
-        Arrays.sort(modes);
1583
-        return new String(modes);
1560
+        return chanModesBool.getModes();
1584 1561
     }
1585 1562
 
1586 1563
     @Override

+ 7
- 9
src/com/dmdirc/parser/irc/processors/ProcessMode.java View File

@@ -115,7 +115,7 @@ public class ProcessMode extends IRCProcessor {
115 115
         String sModeParam;
116 116
         String sTemp;
117 117
         int nParam = 1;
118
-        long nTemp, nValue, nCurrent = 0;
118
+        long nTemp, nValue = 0;
119 119
         boolean bPositive = true, bBooleanMode;
120 120
         char cPositive = '+';
121 121
         final IRCChannelInfo iChannel;
@@ -135,6 +135,7 @@ public class ProcessMode extends IRCProcessor {
135 135
             return;
136 136
         }
137 137
         // Get the current channel modes
138
+        String nCurrent = "";
138 139
         if (!"324".equals(sParam)) {
139 140
             nCurrent = iChannel.getMode();
140 141
         }
@@ -160,8 +161,7 @@ public class ProcessMode extends IRCProcessor {
160 161
                 cPositive = '-';
161 162
                 bPositive = false;
162 163
             } else {
163
-                if (parser.chanModesBool.containsKey(cMode)) {
164
-                    nValue = parser.chanModesBool.get(cMode);
164
+                if (parser.chanModesBool.isMode(cMode)) {
165 165
                     bBooleanMode = true;
166 166
                 } else if (parser.chanModesOther.containsKey(cMode)) {
167 167
                     nValue = parser.chanModesOther.get(cMode);
@@ -196,19 +196,17 @@ public class ProcessMode extends IRCProcessor {
196 196
                     continue;
197 197
                 } else {
198 198
                     // unknown mode - add as boolean
199
-                    parser.chanModesBool.put(cMode, parser.nextKeyCMBool);
200
-                    nValue = parser.nextKeyCMBool;
199
+                    parser.chanModesBool.add(cMode);
201 200
                     bBooleanMode = true;
202
-                    parser.nextKeyCMBool *= 2;
203 201
                 }
204 202
 
205 203
                 if (bBooleanMode) {
206
-                    callDebugInfo(IRCParser.DEBUG_INFO, "Boolean Mode: %c [%d] {Positive: %b}", cMode, nValue, bPositive);
204
+                    callDebugInfo(IRCParser.DEBUG_INFO, "Boolean Mode: %c {Positive: %b}", cMode, bPositive);
207 205
 
208 206
                     if (bPositive) {
209
-                        nCurrent |= nValue;
207
+                        nCurrent = parser.chanModesBool.insertMode(nCurrent, cMode);
210 208
                     } else {
211
-                        nCurrent ^= nCurrent & nValue;
209
+                        nCurrent = parser.chanModesBool.removeMode(nCurrent, cMode);
212 210
                     }
213 211
                 } else {
214 212
 

Loading…
Cancel
Save