Quellcode durchsuchen

Introduce interfaces for identities/configmanagers

Change-Id: If8ead3ae7d8c4997d6735b9ffa871f699bceed27
Reviewed-on: http://gerrit.dmdirc.com/2752
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
tags/0.8rc1
Chris Smith vor 10 Jahren
Ursprung
Commit
b94d5390ae

+ 26
- 68
src/com/dmdirc/config/ConfigManager.java Datei anzeigen

22
 
22
 
23
 package com.dmdirc.config;
23
 package com.dmdirc.config;
24
 
24
 
25
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
25
 import com.dmdirc.interfaces.ConfigChangeListener;
26
 import com.dmdirc.interfaces.ConfigChangeListener;
26
 import com.dmdirc.util.collections.MapList;
27
 import com.dmdirc.util.collections.MapList;
27
 import com.dmdirc.util.validators.Validator;
28
 import com.dmdirc.util.validators.Validator;
44
 @Slf4j
45
 @Slf4j
45
 @SuppressWarnings("PMD.UnusedPrivateField")
46
 @SuppressWarnings("PMD.UnusedPrivateField")
46
 public class ConfigManager extends ConfigSource implements ConfigChangeListener,
47
 public class ConfigManager extends ConfigSource implements ConfigChangeListener,
47
-        IdentityListener {
48
+        IdentityListener, AggregateConfigProvider {
48
 
49
 
49
     /** Temporary map for lookup stats. */
50
     /** Temporary map for lookup stats. */
50
-    private static final Map<String, Integer> STATS = new TreeMap<String, Integer>();
51
+    private static final Map<String, Integer> STATS = new TreeMap<>();
51
 
52
 
52
     /** Magical domain to redirect to the version identity. */
53
     /** Magical domain to redirect to the version identity. */
53
     private static final String VERSION_DOMAIN = "version";
54
     private static final String VERSION_DOMAIN = "version";
56
     private final List<Identity> sources;
57
     private final List<Identity> sources;
57
 
58
 
58
     /** The listeners registered for this manager. */
59
     /** The listeners registered for this manager. */
59
-    private final MapList<String, ConfigChangeListener> listeners
60
-            = new MapList<String, ConfigChangeListener>();
60
+    private final MapList<String, ConfigChangeListener> listeners = new MapList<>();
61
 
61
 
62
     /** The config binder to use for this manager. */
62
     /** The config binder to use for this manager. */
63
     @Getter
63
     @Getter
165
         return false;
165
         return false;
166
     }
166
     }
167
 
167
 
