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) {
128 public Object[] getKeys(Object set) throws BindingException {
129 Set<Object> _set = (Set<Object>) set;
130 Object[] result = _set.toArray(new Object[_set.size()]);
131 Arrays.sort(result, getKeyBinding());
136 public void getKeys(Object set, Set keys) throws BindingException {
137 Set<Object> s = (Set<Object>) set;
142 public int count(Object src, Object from, boolean fromInclusive,
143 Object end, boolean endInclusive) throws BindingException {
145 Set<Object> m = ((Set<Object>)src);
147 int fk = keyBinding.compare(from, k);
148 int ek = keyBinding.compare(k, end);
149 boolean fromMatches = fromInclusive ? fk<=0 : fk<0;
150 boolean endMatches = endInclusive ? ek<=0 : ek <0;
151 if ( fromMatches && endMatches ) result++;
156 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 {
161 public Object[] getValues(Object set) throws BindingException {
162 Set<Object> _set = (Set<Object>) set;
163 return new Object[_set.size()];
167 public void put(Object set, Object key, Object value)
168 throws BindingException {
169 if (value!=null) throw new BindingException("Cannot put non-null to a Set");
170 Set<Object> s = (Set<Object>) set;
171 Binding kb = getKeyBinding();
175 if (kb.equals(e, key)) {
184 Object getComparableKey(Object set, Object key) {
185 // if (keyIsComparable) return key;
188 Binding kb = getKeyBinding();
191 if (kb.equals(k, key)) return k;
196 public void putAll(Object setTo, Set from) {
197 Set<Object> _set = (Set<Object>) setTo;
202 public void putAll(Object setTo, Map from) throws BindingException {
203 Set<Object> s = (Set<Object>) setTo;
204 for (Entry<Object, Object> e : (Set<Entry<Object, Object>>) from.entrySet()) {
205 Object k = getComparableKey(s, e.getKey());
212 public Object remove(Object set, Object key) throws BindingException {
213 Set<Object> _set = (Set<Object>) set;
219 public int size(Object set) throws BindingException {
220 Set<Object> _set = (Set<Object>) set;
225 public boolean isInstance(Object obj) {
226 return obj instanceof Set;
230 public Object getCeilingKey(Object set, Object key) {
231 Set<Object> s = (Set<Object>) set;
232 if (s.isEmpty()) return null;
233 Comparator<Object> comparator = getKeyBinding();
236 // We are trying to find key > o > pivot
237 int c2 = comparator.compare(key, o);
239 if (pivot==null) {pivot = o; continue;}
240 int c1 = comparator.compare(o, pivot);
247 public Object getFirstKey(Object set) {
248 Set<Object> s = (Set<Object>) set;
249 if (s.isEmpty()) return null;
250 Comparator<Object> c = getKeyBinding();
251 Object result = null;
257 if (c.compare(o, result)<0) result = o;
264 public Object getFloorKey(Object set, Object key) {
265 Set<Object> s = (Set<Object>) set;
266 if (s.isEmpty()) return null;
267 Comparator<Object> comparator = getKeyBinding();
270 // We are trying to find pivot <= o <= key
271 int c2 = comparator.compare(o, key);
274 if (pivot==null) {pivot = o; continue;}
275 int c1 = comparator.compare(pivot, o);
282 public Object getHigherKey(Object set, Object key) {
283 Set<Object> s = (Set<Object>) set;
284 if (s.isEmpty()) return null;
285 Comparator<Object> comparator = getKeyBinding();
288 // We are trying to find key > o > pivot
289 int c2 = comparator.compare(key, o);
291 if (pivot==null) {pivot = o; continue;}
292 int c1 = comparator.compare(o, pivot);
299 public Object getLastKey(Object set) {
300 Set<Object> s = (Set<Object>) set;
301 if (s.isEmpty()) return null;
302 Comparator<Object> c = getKeyBinding();
303 Object result = null;
309 if (c.compare(o, result)>0) result = o;
315 public Object getLowerKey(Object set, Object key) {
316 Set<Object> s = (Set<Object>) set;
317 if (s.isEmpty()) return null;
318 Comparator<Object> comparator = getKeyBinding();
321 // We are trying to find pivot < o < key
322 int c2 = comparator.compare(o, key);
324 if (pivot==null) {pivot = o; continue;}
325 int c1 = comparator.compare(pivot, o);