/******************************************************************************* * Copyright (c) 2010 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.databoard.binding.impl; import java.util.IdentityHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.TreeMap; import org.simantics.databoard.Bindings; import org.simantics.databoard.adapter.AdaptException; import org.simantics.databoard.adapter.Adapter; import org.simantics.databoard.adapter.AdapterConstructionException; import org.simantics.databoard.binding.ArrayBinding; import org.simantics.databoard.binding.Binding; import org.simantics.databoard.binding.MapBinding; import org.simantics.databoard.binding.error.BindingException; import org.simantics.databoard.type.MapType; /** * Binds java.util.TreeMap to MapType * * @author Toni Kalajainen */ @SuppressWarnings("all") public class TreeMapBinding extends MapBinding { public TreeMapBinding(Binding keyBinding, Binding valueBinding) { super(keyBinding, valueBinding); } public TreeMapBinding(MapType mapType, Binding keyBinding, Binding valueBinding) { super(mapType, keyBinding, valueBinding); } public void postConstruction() { } @Override public Object create() { return new TreeMap( getKeyBinding() ); } @Override public Object create(Object[] keys, Object[] values) { if (keys.length != values.length) throw new IllegalArgumentException("Equal length arrays expected"); int len = keys.length; TreeMap result = new TreeMap( getKeyBinding() ); for (int i = 0; i < len; i++) { Object key = keys[i]; Object value = values[i]; result.put(key, value); } return result; } @Override public Object create(List keys, List values) { if (keys.size()!=values.size()) throw new IllegalArgumentException("Equal length arrays expected"); int len = keys.size(); TreeMap result = new TreeMap( getKeyBinding() ); for (int i=0; i keys) throws BindingException { Map m = ((Map)map); keys.addAll(m.keySet()); } /** * Count the number of entries between two keyes * @param from * @param fromInclusive * @param end * @param endInclusive * @throws BindingException */ @SuppressWarnings("unchecked") @Override public int count(Object src, Object from, boolean fromInclusive, Object end, boolean endInclusive) throws BindingException { // Assert end > from if (keyBinding.compare(from, end)>0) return 0; TreeMap m = (TreeMap) src; Map sm = m.subMap(from, fromInclusive, end, endInclusive); return sm.size(); } /** * Read a range of entries * * @param src * @param from * @param fromInclusive * @param end * @param endInclusive * @param dstKeyArrayBinding * @param dstKeyArray * @param dstValueArrayBinding * @param dstValueArray * @throws BindingException */ 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 { try { // Assert end > from if (keyBinding.compare(from, end)>0) return 0; TreeMap m = (TreeMap) src; Map sm = m.subMap(from, fromInclusive, end, endInclusive); int dkc = dstKeyArrayBinding.size( dstKeyArray ); int dvc = dstValueArrayBinding.size( dstValueArray ); Adapter ka = Bindings.getTypeAdapter(keyBinding, dstKeyArrayBinding.getComponentBinding()); Adapter va = Bindings.getTypeAdapter(valueBinding, dstValueArrayBinding.getComponentBinding()); int i = 0; Set> es = sm.entrySet(); for (Entry e : es) { if (limit>=0 && i>=limit) break; Object dk = ka.adapt( e.getKey() ); Object dv = va.adapt( e.getValue() ); if (i void put(Object map, K key, V value) { Map m = ((Map) map); m.put(key, value); } @Override public void putAll(Object dstMap, Map srcMap) { Map dst = ((Map) dstMap); dst.putAll(srcMap); } @Override public void getAll(Object mapFrom, Map to) { Map m = ((Map) mapFrom); to.putAll(m); } @SuppressWarnings("unchecked") @Override public void getAll(Object mapFrom, Object[] keys, Object[] values) { TreeMap m = (TreeMap) mapFrom; int i = 0; for (Entry e : (Set>) m.entrySet()) { keys[i] = e.getKey(); values[i] = e.getValue(); i++; } } @SuppressWarnings("unchecked") @Override public Object remove(Object map, Object key) { Map m = ((Map) map); return m.remove(key); } @SuppressWarnings("unchecked") @Override public int size(Object map) { Map m = ((Map) map); return m.size(); } @SuppressWarnings("unchecked") @Override public boolean isInstance(Object obj) { return obj instanceof Map; } @Override public int deepHashValue(Object map, IdentityHashMap hashedObjects) throws BindingException { int result = 0; Map m = ((Map) map); Set s = m.entrySet(); for (Entry e : s) { int keyTree = getKeyBinding().deepHashValue(e.getKey(), hashedObjects); int valueTree = getValueBinding().deepHashValue(e.getValue(), hashedObjects); result += (keyTree ^ valueTree); } return result; } @Override public Object getCeilingKey(Object map, Object key) { TreeMap m = ((TreeMap) map); return m.ceilingKey(key); } @Override public Object getFirstKey(Object map) { TreeMap m = ((TreeMap) map); return m.firstKey(); } @Override public Object getFloorKey(Object map, Object key) { TreeMap m = ((TreeMap) map); return m.floorKey(key); } @Override public Object getHigherKey(Object map, Object key) { TreeMap m = ((TreeMap) map); return m.higherKey(key); } @Override public Object getLastKey(Object map) { TreeMap m = ((TreeMap) map); return m.lastKey(); } @Override public Object getLowerKey(Object map, Object key) { TreeMap m = ((TreeMap) map); return m.lowerKey(key); } }