Browse Source

Add a manager for aliases.

Change-Id: Icdf50211d4b42e4014678f76a69a4c1ad089784a
Reviewed-on: http://gerrit.dmdirc.com/3517
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
pull/1/head
Chris Smith 10 years ago
parent
commit
cf8b7365d1

+ 125
- 0
src/com/dmdirc/commandparser/aliases/AliasManager.java View File

@@ -0,0 +1,125 @@
1
+/*
2
+ * Copyright (c) 2006-2014 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.commandparser.aliases;
24
+
25
+import com.dmdirc.interfaces.CommandController;
26
+
27
+import com.google.common.base.Optional;
28
+import com.google.common.collect.Sets;
29
+
30
+import java.util.Collections;
31
+import java.util.Map;
32
+import java.util.Set;
33
+import java.util.concurrent.ConcurrentSkipListMap;
34
+
35
+import javax.inject.Inject;
36
+import javax.inject.Singleton;
37
+
38
+/**
39
+ * Maintains a list of known aliases, and registers and unregisters them with the command system.
40
+ */
41
+@Singleton
42
+public class AliasManager {
43
+
44
+    /** The controller to register aliases with. */
45
+    private final CommandController commandController;
46
+    /** Map of known alias names to their corresponding aliases. */
47
+    private final Map<String, Alias> aliases = new ConcurrentSkipListMap<>();
48
+
49
+    @Inject
50
+    public AliasManager(final CommandController commandController) {
51
+        this.commandController = commandController;
52
+    }
53
+
54
+    /**
55
+     * Adds a new alias and registers it with the command system.
56
+     * <p>
57
+     * If an existing alias with the same name already exists, it is removed.
58
+     *
59
+     * @param alias The alias to be registered
60
+     */
61
+    public void addAlias(final Alias alias) {
62
+        if (aliases.containsKey(alias.getName())) {
63
+            removeAlias(alias);
64
+        }
65
+
66
+        aliases.put(alias.getName(), alias);
67
+        commandController.registerCommand(new AliasCommandHandler(commandController, alias), alias);
68
+    }
69
+
70
+    /**
71
+     * Removes any existing alias with the same name, and unregisters it with the command system.
72
+     * <p>
73
+     * If the alias was not previously added, no action occurs.
74
+     *
75
+     * @param alias The alias to be removed
76
+     */
77
+    public void removeAlias(final Alias alias) {
78
+        if (aliases.containsKey(alias.getName())) {
79
+            commandController.unregisterCommand(aliases.remove(alias.getName()));
80
+        }
81
+    }
82
+
83
+    /**
84
+     * Removes an existing alias, and unregisters it with the command system.
85
+     * <p>
86
+     * If the alias was not previously added, no action occurs.
87
+     *
88
+     * @param name The name of the alias to be removed
89
+     */
90
+    public void removeAlias(final String name) {
91
+        if (aliases.containsKey(name)) {
92
+            removeAlias(aliases.get(name));
93
+        }
94
+    }
95
+
96
+    /**
97
+     * Retrieves the set of all known alias names.
98
+     *
99
+     * @return Set of all known names.
100
+     */
101
+    public Set<String> getAliasNames() {
102
+        return Collections.unmodifiableSet(aliases.keySet());
103
+    }
104
+
105
+    /**
106
+     * Retrieves the set of all known aliases.
107
+     *
108
+     * @return Set of all known aliases.
109
+     */
110
+    public Set<Alias> getAliases() {
111
+        return Collections.unmodifiableSet(Sets.newHashSet(aliases.values()));
112
+    }
113
+
114
+    /**
115
+     * Retrieves the alias with the given name.
116
+     *
117
+     * @param name The name of the alias to receive.
118
+     *
119
+     * @return The alias, if found.
120
+     */
121
+    public Optional<Alias> getAlias(final String name) {
122
+        return Optional.fromNullable(aliases.get(name));
123
+    }
124
+
125
+}

+ 136
- 0
test/com/dmdirc/commandparser/aliases/AliasManagerTest.java View File

