]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/DefaultSetBinding.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / impl / DefaultSetBinding.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.List;
15 import java.util.Map;
16 import java.util.Set;
17 import java.util.TreeSet;
18
19 import org.simantics.databoard.binding.ArrayBinding;
20 import org.simantics.databoard.binding.Binding;
21 import org.simantics.databoard.binding.MapBinding;
22 import org.simantics.databoard.binding.error.BindingException;
23 import org.simantics.databoard.binding.reflection.VoidBinding;
24 import org.simantics.databoard.type.MapType;
25 import org.simantics.databoard.type.RecordType;
26
27 /**
28  * Binds java.util.Set to Map(T, {})
29  * 
30  * This binding accepts all java.util.Set instances, but instantiates
31  * java.util.TreeSet objects.
32  *
33  * @author Reino Ruusu <reino.ruusu@vtt.fi>
34  */
35 @SuppressWarnings("unchecked")
36 public class DefaultSetBinding extends MapBinding {
37         
38         public DefaultSetBinding(MapType mapType, Binding elementBinding) {
39                 super(mapType, elementBinding, VoidBinding.VOID_BINDING);
40         }
41         
42         public DefaultSetBinding(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<Object> _set = (Set<Object>) set;
49                 _set.clear();
50         }
51
52         @Override
53         public boolean containsKey(Object set, Object key) throws BindingException {
54                 Set<Object> _set = (Set<Object>) set;
55                 return _set.contains(key);
56         }
57
58         @Override
59         public boolean containsValue(Object set, Object value)
60                         throws BindingException {
61                 return false;
62         }
63
64     @Override
65     public Object create() throws BindingException {
66         return new TreeSet<Object>( keyBinding );
67     }
68
69         public Object create(Set<?> initialSet) throws BindingException {
70             return initialSet;
71         }
72
73         @Override
74         public Object create(Map<?, ?> initialMap) throws BindingException {
75                 Set<Object> result = new TreeSet<Object>( keyBinding );
76                 result.addAll(initialMap.keySet());
77                 return result;
78         }
79
80         @Override
81         public Object create(Object[] keys, Object[] values)
82         throws BindingException {               
83                 Set<Object> result = new TreeSet<Object>( keyBinding );
84                 for (int i=0; i<keys.length; i++) {
85                         result.add( keys[i] );
86                 }
87                 return result;
88         }
89         
90         @Override
91         public Object create(List<Object> keys, List<Object> values) {
92                 Set<Object> result = new TreeSet<Object>( keyBinding );
93                 for (int i=0; i<keys.size(); i++)
94                         result.add(keys.get(i));
95                 return result;
96         }       
97
98         @Override
99         public Object get(Object set, Object key) throws BindingException {
100                 return null;
101         }
102         
103     @Override
104     public <K, V> void getAll(Object setFrom, Map<K, V> to) throws BindingException {
105                 Map<K, V> _to = (Map<K, V>) to;
106                 Set<K> _setFrom = (Set<K>) setFrom;
107                 for (K k : _setFrom)
108                         _to.put(k, null);
109         }
110
111         @Override
112         public void getAll(Object setFrom, Object[] keys, Object[] values)
113                         throws BindingException {
114                 Set<Object> _setFrom = (Set<Object>) setFrom;
115                 int i=0;
116                 for (Object k : _setFrom) {
117                         keys[i] = k;
118                         values[i] = null;
119                         i++;
120                 }
121         }
122
123         @Override
124         public Object[] getKeys(Object set) throws BindingException {
125                 Set<Object> _set = (Set<Object>) set;
126                 return _set.toArray(new Object[_set.size()]);
127         }
128
129         @Override
130         public void getKeys(Object set, Set<Object> keys) throws BindingException {
131                 Set<Object> s = (Set<Object>) set;
132                 keys.addAll(s);
133         }
134         
135         @Override
136         public Object[] getValues(Object set) throws BindingException {
137                 Set<Object> _set = (Set<Object>) set;
138                 return new Object[_set.size()];
139         }
140         
141         @Override
142         public int count(Object src, Object from, boolean fromInclusive, Object end, boolean endInclusive) throws BindingException {
143             if (src instanceof TreeSet)
144             return new TreeSetBinding(keyBinding).count(src, from, fromInclusive, end, endInclusive);
145             else
146                 return new HashSetBinding(keyBinding).count(src, from, fromInclusive, end, endInclusive);
147         }
148         
149         @Override
150         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 {
151                 return 0;
152         }
153
154         @Override
155         public void put(Object set, Object key, Object value)
156                         throws BindingException {
157                 Set<Object> _set = (Set<Object>) set;
158                 if (value!=null) throw new BindingException("Cannot put non-null to a Set");
159                 _set.add(key);
160         }
161
162         public void putAll(Object setTo, Set<?> from) {
163                 Set<Object> _set = (Set<Object>) setTo;
164                 _set.addAll(from);
165         }
166         
167         @Override
168         public <K,V> void putAll(Object setTo, Map<K,V> from) throws BindingException {
169                 Set<Object> _set = (Set<Object>) setTo;
170                 _set.addAll(from.keySet());
171         }
172
173         @Override
174         public Object remove(Object set, Object key) throws BindingException {
175                 Set<Object> _set = (Set<Object>) set;
176                 _set.remove(key);
177                 return null;
178         }
179
180         @Override
181         public int size(Object set) throws BindingException {
182                 Set<Object> _set = (Set<Object>) set;
183                 return _set.size();
184         }
185
186         @Override
187         public boolean isInstance(Object obj) {
188                 return obj instanceof Set;
189         }
190
191         @Override
192         public Object getCeilingKey(Object set, Object key) {
193         if (set instanceof TreeSet)
194             return new TreeSetBinding(keyBinding).getCeilingKey(set, key);
195         else
196             return new HashSetBinding(keyBinding).getCeilingKey(set, key);
197         }
198
199         @Override
200         public Object getFirstKey(Object set) {
201         if (set instanceof TreeSet)
202             return new TreeSetBinding(keyBinding).getFirstKey(set);
203         else
204             return new HashSetBinding(keyBinding).getFirstKey(set);
205         }
206
207         @Override
208         public Object getFloorKey(Object set, Object key) {
209         if (set instanceof TreeSet)
210             return new TreeSetBinding(keyBinding).getFloorKey(set, key);
211         else
212             return new HashSetBinding(keyBinding).getFloorKey(set, key);
213         }
214
215         @Override
216         public Object getHigherKey(Object set, Object key) {
217         if (set instanceof TreeSet)
218             return new TreeSetBinding(keyBinding).getHigherKey(set, key);
219         else
220             return new HashSetBinding(keyBinding).getHigherKey(set, key);
221         }
222
223         @Override
224         public Object getLastKey(Object set) {
225         if (set instanceof TreeSet)
226             return new TreeSetBinding(keyBinding).getLastKey(set);
227         else
228             return new HashSetBinding(keyBinding).getLastKey(set);
229         }
230
231         @Override
232         public Object getLowerKey(Object set, Object key) {
233         if (set instanceof TreeSet)
234             return new TreeSetBinding(keyBinding).getLowerKey(set, key);
235         else
236             return new HashSetBinding(keyBinding).getLowerKey(set, key);
237         }
238
239 }
240