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) {
124 public Object[] getKeys(Object set) throws BindingException {
125 Set<Object> _set = (Set<Object>) set;
126 return _set.toArray(new Object[_set.size()]);
130 public void getKeys(Object set, Set<Object> keys) throws BindingException {
131 Set<Object> s = (Set<Object>) set;
136 public Object[] getValues(Object set) throws BindingException {
137 Set<Object> _set = (Set<Object>) set;
138 return new Object[_set.size()];
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);
146 return new HashSetBinding(keyBinding).count(src, from, fromInclusive, end, endInclusive);
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 {
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");
162 public void putAll(Object setTo, Set<?> from) {
163 Set<Object> _set = (Set<Object>) setTo;
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());
174 public Object remove(Object set, Object key) throws BindingException {
175 Set<Object> _set = (Set<Object>) set;
181 public int size(Object set) throws BindingException {
182 Set<Object> _set = (Set<Object>) set;
187 public boolean isInstance(Object obj) {
188 return obj instanceof Set;
192 public Object getCeilingKey(Object set, Object key) {
193 if (set instanceof TreeSet)
194 return new TreeSetBinding(keyBinding).getCeilingKey(set, key);
196 return new HashSetBinding(keyBinding).getCeilingKey(set, key);
200 public Object getFirstKey(Object set) {
201 if (set instanceof TreeSet)
202 return new TreeSetBinding(keyBinding).getFirstKey(set);
204 return new HashSetBinding(keyBinding).getFirstKey(set);
208 public Object getFloorKey(Object set, Object key) {
209 if (set instanceof TreeSet)
210 return new TreeSetBinding(keyBinding).getFloorKey(set, key);
212 return new HashSetBinding(keyBinding).getFloorKey(set, key);
216 public Object getHigherKey(Object set, Object key) {
217 if (set instanceof TreeSet)
218 return new TreeSetBinding(keyBinding).getHigherKey(set, key);
220 return new HashSetBinding(keyBinding).getHigherKey(set, key);
224 public Object getLastKey(Object set) {
225 if (set instanceof TreeSet)
226 return new TreeSetBinding(keyBinding).getLastKey(set);
228 return new HashSetBinding(keyBinding).getLastKey(set);
232 public Object getLowerKey(Object set, Object key) {
233 if (set instanceof TreeSet)
234 return new TreeSetBinding(keyBinding).getLowerKey(set, key);
236 return new HashSetBinding(keyBinding).getLowerKey(set, key);