Просмотр исходного кода

Make AutoCommand use AutoValue.

pull/294/head
Chris Smith 9 лет назад
Родитель
Сommit
6aa487bd6c

+ 2
- 1
src/com/dmdirc/commandparser/auto/ActionServerPerformMigrator.java Просмотреть файл

@@ -101,7 +101,8 @@ public class ActionServerPerformMigrator implements Migrator {
101 101
             final Optional<String> profile = getCondition(configFile,
102 102
                     "SERVER_PROFILE.IDENTITY_NAME");
103 103
 
104
-            autoCommandManager.addAutoCommand(new AutoCommand(server, network, profile, response));
104
+            autoCommandManager.addAutoCommand(
105
+                    AutoCommand.create(server, network, profile, response));
105 106
             return true;
106 107
         } catch (IOException | InvalidConfigFileException | NumberFormatException ex) {
107 108
             return false;

+ 26
- 69
src/com/dmdirc/commandparser/auto/AutoCommand.java Просмотреть файл

@@ -23,9 +23,8 @@
23 23
 package com.dmdirc.commandparser.auto;
24 24
 
25 25
 
26
-import com.google.common.base.MoreObjects;
26
+import com.google.auto.value.AutoValue;
27 27
 
28
-import java.util.Objects;
29 28
 import java.util.Optional;
30 29
 
31 30
 import javax.annotation.Nonnull;
@@ -34,28 +33,36 @@ import javax.annotation.Nonnull;
34 33
  * Describes a command that is executed automatically in response to either the client opening, or a
35 34
  * server connecting.
36 35
  */
37
-public class AutoCommand {
38
-
39
-    /** The name of the server for connection events. */
40
-    private final Optional<String> server;
41
-    /** The name of the network for connection events. */
42
-    private final Optional<String> network;
43
-    /** The name of the profile for connection events. */
44
-    private final Optional<String> profile;
45
-    /** The commands to execute. */
46
-    private final String response;
47
-    /** The type of auto command. */
48
-    private final AutoCommandType type;
36
+@AutoValue
37
+public abstract class AutoCommand {
38
+
39
+    AutoCommand() {
40
+    }
49 41
 
50 42
     public AutoCommand(
51 43
             final Optional<String> server,
52 44
             final Optional<String> network,
53 45
             final Optional<String> profile,
54 46
             @Nonnull final String response) {
55
-        this.server = server;
56
-        this.network = network;
57
-        this.profile = profile;
58
-        this.response = response;
47
+
48
+    }
49
+
50
+    public abstract Optional<String> getServer();
51
+
52
+    public abstract Optional<String> getNetwork();
53
+
54
+    public abstract Optional<String> getProfile();
55
+
56
+    public abstract String getResponse();
57
+
58
+    public abstract AutoCommandType getType();
59
+
60
+    public static AutoCommand create(
61
+            final Optional<String> server,
62
+            final Optional<String> network,
63
+            final Optional<String> profile,
64
+            @Nonnull final String response) {
65
+        final AutoCommandType type;
59 66
         if (!server.isPresent() && !network.isPresent()) {
60 67
             type = AutoCommandType.GLOBAL;
61 68
         } else if (server.isPresent() && !network.isPresent()) {
@@ -65,58 +72,8 @@ public class AutoCommand {
65 72
         } else {
66 73
             type = AutoCommandType.UNKNOWN;
67 74
         }
68
-    }
69
-
70
-    public Optional<String> getServer() {
71
-        return server;
72
-    }
73
-
74
-    public Optional<String> getNetwork() {
75
-        return network;
76
-    }
77
-
78
-    public Optional<String> getProfile() {
79
-        return profile;
80
-    }
81
-
82
-    @Nonnull
83
-    public String getResponse() {
84
-        return response;
85
-    }
86
-
87
-    public AutoCommandType getType() {
88
-        return type;
89
-    }
90
-
91
-    @Override
92
-    public boolean equals(final Object object) {
93
-        if (object == null) {
94
-            return false;
95
-        }
96
-        if (getClass() != object.getClass()) {
97
-            return false;
98
-        }
99
-        final AutoCommand command = (AutoCommand) object;
100
-        return  Objects.equals(server, command.getServer())
101
-                && Objects.equals(network, command.getNetwork())
102
-                && Objects.equals(profile, command.getProfile())
103
-                && Objects.equals(response, command.getResponse());
104
-
105
-    }
106
-
107
-    @Override
108
-    public int hashCode() {
109
-        return Objects.hash(server, network, profile, response);
110
-    }
111 75
 
112
-    @Override
113
-    public String toString() {
114
-        return MoreObjects.toStringHelper(this)
115
-                .add("Connection", server)
116
-                .add("Network", network)
117
-                .add("Profile", profile)
118
-                .add("Response", response)
119
-                .toString();
76
+        return new AutoValue_AutoCommand(server, network, profile, response, type);
120 77
     }
121 78
 
122 79
 }

+ 1
- 1
src/com/dmdirc/commandparser/auto/YamlAutoCommandStore.java Просмотреть файл

@@ -87,7 +87,7 @@ public class YamlAutoCommandStore extends BaseYamlStore<AutoCommand> implements
87 87
             final Optional<String> profile =
88 88
                     Optional.ofNullable(optionalString(map, "profile"));
89 89
             final String command = requiredString(map, "command");
90
-            return Optional.of(new AutoCommand(server, network, profile, command));
90
+            return Optional.of(AutoCommand.create(server, network, profile, command));
91 91
         } catch (IllegalArgumentException ex) {
92 92
             LOG.info("Unable to read auto command", ex);
93 93
             return Optional.empty();

+ 1
- 1
src/com/dmdirc/ui/core/autocommands/MutableAutoCommand.java Просмотреть файл

@@ -106,7 +106,7 @@ public class MutableAutoCommand {
106 106
     }
107 107
 
108 108
     public AutoCommand getAutoCommand() {
109
-        return new AutoCommand(server, network, profile, response);
109
+        return AutoCommand.create(server, network, profile, response);
110 110
     }
111 111
 
112 112
     @Override

+ 4
- 4
test/com/dmdirc/commandparser/auto/AutoCommandManagerTest.java Просмотреть файл

@@ -59,13 +59,13 @@ public class AutoCommandManagerTest {
59 59
     @Before
60 60
     public void setup() {
61 61
         autoCommandManager = new AutoCommandManager(eventBus, factory);
62
-        global = new AutoCommand(Optional.<String>empty(), Optional.<String>empty(),
62
+        global = AutoCommand.create(Optional.<String>empty(), Optional.<String>empty(),
63 63
                 Optional.<String>empty(), "");
64
-        ircquakenet = new AutoCommand(Optional.ofNullable("irc.quakenet.org"),
64
+        ircquakenet = AutoCommand.create(Optional.ofNullable("irc.quakenet.org"),
65 65
                 Optional.ofNullable("Quakenet"), Optional.<String>empty(), "");
66
-        ukquakenet = new AutoCommand(Optional.ofNullable("uk.quakenet.org"),
66
+        ukquakenet = AutoCommand.create(Optional.ofNullable("uk.quakenet.org"),
67 67
                 Optional.ofNullable("Quakenet"), Optional.<String>empty(), "");
68
-        testnet = new AutoCommand(Optional.ofNullable("irc.testnet.org"),
68
+        testnet = AutoCommand.create(Optional.ofNullable("irc.testnet.org"),
69 69
                 Optional.ofNullable("Testnet"), Optional.ofNullable("profileName"), "");
70 70
         when(testProfile.getName()).thenReturn("profileName");
71 71
         when(factory.getAutoCommandHandler(global)).thenReturn(globalHandler);

+ 13
- 13
test/com/dmdirc/commandparser/auto/AutoCommandTest.java Просмотреть файл

@@ -35,81 +35,81 @@ public class AutoCommandTest {
35 35
 
36 36
     @Before
37 37
     public void setUp() throws Exception {
38
-        command = new AutoCommand(Optional.empty(), Optional.empty(), Optional.empty(), "");
38
+        command = AutoCommand.create(Optional.empty(), Optional.empty(), Optional.empty(), "");
39 39
     }
40 40
 
41 41
     @Test
42 42
     public void testGetServer_Empty() throws Exception {
43
-        command = new AutoCommand(Optional.empty(), Optional.empty(), Optional.empty(), "");
43
+        command = AutoCommand.create(Optional.empty(), Optional.empty(), Optional.empty(), "");
44 44
         assertEquals(Optional.<String>empty(), command.getServer());
45 45
     }
46 46
 
47 47
     @Test
48 48
     public void testGetServer_NotEmpty() throws Exception {
49
-        command = new AutoCommand(Optional.of("server"), Optional.empty(), Optional.empty(), "");
49
+        command = AutoCommand.create(Optional.of("server"), Optional.empty(), Optional.empty(), "");
50 50
         assertEquals(Optional.of("server"), command.getServer());
51 51
     }
52 52
 
53 53
     @Test
54 54
     public void testGetNetwork_Empty() throws Exception {
55
-        command = new AutoCommand(Optional.empty(), Optional.empty(), Optional.empty(), "");
55
+        command = AutoCommand.create(Optional.empty(), Optional.empty(), Optional.empty(), "");
56 56
         assertEquals(Optional.<String>empty(), command.getNetwork());
57 57
     }
58 58
 
59 59
     @Test
60 60
     public void testGetNetwork_NotEmpty() throws Exception {
61
-        command = new AutoCommand(Optional.empty(), Optional.of("network"), Optional.empty(), "");
61
+        command = AutoCommand.create(Optional.empty(), Optional.of("network"), Optional.empty(), "");
62 62
         assertEquals(Optional.of("network"), command.getNetwork());
63 63
     }
64 64
 
65 65
     @Test
66 66
     public void testGetProfile_Empty() throws Exception {
67
-        command = new AutoCommand(Optional.empty(), Optional.empty(), Optional.empty(), "");
67
+        command = AutoCommand.create(Optional.empty(), Optional.empty(), Optional.empty(), "");
68 68
         assertEquals(Optional.<String>empty(), command.getProfile());
69 69
     }
70 70
 
71 71
     @Test
72 72
     public void testGetProfile_NotEmpty() throws Exception {
73
-        command = new AutoCommand(Optional.empty(), Optional.empty(), Optional.of("profile"), "");
73
+        command = AutoCommand.create(Optional.empty(), Optional.empty(), Optional.of("profile"), "");
74 74
         assertEquals(Optional.of("profile"), command.getProfile());
75 75
     }
76 76
 
77 77
     @Test
78 78
     public void testGetResponse_Empty() throws Exception {
79
-        command = new AutoCommand(Optional.empty(), Optional.empty(), Optional.empty(), "");
79
+        command = AutoCommand.create(Optional.empty(), Optional.empty(), Optional.empty(), "");
80 80
         assertEquals("", command.getResponse());
81 81
     }
82 82
 
83 83
     @Test
84 84
     public void testGetResponse_NotEmpty() throws Exception {
85
-        command = new AutoCommand(Optional.empty(), Optional.empty(), Optional.empty(), "response");
85
+        command = AutoCommand.create(Optional.empty(), Optional.empty(), Optional.empty(), "response");
86 86
         assertEquals("response", command.getResponse());
87 87
     }
88 88
 
89 89
     @Test
90 90
     public void testGetType_Server() throws Exception {
91
-        command = new AutoCommand(Optional.of("server"), Optional.empty(), Optional.empty(),
91
+        command = AutoCommand.create(Optional.of("server"), Optional.empty(), Optional.empty(),
92 92
                 "response");
93 93
         assertEquals(AutoCommandType.SERVER, command.getType());
94 94
     }
95 95
 
96 96
     @Test
97 97
     public void testGetType_Network() throws Exception {
98
-        command = new AutoCommand(Optional.empty(), Optional.of("network"), Optional.empty(),
98
+        command = AutoCommand.create(Optional.empty(), Optional.of("network"), Optional.empty(),
99 99
                 "response");
100 100
         assertEquals(AutoCommandType.NETWORK, command.getType());
101 101
     }
102 102
 
103 103
     @Test
104 104
     public void testGetType_Global() throws Exception {
105
-        command = new AutoCommand(Optional.empty(), Optional.empty(), Optional.of("profile"),
105
+        command = AutoCommand.create(Optional.empty(), Optional.empty(), Optional.of("profile"),
106 106
                 "response");
107 107
         assertEquals(AutoCommandType.GLOBAL, command.getType());
108 108
     }
109 109
 
110 110
     @Test
111 111
     public void testGetType_Unknown() throws Exception {
112
-        command = new AutoCommand(Optional.of("server"), Optional.of("network"), Optional.empty(),
112
+        command = AutoCommand.create(Optional.of("server"), Optional.of("network"), Optional.empty(),
113 113
                 "response");
114 114
         assertEquals(AutoCommandType.UNKNOWN, command.getType());
115 115
     }

+ 5
- 5
test/com/dmdirc/commandparser/auto/YamlAutoCommandStoreTest.java Просмотреть файл

@@ -54,21 +54,21 @@ public class YamlAutoCommandStoreTest {
54 54
         fs = Jimfs.newFileSystem(Configuration.unix());
55 55
         Files.copy(getClass().getResource("readtest.yml").openStream(),
56 56
                 fs.getPath("readtest.yml"));
57
-        command = new AutoCommand(Optional.ofNullable("server"),
57
+        command = AutoCommand.create(Optional.ofNullable("server"),
58 58
                 Optional.ofNullable("network"),
59 59
                 Optional.ofNullable("profile"),
60 60
                 "command");
61
-        command1 = new AutoCommand(Optional.ofNullable("server1"),
61
+        command1 = AutoCommand.create(Optional.ofNullable("server1"),
62 62
                 Optional.ofNullable("network1"),
63 63
                 Optional.ofNullable("profile1"), "command1");
64
-        command2 = new AutoCommand(Optional.ofNullable("server2"),
64
+        command2 = AutoCommand.create(Optional.ofNullable("server2"),
65 65
                 Optional.ofNullable("network2"),
66 66
                 Optional.<String>empty(),
67 67
                 "command2");
68
-        command3 = new AutoCommand(Optional.<String>empty(),
68
+        command3 = AutoCommand.create(Optional.<String>empty(),
69 69
                 Optional.ofNullable("network3"), Optional.<String>empty(),
70 70
                 "command3");
71
-        command4 = new AutoCommand(Optional.<String>empty(),
71
+        command4 = AutoCommand.create(Optional.<String>empty(),
72 72
                 Optional.<String>empty(),
73 73
                 Optional.<String>empty(),
74 74
                 "command4");

+ 2
- 2
test/com/dmdirc/ui/core/autocommands/MutableAutoCommandTest.java Просмотреть файл

@@ -44,7 +44,7 @@ public class MutableAutoCommandTest {
44 44
 
45 45
     @Test
46 46
     public void testConstructor() {
47
-        final AutoCommand autoCommand = new AutoCommand(server, network, profile, response);
47
+        final AutoCommand autoCommand = AutoCommand.create(server, network, profile, response);
48 48
         command = new MutableAutoCommand(autoCommand);
49 49
         assertEquals(server, command.getServer());
50 50
         assertEquals(network, command.getNetwork());
@@ -122,7 +122,7 @@ public class MutableAutoCommandTest {
122 122
     public void testEqualsAutoCommand() {
123 123
         final MutableAutoCommand command1 = new MutableAutoCommand(server, network, profile,
124 124
                 response, type);
125
-        final AutoCommand command2 = new AutoCommand(server, network, profile, response);
125
+        final AutoCommand command2 = AutoCommand.create(server, network, profile, response);
126 126
         assertTrue(command1.equalsAutoCommand(command2));
127 127
     }
128 128
 

Загрузка…
Отмена
Сохранить