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.IdentityHashMap;
15 import java.util.List;
17 import java.util.Map.Entry;
19 import java.util.TreeMap;
21 import org.simantics.databoard.Bindings;
22 import org.simantics.databoard.adapter.AdaptException;
23 import org.simantics.databoard.adapter.Adapter;
24 import org.simantics.databoard.adapter.AdapterConstructionException;
25 import org.simantics.databoard.binding.ArrayBinding;
26 import org.simantics.databoard.binding.Binding;
27 import org.simantics.databoard.binding.MapBinding;
28 import org.simantics.databoard.binding.error.BindingException;
29 import org.simantics.databoard.type.MapType;
32 * Binds java.util.TreeMap to MapType
34 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
36 @SuppressWarnings("all")
37 public class TreeMapBinding extends MapBinding {
39 public TreeMapBinding(Binding keyBinding, Binding valueBinding) {
40 super(keyBinding, valueBinding);
43 public TreeMapBinding(MapType mapType, Binding keyBinding,
44 Binding valueBinding) {
45 super(mapType, keyBinding, valueBinding);
48 public void postConstruction() {
52 public Object create() {
53 return new TreeMap( getKeyBinding() );
57 public Object create(Object[] keys, Object[] values) {
58 if (keys.length != values.length)
59 throw new IllegalArgumentException("Equal length arrays expected");
61 int len = keys.length;
62 TreeMap result = new TreeMap( getKeyBinding() );
64 for (int i = 0; i < len; i++) {
66 Object value = values[i];
67 result.put(key, value);
75 public Object create(List<Object> keys, List<Object> values) {
76 if (keys.size()!=values.size())
77 throw new IllegalArgumentException("Equal length arrays expected");
79 int len = keys.size();
80 TreeMap result = new TreeMap( getKeyBinding() );
82 for (int i=0; i<len; i++) {
83 Object key = keys.get(i);
84 Object value = values.get(i);
85 result.put(key, value);
92 public Object create(Map initialMap) {
93 // Replace with TreeMap. Create comparator from binding.
94 TreeMap result = new TreeMap( getKeyBinding() );
95 result.putAll(initialMap);
99 @SuppressWarnings("unchecked")
101 public void clear(Object map) {
105 @SuppressWarnings("unchecked")
107 public boolean containsKey(Object map, Object key) {
109 return m.containsKey(key);
112 @SuppressWarnings("unchecked")
114 public boolean containsValue(Object map, Object value) {
116 Binding vb = getValueBinding();
117 for (Object v : m.values())
119 if (vb.equals(v, value)) return true;
124 @SuppressWarnings("unchecked")
126 public Object get(Object map, Object key) {
131 @SuppressWarnings("unchecked")
133 public Object[] getKeys(Object map) {
135 return m.keySet().toArray(new Object[m.size()]);
139 public void getKeys(Object map, Set<Object> keys) throws BindingException {
141 keys.addAll(m.keySet());
145 * Count the number of entries between two keyes
147 * @param fromInclusive
149 * @param endInclusive
150 * @throws BindingException
152 @SuppressWarnings("unchecked")
154 public int count(Object src, Object from, boolean fromInclusive, Object end, boolean endInclusive) throws BindingException {
156 if (keyBinding.compare(from, end)>0) return 0;
158 TreeMap m = (TreeMap) src;
159 Map sm = m.subMap(from, fromInclusive, end, endInclusive);
164 * Read a range of entries
168 * @param fromInclusive
170 * @param endInclusive
171 * @param dstKeyArrayBinding
173 * @param dstValueArrayBinding
174 * @param dstValueArray
175 * @throws BindingException
177 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 {
180 if (keyBinding.compare(from, end)>0) return 0;
182 TreeMap m = (TreeMap) src;
183 Map sm = m.subMap(from, fromInclusive, end, endInclusive);
184 int dkc = dstKeyArrayBinding.size( dstKeyArray );
185 int dvc = dstValueArrayBinding.size( dstValueArray );
186 Adapter ka = Bindings.getTypeAdapter(keyBinding, dstKeyArrayBinding.getComponentBinding());
187 Adapter va = Bindings.getTypeAdapter(valueBinding, dstValueArrayBinding.getComponentBinding());
189 Set<Map.Entry<Object, Object>> es = sm.entrySet();
190 for (Entry<Object, Object> e : es) {
191 if (limit>=0 && i>=limit) break;
192 Object dk = ka.adapt( e.getKey() );
193 Object dv = va.adapt( e.getValue() );
194 if (i<dkc) dstKeyArrayBinding.set(dstKeyArray, i, dk); else dstKeyArrayBinding.add(dstKeyArray, dk);
195 if (i<dvc) dstValueArrayBinding.set(dstValueArray, i, dv); else dstValueArrayBinding.add(dstValueArray, dv);
199 } catch (AdapterConstructionException e) {
200 throw new BindingException(e);
201 } catch (AdaptException e) {
202 throw new BindingException(e);
207 @SuppressWarnings("unchecked")
209 public Object[] getValues(Object map) {
211 return m.values().toArray(new Object[m.size()]);
215 public <K, V> void put(Object map, K key, V value) {
216 Map<K, V> m = ((Map<K, V>) map);
221 public <K, V> void putAll(Object dstMap, Map<K, V> srcMap) {
222 Map<K, V> dst = ((Map<K, V>) dstMap);
227 public void getAll(Object mapFrom, Map to) {
228 Map<?, ?> m = ((Map<?, ?>) mapFrom);
232 @SuppressWarnings("unchecked")
234 public void getAll(Object mapFrom, Object[] keys, Object[] values) {
235 TreeMap m = (TreeMap) mapFrom;
237 for (Entry<Object, Object> e : (Set<Entry<Object, Object>>) m.entrySet()) {
238 keys[i] = e.getKey();
239 values[i] = e.getValue();
244 @SuppressWarnings("unchecked")
246 public Object remove(Object map, Object key) {
248 return m.remove(key);
251 @SuppressWarnings("unchecked")
253 public int size(Object map) {
258 @SuppressWarnings("unchecked")
260 public boolean isInstance(Object obj) {
261 return obj instanceof Map;
265 public int deepHashValue(Object map, IdentityHashMap<Object, Object> hashedObjects) throws BindingException {
268 Set<Entry> s = m.entrySet();
270 int keyTree = getKeyBinding().deepHashValue(e.getKey(), hashedObjects);
271 int valueTree = getValueBinding().deepHashValue(e.getValue(), hashedObjects);
272 result += (keyTree ^ valueTree);
278 public Object getCeilingKey(Object map, Object key) {
279 TreeMap m = ((TreeMap) map);
280 return m.ceilingKey(key);
284 public Object getFirstKey(Object map) {
285 TreeMap m = ((TreeMap) map);
290 public Object getFloorKey(Object map, Object key) {
291 TreeMap m = ((TreeMap) map);
292 return m.floorKey(key);
296 public Object getHigherKey(Object map, Object key) {
297 TreeMap m = ((TreeMap) map);
298 return m.higherKey(key);
302 public Object getLastKey(Object map) {
303 TreeMap m = ((TreeMap) map);
308 public Object getLowerKey(Object map, Object key) {
309 TreeMap m = ((TreeMap) map);
310 return m.lowerKey(key);