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.Arrays;
15 import java.util.Comparator;
16 import java.util.HashSet;
17 import java.util.List;
19 import java.util.Map.Entry;
22 import org.simantics.databoard.binding.ArrayBinding;
23 import org.simantics.databoard.binding.Binding;
24 import org.simantics.databoard.binding.MapBinding;
25 import org.simantics.databoard.binding.error.BindingException;
26 import org.simantics.databoard.binding.reflection.VoidBinding;
27 import org.simantics.databoard.type.MapType;
28 import org.simantics.databoard.type.RecordType;
31 * Binds Databoard Map(T, {}) to java.util.Set and instantiates java.util.HashSet
33 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
35 @SuppressWarnings({"rawtypes", "unchecked"})
36 public class HashSetBinding extends MapBinding {
38 public HashSetBinding(MapType mapType, Binding elementBinding) {
39 super(mapType, elementBinding, VoidBinding.VOID_BINDING);
42 public HashSetBinding(Binding elementBinding) {
43 super(new MapType(elementBinding.type(), RecordType.VOID_TYPE), elementBinding, VoidBinding.VOID_BINDING);
47 public void clear(Object set) throws BindingException {
53 public boolean containsKey(Object set, Object key) throws BindingException {
54 // HashSet _set = (HashSet) set;
55 // return _set.contains(key);
57 Binding kb = getKeyBinding();
61 if (kb.equals(v, key)) return true;
67 public boolean containsValue(Object set, Object value)
68 throws BindingException {
73 public Object create() throws BindingException {
74 return new HashSet<Object>();
77 public Object create(Set<?> initialSet) throws BindingException {
78 return new HashSet<Object>(initialSet);
82 public Object create(Map initialMap) throws BindingException {
83 return new HashSet<Object>(initialMap.keySet());
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++)
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));
104 public Object get(Object set, Object key) throws BindingException {
109 public void getAll(Object setFrom, Map to) throws BindingException {
111 HashSet<Object> _setFrom = (HashSet<Object>) setFrom;
112 for (Object k : _setFrom)
117 public void getAll(Object setFrom, Object[] keys, Object[] values)
118 throws BindingException {
119 Set<Object> _setFrom = (Set<Object>) setFrom;
121 for (Object k : _setFrom) {
129 public Object[] getKeys(Object set) throws BindingException {
130 Set<Object> _set = (Set<Object>) set;
131 Object[] result = _set.toArray(new Object[_set.size()]);
132 Arrays.sort(result, getKeyBinding());
137 public void getKeys(Object set, Set keys) throws BindingException {
138 Set<Object> s = (Set<Object>) set;
143 public int count(Object src, Object from, boolean fromInclusive,
144 Object end, boolean endInclusive) throws BindingException {
146 Set<Object> m = ((Set<Object>)src);
148 int fk = keyBinding.compare(from, k);
149 int ek = keyBinding.compare(k, end);
150 boolean fromMatches = fromInclusive ? fk<=0 : fk<0;
151 boolean endMatches = endInclusive ? ek<=0 : ek <0;
152 if ( fromMatches && endMatches ) result++;
157 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 {
162 public Object[] getValues(Object set) throws BindingException {
163 Set<Object> _set = (Set<Object>) set;
164 return new Object[_set.size()];
168 public void put(Object set, Object key, Object value)
169 throws BindingException {
170 if (value!=null) throw new BindingException("Cannot put non-null to a Set");
171 Set<Object> s = (Set<Object>) set;
172 Binding kb = getKeyBinding();
176 if (kb.equals(e, key)) {
185 Object getComparableKey(Object set, Object key) {
186 // if (keyIsComparable) return key;
189 Binding kb = getKeyBinding();
192 if (kb.equals(k, key)) return k;
197 public void putAll(Object setTo, Set from) {
198 Set<Object> _set = (Set<Object>) setTo;
203 public void putAll(Object setTo, Map from) throws BindingException {
204 Set<Object> s = (Set<Object>) setTo;
205 for (Entry<Object, Object> e : (Set<Entry<Object, Object>>) from.entrySet()) {
206 Object k = getComparableKey(s, e.getKey());
213 public Object remove(Object set, Object key) throws BindingException {
214 Set<Object> _set = (Set<Object>) set;
220 public int size(Object set) throws BindingException {
221 Set<Object> _set = (Set<Object>) set;
226 public boolean isInstance(Object obj) {
227 return obj instanceof Set;
231 public Object getCeilingKey(Object set, Object key) {
232 Set<Object> s = (Set<Object>) set;
233 if (s.isEmpty()) return null;
234 Comparator<Object> comparator = getKeyBinding();
237 // We are trying to find key > o > pivot
238 int c2 = comparator.compare(key, o);
240 if (pivot==null) {pivot = o; continue;}
241 int c1 = comparator.compare(o, pivot);
248 public Object getFirstKey(Object set) {
249 Set<Object> s = (Set<Object>) set;
250 if (s.isEmpty()) return null;
251 Comparator<Object> c = getKeyBinding();
252 Object result = null;
258 if (c.compare(o, result)<0) result = o;
265 public Object getFloorKey(Object set, Object key) {
266 Set<Object> s = (Set<Object>) set;
267 if (s.isEmpty()) return null;
268 Comparator<Object> comparator = getKeyBinding();
271 // We are trying to find pivot <= o <= key
272 int c2 = comparator.compare(o, key);
275 if (pivot==null) {pivot = o; continue;}
276 int c1 = comparator.compare(pivot, o);
283 public Object getHigherKey(Object set, Object key) {
284 Set<Object> s = (Set<Object>) set;
285 if (s.isEmpty()) return null;
286 Comparator<Object> comparator = getKeyBinding();
289 // We are trying to find key > o > pivot
290 int c2 = comparator.compare(key, o);
292 if (pivot==null) {pivot = o; continue;}
293 int c1 = comparator.compare(o, pivot);
300 public Object getLastKey(Object set) {
301 Set<Object> s = (Set<Object>) set;
302 if (s.isEmpty()) return null;
303 Comparator<Object> c = getKeyBinding();
304 Object result = null;
310 if (c.compare(o, result)>0) result = o;
316 public Object getLowerKey(Object set, Object key) {
317 Set<Object> s = (Set<Object>) set;
318 if (s.isEmpty()) return null;
319 Comparator<Object> comparator = getKeyBinding();
322 // We are trying to find pivot < o < key
323 int c2 = comparator.compare(o, key);
325 if (pivot==null) {pivot = o; continue;}
326 int c1 = comparator.compare(pivot, o);