Parcourir la source

Make getChannel optional.

pull/266/head
Chris Smith il y a 9 ans
Parent
révision
deae9bd285

+ 5
- 3
src/com/dmdirc/MessageEncoder.java Voir le fichier

@@ -63,9 +63,11 @@ public class MessageEncoder implements Encoder {
63 63
         String encoding = connection.getWindowModel().getConfigManager()
64 64
                 .getOption("general", "encoding");
65 65
 
66
-        if (target != null && parser.isValidChannelName(target) && connection.hasChannel(target)) {
67
-            final Channel channel = connection.getChannel(target);
68
-            encoding = channel.getConfigManager().getOption("general", "encoding");
66
+        if (target != null && parser.isValidChannelName(target)) {
67
+            encoding = connection.getChannel(target)
68
+                    .map(Channel::getConfigManager)
69
+                    .map(cm -> cm.getOption("general", "encoding"))
70
+                    .orElse(encoding);
69 71
         }
70 72
 
71 73
         try {

+ 9
- 8
src/com/dmdirc/Server.java Voir le fichier

@@ -495,8 +495,8 @@ public class Server extends FrameContainer implements Connection {
495 495
     }
496 496
 
497 497
     @Override
498
-    public Channel getChannel(final String channel) {
499
-        return channels.get(channel).orElse(null);
498
+    public Optional<Channel> getChannel(final String channel) {
499
+        return channels.get(channel);
500 500
     }
501 501
 
502 502
     @Override
@@ -628,9 +628,11 @@ public class Server extends FrameContainer implements Connection {
628 628
 
629 629
         backgroundChannels.remove(chan.getName());
630 630
 
631
-        if (hasChannel(chan.getName())) {
632
-            getChannel(chan.getName()).setChannelInfo(chan);
633
-            getChannel(chan.getName()).selfJoin();
631
+        final Optional<Channel> channel = getChannel(chan.getName());
632
+        if (channel.isPresent()) {
633
+            channel.get().setChannelInfo(chan);
634
+            channel.get().selfJoin();
635
+            return channel.get();
634 636
         } else {
635 637
             final ConfigProviderMigrator channelConfig = identityFactory.createMigratableConfig(
636 638
                     getProtocol(), getIrcd(), getNetwork(), getAddress(), chan.getName());
@@ -641,9 +643,8 @@ public class Server extends FrameContainer implements Connection {
641 643
 
642 644
             getTabCompleter().addEntry(TabCompletionType.CHANNEL, chan.getName());
643 645
             channels.add(newChan);
646
+            return newChan;
644 647
         }
645
-
646
-        return getChannel(chan.getName());
647 648
     }
648 649
 
649 650
     /**
@@ -761,7 +762,7 @@ public class Server extends FrameContainer implements Connection {
761 762
                                 + request.getName();
762 763
                     }
763 764
 
764
-                    if (!hasChannel(name) || !getChannel(name).isOnChannel()) {
765
+                    if (getChannel(name).map(Channel::isOnChannel).orElse(false)) {
765 766
                         if (!focus) {
766 767
                             backgroundChannels.add(name);
767 768
                         }

+ 4
- 2
src/com/dmdirc/commandparser/commands/server/Message.java Voir le fichier

@@ -22,6 +22,7 @@
22 22
 
23 23
 package com.dmdirc.commandparser.commands.server;
24 24
 
25
+import com.dmdirc.Channel;
25 26
 import com.dmdirc.FrameContainer;
26 27
 import com.dmdirc.commandparser.BaseCommandInfo;
27 28
 import com.dmdirc.commandparser.CommandArguments;
@@ -79,8 +80,9 @@ public class Message extends Command implements IntelligentCommand,
79 80
 
80 81
             // If this is a known server or channel, and this is not a silent
81 82
             // invocation, use sendLine, else send it raw to the parser.
82
-            if (!args.isSilent() && connection.hasChannel(target)) {
83
-                connection.getChannel(target).sendLine(message);
83
+            final Optional<Channel> channel = connection.getChannel(target);
84
+            if (!args.isSilent() && channel.isPresent()) {
85
+                channel.get().sendLine(message);
84 86
             } else if (!args.isSilent() && connection.hasQuery(target)) {
85 87
                 connection.getQuery(target).sendLine(message, target);
86 88
             } else {

+ 11
- 9
src/com/dmdirc/commandparser/parsers/CommandParser.java Voir le fichier

@@ -22,6 +22,7 @@
22 22
 
23 23
 package com.dmdirc.commandparser.parsers;
24 24
 
25
+import com.dmdirc.Channel;
25 26
 import com.dmdirc.DMDircMBassador;
26 27
 import com.dmdirc.FrameContainer;
27 28
 import com.dmdirc.commandparser.CommandArguments;
@@ -36,7 +37,7 @@ import com.dmdirc.commandparser.commands.context.CommandContext;
36 37
 import com.dmdirc.events.UnknownCommandEvent;
37 38
 import com.dmdirc.interfaces.CommandController;
38 39
 import com.dmdirc.interfaces.Connection;
39
-import com.dmdirc.interfaces.config.AggregateConfigProvider;
40
+import com.dmdirc.interfaces.config.ReadOnlyConfigProvider;
40 41
 import com.dmdirc.util.EventUtils;
41 42
 import com.dmdirc.util.collections.RollingList;
42 43
 
@@ -74,7 +75,8 @@ public abstract class CommandParser implements Serializable {
74 75
      * @param commandManager Command manager to load plugins from
75 76
      * @param eventBus       The event bus to post events to.
76 77
      */
77
-    protected CommandParser(final AggregateConfigProvider configManager,
78
+    protected CommandParser(
79
+            final ReadOnlyConfigProvider configManager,
78 80
             final CommandController commandManager,
79 81
             final DMDircMBassador eventBus) {
80 82
         this.eventBus = eventBus;
@@ -195,9 +197,9 @@ public abstract class CommandParser implements Serializable {
195 197
         }
196 198
 
197 199
         if (someValid) {
198
-            for (String channel : parts) {
199
-                if (!server.isValidChannelName(channel)) {
200
-                    origin.addLine("commandError", "Invalid channel name: " + channel);
200
+            for (String channelName : parts) {
201
+                if (!server.isValidChannelName(channelName)) {
202
+                    origin.addLine("commandError", "Invalid channel name: " + channelName);
201 203
                     continue;
202 204
                 }
203 205
 
@@ -206,16 +208,16 @@ public abstract class CommandParser implements Serializable {
206 208
                         + args.getCommandName()
207 209
                         + (cargs.length > 1 ? ' ' + args.getArgumentsAsString(1) : "");
208 210
 
209
-                if (server.hasChannel(channel)) {
210
-                    server.getChannel(channel).getCommandParser().parseCommand(origin,
211
-                            newCommandString, false);
211
+                final Optional<Channel> channel = server.getChannel(channelName);
212
+                if (channel.isPresent()) {
213
+                    channel.get().getCommandParser().parseCommand(origin, newCommandString, false);
212 214
                 } else {
213 215
                     final Map.Entry<CommandInfo, Command> actCommand = commandManager.getCommand(
214 216
                             CommandType.TYPE_CHANNEL, command);
215 217
 
216 218
                     if (actCommand != null && actCommand.getValue() instanceof ExternalCommand) {
217 219
                         ((ExternalCommand) actCommand.getValue()).execute(
218
-                                origin, server, channel, silent,
220
+                                origin, server, channelName, silent,
219 221
                                 new CommandArguments(commandManager, newCommandString));
220 222
                     }
221 223
                 }

+ 1
- 1
src/com/dmdirc/interfaces/Connection.java Voir le fichier

@@ -192,7 +192,7 @@ public interface Connection {
192 192
      *
193 193
      * @return The appropriate channel object
194 194
      */
195
-    Channel getChannel(final String channel);
195
+    Optional<Channel> getChannel(final String channel);
196 196
 
197 197
     /**
198 198
      * Retrieves the possible channel prefixes in use on this server.

+ 1
- 1
test/com/dmdirc/commandparser/parsers/CommandParserTest.java Voir le fichier

@@ -78,7 +78,7 @@ public class CommandParserTest {
78 78
         when(connection.isValidChannelName("#channel1")).thenReturn(true);
79 79
         when(connection.isValidChannelName("#channel2")).thenReturn(true);
80 80
         when(connection.hasChannel("#channel1")).thenReturn(true);
81
-        when(connection.getChannel("#channel1")).thenReturn(channel);
81
+        when(connection.getChannel("#channel1")).thenReturn(Optional.of(channel));
82 82
 
83 83
         commandParser = new TestCommandParser(configProvider, commandController, eventBus);
84 84
         commandParser.registerCommand(command, commandInfo);

Chargement…
Annuler
Enregistrer