Skip to content

Package: BidirectionalMap

BidirectionalMap

nameinstructionbranchcomplexitylinemethod
BidirectionalMap()
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getByValue(Object)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getValue(Object)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
keys()
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
put(Object, Object)
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
removeByKey(Object)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
removeByValue(Object)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
values()
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2013 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Edgar - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.common.spi;
15:
16: import java.util.LinkedHashMap;
17: import java.util.LinkedHashSet;
18: import java.util.Map;
19: import java.util.Set;
20:
21: /**
22: * Convenience class for mapping keys to values and vice versa.
23: * Both, keys and values, have to be unique.
24: *
25: * @author emuller
26: *
27: * @param <K>
28: * the type of the key
29: * @param <V>
30: * the type of the value
31: * @since 1.5
32: */
33: public class BidirectionalMap<K, V> {
34:
35:         private final Map<K, V> keyToValues;
36:         private final Map<V, K> valuesToKeys;
37:
38:         /**
39:          * Default constructor.
40:          */
41:         public BidirectionalMap() {
42:                 keyToValues = new LinkedHashMap<K, V>();
43:                 valuesToKeys = new LinkedHashMap<V, K>();
44:         }
45:
46:         /**
47:          * Associates the specified value with the specified key in this map
48:          * and vice versa.
49:          *
50:          * @param key
51:          * the key
52:          * @param value
53:          * the value
54:          */
55:         public void put(K key, V value) {
56:                 keyToValues.put(key, value);
57:                 valuesToKeys.put(value, key);
58:         }
59:
60:         /**
61:          * Removes the bidirectional mapping for the given key.
62:          *
63:          * @param key
64:          * the key to be removed
65:          * @return the value that was associated with the key
66:          */
67:         public synchronized V removeByKey(K key) {
68:                 final V v = keyToValues.get(key);
69:                 keyToValues.remove(key);
70:                 valuesToKeys.remove(v);
71:                 return v;
72:         }
73:
74:         /**
75:          * Removes the bidirectional mapping for the given value.
76:          *
77:          * @param value
78:          * the value to be removed
79:          * @return the key that was associated with the value
80:          */
81:         public synchronized K removeByValue(V value) {
82:                 final K k = valuesToKeys.get(value);
83:                 valuesToKeys.remove(value);
84:                 keyToValues.remove(k);
85:                 return k;
86:         }
87:
88:         /**
89:          * Returns the value belonging to the given key.
90:          *
91:          * @param key
92:          * the key whose value should be looked up
93:          * @return the value belonging to the given key
94:          */
95:         public V getValue(K key) {
96:                 return keyToValues.get(key);
97:         }
98:
99:         /**
100:          * Returns the key belonging to the given value.
101:          *
102:          * @param value
103:          * the key whose value should be looked up
104:          * @return the key belonging to the given value
105:          */
106:         public K getByValue(V value) {
107:                 return valuesToKeys.get(value);
108:         }
109:
110:         /**
111:          * Returns all keys.
112:          *
113:          * @return a set of all keys
114:          */
115:         public Set<K> keys() {
116:                 final LinkedHashSet<K> linkedHashSet = new LinkedHashSet<K>();
117:                 linkedHashSet.addAll(keyToValues.keySet());
118:                 return linkedHashSet;
119:         }
120:
121:         /**
122:          * Returns all values.
123:          *
124:          * @return a set of all values
125:          */
126:         public Set<V> values() {
127:                 final LinkedHashSet<V> linkedHashSet = new LinkedHashSet<V>();
128:                 linkedHashSet.addAll(valuesToKeys.keySet());
129:                 return linkedHashSet;
130:         }
131:
132: }