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