2
|
1 package cl.maps.duplex;
|
|
2
|
|
3 import java.util.ArrayList;
|
|
4 import java.util.Collection;
|
|
5 import java.util.HashMap;
|
|
6 import java.util.LinkedList;
|
|
7 import java.util.List;
|
|
8 import java.util.Map;
|
|
9 import java.util.Set;
|
|
10
|
|
11 public class DuplexMap<V, A, B> implements IDuplexMap<V, A, B>{
|
|
12
|
|
13 private Map<DuplexKey<A, B>, V> map;
|
|
14 private Map<A, List<DuplexKey<A, B>>> mapAKey;
|
|
15 private Map<B, DuplexKey<A, B>> mapOwnKey;
|
|
16
|
|
17 public DuplexMap(){
|
|
18 this.map = new HashMap<DuplexKey<A, B>, V>();
|
|
19 this.mapAKey = new HashMap<A, List<DuplexKey<A, B>>>();
|
|
20 this.mapOwnKey = new HashMap<B, DuplexKey<A, B>>();
|
|
21 }
|
|
22
|
|
23 public DuplexMap(DuplexMap<? extends V, A, B> m) {
|
|
24 this.map = new HashMap<DuplexKey<A, B>, V>();
|
|
25 this.mapAKey = new HashMap<A, List<DuplexKey<A, B>>>();
|
|
26 this.mapOwnKey = new HashMap<B, DuplexKey<A, B>>();
|
|
27 this.putAllForCreate(m);
|
|
28 }
|
|
29
|
|
30 private void putAllForCreate(DuplexMap<? extends V, A, B> m) {
|
|
31 for(Map.Entry<DuplexKey<A, B>, ? extends V> e : m.entrySet()){
|
|
32 DuplexKey<A, B> tKey = e.getKey();
|
|
33 this.map.put(tKey, e.getValue());
|
|
34 this.mapOwnKey.put(tKey.getOwnKey(), tKey);
|
|
35
|
|
36 if(!mapAKey.containsKey(tKey.getAKey())){
|
|
37 mapAKey.put(tKey.getAKey(), new LinkedList<DuplexKey<A, B>>());
|
|
38 }
|
|
39 if(!mapAKey.get(tKey.getAKey()).contains(tKey)){
|
|
40 mapAKey.get(tKey.getAKey()).add(tKey);
|
|
41 }
|
|
42 }
|
|
43 }
|
|
44
|
|
45 public List<V>getValuesByAKey(A srcKey){
|
|
46 List<V> list = new ArrayList<V>();
|
|
47 if(mapAKey.containsKey(srcKey)){
|
|
48 for(DuplexKey<A, B> tKey : mapAKey.get(srcKey)){
|
|
49 list.add(map.get(tKey));
|
|
50 }
|
|
51 }
|
|
52 return list;
|
|
53 }
|
|
54
|
|
55 public V getValuesByOwnKey(B ownKey){
|
|
56 DuplexKey<A, B> tKey = mapOwnKey.get(ownKey);
|
|
57 if(tKey != null){
|
|
58 return this.map.get(tKey);
|
|
59 }
|
|
60 return null;
|
|
61 }
|
|
62
|
|
63 public Set<DuplexKey<A, B>> keySet(){
|
|
64 return this.map.keySet();
|
|
65 }
|
|
66
|
|
67 public Set<Map.Entry<DuplexKey<A, B>, V>> entrySet() {
|
|
68 return this.map.entrySet();
|
|
69 }
|
|
70
|
|
71 @Override
|
|
72 public int size() {
|
|
73 return this.map.size();
|
|
74 }
|
|
75
|
|
76 @Override
|
|
77 public boolean isEmpty() {
|
|
78 return this.map.isEmpty();
|
|
79 }
|
|
80
|
|
81 @Override
|
|
82 public boolean containsKey(DuplexKey<A, B> key) {
|
|
83 return this.map.containsKey(key);
|
|
84 }
|
|
85
|
|
86 @Override
|
|
87 public boolean containsValue(Object value) {
|
|
88 return this.map.containsValue(value);
|
|
89 }
|
|
90
|
|
91 @Override
|
|
92 public V get(DuplexKey<A, B> key) {
|
|
93 return map.get(key);
|
|
94 }
|
|
95
|
|
96 @Override
|
|
97 public V put(DuplexKey<A, B> tKey, V value) {
|
|
98 if(!mapAKey.containsKey(tKey.getAKey())){
|
|
99 List<DuplexKey<A, B>> list = new ArrayList<DuplexKey<A, B>>();
|
|
100 mapAKey.put(tKey.getAKey(), list);
|
|
101 }
|
|
102 if(!mapAKey.get(tKey.getAKey()).contains(tKey)){
|
|
103 mapAKey.get(tKey.getAKey()).add(tKey);
|
|
104 }
|
|
105
|
|
106 this.mapOwnKey.put(tKey.getOwnKey(), tKey);
|
|
107 return this.map.put(tKey, value);
|
|
108 }
|
|
109
|
|
110 @Override
|
|
111 public V remove(DuplexKey<A, B> key) {
|
|
112 if(mapAKey.containsKey(key.getAKey())){
|
|
113 mapAKey.get(key.getAKey()).remove(key);
|
|
114 }
|
|
115 this.mapOwnKey.remove(key.getOwnKey());
|
|
116 return this.map.remove(key);
|
|
117 }
|
|
118
|
|
119 @Override
|
|
120 public void clear() {
|
|
121 this.map.clear();
|
|
122 this.mapAKey.clear();
|
|
123 this.mapOwnKey.clear();
|
|
124 }
|
|
125
|
|
126 @Override
|
|
127 public Collection<V> values() {
|
|
128 return this.map.values();
|
|
129 }
|
|
130
|
|
131
|
|
132 }
|