comparison src/main/java/de/mpiwg/indexmeta/utils/QuadMap.java @ 0:dfce13a5f5f9

nit project!
author Jorge Urzua <jurzua@mpiwg-berlin.mpg.de>
date Thu, 11 Apr 2013 15:25:26 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:dfce13a5f5f9
1 package de.mpiwg.indexmeta.utils;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Set;
9
10 public class QuadMap<V> implements IQuadMap<V>{
11
12 private Map<QuadKey, V> map;
13 private Map<Object, List<QuadKey>> mapFirstKey;
14 private Map<Object, List<QuadKey>> mapSecondKey;
15 private Map<Object, List<QuadKey>> mapThirdKey;
16 private Map<Object, QuadKey> mapOwnKey;
17
18 public QuadMap(){
19 this.map = new HashMap<QuadKey, V>();
20 this.mapFirstKey = new HashMap<Object, List<QuadKey>>();
21 this.mapSecondKey = new HashMap<Object, List<QuadKey>>();
22 this.mapThirdKey = new HashMap<Object, List<QuadKey>>();
23 this.mapOwnKey = new HashMap<Object, QuadKey>();
24 }
25
26 public QuadMap(QuadMap<? extends V> m) {
27 this.map = new HashMap<QuadKey, V>();
28 this.mapFirstKey = new HashMap<Object, List<QuadKey>>();
29 this.mapSecondKey = new HashMap<Object, List<QuadKey>>();
30 this.mapThirdKey = new HashMap<Object, List<QuadKey>>();
31 this.mapOwnKey = new HashMap<Object, QuadKey>();
32 this.putAllForCreate(m);
33 }
34
35 private void putAllForCreate(QuadMap<? extends V> m) {
36 for(Map.Entry<QuadKey, ? extends V> e : m.entrySet()){
37 QuadKey tKey = e.getKey();
38 this.map.put(tKey, e.getValue());
39 this.mapOwnKey.put(tKey.getOwnKey(), tKey);
40
41 if(!mapFirstKey.containsKey(tKey.getFirstKey())){
42 mapFirstKey.put(tKey.getFirstKey(), new ArrayList<QuadKey>());
43 }
44 if(!mapFirstKey.get(tKey.getFirstKey()).contains(tKey)){
45 mapFirstKey.get(tKey.getFirstKey()).add(tKey);
46 }
47
48 if(!mapSecondKey.containsKey(tKey.getSecondKey())){
49 mapSecondKey.put(tKey.getSecondKey(), new ArrayList<QuadKey>());
50 }
51 if(!mapSecondKey.get(tKey.getSecondKey()).contains(tKey)){
52 mapSecondKey.get(tKey.getSecondKey()).add(tKey);
53 }
54
55 if(!mapThirdKey.containsKey(tKey.getThirdKey())){
56 mapThirdKey.put(tKey.getThirdKey(), new ArrayList<QuadKey>());
57 }
58 if(!mapThirdKey.get(tKey.getThirdKey()).contains(tKey)){
59 mapThirdKey.get(tKey.getThirdKey()).add(tKey);
60 }
61 }
62 }
63
64 public List<V> getValuesByFirstKey(Object srcKey){
65 List<V> list = new ArrayList<V>();
66 if(mapFirstKey.containsKey(srcKey)){
67 for(QuadKey tKey : mapFirstKey.get(srcKey)){
68 list.add(map.get(tKey));
69 }
70 }
71 return list;
72 }
73
74 public List<V> getValuesBySecondKey(Object tarKey){
75 List<V> list = new ArrayList<V>();
76 if(mapSecondKey.containsKey(tarKey)){
77 for(QuadKey tKey : mapSecondKey.get(tarKey)){
78 list.add(map.get(tKey));
79 }
80 }
81 return list;
82 }
83
84 public List<V> getValuesByThirdKey(Object thirdKey){
85 List<V> list = new ArrayList<V>();
86 if(mapThirdKey.containsKey(thirdKey)){
87 for(QuadKey tKey : mapThirdKey.get(thirdKey)){
88 list.add(map.get(tKey));
89 }
90 }
91 return list;
92 }
93
94 public V getValuesByOwnKey(Object ownKey){
95 QuadKey tKey = mapOwnKey.get(ownKey);
96 if(tKey != null){
97 return this.map.get(tKey);
98 }
99 return null;
100 }
101
102 public Set<QuadKey> keySet(){
103 return this.map.keySet();
104 }
105
106 public Set<Map.Entry<QuadKey, V>> entrySet() {
107 return this.map.entrySet();
108 }
109
110 @Override
111 public int size() {
112 return this.map.size();
113 }
114
115 @Override
116 public boolean isEmpty() {
117 return this.map.isEmpty();
118 }
119
120 @Override
121 public boolean containsKey(QuadKey key) {
122 return this.map.containsKey(key);
123 }
124
125 @Override
126 public boolean containsValue(Object value) {
127 return this.map.containsValue(value);
128 }
129
130 @Override
131 public V get(QuadKey key) {
132 return map.get(key);
133 }
134
135 @Override
136 public V put(QuadKey tKey, V value) {
137
138 if(!mapFirstKey.containsKey(tKey.getFirstKey())){
139 mapFirstKey.put(tKey.getFirstKey(), new ArrayList<QuadKey>());
140 }
141 if(!mapFirstKey.get(tKey.getFirstKey()).contains(tKey)){
142 mapFirstKey.get(tKey.getFirstKey()).add(tKey);
143 }
144
145 if(!mapSecondKey.containsKey(tKey.getSecondKey())){
146 mapSecondKey.put(tKey.getSecondKey(), new ArrayList<QuadKey>());
147 }
148 if(!mapSecondKey.get(tKey.getSecondKey()).contains(tKey)){
149 mapSecondKey.get(tKey.getSecondKey()).add(tKey);
150 }
151
152 if(!mapThirdKey.containsKey(tKey.getThirdKey())){
153 mapThirdKey.put(tKey.getThirdKey(), new ArrayList<QuadKey>());
154 }
155 if(!mapThirdKey.get(tKey.getThirdKey()).contains(tKey)){
156 mapThirdKey.get(tKey.getThirdKey()).add(tKey);
157 }
158
159 this.mapOwnKey.put(tKey.getOwnKey(), tKey);
160 return this.map.put(tKey, value);
161 }
162
163 @Override
164 public V remove(QuadKey key) {
165 if(mapFirstKey.containsKey(key.getFirstKey())){
166 mapFirstKey.get(key.getFirstKey()).remove(key);
167 }
168 if(mapSecondKey.containsKey(key.getSecondKey())){
169 mapSecondKey.get(key.getSecondKey()).remove(key);
170 }
171 if(mapThirdKey.containsKey(key.getThirdKey())){
172 mapThirdKey.get(key.getThirdKey()).remove(key);
173 }
174 this.mapOwnKey.remove(key.getOwnKey());
175 return this.map.remove(key);
176 }
177
178 @Override
179 public void clear() {
180 this.map.clear();
181 this.mapFirstKey.clear();
182 this.mapSecondKey.clear();
183 this.mapThirdKey.clear();
184 this.mapOwnKey.clear();
185 }
186
187 @Override
188 public Collection<V> values() {
189 return this.map.values();
190 }
191 }