]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/DefaultSetBinding.java
Fixed all line endings of the repository
[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                 }               
120         }
121
122         @Override
123         public Object[] getKeys(Object set) throws BindingException {
124                 Set<Object> _set = (Set<Object>) set;
125                 return _set.toArray(new Object[_set.size()]);
126         }
127
128         @Override
129         public void getKeys(Object set, Set<Object> keys) throws BindingException {
130                 Set<Object> s = (Set<Object>) set;
131                 keys.addAll(s);
132         }
133         
134         @Override
135         public Object[] getValues(Object set) throws BindingException {
136                 Set<Object> _set = (Set<Object>) set;
137                 return new Object[_set.size()];
138         }
139         
140         @Override
141         public int count(Object src, Object from, boolean fromInclusive, Object end, boolean endInclusive) throws BindingException {
142             if (src instanceof TreeSet)
143             return new TreeSetBinding(keyBinding).count(src, from, fromInclusive, end, endInclusive);
144             else
145                 return new HashSetBinding(keyBinding).count(src, from, fromInclusive, end, endInclusive);
146         }
147         
148         @Override
149         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 {
150                 return 0;
151         }
152
153         @Override
154         public void put(Object set, Object key, Object value)
155                         throws BindingException {
156                 Set<Object> _set = (Set<Object>) set;
157                 if (value!=null) throw new BindingException("Cannot put non-null to a Set");
158                 _set.add(key);
159         }
160
161         public void putAll(Object setTo, Set<?> from) {
162                 Set<Object> _set = (Set<Object>) setTo;
163                 _set.addAll(from);
164         }
165         
166         @Override
167         public <K,V> void putAll(Object setTo, Map<K,V> from) throws BindingException {
168                 Set<Object> _set = (Set<Object>) setTo;
169                 _set.addAll(from.keySet());
170         }
171
172         @Override
173         public Object remove(Object set, Object key) throws BindingException {
174                 Set<Object> _set = (Set<Object>) set;
175                 _set.remove(key);
176                 return null;
177         }
178
179         @Override
180         public int size(Object set) throws BindingException {
181                 Set<Object> _set = (Set<Object>) set;
182                 return _set.size();
183         }
184
185         @Override
186         public boolean isInstance(Object obj) {
187                 return obj instanceof Set;
188         }
189
190         @Override
191         public Object getCeilingKey(Object set, Object key) {
192         if (set instanceof TreeSet)
193             return new TreeSetBinding(keyBinding).getCeilingKey(set, key);
194         else
195             return new HashSetBinding(keyBinding).getCeilingKey(set, key);
196         }
197
198         @Override
199         public Object getFirstKey(Object set) {
200         if (set instanceof TreeSet)
201             return new TreeSetBinding(keyBinding).getFirstKey(set);
202         else
203             return new HashSetBinding(keyBinding).getFirstKey(set);
204         }
205
206         @Override
207         public Object getFloorKey(Object set, Object key) {
208         if (set instanceof TreeSet)
209             return new TreeSetBinding(keyBinding).getFloorKey(set, key);
210         else
211             return new HashSetBinding(keyBinding).getFloorKey(set, key);
212         }
213
214         @Override
215         public Object getHigherKey(Object set, Object key) {
216         if (set instanceof TreeSet)
217             return new TreeSetBinding(keyBinding).getHigherKey(set, key);
218         else
219             return new HashSetBinding(keyBinding).getHigherKey(set, key);
220         }
221
222         @Override
223         public Object getLastKey(Object set) {
224         if (set instanceof TreeSet)
225             return new TreeSetBinding(keyBinding).getLastKey(set);
226         else
227             return new HashSetBinding(keyBinding).getLastKey(set);
228         }
229
230         @Override
231         public Object getLowerKey(Object set, Object key) {
232         if (set instanceof TreeSet)
233             return new TreeSetBinding(keyBinding).getLowerKey(set, key);
234         else
235             return new HashSetBinding(keyBinding).getLowerKey(set, key);
236         }
237
238 }
239