]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/HashSetBinding.java
Merge commit 'bd5bc6e45f700e755b61bd112631796631330ecb'
[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({"rawtypes", "unchecked"})\r
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                 }               
125         }
126
127         @Override
128         public Object[] getKeys(Object set) throws BindingException {
129                 Set<Object> _set = (Set<Object>) set;
130                 Object[] result = _set.toArray(new Object[_set.size()]);
131                 Arrays.sort(result, getKeyBinding());           
132                 return result;
133         }
134         
135         @Override
136         public void getKeys(Object set, Set keys) throws BindingException {
137                 Set<Object> s = (Set<Object>) set;
138                 keys.addAll(s);
139         }
140         \r
141         @Override\r
142         public int count(Object src, Object from, boolean fromInclusive,\r
143                         Object end, boolean endInclusive) throws BindingException {\r
144                 int result = 0;\r
145                 Set<Object> m = ((Set<Object>)src);\r
146                 for (Object k : m) {\r
147                         int fk = keyBinding.compare(from, k);\r
148                         int ek = keyBinding.compare(k, end);\r
149                         boolean fromMatches = fromInclusive ? fk<=0 : fk<0;\r
150                         boolean endMatches = endInclusive ? ek<=0 : ek <0;                      \r
151                         if ( fromMatches && endMatches ) result++;\r
152                 }               \r
153                 return result;\r
154         }\r
155 \r
156         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
157                 return 0;\r
158         }\r
159
160         @Override
161         public Object[] getValues(Object set) throws BindingException {
162                 Set<Object> _set = (Set<Object>) set;
163                 return new Object[_set.size()];
164         }
165
166         @Override
167         public void put(Object set, Object key, Object value)
168                         throws BindingException {
169                 if (value!=null) throw new BindingException("Cannot put non-null to a Set");
170                 Set<Object> s = (Set<Object>) set;
171                 Binding kb = getKeyBinding();
172                 
173                 for (Object e : s)
174                 {
175                         if (kb.equals(e, key)) {
176                                 s.remove(e);
177                                 s.add(value);
178                                 return;
179                         }
180                 }               
181                 s.add(key);
182         }
183         
184         Object getComparableKey(Object set, Object key) {
185                 // if (keyIsComparable) return key;
186                 
187                 Set s = (Set) set;
188                 Binding kb = getKeyBinding();
189                 for (Object k : s)
190                 {
191                         if (kb.equals(k, key)) return k;
192                 }
193                 return key;
194         }
195
196         public void putAll(Object setTo, Set from) {
197                 Set<Object> _set = (Set<Object>) setTo;
198                 _set.addAll(from);
199         }
200         
201         @Override
202         public void putAll(Object setTo, Map from) throws BindingException {
203                 Set<Object> s = (Set<Object>) setTo;
204                 for (Entry<Object, Object> e : (Set<Entry<Object, Object>>) from.entrySet()) {
205                         Object k = getComparableKey(s, e.getKey());
206                         s.remove(k);                    
207                         s.add(e.getKey());
208                 }
209         }
210
211         @Override
212         public Object remove(Object set, Object key) throws BindingException {
213                 Set<Object> _set = (Set<Object>) set;
214                 _set.remove(key);
215                 return null;
216         }
217
218         @Override
219         public int size(Object set) throws BindingException {
220                 Set<Object> _set = (Set<Object>) set;
221                 return _set.size();
222         }
223
224         @Override
225         public boolean isInstance(Object obj) {
226                 return obj instanceof Set;
227         }
228
229         @Override
230         public Object getCeilingKey(Object set, Object key) {
231                 Set<Object> s = (Set<Object>) set;
232                 if (s.isEmpty()) return null;
233                 Comparator<Object> comparator = getKeyBinding();
234                 Object pivot = null;
235                 for (Object o : s) {
236                         // We are trying to find key > o > pivot
237                         int c2 = comparator.compare(key, o);
238                         if (c2>0) continue;
239                         if (pivot==null) {pivot = o; continue;}
240                         int c1 = comparator.compare(o, pivot);
241                         if (c1<0) pivot = o;
242                 }
243                 return pivot;
244         }
245
246         @Override
247         public Object getFirstKey(Object set) {
248                 Set<Object> s = (Set<Object>) set;
249                 if (s.isEmpty()) return null;
250                 Comparator<Object> c = getKeyBinding();
251                 Object result = null;
252                 for (Object o : s) {
253                         if (result==null) {
254                                 result = o;
255                                 continue;
256                         }
257                         if (c.compare(o, result)<0) result = o;
258                 }       
259                 
260                 return result;  
261         }
262
263         @Override
264         public Object getFloorKey(Object set, Object key) {
265                 Set<Object> s = (Set<Object>) set;
266                 if (s.isEmpty()) return null;   
267                 Comparator<Object> comparator = getKeyBinding();
268                 Object pivot = null;
269                 for (Object o : s) {
270                         // We are trying to find pivot <= o <= key
271                         int c2 = comparator.compare(o, key);
272                         if (c2==0) return o;
273                         if (c2>0) continue;
274                         if (pivot==null) {pivot = o; continue;}
275                         int c1 = comparator.compare(pivot, o);
276                         if (c1<0) pivot = o;
277                 }
278                 return pivot;
279         }
280
281         @Override
282         public Object getHigherKey(Object set, Object key) {
283                 Set<Object> s = (Set<Object>) set;
284                 if (s.isEmpty()) return null;
285                 Comparator<Object> comparator = getKeyBinding();
286                 Object pivot = null;
287                 for (Object o : s) {
288                         // We are trying to find key > o > pivot
289                         int c2 = comparator.compare(key, o);
290                         if (c2>=0) continue;
291                         if (pivot==null) {pivot = o; continue;}
292                         int c1 = comparator.compare(o, pivot);
293                         if (c1<0) pivot = o;
294                 }
295                 return pivot;
296         }
297
298         @Override
299         public Object getLastKey(Object set) {
300                 Set<Object> s = (Set<Object>) set;
301                 if (s.isEmpty()) return null;
302                 Comparator<Object> c = getKeyBinding();
303                 Object result = null;
304                 for (Object o : s) {
305                         if (result==null) {
306                                 result = o;
307                                 continue;
308                         }
309                         if (c.compare(o, result)>0) result = o;
310                 }                       
311                 return result;  
312         }
313
314         @Override
315         public Object getLowerKey(Object set, Object key) {
316                 Set<Object> s = (Set<Object>) set;
317                 if (s.isEmpty()) return null;
318                 Comparator<Object> comparator = getKeyBinding();
319                 Object pivot = null;
320                 for (Object o : s) {
321                         // We are trying to find pivot < o < key
322                         int c2 = comparator.compare(o, key);
323                         if (c2>=0) continue;
324                         if (pivot==null) {pivot = o; continue;}
325                         int c1 = comparator.compare(pivot, o);
326                         if (c1<0) pivot = o;
327                 }
328                 return pivot;
329         }
330         
331
332 }
333