@@ -0,0 +1,136 @@
1
+/*
2
+ * Copyright (c) 2006-2014 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.commandparser.aliases;
24
+
25
+import com.dmdirc.commandparser.CommandType;
26
+import com.dmdirc.commandparser.commands.Command;
27
+import com.dmdirc.interfaces.CommandController;
28
+
29
+import java.util.Set;
30
+
31
+import org.junit.Before;
32
+import org.junit.Test;
33
+import org.junit.runner.RunWith;
34
+import org.mockito.Mock;
35
+import org.mockito.runners.MockitoJUnitRunner;
36
+
37
+import static org.junit.Assert.assertEquals;
38
+import static org.junit.Assert.assertTrue;
39
+import static org.mockito.Matchers.any;
40
+import static org.mockito.Matchers.eq;
41
+import static org.mockito.Mockito.verify;
42
+
43
+@RunWith(MockitoJUnitRunner.class)
44
+public class AliasManagerTest {
45
+
46
+    @Mock private CommandController controller;
47
+    private AliasManager manager;
48
+    private Alias alias1;
49
+    private Alias alias2;
50
+
51
+    @Before
52
+    public void setUp() {
53
+        manager = new AliasManager(controller);
54
+        alias1 = new Alias(CommandType.TYPE_CHAT, "alias", 1, "exit");
55
+        alias2 = new Alias(CommandType.TYPE_CHAT, "alias", 1, "exit");
56
+    }
57
+
58
+    @Test
59
+    public void testAddedAliasReturnedByGetters() {
60
+        manager.addAlias(alias1);
61
+
62
+        final Set<String> names = manager.getAliasNames();
63
+        assertEquals(1, names.size());
64
+        assertTrue(names.contains(alias1.getName()));
65
+
66
+        final Set<Alias> aliases = manager.getAliases();
67
+        assertEquals(1, aliases.size());
68
+        assertTrue(aliases.contains(alias1));
69
+    }
70
+
71
+    @Test
72
+    public void testAddedAliasRegisteredWithController() {
73
+        manager.addAlias(alias1);
74
+
75
+        verify(controller).registerCommand(any(Command.class), eq(alias1));
76
+    }
77
+
78
+    @Test
79
+    public void testAddingAliasWithSameNameReplacesExisting() {
80
+        manager.addAlias(alias1);
81
+        manager.addAlias(alias2);
82
+
83
+        final Set<String> names = manager.getAliasNames();
84
+        assertEquals(1, names.size());
85
+        assertTrue(names.contains(alias1.getName()));
86
+
87
+        final Set<Alias> aliases = manager.getAliases();
88
+        assertEquals(1, aliases.size());
89
+        assertTrue(aliases.contains(alias2));
90
+    }
91
+
92
+    @Test
93
+    public void testRemovedAliasNotReturnedByGetters() {
94
+        manager.addAlias(alias1);
95
+        manager.removeAlias(alias1);
96
+
97
+        final Set<String> names = manager.getAliasNames();
98
+        assertEquals(0, names.size());
99
+
100
+        final Set<Alias> aliases = manager.getAliases();
101
+        assertEquals(0, aliases.size());
102
+    }
103
+
104
+    @Test
105
+    public void testRemovedAliasByNameNotReturnedByGetters() {
106
+        manager.addAlias(alias1);
107
+        manager.removeAlias("alias");
108
+
109
+        final Set<String> names = manager.getAliasNames();
110
+        assertEquals(0, names.size());
111
+
112
+        final Set<Alias> aliases = manager.getAliases();
113
+        assertEquals(0, aliases.size());
114
+    }
115
+
116
+    @Test
117
+    public void testRemovedAliasByCopyNotReturnedByGetters() {
118
+        manager.addAlias(alias1);
119
+        manager.removeAlias(alias2);
120
+
121
+        final Set<String> names = manager.getAliasNames();
122
+        assertEquals(0, names.size());
123
+
124
+        final Set<Alias> aliases = manager.getAliases();
125
+        assertEquals(0, aliases.size());
126
+    }
127
+
128
+    @Test
129
+    public void testRemovedAliasUnregisteredWithController() {
130
+        manager.addAlias(alias1);
131
+        manager.removeAlias(alias2);
132
+
133
+        verify(controller).unregisterCommand(alias1);
134
+    }
135
+
136
+}

Loading…
Cancel
Save