1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.databoard.binding.impl;
14 import java.util.Arrays;
\r
15 import java.util.Comparator;
\r
16 import java.util.HashSet;
\r
17 import java.util.List;
\r
18 import java.util.Map;
\r
19 import java.util.Map.Entry;
\r
20 import java.util.Set;
\r
22 import org.simantics.databoard.binding.ArrayBinding;
\r
23 import org.simantics.databoard.binding.Binding;
\r
24 import org.simantics.databoard.binding.MapBinding;
\r
25 import org.simantics.databoard.binding.error.BindingException;
\r
26 import org.simantics.databoard.binding.reflection.VoidBinding;
\r
27 import org.simantics.databoard.type.MapType;
\r
28 import org.simantics.databoard.type.RecordType;
\r
31 * Binds Databoard Map(T, {}) to java.util.Set and instantiates java.util.HashSet
33 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
35 @SuppressWarnings("all")
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);
46 @SuppressWarnings("unchecked")
48 public void clear(Object set) throws BindingException {
54 public boolean containsKey(Object set, Object key) throws BindingException {
55 // HashSet _set = (HashSet) set;
56 // return _set.contains(key);
58 Binding kb = getKeyBinding();
62 if (kb.equals(v, key)) return true;
68 public boolean containsValue(Object set, Object value)
69 throws BindingException {
74 public Object create() throws BindingException {
75 return new HashSet<Object>();
78 public Object create(Set<?> initialSet) throws BindingException {
79 return new HashSet<Object>(initialSet);
83 public Object create(Map initialMap) throws BindingException {
84 return new HashSet<Object>(initialMap.keySet());
88 public Object create(Object[] keys, Object[] values)
89 throws BindingException {
90 HashSet<Object> result = new HashSet<Object>(keys.length);
91 for (int i=0; i<keys.length; i++)
97 public Object create(List<Object> keys, List<Object> values) {
98 HashSet<Object> result = new HashSet<Object>(keys.size());
99 for (int i=0; i<keys.size(); i++)
100 result.add(keys.get(i));
105 public Object get(Object set, Object key) throws BindingException {
110 public void getAll(Object setFrom, Map to) throws BindingException {
112 HashSet<Object> _setFrom = (HashSet<Object>) setFrom;
113 for (Object k : _setFrom)
118 public void getAll(Object setFrom, Object[] keys, Object[] values)
119 throws BindingException {
120 Set<Object> _setFrom = (Set<Object>) setFrom;
122 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;
142 @SuppressWarnings("unchecked")
\r
144 public int count(Object src, Object from, boolean fromInclusive,
\r
145 Object end, boolean endInclusive) throws BindingException {
\r
147 Set<Object> m = ((Set<Object>)src);
\r
148 for (Object k : m) {
\r
149 int fk = keyBinding.compare(from, k);
\r
150 int ek = keyBinding.compare(k, end);
\r
151 boolean fromMatches = fromInclusive ? fk<=0 : fk<0;
\r
152 boolean endMatches = endInclusive ? ek<=0 : ek <0;
\r
153 if ( fromMatches && endMatches ) result++;
\r
158 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 {
\r
163 public Object[] getValues(Object set) throws BindingException {
164 Set<Object> _set = (Set<Object>) set;
165 return new Object[_set.size()];
169 public void put(Object set, Object key, Object value)
170 throws BindingException {
171 if (value!=null) throw new BindingException("Cannot put non-null to a Set");
172 Set<Object> s = (Set<Object>) set;
173 Binding kb = getKeyBinding();
177 if (kb.equals(e, key)) {
186 @SuppressWarnings("unchecked")
187 Object getComparableKey(Object set, Object key) {
188 // if (keyIsComparable) return key;
191 Binding kb = getKeyBinding();
194 if (kb.equals(k, key)) return k;
199 public void putAll(Object setTo, Set from) {
200 Set<Object> _set = (Set<Object>) setTo;
204 @SuppressWarnings("unchecked")
206 public void putAll(Object setTo, Map from) throws BindingException {
207 Set<Object> s = (Set<Object>) setTo;
208 Binding kb = getKeyBinding();
209 for (Entry<Object, Object> e : (Set<Entry<Object, Object>>) from.entrySet()) {
210 Object k = getComparableKey(s, e.getKey());
217 public Object remove(Object set, Object key) throws BindingException {
218 Set<Object> _set = (Set<Object>) set;
224 public int size(Object set) throws BindingException {
225 Set<Object> _set = (Set<Object>) set;
230 public boolean isInstance(Object obj) {
231 return obj instanceof Set;
235 public Object getCeilingKey(Object set, Object key) {
236 Set<Object> s = (Set<Object>) set;
237 if (s.isEmpty()) return null;
238 Comparator<Object> comparator = getKeyBinding();
241 // We are trying to find key > o > pivot
242 int c2 = comparator.compare(key, o);
244 if (pivot==null) {pivot = o; continue;}
245 int c1 = comparator.compare(o, pivot);
252 public Object getFirstKey(Object set) {
253 Set<Object> s = (Set<Object>) set;
254 if (s.isEmpty()) return null;
255 Comparator<Object> c = getKeyBinding();
256 Object result = null;
262 if (c.compare(o, result)<0) result = o;
269 public Object getFloorKey(Object set, Object key) {
270 Set<Object> s = (Set<Object>) set;
271 if (s.isEmpty()) return null;
272 Comparator<Object> comparator = getKeyBinding();
275 // We are trying to find pivot <= o <= key
276 int c2 = comparator.compare(o, key);
279 if (pivot==null) {pivot = o; continue;}
280 int c1 = comparator.compare(pivot, o);
287 public Object getHigherKey(Object set, Object key) {
288 Set<Object> s = (Set<Object>) set;
289 if (s.isEmpty()) return null;
290 Comparator<Object> comparator = getKeyBinding();
293 // We are trying to find key > o > pivot
294 int c2 = comparator.compare(key, o);
296 if (pivot==null) {pivot = o; continue;}
297 int c1 = comparator.compare(o, pivot);
304 public Object getLastKey(Object set) {
305 Set<Object> s = (Set<Object>) set;
306 if (s.isEmpty()) return null;
307 Comparator<Object> c = getKeyBinding();
308 Object result = null;
314 if (c.compare(o, result)>0) result = o;
320 public Object getLowerKey(Object set, Object key) {
321 Set<Object> s = (Set<Object>) set;
322 if (s.isEmpty()) return null;
323 Comparator<Object> comparator = getKeyBinding();
326 // We are trying to find pivot < o < key
327 int c2 = comparator.compare(o, key);
329 if (pivot==null) {pivot = o; continue;}
330 int c1 = comparator.compare(pivot, o);