168
-    /**
169
-     * Returns the name of all the options in the specified domain. If the
170
-     * domain doesn't exist, an empty list is returned.
171
-     *
172
-     * @param domain The domain to search
173
-     * @return A list of options in the specified domain
174
-     */
168
+    /** {@inheritDoc} */
169
+    @Override
175
     public Map<String, String> getOptions(final String domain) {
170
     public Map<String, String> getOptions(final String domain) {
176
         if (VERSION_DOMAIN.equals(domain)) {
171
         if (VERSION_DOMAIN.equals(domain)) {
177
             return IdentityManager.getIdentityManager()
172
             return IdentityManager.getIdentityManager()
178
                     .getGlobalVersionIdentity().getOptions(domain);
173
                     .getGlobalVersionIdentity().getOptions(domain);
179
         }
174
         }
180
 
175
 
181
-        final Map<String, String> res = new HashMap<String, String>();
176
+        final Map<String, String> res = new HashMap<>();
182
 
177
 
183
         synchronized (sources) {
178
         synchronized (sources) {
184
             for (int i = sources.size() - 1; i >= 0; i--) {
179
             for (int i = sources.size() - 1; i >= 0; i--) {
199
             return;
194
             return;
200
         }
195
         }
201
 
196
 
202
-        final List<String[]> changed = new ArrayList<String[]>();
197
+        final List<String[]> changed = new ArrayList<>();
203
 
198
 
204
         // Determine which settings will have changed
199
         // Determine which settings will have changed
205
         for (String domain : identity.getDomains()) {
200
         for (String domain : identity.getDomains()) {
330
         }
325
         }
331
     }
326
     }
332
 
327
 
333
-    /**
334
-     * Returns the name of all domains known by this manager.
335
-     *
336
-     * @return A list of domains known to this manager
337
-     */
328
+    /** {@inheritDoc} */
329
+    @Override
338
     public Set<String> getDomains() {
330
     public Set<String> getDomains() {
339
-        final Set<String> res = new HashSet<String>();
331
+        final Set<String> res = new HashSet<>();
340
 
332
 
341
         synchronized (sources) {
333
         synchronized (sources) {
342
             for (Identity source : sources) {
334
             for (Identity source : sources) {
347
         return res;
339
         return res;
348
     }
340
     }
349
 
341
 
350
-    /**
351
-     * Retrieves a list of sources for this config manager.
352
-     * @return This config manager's sources.
353
-     */
342
+    /** {@inheritDoc} */
343
+    @Override
354
     public List<Identity> getSources() {
344
     public List<Identity> getSources() {
355
-        return new ArrayList<Identity>(sources);
345
+        return new ArrayList<>(sources);
356
     }
346
     }
357
 
347
 
358
-    /**
359
-     * Migrates this ConfigManager from its current configuration to the
360
-     * appropriate one for the specified new parameters, firing listeners where
361
-     * settings have changed.
362
-     *
363
-     * @param protocol The protocol for this manager
364
-     * @param ircd The new name of the ircd for this manager
365
-     * @param network The new name of the network for this manager
366
-     * @param server The new name of the server for this manager
367
-     * @since 0.6.3
368
-     */
348
+    /** {@inheritDoc} */
349
+    @Override
369
     public void migrate(final String protocol, final String ircd,
350
     public void migrate(final String protocol, final String ircd,
370
             final String network, final String server) {
351
             final String network, final String server) {
371
         migrate(protocol, ircd, network, server, "<Unknown>");
352
         migrate(protocol, ircd, network, server, "<Unknown>");
372
     }
353
     }
373
 
354
 
374
-    /**
375
-     * Migrates this ConfigManager from its current configuration to the
376
-     * appropriate one for the specified new parameters, firing listeners where
377
-     * settings have changed.
378
-     *
379
-     * @param protocol The protocol for this manager
380
-     * @param ircd The new name of the ircd for this manager
381
-     * @param network The new name of the network for this manager
382
-     * @param server The new name of the server for this manager
383
-     * @param channel The new name of the channel for this manager
384
-     * @since 0.6.3
385
-     */
355
+    /** {@inheritDoc} */
356
+    @Override
386
     public void migrate(final String protocol, final String ircd,
357
     public void migrate(final String protocol, final String ircd,
387
             final String network, final String server, final String channel) {
358
             final String network, final String server, final String channel) {
388
         log.debug("Migrating from {{}, {}, {}, {}, {}} to {{}, {}, {}, {}, {}}",
359
         log.debug("Migrating from {{}, {}, {}, {}, {}} to {{}, {}, {}, {}, {}}",
397
         this.server = server;
368
         this.server = server;
398
         this.channel = channel + "@" + network;
369
         this.channel = channel + "@" + network;
399
 
370
 
400
-        for (Identity identity : new ArrayList<Identity>(sources)) {
371
+        for (Identity identity : new ArrayList<>(sources)) {
401
             if (!identityApplies(identity)) {
372
             if (!identityApplies(identity)) {
402
                 log.debug("Removing identity that no longer applies: {}", identity);
373
                 log.debug("Removing identity that no longer applies: {}", identity);
403
                 removeIdentity(identity);
374
                 removeIdentity(identity);
440
         return STATS;
411
         return STATS;
441
     }
412
     }
442
 
413
 
443
-    /**
444
-     * Adds a change listener for the specified domain.
445
-     *
446
-     * @param domain The domain to be monitored
447
-     * @param listener The listener to register
448
-     */
414
+    /** {@inheritDoc} */
415
+    @Override
449
     public void addChangeListener(final String domain,
416
     public void addChangeListener(final String domain,
450
             final ConfigChangeListener listener) {
417
             final ConfigChangeListener listener) {
451
         addListener(domain, listener);
418
         addListener(domain, listener);
452
     }
419
     }
453
 
420
 
454
-    /**
455
-     * Adds a change listener for the specified domain and key.
456
-     *
457
-     * @param domain The domain of the option
458
-     * @param key The option to be monitored
459
-     * @param listener The listener to register
460
-     */
421
+    /** {@inheritDoc} */
422
+    @Override
461
     public void addChangeListener(final String domain, final String key,
423
     public void addChangeListener(final String domain, final String key,
462
             final ConfigChangeListener listener) {
424
             final ConfigChangeListener listener) {
463
         addListener(domain + "." + key, listener);
425
         addListener(domain + "." + key, listener);
464
     }
426
     }
465
 
427
 
466
-    /**
467
-     * Removes the specified listener for all domains and options.
468
-     *
469
-     * @param listener The listener to be removed
470
-     */
428
+    /** {@inheritDoc} */
429
+    @Override
471
     public void removeListener(final ConfigChangeListener listener) {
430
     public void removeListener(final ConfigChangeListener listener) {
472
         synchronized (listeners) {
431
         synchronized (listeners) {
473
             listeners.removeFromAll(listener);
432
             listeners.removeFromAll(listener);
490
     /** {@inheritDoc} */
449
     /** {@inheritDoc} */
491
     @Override
450
     @Override
492
     public void configChanged(final String domain, final String key) {
451
     public void configChanged(final String domain, final String key) {
493
-        final List<ConfigChangeListener> targets
494
-                = new ArrayList<ConfigChangeListener>();
452
+        final List<ConfigChangeListener> targets = new ArrayList<>();
495
 
453
 
496
         if (listeners.containsKey(domain)) {
454
         if (listeners.containsKey(domain)) {
497
             targets.addAll(listeners.get(domain));
455
             targets.addAll(listeners.get(domain));

+ 37
- 177
src/com/dmdirc/config/ConfigSource.java Datei anzeigen

22
 
22
 
23
 package com.dmdirc.config;
23
 package com.dmdirc.config;
24
 
24
 
25
+import com.dmdirc.interfaces.config.ReadOnlyConfigProvider;
25
 import com.dmdirc.ui.Colour;
26
 import com.dmdirc.ui.Colour;
26
 import com.dmdirc.ui.messages.ColourManager;
27
 import com.dmdirc.ui.messages.ColourManager;
27
 import com.dmdirc.util.validators.ColourValidator;
28
 import com.dmdirc.util.validators.ColourValidator;
54
  * an implementation that takes values from multiple sources, such as a
55
  * an implementation that takes values from multiple sources, such as a
55
  * {@link ConfigManager}.
56
  * {@link ConfigManager}.
56
  */
57
  */
57
-public abstract class ConfigSource {
58
+public abstract class ConfigSource implements ReadOnlyConfigProvider {
58
 
59
 
59
     /** A permissive validator to use when callers don't specify one. */
60
     /** A permissive validator to use when callers don't specify one. */
60
     private static final Validator<String> PERMISSIVE_VALIDATOR
61
     private static final Validator<String> PERMISSIVE_VALIDATOR
91
         this(ColourManager.getColourManagerProvider());
92
         this(ColourManager.getColourManagerProvider());
92
     }
93
     }
93
 
94
 
94
-    /**
95
-     * Retrieves the specified option from this config source.
96
-     *
97
-     * @param domain The domain of the option
98
-     * @param option The name of the option
99
-     * @return The value of the option, or null if it doesn't exist
100
-     */
95
+    /** {@inheritDoc} */
96
+    @Override
101
     public String getOption(final String domain, final String option) {
97
     public String getOption(final String domain, final String option) {
102
         return getOption(domain, option, PERMISSIVE_VALIDATOR);
98
         return getOption(domain, option, PERMISSIVE_VALIDATOR);
103
     }
99
     }
104
 
100
 
105
-    /**
106
-     * Retrieves the first value for the specified option that matches the
107
-     * specified validator.
108
-     *
109
-     * @since 0.6.5
110
-     * @param domain The domain of the option
111
-     * @param option The name of the option
112
-     * @param validator The validator to use to check legal values
113
-     * @return The value of the option, or null if no matching values exist
114
-     */
115
-    protected abstract String getOption(final String domain,
101
+    /** {@inheritDoc} */
102
+    @Override
103
+    public abstract String getOption(final String domain,
116
             final String option, final Validator<String> validator);
104
             final String option, final Validator<String> validator);
117
 
105
 
118
     /**
106
     /**
128
     protected abstract boolean hasOption(final String domain,
116
     protected abstract boolean hasOption(final String domain,
129
             final String option, final Validator<String> validator);
117
             final String option, final Validator<String> validator);
130
 
118
 
131
-    /**
132
-     * Determines if this source has the specified String option.
133
-     * <p>
134
-     * A String option is considered to exist if:
135
-     * <ul>
136
-     *  <li>there is a value for the specified option,
137
-     *  <li>that value is not empty, and
138
-     *  <li>that value is not disabled (i.e., it doesn't begin with "false:")
139
-     * </ul>
140
-     *
141
-     * @param domain The domain of the option
142
-     * @param option The name of the option
143
-     * @since 0.6.3m1
144
-     * @return True iff the option exists and is not empty, false otherwise
145
-     */
119
+    /** {@inheritDoc} */
120
+    @Override
146
     public boolean hasOptionString(final String domain, final String option) {
121
     public boolean hasOptionString(final String domain, final String option) {
147
         return hasOptionString(domain, option, PERMISSIVE_VALIDATOR);
122
         return hasOptionString(domain, option, PERMISSIVE_VALIDATOR);
148
     }
123
     }
149
 
124
 
150
-    /**
151
-     * Determines if this source has the specified String option.
152
-     * <p>
153
-     * A String option is considered to exist if:
154
-     * <ul>
155
-     *  <li>there is a value for the specified option that
156
-     *      satisfies the specified validator,
157
-     *  <li>that value is not empty, and
158
-     *  <li>that value is not disabled (i.e., it doesn't begin with "false:")
159
-     * </ul>
160
-     *
161
-     * @param domain The domain of the option
162
-     * @param option The name of the option
163
-     * @param validator The validator to use to check legal values
164
-     * @since 0.6.5
165
-     * @return True iff the option exists and is not empty, false otherwise
166
-     */
125
+    /** {@inheritDoc} */
126
+    @Override
167
     public boolean hasOptionString(final String domain, final String option,
127
     public boolean hasOptionString(final String domain, final String option,
168
             final Validator<String> validator) {
128
             final Validator<String> validator) {
169
         String value;
129
         String value;
173
                 && !value.startsWith("false:");
133
                 && !value.startsWith("false:");
174
     }
134
     }
175
 
135
 
176
-    /**
177
-     * Determines if this source has the specified integer option.
178
-     *
179
-     * @param domain The domain of the option
180
-     * @param option The name of the option
181
-     * @since 0.6.3m1
182
-     * @return True iff the option exists and is parsable as an integer,
183
-     * false otherwise.
184
-     */
136
+    /** {@inheritDoc} */
137
+    @Override
185
     public boolean hasOptionInt(final String domain, final String option) {
138
     public boolean hasOptionInt(final String domain, final String option) {
186
         return hasOptionString(domain, option, INT_VALIDATOR);
139
         return hasOptionString(domain, option, INT_VALIDATOR);
187
     }
140
     }
188
 
141
 
189
-    /**
190
-     * Determines if this source has the specified character option.
191
-     *
192
-     * @param domain The domain of the option
193
-     * @param option The name of the option
194
-     * @since 0.6.3m1
195
-     * @return True iff the option exists and is parsable as a char,
196
-     * false otherwise.
197
-     */
142
+    /** {@inheritDoc} */
143
+    @Override
198
     public boolean hasOptionChar(final String domain, final String option) {
144
     public boolean hasOptionChar(final String domain, final String option) {
199
         return hasOptionString(domain, option);
145
         return hasOptionString(domain, option);
200
     }
146
     }
201
 
147
 
202
-    /**
203
-     * Determines if this source has the specified colour option.
204
-     *
205
-     * @param domain The domain of the option
206
-     * @param option The name of the option
207
-     * @since 0.6.3m1
208
-     * @return True iff the option exists and is parsable as a colour,
209
-     * false otherwise.
210
-     */
148
+    /** {@inheritDoc} */
149
+    @Override
211
     public boolean hasOptionColour(final String domain, final String option) {
150
     public boolean hasOptionColour(final String domain, final String option) {
212
         return hasOptionString(domain, option, COLOUR_VALIDATOR);
151
         return hasOptionString(domain, option, COLOUR_VALIDATOR);
213
     }
152
     }
214
 
153
 
215
-    /**
216
-     * Retrieves a boolean representation of the specified option.
217
-     *
218
-     * @param domain The domain of the option
219
-     * @param option The name of the option
220
-     * @return The boolean representation of the option
221
-     */
154
+    /** {@inheritDoc} */
155
+    @Override
222
     public boolean hasOptionBool(final String domain, final String option) {
156
     public boolean hasOptionBool(final String domain, final String option) {
223
         return hasOption(domain, option, PERMISSIVE_VALIDATOR);
157
         return hasOption(domain, option, PERMISSIVE_VALIDATOR);
224
     }
158
     }
225
 
159
 
226
-    /**
227
-     * Retrieves the specified option as a String, if it exists and satisfies
228
-     * the specified validator.
229
-     * <p>
230
-     * If no fallback settings are supplied (or if fallback settings are
231
-     * supplied and execution reaches the last fallback setting) AND if the
232
-     * 'required' parameter is true, then all disabled values (i.e., those
233
-     * starting with <code>false:</code>) will be ignored. To check if a valid
234
-     * non-disabled value exists, call
235
-     * {@link #hasOptionString(java.lang.String, java.lang.String, com.dmdirc.util.validators.Validator)}.
236
-     *
237
-     * @param domain The domain of the option
238
-     * @param option The name of the option
239
-     * @param required Whether the specified option is required or not
240
-     * @param validator The validator to use to check the value
241
-     * @param fallbacks An ordered array of further domains and options
242
-     * (in pairs) to try if the specified domain/option isn't found
243
-     * @return The string representation of the option or null if optional
244
-     * setting is not specified
245
-     * @since 0.6.5
246
-     */
160
+    /** {@inheritDoc} */
161
+    @Override
247
     public String getOptionString(final String domain, final String option,
162
     public String getOptionString(final String domain, final String option,
248
             final boolean required, final Validator<String> validator,
163
             final boolean required, final Validator<String> validator,
249
             final String ... fallbacks) {
164
             final String ... fallbacks) {
266
         }
181
         }
267
     }
182
     }
268
 
183
 
269
-    /**
270
-     * Retrieves the specified option as a String, if it exists.
271
-     *
272
-     * @param domain The domain of the option
273
-     * @param option The name of the option
274
-     * @param fallbacks An ordered array of further domains and options
275
-     * (in pairs) to try if the specified domain/option isn't found
276
-     * @return The string representation of the option or null if optional
277
-     * setting is not specified
278
-     * @since 0.6.3
279
-     */
184
+    /** {@inheritDoc} */
185
+    @Override
280
     public String getOptionString(final String domain, final String option,
186
     public String getOptionString(final String domain, final String option,
281
             final String ... fallbacks) {
187
             final String ... fallbacks) {
282
         return getOptionString(domain, option, true, PERMISSIVE_VALIDATOR, fallbacks);
188
         return getOptionString(domain, option, true, PERMISSIVE_VALIDATOR, fallbacks);
283
     }
189
     }
284
 
190
 
285
-    /**
286
-     * Retrieves the specified option as a character.
287
-     *
288
-     * @param domain The domain of the option
289
-     * @param option The name of the option
290
-     * @return The value of the option
291
-     */
191
+    /** {@inheritDoc} */
192
+    @Override
292
     public char getOptionChar(final String domain, final String option) {
193
     public char getOptionChar(final String domain, final String option) {
293
         return getOption(domain, option).charAt(0);
194
         return getOption(domain, option).charAt(0);
294
     }
195
     }
295
 
196
 
296
-    /**
297
-     * Retrieves a colour representation of the specified option.
298
-     *
299
-     * @param domain The domain of the option
300
-     * @param option The name of the option
301
-     * @param fallbacks An ordered array of further domains and options
302
-     * (in pairs) to try if the specified domain/option isn't found
303
-     * @return The colour representation of the option
304
-     * @since 0.6.3m1
305
-     */
197
+    /** {@inheritDoc} */
198
+    @Override
306
     public Colour getOptionColour(final String domain, final String option,
199
     public Colour getOptionColour(final String domain, final String option,
307
             final String ... fallbacks) {
200
             final String ... fallbacks) {
308
         final String value = getOptionString(domain, option, true, COLOUR_VALIDATOR, fallbacks);
201
         final String value = getOptionString(domain, option, true, COLOUR_VALIDATOR, fallbacks);
310
         return value == null ? null : colourManager.get().getColourFromString(value, null);
203
         return value == null ? null : colourManager.get().getColourFromString(value, null);
311
     }
204
     }
312
 
205
 
313
-    /**
314
-     * Retrieves a boolean representation of the specified option.
315
-     *
316
-     * @param domain The domain of the option
317
-     * @param option The name of the option
318
-     * @return The boolean representation of the option
319
-     */
206
+    /** {@inheritDoc} */
207
+    @Override
320
     public boolean getOptionBool(final String domain, final String option) {
208
     public boolean getOptionBool(final String domain, final String option) {
321
         return Boolean.parseBoolean(getOption(domain, option));
209
         return Boolean.parseBoolean(getOption(domain, option));
322
     }
210
     }
323
 
211
 
324
-    /**
325
-     * Retrieves a list representation of the specified option.
326
-     *
327
-     * @param domain The domain of the option
328
-     * @param option The name of the option
329
-     * @param trimEmpty Whether or not to trim empty lines
330
-     * @return The list representation of the option
331
-     */
212
+    /** {@inheritDoc} */
213
+    @Override
332
     public List<String> getOptionList(final String domain, final String option,
214
     public List<String> getOptionList(final String domain, final String option,
333
             final boolean trimEmpty) {
215
             final boolean trimEmpty) {
334
         final List<String> res = new ArrayList<>();
216
         final List<String> res = new ArrayList<>();
344
         return res;
226
         return res;
345
     }
227
     }
346
 
228
 
347
-    /**
348
-     * Retrieves a list representation of the specified option, trimming empty
349
-     * lines by default.
350
-     *
351
-     * @param domain The domain of the option
352
-     * @param option The name of the option
353
-     * @return The list representation of the option
354
-     */
229
+    /** {@inheritDoc} */
230
+    @Override
355
     public List<String> getOptionList(final String domain, final String option) {
231
     public List<String> getOptionList(final String domain, final String option) {
356
         return getOptionList(domain, option, true);
232
         return getOptionList(domain, option, true);
357
     }
233
     }
358
 
234
 
359
-    /**
360
-     * Retrieves an integral representation of the specified option.
361
-     *
362
-     * @param domain The domain of the option
363
-     * @param option The name of the option
364
-     * @param fallbacks An ordered array of further domains and options
365
-     * (in pairs) to try if the specified domain/option isn't found
366
-     * @return The integer representation of the option
367
-     */
235
+    /** {@inheritDoc} */
236
+    @Override
368
     public Integer getOptionInt(final String domain, final String option,
237
     public Integer getOptionInt(final String domain, final String option,
369
             final String ... fallbacks) {
238
             final String ... fallbacks) {
370
         return getOptionInt(domain, option, true, fallbacks);
239
         return getOptionInt(domain, option, true, fallbacks);
371
     }
240
     }
372
 
241
 
373
-    /**
374
-     * Retrieves an integral representation of the specified option.
375
-     *
376
-     * @since 0.6.5
377
-     * @param domain The domain of the option
378
-     * @param option The name of the option
379
-     * @param required Whether this option is required or optional
380
-     * @param fallbacks An ordered array of further domains and options
381
-     * (in pairs) to try if the specified domain/option isn't found
382
-     * @return The integer representation of the option
383
-     */
242
+    /** {@inheritDoc} */
243
+    @Override
384
     public Integer getOptionInt(final String domain, final String option,
244
     public Integer getOptionInt(final String domain, final String option,
385
             final boolean required, final String ... fallbacks) {
245
             final boolean required, final String ... fallbacks) {
386
         final String value = getOptionString(domain, option, required, INT_VALIDATOR, fallbacks);
246
         final String value = getOptionString(domain, option, required, INT_VALIDATOR, fallbacks);

+ 41
- 107
src/com/dmdirc/config/Identity.java Datei anzeigen

23
 package com.dmdirc.config;
23
 package com.dmdirc.config;
24
 
24
 
25
 import com.dmdirc.interfaces.ConfigChangeListener;
25
 import com.dmdirc.interfaces.ConfigChangeListener;
26
+import com.dmdirc.interfaces.config.ConfigProvider;
26
 import com.dmdirc.logger.ErrorLevel;
27
 import com.dmdirc.logger.ErrorLevel;
27
 import com.dmdirc.logger.Logger;
28
 import com.dmdirc.logger.Logger;
28
 import com.dmdirc.util.collections.WeakList;
29
 import com.dmdirc.util.collections.WeakList;
51
  * Note: this class has a natural ordering that is inconsistent with equals.
52
  * Note: this class has a natural ordering that is inconsistent with equals.
52
  */
53
  */
53
 @Slf4j
54
 @Slf4j
54
-public class Identity extends ConfigSource implements Comparable<Identity> {
55
+public class Identity extends ConfigSource implements Comparable<Identity>, ConfigProvider {
55
 
56
 
56
     /** A regular expression that will match all characters illegal in file names. */
57
     /** A regular expression that will match all characters illegal in file names. */
57
     protected static final String ILLEGAL_CHARS = "[\\\\\"/:\\*\\?\"<>\\|]";
58
     protected static final String ILLEGAL_CHARS = "[\\\\\"/:\\*\\?\"<>\\|]";
72
     protected ConfigManager globalConfig;
73
     protected ConfigManager globalConfig;
73
 
74
 
74
     /** The config change listeners for this source. */
75
     /** The config change listeners for this source. */
75
-    protected final List<ConfigChangeListener> listeners
76
-            = new WeakList<ConfigChangeListener>();
76
+    protected final List<ConfigChangeListener> listeners = new WeakList<>();
77
 
77
 
78
     /** Whether this identity needs to be saved. */
78
     /** Whether this identity needs to be saved. */
79
     protected boolean needSave;
79
     protected boolean needSave;
187
      *
187
      *
188
      * @since 0.6.3
188
      * @since 0.6.3
189
      * @param forceDefault Whether to force this to be a default identity
189
      * @param forceDefault Whether to force this to be a default identity
190
-     * @param file The file to load this identity from (or null)
191
      * @throws InvalidIdentityFileException if the identity file is invalid
190
      * @throws InvalidIdentityFileException if the identity file is invalid
192
      * @throws IOException On I/O exception when reading the identity
191
      * @throws IOException On I/O exception when reading the identity
193
      */
192
      */
204
         }
203
         }
205
     }
204
     }
206
 
205
 
207
-    /**
208
-     * Attempts to reload this identity from disk. If this identity has been
209
-     * modified (i.e., {@code needSave} is true), then this method silently
210
-     * returns straight away. All relevant ConfigChangeListeners are fired for
211
-     * new, altered and deleted properties. The target of the identity will not
212
-     * be changed by this method, even if it has changed on disk.
213
-     *
214
-     * @throws java.io.IOException On I/O exception when reading the identity
215
-     * @throws InvalidConfigFileException if the config file is no longer valid
216
-     */
206
+    /** {@inheritDoc} */
207
+    @Override
217
     public void reload() throws IOException, InvalidConfigFileException {
208
     public void reload() throws IOException, InvalidConfigFileException {
218
         if (needSave) {
209
         if (needSave) {
219
             return;
210
             return;
220
         }
211
         }
221
 
212
 
222
-        final List<String[]> changes = new LinkedList<String[]>();
213
+        final List<String[]> changes = new LinkedList<>();
223
 
214
 
224
         synchronized (this) {
215
         synchronized (this) {
225
             final Map<String, Map<String, String>> oldProps = file.getKeyDomains();
216
             final Map<String, Map<String, String>> oldProps = file.getKeyDomains();
264
      * @since 0.6.3m1
255
      * @since 0.6.3m1
265
      */
256
      */
266
     private void fireSettingChange(final String domain, final String key) {
257
     private void fireSettingChange(final String domain, final String key) {
267
-        for (ConfigChangeListener listener : new ArrayList<ConfigChangeListener>(listeners)) {
258
+        for (ConfigChangeListener listener : new ArrayList<>(listeners)) {
268
             listener.configChanged(domain, key);
259
             listener.configChanged(domain, key);
269
         }
260
         }
270
     }
261
     }
271
 
262
 
272
-    /**
273
-     * Returns the name of this identity.
274
-     *
275
-     * @return The name of this identity
276
-     */
263
+    /** {@inheritDoc} */
264
+    @Override
277
     public String getName() {
265
     public String getName() {
278
         if (hasOptionString(DOMAIN, "name")) {
266
         if (hasOptionString(DOMAIN, "name")) {
279
             return getOption(DOMAIN, "name");
267
             return getOption(DOMAIN, "name");
301
         }
289
         }
302
     }
290
     }
303
 
291
 
304
-    /**
305
-     * Determines whether this identity can be used as a profile when
306
-     * connecting to a server. Profiles are identities that can supply
307
-     * nick, ident, real name, etc.
308
-     *
309
-     * @return True iff this identity can be used as a profile
310
-     */
292
+    /** {@inheritDoc} */
293
+    @Override
311
     public boolean isProfile() {
294
     public boolean isProfile() {
312
         return (hasOptionString(PROFILE_DOMAIN, "nicknames")
295
         return (hasOptionString(PROFILE_DOMAIN, "nicknames")
313
                 || hasOptionString(PROFILE_DOMAIN, "nickname"))
296
                 || hasOptionString(PROFILE_DOMAIN, "nickname"))
336
         return value;
319
         return value;
337
     }
320
     }
338
 
321
 
339
-    /**
340
-     * Sets the specified option in this identity to the specified value.
341
-     *
342
-     * @param domain The domain of the option
343
-     * @param option The name of the option
344
-     * @param value The new value for the option
345
-     */
322
+    /** {@inheritDoc} */
323
+    @Override
346
     public void setOption(final String domain, final String option,
324
     public void setOption(final String domain, final String option,
347
             final String value) {
325
             final String value) {
348
         String oldValue;
326
         String oldValue;
394
         }
372
         }
395
     }
373
     }
396
 
374
 
397
-    /**
398
-     * Sets the specified option in this identity to the specified value.
399
-     *
400
-     * @param domain The domain of the option
401
-     * @param option The name of the option
402
-     * @param value The new value for the option
403
-     */
375
+    /** {@inheritDoc} */
376
+    @Override
404
     public void setOption(final String domain, final String option,
377
     public void setOption(final String domain, final String option,
405
             final int value) {
378
             final int value) {
406
         setOption(domain, option, String.valueOf(value));
379
         setOption(domain, option, String.valueOf(value));
407
     }
380
     }
408
 
381
 
409
-    /**
410
-     * Sets the specified option in this identity to the specified value.
411
-     *
412
-     * @param domain The domain of the option
413
-     * @param option The name of the option
414
-     * @param value The new value for the option
415
-     */
382
+    /** {@inheritDoc} */
383
+    @Override
416
     public void setOption(final String domain, final String option,
384
     public void setOption(final String domain, final String option,
417
             final boolean value) {
385
             final boolean value) {
418
         setOption(domain, option, String.valueOf(value));
386
         setOption(domain, option, String.valueOf(value));
419
     }
387
     }
420
 
388
 
421
-    /**
422
-     * Sets the specified option in this identity to the specified value.
423
-     *
424
-     * @param domain The domain of the option
425
-     * @param option The name of the option
426
-     * @param value The new value for the option
427
-     */
389
+    /** {@inheritDoc} */
390
+    @Override
428
     public void setOption(final String domain, final String option,
391
     public void setOption(final String domain, final String option,
429
             final List<String> value) {
392
             final List<String> value) {
430
         final StringBuilder temp = new StringBuilder();
393
         final StringBuilder temp = new StringBuilder();
435
         setOption(domain, option, temp.length() > 0 ? temp.substring(1) : temp.toString());
398
         setOption(domain, option, temp.length() > 0 ? temp.substring(1) : temp.toString());
436
     }
399
     }
437
 
400
 
438
-    /**
439
-     * Unsets a specified option.
440
-     *
441
-     * @param domain domain of the option
442
-     * @param option name of the option
443
-     */
401
+    /** {@inheritDoc} */
402
+    @Override
444
     public void unsetOption(final String domain, final String option) {
403
     public void unsetOption(final String domain, final String option) {
445
         synchronized (this) {
404
         synchronized (this) {
446
             file.getKeyDomain(domain).remove(option);
405
             file.getKeyDomain(domain).remove(option);
450
         fireSettingChange(domain, option);
409
         fireSettingChange(domain, option);
451
     }
410
     }
452
 
411
 
453
-    /**
454
-     * Returns the set of domains available in this identity.
455
-     *
456
-     * @since 0.6
457
-     * @return The set of domains used by this identity
458
-     */
412
+    /** {@inheritDoc} */
413
+    @Override
459
     public Set<String> getDomains() {
414
     public Set<String> getDomains() {
460
-        return new HashSet<String>(file.getKeyDomains().keySet());
415
+        return new HashSet<>(file.getKeyDomains().keySet());
461
     }
416
     }
462
 
417
 
463
-    /**
464
-     * Retrieves a map of all options within the specified domain in this
465
-     * identity.
466
-     *
467
-     * @param domain The domain to retrieve
468
-     * @since 0.6
469
-     * @return A map of option names to values
470
-     */
418
+    /** {@inheritDoc} */
419
+    @Override
471
     public synchronized Map<String, String> getOptions(final String domain) {
420
     public synchronized Map<String, String> getOptions(final String domain) {
472
-        return new HashMap<String, String>(file.getKeyDomain(domain));
421
+        return new HashMap<>(file.getKeyDomain(domain));
473
     }
422
     }
474
 
423
 
475
-    /**
476
-     * Saves this identity to disk if it has been updated.
477
-     */
424
+    /** {@inheritDoc} */
425
+    @Override
478
     public synchronized void save() {
426
     public synchronized void save() {
479
         log.info("{}: saving. Needsave = {}", new Object[]{ getName(), needSave });
427
         log.info("{}: saving. Needsave = {}", new Object[]{ getName(), needSave });
480
 
428
 
514
                         : file.getKeyDomains().entrySet()) {
462
                         : file.getKeyDomains().entrySet()) {
515
                     final String domain = entry.getKey();
463
                     final String domain = entry.getKey();
516
 
464
 
517
-                    for (Map.Entry<String, String> subentry
518
-                        : new HashSet<Map.Entry<String, String>>(entry.getValue().entrySet())) {
465
+                    for (Map.Entry<String, String> subentry : new HashSet<>(entry.getValue().entrySet())) {
519
                         final String key = subentry.getKey();
466
                         final String key = subentry.getKey();
520
                         final String value = subentry.getValue();
467
                         final String value = subentry.getValue();
521
 
468
 
544
         }
491
         }
545
     }
492
     }
546
 
493
 
547
-    /**
548
-     * Deletes this identity from disk.
549
-     */
494
+    /** {@inheritDoc} */
495
+    @Override
550
     public synchronized void delete() {
496
     public synchronized void delete() {
551
         if (file != null) {
497
         if (file != null) {
552
             file.delete();
498
             file.delete();
555
         IdentityManager.getIdentityManager().unregisterIdentity(this);
501
         IdentityManager.getIdentityManager().unregisterIdentity(this);
556
     }
502
     }
557
 
503
 
558
-    /**
559
-     * Retrieves this identity's target.
560
-     *
561
-     * @return The target of this identity
562
-     */
504
+    /** {@inheritDoc} */
505
+    @Override
563
     public ConfigTarget getTarget() {
506
     public ConfigTarget getTarget() {
564
         return myTarget;
507
         return myTarget;
565
     }
508
     }
575
         return file != null && file.getFile() != null && file.getFile().equals(target);
518
         return file != null && file.getFile() != null && file.getFile().equals(target);
576
     }
519
     }
577
 
520
 
578
-    /**
579
-     * Adds a new config change listener for this identity.
580
-     *
581
-     * @param listener The listener to be added
582
-     */
521
+    /** {@inheritDoc} */
522
+    @Override
583
     public void addListener(final ConfigChangeListener listener) {
523
     public void addListener(final ConfigChangeListener listener) {
584
         listeners.add(listener);
524
         listeners.add(listener);
585
     }
525
     }
586
 
526
 
587
-    /**
588
-     * Removes the specific config change listener from this identity.
589
-     *
590
-     * @param listener The listener to be removed
591
-     */
527
+    /** {@inheritDoc} */
528
+    @Override
592
     public void removeListener(final ConfigChangeListener listener) {
529
     public void removeListener(final ConfigChangeListener listener) {
593
         listeners.remove(listener);
530
         listeners.remove(listener);
594
     }
531
     }
681
      */
618
      */
682
     public static Identity buildIdentity(final ConfigTarget target)
619
     public static Identity buildIdentity(final ConfigTarget target)
683
             throws IOException {
620
             throws IOException {
684
-        final Map<String, Map<String, String>> settings
685
-                = new HashMap<String, Map<String, String>>();
621
+        final Map<String, Map<String, String>> settings = new HashMap<>();
686
         settings.put(DOMAIN, new HashMap<String, String>(2));
622
         settings.put(DOMAIN, new HashMap<String, String>(2));
687
         settings.get(DOMAIN).put("name", target.getData());
623
         settings.get(DOMAIN).put("name", target.getData());
688
         settings.get(DOMAIN).put(target.getTypeName(), target.getData());
624
         settings.get(DOMAIN).put(target.getTypeName(), target.getData());
705
      * @see #createIdentity(java.util.Map)
641
      * @see #createIdentity(java.util.Map)
706
      */
642
      */
707
     public static Identity buildProfile(final String name) throws IOException {
643
     public static Identity buildProfile(final String name) throws IOException {
708
-        final Map<String, Map<String, String>> settings
709
-                = new HashMap<String, Map<String, String>>();
644
+        final Map<String, Map<String, String>> settings = new HashMap<>();
710
         settings.put(DOMAIN, new HashMap<String, String>(1));
645
         settings.put(DOMAIN, new HashMap<String, String>(1));
711
         settings.put(PROFILE_DOMAIN, new HashMap<String, String>(2));
646
         settings.put(PROFILE_DOMAIN, new HashMap<String, String>(2));
712
 
647
 
735
      */
670
      */
736
     public static Identity buildIdentity(final String name, final String type)
671
     public static Identity buildIdentity(final String name, final String type)
737
             throws IOException {
672
             throws IOException {
738
-        final Map<String, Map<String, String>> settings
739
-                = new HashMap<String, Map<String, String>>();
673
+        final Map<String, Map<String, String>> settings = new HashMap<>();
740
         settings.put(DOMAIN, new HashMap<String, String>(2));
674
         settings.put(DOMAIN, new HashMap<String, String>(2));
741
 
675
 
742
         settings.get(DOMAIN).put("name", name);
676
         settings.get(DOMAIN).put("name", name);

+ 104
- 0
src/com/dmdirc/interfaces/config/AggregateConfigProvider.java Datei anzeigen

1
+/*
2
+ * Copyright (c) 2006-2013 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.interfaces.config;
24
+
25
+import com.dmdirc.config.Identity;
26
+import com.dmdirc.interfaces.ConfigChangeListener;
27
+
28
+import java.util.List;
29
+import java.util.Set;
30
+
31
+/**
32
+ * A configuration provider which aggregates an ordered list of other
33
+ * {@link ConfigProvider}s.
34
+ *
35
+ * Because this provider exposes settings from multiple independent sources,
36
+ * it cannot change any settings as the scope of such changes may be unclear.
37
+ */
38
+public interface AggregateConfigProvider extends ReadOnlyConfigProvider {
39
+
40
+    /**
41
+     * Adds a change listener for the specified domain.
42
+     *
43
+     * @param domain The domain to be monitored
44
+     * @param listener The listener to register
45
+     */
46
+    void addChangeListener(String domain, ConfigChangeListener listener);
47
+
48
+    /**
49
+     * Adds a change listener for the specified domain and key.
50
+     *
51
+     * @param domain The domain of the option
52
+     * @param key The option to be monitored
53
+     * @param listener The listener to register
54
+     */
55
+    void addChangeListener(String domain, String key, ConfigChangeListener listener);
56
+
57
+    /**
58
+     * Returns the name of all domains known by this manager.
59
+     *
60
+     * @return A list of domains known to this manager
61
+     */
62
+    Set<String> getDomains();
63
+
64
+    /**
65
+     * Retrieves a list of sources for this config manager.
66
+     * @return This config manager's sources.
67
+     */
68
+    List<Identity> getSources();
69
+
70
+    /**
71
+     * Removes the specified listener for all domains and options.
72
+     *
73
+     * @param listener The listener to be removed
74
+     */
75
+    void removeListener(ConfigChangeListener listener);
76
+
77
+    /**
78
+     * Migrates this ConfigManager from its current configuration to the
79
+     * appropriate one for the specified new parameters, firing listeners where
80
+     * settings have changed.
81
+     *
82
+     * @param protocol The protocol for this manager
83
+     * @param ircd The new name of the ircd for this manager
84
+     * @param network The new name of the network for this manager
85
+     * @param server The new name of the server for this manager
86
+     * @since 0.6.3
87
+     */
88
+    void migrate(String protocol, String ircd, String network, String server);
89
+
90
+    /**
91
+     * Migrates this ConfigManager from its current configuration to the
92
+     * appropriate one for the specified new parameters, firing listeners where
93
+     * settings have changed.
94
+     *
95
+     * @param protocol The protocol for this manager
96
+     * @param ircd The new name of the ircd for this manager
97
+     * @param network The new name of the network for this manager
98
+     * @param server The new name of the server for this manager
99
+     * @param channel The new name of the channel for this manager
100
+     * @since 0.6.3
101
+     */
102
+    void migrate(String protocol, String ircd, String network, String server, String channel);
103
+
104
+}

+ 150
- 0
src/com/dmdirc/interfaces/config/ConfigProvider.java Datei anzeigen

1
+/*
2
+ * Copyright (c) 2006-2013 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.interfaces.config;
24
+
25
+import com.dmdirc.config.ConfigTarget;
26
+import com.dmdirc.interfaces.ConfigChangeListener;
27
+import com.dmdirc.util.io.InvalidConfigFileException;
28
+
29
+import java.io.IOException;
30
+import java.util.List;
31
+import java.util.Set;
32
+
33
+/**
34
+ * Provides methods to read and write from a single configuration source, such
35
+ * as a file on disk.
36
+ */
37
+public interface ConfigProvider extends ReadOnlyConfigProvider {
38
+
39
+    /**
40
+     * Adds a new config change listener for this identity.
41
+     *
42
+     * @param listener The listener to be added
43
+     */
44
+    void addListener(ConfigChangeListener listener);
45
+
46
+    /**
47
+     * Deletes this identity from disk.
48
+     */
49
+    void delete();
50
+
51
+    /**
52
+     * Returns the set of domains available in this identity.
53
+     *
54
+     * @since 0.6
55
+     * @return The set of domains used by this identity
56
+     */
57
+    Set<String> getDomains();
58
+
59
+    /**
60
+     * Returns the name of this identity.
61
+     *
62
+     * @return The name of this identity
63
+     */
64
+    String getName();
65
+
66
+    /**
67
+     * Retrieves this identity's target.
68
+     *
69
+     * @return The target of this identity
70
+     */
71
+    ConfigTarget getTarget();
72
+
73
+    /**
74
+     * Determines whether this identity can be used as a profile when
75
+     * connecting to a server. Profiles are identities that can supply
76
+     * nick, ident, real name, etc.
77
+     *
78
+     * @return True iff this identity can be used as a profile
79
+     */
80
+    boolean isProfile();
81
+
82
+    /**
83
+     * Attempts to reload this identity from disk. If this identity has been
84
+     * modified (i.e., {@code needSave} is true), then this method silently
85
+     * returns straight away. All relevant ConfigChangeListeners are fired for
86
+     * new, altered and deleted properties. The target of the identity will not
87
+     * be changed by this method, even if it has changed on disk.
88
+     *
89
+     * @throws java.io.IOException On I/O exception when reading the identity
90
+     * @throws InvalidConfigFileException if the config file is no longer valid
91
+     */
92
+    void reload() throws IOException, InvalidConfigFileException;
93
+
94
+    /**
95
+     * Removes the specific config change listener from this identity.
96
+     *
97
+     * @param listener The listener to be removed
98
+     */
99
+    void removeListener(ConfigChangeListener listener);
100
+
101
+    /**
102
+     * Saves this identity to disk if it has been updated.
103
+     */
104
+    void save();
105
+
106
+    /**
107
+     * Sets the specified option in this identity to the specified value.
108
+     *
109
+     * @param domain The domain of the option
110
+     * @param option The name of the option
111
+     * @param value The new value for the option
112
+     */
113
+    void setOption(String domain, String option, String value);
114
+
115
+    /**
116
+     * Sets the specified option in this identity to the specified value.
117
+     *
118
+     * @param domain The domain of the option
119
+     * @param option The name of the option
120
+     * @param value The new value for the option
121
+     */
122
+    void setOption(String domain, String option, int value);
123
+
124
+    /**
125
+     * Sets the specified option in this identity to the specified value.
126
+     *
127
+     * @param domain The domain of the option
128
+     * @param option The name of the option
129
+     * @param value The new value for the option
130
+     */
131
+    void setOption(String domain, String option, boolean value);
132
+
133
+    /**
134
+     * Sets the specified option in this identity to the specified value.
135
+     *
136
+     * @param domain The domain of the option
137
+     * @param option The name of the option
138
+     * @param value The new value for the option
139
+     */
140
+    void setOption(String domain, String option, List<String> value);
141
+
142
+    /**
143
+     * Unsets a specified option.
144
+     *
145
+     * @param domain domain of the option
146
+     * @param option name of the option
147
+     */
148
+    void unsetOption(String domain, String option);
149
+
150
+}

+ 254
- 0
src/com/dmdirc/interfaces/config/ReadOnlyConfigProvider.java Datei anzeigen

1
+/*
2
+ * Copyright (c) 2006-2013 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.interfaces.config;
24
+
25
+import com.dmdirc.ui.Colour;
26
+import com.dmdirc.util.validators.Validator;
27
+
28
+import java.util.List;
29
+import java.util.Map;
30
+
31
+/**
32
+ * A source of configuration settings that can only be read from.
33
+ */
34
+public interface ReadOnlyConfigProvider {
35
+
36
+    /**
37
+     * Retrieves the specified option from this config source.
38
+     *
39
+     * @param domain The domain of the option
40
+     * @param option The name of the option
41
+     * @return The value of the option, or null if it doesn't exist
42
+     */
43
+    String getOption(String domain, String option);
44
+
45
+    /**
46
+     * Retrieves the first value for the specified option that matches the
47
+     * specified validator.
48
+     *
49
+     * @since 0.6.5
50
+     * @param domain The domain of the option
51
+     * @param option The name of the option
52
+     * @param validator The validator to use to check legal values
53
+     * @return The value of the option, or null if no matching values exist
54
+     */
55
+    String getOption(String domain, String option, Validator<String> validator);
56
+
57
+    /**
58
+     * Retrieves a boolean representation of the specified option.
59
+     *
60
+     * @param domain The domain of the option
61
+     * @param option The name of the option
62
+     * @return The boolean representation of the option
63
+     */
64
+    boolean getOptionBool(String domain, String option);
65
+
66
+    /**
67
+     * Retrieves the specified option as a character.
68
+     *
69
+     * @param domain The domain of the option
70
+     * @param option The name of the option
71
+     * @return The value of the option
72
+     */
73
+    char getOptionChar(String domain, String option);
74
+
75
+    /**
76
+     * Retrieves a colour representation of the specified option.
77
+     *
78
+     * @param domain The domain of the option
79
+     * @param option The name of the option
80
+     * @param fallbacks An ordered array of further domains and options
81
+     * (in pairs) to try if the specified domain/option isn't found
82
+     * @return The colour representation of the option
83
+     * @since 0.6.3m1
84
+     */
85
+    Colour getOptionColour(String domain, String option, String... fallbacks);
86
+
87
+    /**
88
+     * Retrieves an integral representation of the specified option.
89
+     *
90
+     * @param domain The domain of the option
91
+     * @param option The name of the option
92
+     * @param fallbacks An ordered array of further domains and options
93
+     * (in pairs) to try if the specified domain/option isn't found
94
+     * @return The integer representation of the option
95
+     */
96
+    Integer getOptionInt(String domain, String option, String... fallbacks);
97
+
98
+    /**
99
+     * Retrieves an integral representation of the specified option.
100
+     *
101
+     * @since 0.6.5
102
+     * @param domain The domain of the option
103
+     * @param option The name of the option
104
+     * @param required Whether this option is required or optional
105
+     * @param fallbacks An ordered array of further domains and options
106
+     * (in pairs) to try if the specified domain/option isn't found
107
+     * @return The integer representation of the option
108
+     */
109
+    Integer getOptionInt(String domain, String option, boolean required, String... fallbacks);
110
+
111
+    /**
112
+     * Retrieves a list representation of the specified option.
113
+     *
114
+     * @param domain The domain of the option
115
+     * @param option The name of the option
116
+     * @param trimEmpty Whether or not to trim empty lines
117
+     * @return The list representation of the option
118
+     */
119
+    List<String> getOptionList(String domain, String option, boolean trimEmpty);
120
+
121
+    /**
122
+     * Retrieves a list representation of the specified option, trimming empty
123
+     * lines by default.
124
+     *
125
+     * @param domain The domain of the option
126
+     * @param option The name of the option
127
+     * @return The list representation of the option
128
+     */
129
+    List<String> getOptionList(String domain, String option);
130
+
131
+    /**
132
+     * Retrieves the specified option as a String, if it exists and satisfies
133
+     * the specified validator.
134
+     * <p>
135
+     * If no fallback settings are supplied (or if fallback settings are
136
+     * supplied and execution reaches the last fallback setting) AND if the
137
+     * 'required' parameter is true, then all disabled values (i.e., those
138
+     * starting with <code>false:</code>) will be ignored. To check if a valid
139
+     * non-disabled value exists, call
140
+     * {@link #hasOptionString(java.lang.String, java.lang.String, com.dmdirc.util.validators.Validator)}.
141
+     *
142
+     * @param domain The domain of the option
143
+     * @param option The name of the option
144
+     * @param required Whether the specified option is required or not
145
+     * @param validator The validator to use to check the value
146
+     * @param fallbacks An ordered array of further domains and options
147
+     * (in pairs) to try if the specified domain/option isn't found
148
+     * @return The string representation of the option or null if optional
149
+     * setting is not specified
150
+     * @since 0.6.5
151
+     */
152
+    String getOptionString(String domain, String option, boolean required, Validator<String> validator, String... fallbacks);
153
+
154
+    /**
155
+     * Retrieves the specified option as a String, if it exists.
156
+     *
157
+     * @param domain The domain of the option
158
+     * @param option The name of the option
159
+     * @param fallbacks An ordered array of further domains and options
160
+     * (in pairs) to try if the specified domain/option isn't found
161
+     * @return The string representation of the option or null if optional
162
+     * setting is not specified
163
+     * @since 0.6.3
164
+     */
165
+    String getOptionString(String domain, String option, String... fallbacks);
166
+
167
+    /**
168
+     * Retrieves a boolean representation of the specified option.
169
+     *
170
+     * @param domain The domain of the option
171
+     * @param option The name of the option
172
+     * @return The boolean representation of the option
173
+     */
174
+    boolean hasOptionBool(String domain, String option);
175
+
176
+    /**
177
+     * Determines if this source has the specified character option.
178
+     *
179
+     * @param domain The domain of the option
180
+     * @param option The name of the option
181
+     * @since 0.6.3m1
182
+     * @return True iff the option exists and is parsable as a char,
183
+     * false otherwise.
184
+     */
185
+    boolean hasOptionChar(String domain, String option);
186
+
187
+    /**
188
+     * Determines if this source has the specified colour option.
189
+     *
190
+     * @param domain The domain of the option
191
+     * @param option The name of the option
192
+     * @since 0.6.3m1
193
+     * @return True iff the option exists and is parsable as a colour,
194
+     * false otherwise.
195
+     */
196
+    boolean hasOptionColour(String domain, String option);
197
+
198
+    /**
199
+     * Determines if this source has the specified integer option.
200
+     *
201
+     * @param domain The domain of the option
202
+     * @param option The name of the option
203
+     * @since 0.6.3m1
204
+     * @return True iff the option exists and is parsable as an integer,
205
+     * false otherwise.
206
+     */
207
+    boolean hasOptionInt(String domain, String option);
208
+
209
+    /**
210
+     * Determines if this source has the specified String option.
211
+     * <p>
212
+     * A String option is considered to exist if:
213
+     * <ul>
214
+     *  <li>there is a value for the specified option,
215
+     *  <li>that value is not empty, and
216
+     *  <li>that value is not disabled (i.e., it doesn't begin with "false:")
217
+     * </ul>
218
+     *
219
+     * @param domain The domain of the option
220
+     * @param option The name of the option
221
+     * @since 0.6.3m1
222
+     * @return True iff the option exists and is not empty, false otherwise
223
+     */
224
+    boolean hasOptionString(String domain, String option);
225
+
226
+    /**
227
+     * Determines if this source has the specified String option.
228
+     * <p>
229
+     * A String option is considered to exist if:
230
+     * <ul>
231
+     *  <li>there is a value for the specified option that
232
+     *      satisfies the specified validator,
233
+     *  <li>that value is not empty, and
234
+     *  <li>that value is not disabled (i.e., it doesn't begin with "false:")
235
+     * </ul>
236
+     *
237
+     * @param domain The domain of the option
238
+     * @param option The name of the option
239
+     * @param validator The validator to use to check legal values
240
+     * @since 0.6.5
241
+     * @return True iff the option exists and is not empty, false otherwise
242
+     */
243
+    boolean hasOptionString(String domain, String option, Validator<String> validator);
244
+
245
+    /**
246
+     * Returns the name of all the options in the specified domain. If the
247
+     * domain doesn't exist, an empty list is returned.
248
+     *
249
+     * @param domain The domain to search
250
+     * @return A list of options in the specified domain
251
+     */
252
+    Map<String, String> getOptions(String domain);
253
+
254
+}

+ 9
- 2
test/com/dmdirc/harness/TestConfigSource.java Datei anzeigen

22
 
22
 
23
 package com.dmdirc.harness;
23
 package com.dmdirc.harness;
24
 
24
 
25
-import com.dmdirc.config.*;
25
+import com.dmdirc.config.ConfigSource;
26
 import com.dmdirc.util.validators.Validator;
26
 import com.dmdirc.util.validators.Validator;
27
 
27
 
28
+import java.util.Map;
29
+
28
 public class TestConfigSource extends ConfigSource {
30
 public class TestConfigSource extends ConfigSource {
29
 
31
 
30
     @Override
32
     @Override
33
     }
35
     }
34
 
36
 
35
     @Override
37
     @Override
36
-    protected String getOption(String domain, String option, Validator<String> validator) {
38
+    public String getOption(String domain, String option, Validator<String> validator) {
37
         return option;
39
         return option;
38
     }
40
     }
41
+
42
+    @Override
43
+    public Map<String, String> getOptions(String domain) {
44
+        throw new UnsupportedOperationException("Not supported yet.");
45
+    }
39
 }
46
 }

Laden…
Abbrechen
Speichern