Browse Source

Redo the Identity/ConfigManager get options so they use maps where appropriate, rather than stupid string building

Remove all deprecated Identity/ConfigManager methods
Fixes issue 1335

git-svn-id: http://svn.dmdirc.com/trunk@4200 00569f92-eb28-0410-84fd-f71c24880f
tags/0.6
Chris Smith 16 years ago
parent
commit
335e9c3629

+ 6
- 6
src/com/dmdirc/actions/ActionSubstitutor.java View File

@@ -30,8 +30,8 @@ import com.dmdirc.ServerState;
30 30
 import com.dmdirc.config.IdentityManager;
31 31
 
32 32
 import java.util.HashMap;
33
-import java.util.List;
34 33
 import java.util.Map;
34
+import java.util.Set;
35 35
 
36 36
 /**
37 37
  * Handles the substitution of variables into action targets and responses.
@@ -58,8 +58,8 @@ public class ActionSubstitutor {
58 58
      *
59 59
      * @return A list of global variable names that will be substituted
60 60
      */
61
-    public List<String> getConfigSubstitutions() {
62
-        return IdentityManager.getGlobalConfig().getOptions("actions");
61
+    public Set<String> getConfigSubstitutions() {
62
+        return IdentityManager.getGlobalConfig().getOptions("actions").keySet();
63 63
     }
64 64
     
65 65
     /**
@@ -68,9 +68,9 @@ public class ActionSubstitutor {
68 68
      * @param target The StringBuilder to modify
69 69
      */
70 70
     private void doConfigSubstitutions(final StringBuilder target) {
71
-        for (String option : IdentityManager.getGlobalConfig().getOptions("actions")) {
72
-            doReplacement(target, "$" + option,
73
-                    IdentityManager.getGlobalConfig().getOption("actions", option));
71
+        for (Map.Entry<String, String> option 
72
+                : IdentityManager.getGlobalConfig().getOptions("actions").entrySet()) {
73
+            doReplacement(target, "$" + option.getKey(), option.getValue());
74 74
         }
75 75
     }
76 76
     

+ 5
- 3
src/com/dmdirc/addons/nickcolours/NickColourPlugin.java View File

@@ -139,11 +139,13 @@ public final class NickColourPlugin extends Plugin implements ActionListener {
139 139
      */
140 140
     @SuppressWarnings("unchecked")
141 141
     private void putColour(final Map map, final Color textColour, final Color nickColour) {
142
-        if (IdentityManager.getGlobalConfig().getOptionBool(DOMAIN, "settext", false) && textColour != null) {
142
+        if (IdentityManager.getGlobalConfig().getOptionBool(DOMAIN, "settext", false) 
143
+                && textColour != null) {
143 144
             map.put(ChannelClientProperty.TEXT_FOREGROUND, textColour);
144 145
         }
145 146
         
146
-        if (IdentityManager.getGlobalConfig().getOptionBool(DOMAIN, "setnicklist", false) && nickColour != null) {
147
+        if (IdentityManager.getGlobalConfig().getOptionBool(DOMAIN, "setnicklist", false) 
148
+                && nickColour != null) {
147 149
             map.put(ChannelClientProperty.NICKLIST_FOREGROUND, nickColour);
148 150
         }
149 151
     }
@@ -174,7 +176,7 @@ public final class NickColourPlugin extends Plugin implements ActionListener {
174 176
     public Object[][] getData() {
175 177
         final List<Object[]> data = new ArrayList<Object[]>();
176 178
         
177
-        for (String key : IdentityManager.getGlobalConfig().getOptions(DOMAIN)) {
179
+        for (String key : IdentityManager.getGlobalConfig().getOptions(DOMAIN).keySet()) {
178 180
             if (key.startsWith("color:")) {
179 181
                 final String network = key.substring(6, key.indexOf(':', 6));
180 182
                 final String user = key.substring(1 + key.indexOf(':', 6));

+ 8
- 2
src/com/dmdirc/addons/userlevel/UserLevelPlugin.java View File

@@ -72,6 +72,7 @@ public class UserLevelPlugin extends Plugin implements ActionListener,
72 72
     }
73 73
 
74 74
     /** {@inheritDoc} */
75
+    @Override
75 76
     public void processEvent(final ActionType type, final StringBuffer format,
76 77
                              final Object... arguments) {
77 78
         switch ((CoreActionType) type) {
@@ -127,8 +128,13 @@ public class UserLevelPlugin extends Plugin implements ActionListener,
127 128
     private void loadLevels() {
128 129
         LEVELS.clear();
129 130
         
130
-        for (String key : IdentityManager.getGlobalConfig().getOptions(DOMAIN)) {
131
-            LEVELS.put(key, IdentityManager.getGlobalConfig().getOptionInt(DOMAIN, key, 0));
131
+        for (Map.Entry<String, String> item 
132
+                : IdentityManager.getGlobalConfig().getOptions(DOMAIN).entrySet()) {
133
+            try {
134
+                LEVELS.put(item.getKey(), Integer.parseInt(item.getValue()));
135
+            } catch (NumberFormatException ex) {
136
+                LEVELS.put(item.getKey(), 0);
137
+            }
132 138
         }
133 139
     }
134 140
 

+ 3
- 3
src/com/dmdirc/commandparser/commands/global/Set.java View File

@@ -129,7 +129,7 @@ public final class Set extends GlobalCommand implements IntelligentCommand {
129 129
         
130 130
         boolean found = false;
131 131
         
132
-        for (String option : manager.getOptions(domain)) {
132
+        for (String option : manager.getOptions(domain).keySet()) {
133 133
             output.append(option);
134 134
             output.append(", ");
135 135
             found = true;
@@ -254,13 +254,13 @@ public final class Set extends GlobalCommand implements IntelligentCommand {
254 254
                     || previousArgs.get(0).equalsIgnoreCase("--server")) {
255 255
                 res.addAll(IdentityManager.getGlobalConfig().getDomains());
256 256
             } else {
257
-                res.addAll(IdentityManager.getGlobalConfig().getOptions(previousArgs.get(0)));
257
+                res.addAll(IdentityManager.getGlobalConfig().getOptions(previousArgs.get(0)).keySet());
258 258
             }
259 259
             res.excludeAll();
260 260
         } else if (arg == 2 && (previousArgs.get(0).equalsIgnoreCase("--unset")
261 261
                 || previousArgs.get(0).equalsIgnoreCase("--append")
262 262
                 || previousArgs.get(0).equalsIgnoreCase("--server"))) {
263
-            res.addAll(IdentityManager.getGlobalConfig().getOptions(previousArgs.get(1)));
263
+            res.addAll(IdentityManager.getGlobalConfig().getOptions(previousArgs.get(1)).keySet());
264 264
             res.excludeAll();
265 265
         }
266 266
         

+ 13
- 33
src/com/dmdirc/config/ConfigManager.java View File

@@ -28,6 +28,7 @@ import com.dmdirc.util.MapList;
28 28
 import java.io.Serializable;
29 29
 import java.util.ArrayList;
30 30
 import java.util.Collections;
31
+import java.util.HashMap;
31 32
 import java.util.HashSet;
32 33
 import java.util.List;
33 34
 import java.util.Map;
@@ -137,25 +138,6 @@ public class ConfigManager extends ConfigSource implements Serializable,
137 138
         return false;
138 139
     }
139 140
 
140
-    /**
141
-     * Returns the name of all known options.
142
-     *
143
-     * @return A list of options
144
-     */
145
-    public Set<String> getOptions() {
146
-        final HashSet<String> res = new HashSet<String>();
147
-
148
-        synchronized (sources) {
149
-            for (Identity source : sources) {
150
-                for (String key : source.getOptions()) {
151
-                    res.add(key);
152
-                }
153
-            }
154
-        }
155
-
156
-        return res;
157
-    }
158
-
159 141
     /**
160 142
      * Returns the name of all the options in the specified domain. If the
161 143
      * domain doesn't exist, an empty list is returned.
@@ -163,12 +145,12 @@ public class ConfigManager extends ConfigSource implements Serializable,
163 145
      * @param domain The domain to search
164 146
      * @return A list of options in the specified domain
165 147
      */
166
-    public List<String> getOptions(final String domain) {
167
-        final ArrayList<String> res = new ArrayList<String>();
148
+    public Map<String, String> getOptions(final String domain) {
149
+        final Map<String, String> res = new HashMap<String, String>();
168 150
 
169
-        for (String key : getOptions()) {
170
-            if (key.startsWith(domain + ".")) {
171
-                res.add(key.substring(domain.length() + 1));
151
+        synchronized (sources) {
152
+            for (Identity source : sources) {
153
+                res.putAll(source.getOptions(domain));
172 154
             }
173 155
         }
174 156
 
@@ -297,17 +279,15 @@ public class ConfigManager extends ConfigSource implements Serializable,
297 279
      *
298 280
      * @return A list of domains known to this manager
299 281
      */
300
-    public List<String> getDomains() {
301
-        final ArrayList<String> res = new ArrayList<String>();
302
-        String domain;
303
-
304
-        for (String key : getOptions()) {
305
-            domain = key.substring(0, key.indexOf('.'));
306
-            if (!res.contains(domain)) {
307
-                res.add(domain);
282
+    public Set<String> getDomains() {
283
+        final Set<String> res = new HashSet<String>();
284
+
285
+        synchronized (sources) {
286
+            for (Identity source : sources) {
287
+                res.addAll(source.getDomains());
308 288
             }
309 289
         }
310
-
290
+        
311 291
         return res;
312 292
     }
313 293
 

+ 24
- 37
src/com/dmdirc/config/Identity.java View File

@@ -35,12 +35,14 @@ import java.io.FileInputStream;
35 35
 import java.io.FileWriter;
36 36
 import java.io.IOException;
37 37
 import java.io.InputStream;
38
+import java.io.Reader;
38 39
 import java.io.Serializable;
39 40
 import java.net.MalformedURLException;
40 41
 import java.util.ArrayList;
41 42
 import java.util.List;
42 43
 import java.util.Map;
43 44
 import java.util.Properties;
45
+import java.util.Set;
44 46
 
45 47
 /**
46 48
  * An identity is a group of settings that are applied to a connection, server,
@@ -211,7 +213,9 @@ public class Identity extends ConfigSource implements Serializable,
211 213
             this.file.read();
212 214
         } catch (InvalidConfigFileException ex) {            
213 215
             final Properties properties = new Properties();
214
-            properties.load(new LineReader(this.file.getLines()));            
216
+            final Reader reader = new LineReader(this.file.getLines());
217
+            properties.load(reader);
218
+            reader.close();
215 219
             migrateProperties(properties);
216 220
         }
217 221
 
@@ -276,26 +280,6 @@ public class Identity extends ConfigSource implements Serializable,
276 280
         }
277 281
     }
278 282
 
279
-    /**
280
-     * Returns the properties object belonging to this identity.
281
-     *
282
-     * @return This identity's property object
283
-     * @deprecated Get a map
284
-     */
285
-    @Deprecated
286
-    public Properties getProperties() {
287
-        final Properties properties = new Properties();
288
-
289
-        for (Map.Entry<String, Map<String, String>> entry : file.getKeyDomains().entrySet()) {
290
-            for (Map.Entry<String, String> subentry : entry.getValue().entrySet()) {
291
-                properties.setProperty(entry.getKey() + "." + subentry.getKey(),
292
-                        subentry.getValue());
293
-            }
294
-        }
295
-
296
-        return properties;
297
-    }
298
-
299 283
     /**
300 284
      * Returns the name of this identity.
301 285
      *
@@ -431,24 +415,27 @@ public class Identity extends ConfigSource implements Serializable,
431 415
             listener.configChanged(domain, option);
432 416
         }
433 417
     }
434
-
418
+    
435 419
     /**
436
-     * Returns a list of options avaiable in this identity.
437
-     *
438
-     * @return Option list
439
-     * @deprecated Get a map
420
+     * Returns the set of domains available in this identity.
421
+     * 
422
+     * @since 0.6
423
+     * @return The set of domains used by this identity
440 424
      */
441
-    @Deprecated
442
-    public List<String> getOptions() {
443
-        final List<String> res = new ArrayList<String>();
444
-
445
-        for (Map.Entry<String, Map<String, String>> entry : file.getKeyDomains().entrySet()) {
446
-            for (String key : entry.getValue().keySet()) {
447
-                res.add(entry.getKey() + "." + key);
448
-            }
449
-        }
450
-
451
-        return res;
425
+    public Set<String> getDomains() {
426
+        return file.getKeyDomains().keySet();
427
+    }
428
+    
429
+    /**
430
+     * Retrieves a map of all options within the specified domain in this
431
+     * identity.
432
+     * 
433
+     * @param domain The domain to retrieve
434
+     * @since 0.6
435
+     * @return A map of option names to values
436
+     */
437
+    public Map<String, String> getOptions(final String domain) {
438
+        return file.getKeyDomain(domain);
452 439
     }
453 440
 
454 441
     /**

+ 3
- 2
src/com/dmdirc/ui/swing/dialogs/actioneditor/ActionResponsePanel.java View File

@@ -24,7 +24,7 @@ package com.dmdirc.ui.swing.dialogs.actioneditor;
24 24
 
25 25
 import com.dmdirc.config.IdentityManager;
26 26
 
27
-import java.util.List;
27
+import java.util.Set;
28 28
 
29 29
 import javax.swing.BorderFactory;
30 30
 import javax.swing.DefaultComboBoxModel;
@@ -69,7 +69,8 @@ public class ActionResponsePanel extends JPanel {
69 69
         ((DefaultComboBoxModel) formatter.getModel()).addElement("No change");
70 70
         ((DefaultComboBoxModel) formatter.getModel()).addElement("No response");
71 71
         
72
-        final List<String> formatters = IdentityManager.getGlobalConfig().getOptions("formatter");
72
+        final Set<String> formatters
73
+                = IdentityManager.getGlobalConfig().getOptions("formatter").keySet();
73 74
         
74 75
         for (String format : formatters) {
75 76
             ((DefaultComboBoxModel) formatter.getModel()).addElement(format);

+ 3
- 2
src/com/dmdirc/ui/swing/dialogs/actionseditor/ResponseTabPanel.java View File

@@ -32,7 +32,7 @@ import java.awt.Dimension;
32 32
 import java.awt.GridBagConstraints;
33 33
 import java.awt.GridBagLayout;
34 34
 import java.awt.Insets;
35
-import java.util.List;
35
+import java.util.Set;
36 36
 
37 37
 import javax.swing.BorderFactory;
38 38
 import javax.swing.DefaultComboBoxModel;
@@ -103,7 +103,8 @@ public final class ResponseTabPanel extends JPanel implements
103 103
         ((DefaultComboBoxModel) formatter.getModel()).addElement("No change");
104 104
         ((DefaultComboBoxModel) formatter.getModel()).addElement("No response");
105 105
         
106
-        final List<String> formatters = IdentityManager.getGlobalConfig().getOptions("formatter");
106
+        final Set<String> formatters
107
+                = IdentityManager.getGlobalConfig().getOptions("formatter").keySet();
107 108
         
108 109
         for (String format : formatters) {
109 110
             ((DefaultComboBoxModel) formatter.getModel()).addElement(format);

+ 7
- 7
src/com/dmdirc/ui/swing/dialogs/prefs/URLConfigPanel.java View File

@@ -36,20 +36,20 @@ import com.dmdirc.ui.swing.components.URLProtocolPanel;
36 36
 import java.awt.event.ActionEvent;
37 37
 import java.awt.event.ActionListener;
38 38
 import java.net.URI;
39
-
40 39
 import java.net.URISyntaxException;
41 40
 import java.util.HashMap;
42
-import java.util.List;
43 41
 import java.util.Map;
44 42
 import java.util.Map.Entry;
43
+import java.util.Set;
44
+
45 45
 import javax.swing.JButton;
46 46
 import javax.swing.JPanel;
47 47
 import javax.swing.JScrollPane;
48 48
 import javax.swing.ListSelectionModel;
49 49
 import javax.swing.event.ListSelectionEvent;
50 50
 import javax.swing.event.ListSelectionListener;
51
-
52 51
 import javax.swing.table.TableCellRenderer;
52
+
53 53
 import net.miginfocom.swing.MigLayout;
54 54
 
55 55
 /**
@@ -139,8 +139,8 @@ public class URLConfigPanel extends JPanel implements ListSelectionListener,
139 139
 
140 140
         tableScrollPane.setViewportView(table);
141 141
 
142
-        final List<String> options = IdentityManager.getGlobalConfig().
143
-                getOptions("protocol");
142
+        final Set<String> options = IdentityManager.getGlobalConfig().
143
+                getOptions("protocol").keySet();
144 144
 
145 145
         for (String option : options) {
146 146
             try {
@@ -180,8 +180,8 @@ public class URLConfigPanel extends JPanel implements ListSelectionListener,
180 180
     public void save() {
181 181
         valueChanged(null);
182 182
         final Map<URI, String> handlers = model.getURLHandlers();
183
-        final List<String> protocols = IdentityManager.getGlobalConfig().
184
-                getOptions("protocol");
183
+        final Set<String> protocols = IdentityManager.getGlobalConfig().
184
+                getOptions("protocol").keySet();
185 185
         for (String protocol : protocols) {
186 186
             URI uri;
187 187
             try {

+ 12
- 20
test/com/dmdirc/config/IdentityTest.java View File

@@ -23,10 +23,9 @@
23 23
 package com.dmdirc.config;
24 24
 
25 25
 import com.dmdirc.harness.TestConfigListener;
26
-import com.dmdirc.interfaces.ConfigChangeListener;
27 26
 
28 27
 import java.io.IOException;
29
-import java.util.Properties;
28
+import java.util.Map;
30 29
 
31 30
 import org.junit.After;
32 31
 import org.junit.Before;
@@ -51,21 +50,11 @@ public class IdentityTest {
51 50
         myIdent = null;
52 51
     }    
53 52
     
54
-    @Test
55
-    public void testGetProperties() {
56
-        myIdent.setOption("domain", "option", "value");
57
-        final Properties props = myIdent.getProperties();
58
-        
59
-        assertEquals(props.getProperty("domain.option"), "value");
60
-        
61
-        myIdent.unsetOption("domain", "option");
62
-    }
63
-
64 53
     @Test
65 54
     public void testGetName() {
66
-        final Properties props = myIdent.getProperties();
55
+        final Map<String, String> props = myIdent.getOptions("identity");
67 56
         
68
-        assertEquals(props.getProperty("identity.name"), myIdent.getName());
57
+        assertEquals(props.get("name"), myIdent.getName());
69 58
     }
70 59
     
71 60
     @Test
@@ -100,20 +89,20 @@ public class IdentityTest {
100 89
     @Test
101 90
     public void testGetOption() {
102 91
         myIdent.setOption("domain", "option", "value");
103
-        final Properties props = myIdent.getProperties();
92
+        final Map<String, String> props = myIdent.getOptions("domain");
104 93
         
105
-        assertEquals(props.getProperty("domain.option"), myIdent.getOption("domain", "option"));
94
+        assertEquals(props.get("option"), myIdent.getOption("domain", "option"));
106 95
         
107 96
         myIdent.unsetOption("domain", "option");
108 97
     }
109 98
 
110 99
     @Test
111 100
     public void testSetOption() {
112
-        final int count = myIdent.getProperties().size();
101
+        final int count = myIdent.getOptions("foo").size();
113 102
         
114 103
         myIdent.setOption("foo", "bar", "baz");
115 104
         
116
-        assertEquals(count + 1, myIdent.getProperties().size());
105
+        assertEquals(count + 1, myIdent.getOptions("foo").size());
117 106
         
118 107
         myIdent.unsetOption("foo", "bar");
119 108
     }
@@ -134,13 +123,16 @@ public class IdentityTest {
134 123
 
135 124
     @Test
136 125
     public void testRemoveOption() {
137
-        final Properties props = myIdent.getProperties();
126
+        final Map<String, String> props = myIdent.getOptions("foo");
138 127
         final int count = props.size();
139 128
         
140 129
         myIdent.setOption("foo", "bar", "baz");
130
+        
131
+        assertEquals(count + 1, myIdent.getOptions("foo").size());
132
+        
141 133
         myIdent.unsetOption("foo", "bar");
142 134
         
143
-        assertEquals(count, props.size());
135
+        assertEquals(count, myIdent.getOptions("foo").size());
144 136
     }
145 137
 
146 138
     @Test

Loading…
Cancel
Save