Browse Source

Remove DoubleMap.

pull/44/head
Greg Holmes 9 years ago
parent
commit
32cb1129d9

+ 0
- 156
src/com/dmdirc/util/collections/DoubleMap.java View File

@@ -1,156 +0,0 @@
1
-/*
2
- * Copyright (c) 2006-2015 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
-package com.dmdirc.util.collections;
23
-
24
-import java.util.AbstractMap.SimpleEntry;
25
-import java.util.ArrayList;
26
-import java.util.Collection;
27
-import java.util.HashSet;
28
-import java.util.List;
29
-import java.util.Map;
30
-import java.util.Set;
31
-import java.util.stream.Collectors;
32
-
33
-import javax.annotation.Nonnull;
34
-
35
-/**
36
- * An object that maps keys to values, and values back to keys. Currently
37
- * does no checking for duplicates. Does not allow null values.
38
- * <p>Note that this implementation is NOT thread safe.
39
- *
40
- * @param <A> The first type of data to be mapped
41
- * @param <B> The second type of data to be mapped
42
- */
43
-public class DoubleMap<A,B> implements Map<A, B> {
44
-
45
-    /** The keys in this map. */
46
-    private final List<A> keys = new ArrayList<>();
47
-    /** The values in this map. */
48
-    private final List<B> values = new ArrayList<>();
49
-
50
-    /**
51
-     * Retrieves the value associated with the specified key.
52
-     *
53
-     * @param key The key to search for
54
-     * @return The value of the specified key
55
-     */
56
-    public B getValue(final A key) {
57
-        if (keys.indexOf(key) == -1) {
58
-            return null;
59
-        }
60
-        return values.get(keys.indexOf(key));
61
-    }
62
-
63
-    /**
64
-     * Retrieves the key associated with the specified value.
65
-     *
66
-     * @param value The value to search for
67
-     * @return The key of the specified value
68
-     */
69
-    public A getKey(final B value) {
70
-        if (values.indexOf(value) == -1) {
71
-            return null;
72
-        }
73
-        return keys.get(values.indexOf(value));
74
-    }
75
-
76
-    @Override
77
-    public B put(final A key, final B value) {
78
-        if (key == null || value == null) {
79
-            throw new NullPointerException();
80
-        }
81
-
82
-        keys.add(key);
83
-        values.add(value);
84
-
85
-        return value;
86
-    }
87
-
88
-    @Nonnull
89
-    @Override
90
-    public Set<A> keySet() {
91
-        return new HashSet<>(keys);
92
-    }
93
-
94
-    @Override
95
-    public int size() {
96
-        return keys.size();
97
-    }
98
-
99
-    @Override
100
-    public boolean isEmpty() {
101
-        return keys.isEmpty();
102
-    }
103
-
104
-    @Override
105
-    public boolean containsKey(final Object key) {
106
-        return keys.contains(key);
107
-    }
108
-
109
-    @Override
110
-    public boolean containsValue(final Object value) {
111
-        return values.contains(value);
112
-    }
113
-
114
-    @Override
115
-    @SuppressWarnings("unchecked")
116
-    public B get(final Object key) {
117
-        return getValue((A) key);
118
-    }
119
-
120
-    @Override
121
-    public B remove(final Object key) {
122
-        if (keys.indexOf(key) == -1) {
123
-            return null;
124
-        }
125
-        final B value = values.remove(keys.indexOf(key));
126
-        keys.remove(keys.indexOf(key));
127
-        return value;
128
-    }
129
-
130
-    @Override
131
-    public void putAll(@Nonnull final Map<? extends A, ? extends B> m) {
132
-        for (Entry<? extends A, ? extends B> entry : m.entrySet()) {
133
-            put(entry.getKey(), entry.getValue());
134
-        }
135
-    }
136
-
137
-    @Override
138
-    public void clear() {
139
-        keys.clear();
140
-        values.clear();
141
-    }
142
-
143
-    @Nonnull
144
-    @Override
145
-    public Collection<B> values() {
146
-        return new ArrayList<>(values);
147
-    }
148
-
149
-    @Nonnull
150
-    @Override
151
-    public Set<Entry<A, B>> entrySet() {
152
-        return keys.stream()
153
-                .map(key -> new SimpleEntry<>(key, getValue(key)))
154
-                .collect(Collectors.toSet());
155
-    }
156
-}

+ 0
- 67
test/com/dmdirc/util/collections/DoubleMapTest.java View File

@@ -1,67 +0,0 @@
1
-/*
2
- * Copyright (c) 2006-2015 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.util.collections;
24
-
25
-import java.util.Map;
26
-
27
-import org.junit.Test;
28
-import static org.junit.Assert.*;
29
-
30
-public class DoubleMapTest {
31
-
32
-    @Test
33
-    public void testPut() {
34
-        final Map<String, String> dm = new DoubleMap<>();
35
-        dm.put("a", "b");
36
-        
37
-        assertEquals(1, dm.keySet().size());
38
-        assertEquals(1, dm.values().size());
39
-        assertTrue(dm.keySet().contains("a"));
40
-        assertTrue(dm.values().contains("b"));
41
-    }
42
-    
43
-    @Test(expected=NullPointerException.class)
44
-    public void testPutNull1() {
45
-        final Map<String, String> dm = new DoubleMap<>();
46
-        dm.put(null, "b");
47
-    }
48
-    
49
-    @Test(expected=NullPointerException.class)
50
-    public void testPutNull2() {
51
-        final Map<String, String> dm = new DoubleMap<>();
52
-        dm.put("a", null);
53
-    }    
54
-    
55
-    @Test
56
-    public void testGet() {
57
-        final DoubleMap<String, String> dm = new DoubleMap<>();
58
-        dm.put("a", "b");
59
-        dm.put("b", "c");
60
-        dm.put("c", "a");
61
-        
62
-        assertEquals("b", dm.getValue("a"));
63
-        assertEquals("b", dm.getKey("c"));
64
-        assertEquals("c", dm.getKey("a"));
65
-    }    
66
-
67
-}

Loading…
Cancel
Save