59
|
1 package cl.maps.triple;
|
|
2
|
|
3
|
|
4 import java.util.ArrayList;
|
|
5 import java.util.Collection;
|
|
6 import java.util.HashMap;
|
|
7 import java.util.List;
|
|
8 import java.util.Map;
|
|
9 import java.util.Set;
|
|
10
|
|
11 public class TripleMap<V, A, B, C> implements ITripleMap<V, A, B, C>{
|
|
12
|
|
13 private Map<TripleKey<A, B, C>, V> map;
|
|
14 private Map<A, List<TripleKey<A, B, C>>> mapAKey;
|
|
15 private Map<B, List<TripleKey<A, B, C>>> mapBKey;
|
|
16 private Map<C, TripleKey<A, B, C>> mapOwnKey;
|
|
17
|
|
18 public TripleMap(){
|
|
19 this.map = new HashMap<TripleKey<A, B, C>, V>();
|
|
20 this.mapAKey = new HashMap<A, List<TripleKey<A, B, C>>>();
|
|
21 this.mapBKey = new HashMap<B, List<TripleKey<A, B, C>>>();
|
|
22 this.mapOwnKey = new HashMap<C, TripleKey<A, B, C>>();
|
|
23 }
|
|
24
|
|
25 public TripleMap(TripleMap<? extends V, A, B, C> m) {
|
|
26 this.map = new HashMap<TripleKey<A, B, C>, V>();
|
|
27 this.mapAKey = new HashMap<A, List<TripleKey<A, B, C>>>();
|
|
28 this.mapBKey = new HashMap<B, List<TripleKey<A, B, C>>>();
|
|
29 this.mapOwnKey = new HashMap<C, TripleKey<A, B, C>>();
|
|
30 this.putAllForCreate(m);
|
|
31 }
|
|
32
|
|
33 private void putAllForCreate(TripleMap<? extends V, A, B, C> m) {
|
|
34 for(Map.Entry<TripleKey<A, B, C>, ? extends V> e : m.entrySet()){
|
|
35 TripleKey<A, B, C> tKey = e.getKey();
|
|
36 this.map.put(tKey, e.getValue());
|
|
37 this.mapOwnKey.put(tKey.getOwnKey(), tKey);
|
|
38
|
|
39 if(!mapAKey.containsKey(tKey.getAKey())){
|
|
40 mapAKey.put(tKey.getAKey(), new ArrayList<TripleKey<A, B, C>>());
|
|
41 }
|
|
42 if(!mapAKey.get(tKey.getAKey()).contains(tKey)){
|
|
43 mapAKey.get(tKey.getAKey()).add(tKey);
|
|
44 }
|
|
45
|
|
46 if(!mapBKey.containsKey(tKey.getBKey())){
|
|
47 mapBKey.put(tKey.getBKey(), new ArrayList<TripleKey<A, B, C>>());
|
|
48 }
|
|
49 if(!mapBKey.get(tKey.getBKey()).contains(tKey)){
|
|
50 mapBKey.get(tKey.getBKey()).add(tKey);
|
|
51 }
|
|
52 }
|
|
53 }
|
|
54
|
|
55 public List<V> getValuesByAKey(A srcKey){
|
|
56 List<V> list = new ArrayList<V>();
|
|
57 if(mapAKey.containsKey(srcKey)){
|
|
58 for(TripleKey<A, B, C> tKey : mapAKey.get(srcKey)){
|
|
59 list.add(map.get(tKey));
|
|
60 }
|
|
61 }
|
|
62 return list;
|
|
63 }
|
|
64
|
|
65 public List<V> getValuesByBKey(B tarKey){
|
|
66 List<V> list = new ArrayList<V>();
|
|
67 if(mapBKey.containsKey(tarKey)){
|
|
68 for(TripleKey<A, B, C> tKey : mapBKey.get(tarKey)){
|
|
69 list.add(map.get(tKey));
|
|
70 }
|
|
71 }
|
|
72 return list;
|
|
73 }
|
|
74
|
|
75 public V getValuesByOwnKey(C ownKey){
|
|
76 TripleKey<A, B, C> tKey = mapOwnKey.get(ownKey);
|
|
77 if(tKey != null){
|
|
78 return this.map.get(tKey);
|
|
79 }
|
|
80 return null;
|
|
81 }
|
|
82
|
|
83 public Set<TripleKey<A, B, C>> keySet(){
|
|
84 return this.map.keySet();
|
|
85 }
|
|
86
|
|
87 public Set<Map.Entry<TripleKey<A, B, C>, V>> entrySet() {
|
|
88 return this.map.entrySet();
|
|
89 }
|
|
90
|
|
91 @Override
|
|
92 public int size() {
|
|
93 return this.map.size();
|
|
94 }
|
|
95
|
|
96 @Override
|
|
97 public boolean isEmpty() {
|
|
98 return this.map.isEmpty();
|
|
99 }
|
|
100
|
|
101 @Override
|
|
102 public boolean containsKey(TripleKey<A, B, C> key) {
|
|
103 return this.map.containsKey(key);
|
|
104 }
|
|
105
|
|
106 @Override
|
|
107 public boolean containsValue(Object value) {
|
|
108 return this.map.containsValue(value);
|
|
109 }
|
|
110
|
|
111 @Override
|
|
112 public V get(TripleKey<A, B, C> key) {
|
|
113 return map.get(key);
|
|
114 }
|
|
115
|
|
116 @Override
|
|
117 public V put(TripleKey<A, B, C> tKey, V value) {
|
|
118 if(!mapAKey.containsKey(tKey.getAKey())){
|
|
119 mapAKey.put(tKey.getAKey(), new ArrayList<TripleKey<A, B, C>>());
|
|
120 }
|
|
121 if(!mapAKey.get(tKey.getAKey()).contains(tKey)){
|
|
122 mapAKey.get(tKey.getAKey()).add(tKey);
|
|
123 }
|
|
124
|
|
125 if(!mapBKey.containsKey(tKey.getBKey())){
|
|
126 mapBKey.put(tKey.getBKey(), new ArrayList<TripleKey<A, B, C>>());
|
|
127 }
|
|
128 if(!mapBKey.get(tKey.getBKey()).contains(tKey)){
|
|
129 mapBKey.get(tKey.getBKey()).add(tKey);
|
|
130 }
|
|
131
|
|
132 this.mapOwnKey.put(tKey.getOwnKey(), tKey);
|
|
133 return this.map.put(tKey, value);
|
|
134 }
|
|
135
|
|
136 @Override
|
|
137 public V remove(TripleKey<A, B, C> key) {
|
|
138 if(mapAKey.containsKey(key.getAKey())){
|
|
139 mapAKey.get(key.getAKey()).remove(key);
|
|
140 }
|
|
141 if(mapBKey.containsKey(key.getBKey())){
|
|
142 mapBKey.get(key.getBKey()).remove(key);
|
|
143 }
|
|
144 this.mapOwnKey.remove(key.getOwnKey());
|
|
145
|
62
|
146 /*
|
59
|
147 int hashCodeInput = key.hashCode();
|
|
148 System.out.println(hashCodeInput);
|
|
149 if(map.size() == 1){
|
|
150 Object uu = new ArrayList<TripleKey<A, B, C>>(map.keySet()).get(0);
|
|
151 int hashCode2 = uu.hashCode();
|
|
152 System.out.println(hashCode2);
|
|
153 }
|
62
|
154 */
|
59
|
155
|
|
156 return this.map.remove(key);
|
|
157 }
|
|
158
|
|
159 @Override
|
|
160 public void clear() {
|
|
161 this.map.clear();
|
|
162 this.mapAKey.clear();
|
|
163 this.mapBKey.clear();
|
|
164 this.mapOwnKey.clear();
|
|
165 }
|
|
166
|
|
167 @Override
|
|
168 public Collection<V> values() {
|
|
169 return this.map.values();
|
|
170 }
|
|
171 }
|