]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/HashSetBinding.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / impl / HashSetBinding.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
4  *  All rights reserved. This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License v1.0
6  *  which accompanies this distribution, and is available at
7  *  http://www.eclipse.org/legal/epl-v10.html
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.binding.impl;
13
14 import java.util.Arrays;
15 import java.util.Comparator;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Map.Entry;
20 import java.util.Set;
21
22 import org.simantics.databoard.binding.ArrayBinding;
23 import org.simantics.databoard.binding.Binding;
24 import org.simantics.databoard.binding.MapBinding;
25 import org.simantics.databoard.binding.error.BindingException;
26 import org.simantics.databoard.binding.reflection.VoidBinding;
27 import org.simantics.databoard.type.MapType;
28 import org.simantics.databoard.type.RecordType;
29
30 /**
31  * Binds Databoard Map(T, {}) to java.util.Set and instantiates java.util.HashSet
32  *
33  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
34  */
35 @SuppressWarnings({"rawtypes", "unchecked"})
36 public class HashSetBinding extends MapBinding {
37         
38         public HashSetBinding(MapType mapType, Binding elementBinding) {
39                 super(mapType, elementBinding, VoidBinding.VOID_BINDING);
40         }
41         
42         public HashSetBinding(Binding elementBinding) {
43                 super(new MapType(elementBinding.type(), RecordType.VOID_TYPE), elementBinding, VoidBinding.VOID_BINDING);
44         }
45
46         @Override
47         public void clear(Object set) throws BindingException {
48                 Set _set = (Set) set;
49                 _set.clear();
50         }
51
52         @Override
53         public boolean containsKey(Object set, Object key) throws BindingException {
54 //              HashSet _set = (HashSet) set;
55 //              return _set.contains(key);
56                 Set s = ((Set)set);
57                 Binding kb = getKeyBinding();
58                 
59                 for (Object v : s)
60                 {
61                         if (kb.equals(v, key)) return true;
62                 }
63                 return false;           
64         }
65
66         @Override
67         public boolean containsValue(Object set, Object value)
68                         throws BindingException {
69                 return false;
70         }
71
72         @Override
73         public Object create() throws BindingException {
74                 return new HashSet<Object>();
75         }
76
77         public Object create(Set<?> initialSet) throws BindingException {
78                 return new HashSet<Object>(initialSet);
79         }
80
81         @Override
82         public Object create(Map initialMap) throws BindingException {
83                 return new HashSet<Object>(initialMap.keySet());
84         }
85
86         @Override
87         public Object create(Object[] keys, Object[] values)
88         throws BindingException {               
89                 HashSet<Object> result = new HashSet<Object>(keys.length);
90                 for (int i=0; i<keys.length; i++)
91                         result.add(keys[i]);
92                 return result;
93         }
94         
95         @Override
96         public Object create(List<Object> keys, List<Object> values) {
97                 HashSet<Object> result = new HashSet<Object>(keys.size());
98                 for (int i=0; i<keys.size(); i++)
99                         result.add(keys.get(i));
100                 return result;
101         }
102         
103         @Override
104         public Object get(Object set, Object key) throws BindingException {
105                 return null;
106         }
107
108         @Override
109         public void getAll(Object setFrom, Map to) throws BindingException {
110                 Map _to = (Map) to;
111                 HashSet<Object> _setFrom = (HashSet<Object>) setFrom;
112                 for (Object k : _setFrom)
113                         _to.put(k, null);
114         }
115
116         @Override
117         public void getAll(Object setFrom, Object[] keys, Object[] values)
118                         throws BindingException {
119                 Set<Object> _setFrom = (Set<Object>) setFrom;
120                 int i=0;
121                 for (Object k : _setFrom) {
122                         keys[i] = k;
123                         values[i] = null;
124                         i++;
125                 }
126         }
127
128         @Override
129         public Object[] getKeys(Object set) throws BindingException {
130                 Set<Object> _set = (Set<Object>) set;
131                 Object[] result = _set.toArray(new Object[_set.size()]);
132                 Arrays.sort(result, getKeyBinding());           
133                 return result;
134         }
135         
136         @Override
137         public void getKeys(Object set, Set keys) throws BindingException {
138                 Set<Object> s = (Set<Object>) set;
139                 keys.addAll(s);
140         }
141         
142         @Override
143         public int count(Object src, Object from, boolean fromInclusive,
144                         Object end, boolean endInclusive) throws BindingException {
145                 int result = 0;
146                 Set<Object> m = ((Set<Object>)src);
147                 for (Object k : m) {
148                         int fk = keyBinding.compare(from, k);
149                         int ek = keyBinding.compare(k, end);
150                         boolean fromMatches = fromInclusive ? fk<=0 : fk<0;
151                         boolean endMatches = endInclusive ? ek<=0 : ek <0;                      
152                         if ( fromMatches && endMatches ) result++;
153                 }               
154                 return result;
155         }
156
157         public int getEntries(Object src, Object from, boolean fromInclusive, Object end, boolean endInclusive, ArrayBinding dstKeyArrayBinding, Object dstKeyArray, ArrayBinding dstValueArrayBinding, Object dstValueArray, int limit) throws BindingException {
158                 return 0;
159         }
160
161         @Override
162         public Object[] getValues(Object set) throws BindingException {
163                 Set<Object> _set = (Set<Object>) set;
164                 return new Object[_set.size()];
165         }
166
167         @Override
168         public void put(Object set, Object key, Object value)
169                         throws BindingException {
170                 if (value!=null) throw new BindingException("Cannot put non-null to a Set");
171                 Set<Object> s = (Set<Object>) set;
172                 Binding kb = getKeyBinding();
173                 
174                 for (Object e : s)
175                 {
176                         if (kb.equals(e, key)) {
177                                 s.remove(e);
178                                 s.add(value);
179                                 return;
180                         }
181                 }               
182                 s.add(key);
183         }
184         
185         Object getComparableKey(Object set, Object key) {
186                 // if (keyIsComparable) return key;
187                 
188                 Set s = (Set) set;
189                 Binding kb = getKeyBinding();
190                 for (Object k : s)
191                 {
192                         if (kb.equals(k, key)) return k;
193                 }
194                 return key;
195         }
196
197         public void putAll(Object setTo, Set from) {
198                 Set<Object> _set = (Set<Object>) setTo;
199                 _set.addAll(from);
200         }
201         
202         @Override
203         public void putAll(Object setTo, Map from) throws BindingException {
204                 Set<Object> s = (Set<Object>) setTo;
205                 for (Entry<Object, Object> e : (Set<Entry<Object, Object>>) from.entrySet()) {
206                         Object k = getComparableKey(s, e.getKey());
207                         s.remove(k);                    
208                         s.add(e.getKey());
209                 }
210         }
211
212         @Override
213         public Object remove(Object set, Object key) throws BindingException {
214                 Set<Object> _set = (Set<Object>) set;
215                 _set.remove(key);
216                 return null;
217         }
218
219         @Override
220         public int size(Object set) throws BindingException {
221                 Set<Object> _set = (Set<Object>) set;
222                 return _set.size();
223         }
224
225         @Override
226         public boolean isInstance(Object obj) {
227                 return obj instanceof Set;
228         }
229
230         @Override
231         public Object getCeilingKey(Object set, Object key) {
232                 Set<Object> s = (Set<Object>) set;
233                 if (s.isEmpty()) return null;
234                 Comparator<Object> comparator = getKeyBinding();
235                 Object pivot = null;
236                 for (Object o : s) {
237                         // We are trying to find key > o > pivot
238                         int c2 = comparator.compare(key, o);
239                         if (c2>0) continue;
240                         if (pivot==null) {pivot = o; continue;}
241                         int c1 = comparator.compare(o, pivot);
242                         if (c1<0) pivot = o;
243                 }
244                 return pivot;
245         }
246
247         @Override
248         public Object getFirstKey(Object set) {
249                 Set<Object> s = (Set<Object>) set;
250                 if (s.isEmpty()) return null;
251                 Comparator<Object> c = getKeyBinding();
252                 Object result = null;
253                 for (Object o : s) {
254                         if (result==null) {
255                                 result = o;
256                                 continue;
257                         }
258                         if (c.compare(o, result)<0) result = o;
259                 }       
260                 
261                 return result;  
262         }
263
264         @Override
265         public Object getFloorKey(Object set, Object key) {
266                 Set<Object> s = (Set<Object>) set;
267                 if (s.isEmpty()) return null;   
268                 Comparator<Object> comparator = getKeyBinding();
269                 Object pivot = null;
270                 for (Object o : s) {
271                         // We are trying to find pivot <= o <= key
272                         int c2 = comparator.compare(o, key);
273                         if (c2==0) return o;
274                         if (c2>0) continue;
275                         if (pivot==null) {pivot = o; continue;}
276                         int c1 = comparator.compare(pivot, o);
277                         if (c1<0) pivot = o;
278                 }
279                 return pivot;
280         }
281
282         @Override
283         public Object getHigherKey(Object set, Object key) {
284                 Set<Object> s = (Set<Object>) set;
285                 if (s.isEmpty()) return null;
286                 Comparator<Object> comparator = getKeyBinding();
287                 Object pivot = null;
288                 for (Object o : s) {
289                         // We are trying to find key > o > pivot
290                         int c2 = comparator.compare(key, o);
291                         if (c2>=0) continue;
292                         if (pivot==null) {pivot = o; continue;}
293                         int c1 = comparator.compare(o, pivot);
294                         if (c1<0) pivot = o;
295                 }
296                 return pivot;
297         }
298
299         @Override
300         public Object getLastKey(Object set) {
301                 Set<Object> s = (Set<Object>) set;
302                 if (s.isEmpty()) return null;
303                 Comparator<Object> c = getKeyBinding();
304                 Object result = null;
305                 for (Object o : s) {
306                         if (result==null) {
307                                 result = o;
308                                 continue;
309                         }
310                         if (c.compare(o, result)>0) result = o;
311                 }                       
312                 return result;  
313         }
314
315         @Override
316         public Object getLowerKey(Object set, Object key) {
317                 Set<Object> s = (Set<Object>) set;
318                 if (s.isEmpty()) return null;
319                 Comparator<Object> comparator = getKeyBinding();
320                 Object pivot = null;
321                 for (Object o : s) {
322                         // We are trying to find pivot < o < key
323                         int c2 = comparator.compare(o, key);
324                         if (c2>=0) continue;
325                         if (pivot==null) {pivot = o; continue;}
326                         int c1 = comparator.compare(pivot, o);
327                         if (c1<0) pivot = o;
328                 }
329                 return pivot;
330         }
331         
332
333 }
334