1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.binding.impl;
14 import java.util.List;
17 import java.util.TreeSet;
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;
28 * Binds java.util.Set to Map(T, {})
30 * This binding accepts all java.util.Set instances, but instantiates
31 * java.util.TreeSet objects.
33 * @author Reino Ruusu <reino.ruusu@vtt.fi>
35 @SuppressWarnings("unchecked")
36 public class DefaultSetBinding extends MapBinding {
38 public DefaultSetBinding(MapType mapType, Binding elementBinding) {
39 super(mapType, elementBinding, VoidBinding.VOID_BINDING);
42 public DefaultSetBinding(Binding elementBinding) {
43 super(new MapType(elementBinding.type(), RecordType.VOID_TYPE), elementBinding, VoidBinding.VOID_BINDING);
47 public void clear(Object set) throws BindingException {
48 Set<Object> _set = (Set<Object>) set;
53 public boolean containsKey(Object set, Object key) throws BindingException {
54 Set<Object> _set = (Set<Object>) set;
55 return _set.contains(key);
59 public boolean containsValue(Object set, Object value)
60 throws BindingException {
65 public Object create() throws BindingException {
66 return new TreeSet<Object>( keyBinding );
69 public Object create(Set<?> initialSet) throws BindingException {
74 public Object create(Map<?, ?> initialMap) throws BindingException {
75 Set<Object> result = new TreeSet<Object>( keyBinding );
76 result.addAll(initialMap.keySet());
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] );
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));
99 public Object get(Object set, Object key) throws BindingException {
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;
112 public void getAll(Object setFrom, Object[] keys, Object[] values)
113 throws BindingException {
114 Set<Object> _setFrom = (Set<Object>) setFrom;
116 for (Object k : _setFrom) {
123 public Object[] getKeys(Object set) throws BindingException {
124 Set<Object> _set = (Set<Object>) set;
125 return _set.toArray(new Object[_set.size()]);
129 public void getKeys(Object set, Set<Object> keys) throws BindingException {
130 Set<Object> s = (Set<Object>) set;
135 public Object[] getValues(Object set) throws BindingException {
136 Set<Object> _set = (Set<Object>) set;
137 return new Object[_set.size()];
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);
145 return new HashSetBinding(keyBinding).count(src, from, fromInclusive, end, endInclusive);
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 {
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");
161 public void putAll(Object setTo, Set<?> from) {
162 Set<Object> _set = (Set<Object>) setTo;
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());
173 public Object remove(Object set, Object key) throws BindingException {
174 Set<Object> _set = (Set<Object>) set;
180 public int size(Object set) throws BindingException {
181 Set<Object> _set = (Set<Object>) set;
186 public boolean isInstance(Object obj) {
187 return obj instanceof Set;
191 public Object getCeilingKey(Object set, Object key) {
192 if (set instanceof TreeSet)
193 return new TreeSetBinding(keyBinding).getCeilingKey(set, key);
195 return new HashSetBinding(keyBinding).getCeilingKey(set, key);
199 public Object getFirstKey(Object set) {
200 if (set instanceof TreeSet)
201 return new TreeSetBinding(keyBinding).getFirstKey(set);
203 return new HashSetBinding(keyBinding).getFirstKey(set);
207 public Object getFloorKey(Object set, Object key) {
208 if (set instanceof TreeSet)
209 return new TreeSetBinding(keyBinding).getFloorKey(set, key);
211 return new HashSetBinding(keyBinding).getFloorKey(set, key);
215 public Object getHigherKey(Object set, Object key) {
216 if (set instanceof TreeSet)
217 return new TreeSetBinding(keyBinding).getHigherKey(set, key);
219 return new HashSetBinding(keyBinding).getHigherKey(set, key);
223 public Object getLastKey(Object set) {
224 if (set instanceof TreeSet)
225 return new TreeSetBinding(keyBinding).getLastKey(set);
227 return new HashSetBinding(keyBinding).getLastKey(set);
231 public Object getLowerKey(Object set, Object key) {
232 if (set instanceof TreeSet)
233 return new TreeSetBinding(keyBinding).getLowerKey(set, key);
235 return new HashSetBinding(keyBinding).getLowerKey(set, key);