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