Mapping for generic java.util.Map and java.util.Set references.
Moved databoard-related SCL definitions to a new Databoard module.
Added documentation string to Databoard.scl.
Added a few SCL bindings to potentially useful methods in the Binding
class.
refs #6738
Change-Id: I07369ca5a5ae23966b11ce5ff9787ab07e38af29
import org.simantics.databoard.binding.impl.BooleanBindingDefault;\r
import org.simantics.databoard.binding.impl.ByteArrayBinding;\r
import org.simantics.databoard.binding.impl.ByteBindingDefault;\r
+import org.simantics.databoard.binding.impl.DefaultMapBinding;\r
import org.simantics.databoard.binding.impl.DoubleArrayBinding;\r
import org.simantics.databoard.binding.impl.DoubleBindingDefault;\r
import org.simantics.databoard.binding.impl.FloatArrayBinding;\r
import org.simantics.databoard.binding.impl.LongBindingDefault;\r
import org.simantics.databoard.binding.impl.ObjectArrayBinding;\r
import org.simantics.databoard.binding.impl.StringBindingDefault;\r
-import org.simantics.databoard.binding.impl.TreeMapBinding;\r
import org.simantics.databoard.binding.mutable.ContainerOptionalBinding;\r
import org.simantics.databoard.binding.mutable.UnionTaggedObjectBinding;\r
import org.simantics.databoard.type.ArrayType;\r
return binding;\r
}
- if (type instanceof MapType) {
+ if (type instanceof MapType) {
MapType mapType = (MapType) type;
- TreeMapBinding binding = new TreeMapBinding(mapType, null, null);
+ DefaultMapBinding binding = new DefaultMapBinding(mapType, null, null);
inprogress.put(type, binding);\r
binding.setKeyBinding( construct(mapType.keyType) );\r
binding.setValueBinding( construct(mapType.valueType) );\r
import org.simantics.databoard.binding.impl.ArrayListBinding;\r
import org.simantics.databoard.binding.impl.BooleanArrayBinding;\r
import org.simantics.databoard.binding.impl.ByteArrayBinding;\r
+import org.simantics.databoard.binding.impl.DefaultMapBinding;\r
+import org.simantics.databoard.binding.impl.DefaultSetBinding;\r
import org.simantics.databoard.binding.impl.DoubleArrayBinding;\r
import org.simantics.databoard.binding.impl.FloatArrayBinding;\r
import org.simantics.databoard.binding.impl.HashMapBinding;\r
if (Set.class.isAssignableFrom(request.getClazz())) {\r
MapType type = new MapType();\r
type.valueType = Datatypes.VOID;\r
- return new TreeSetBinding(type, null);\r
+ return new DefaultSetBinding(type, null);\r
}\r
\r
if (TreeMap.class.isAssignableFrom(request.getClazz())) {\r
}\r
\r
if (Map.class.isAssignableFrom(request.getClazz())) {\r
- return new HashMapBinding(new MapType(), null, null);\r
+ return new DefaultMapBinding(new MapType(), null, null);\r
}\r
\r
return null;\r
\r
@Override\r
public Object create(Map<?, ?> initialMap) throws BindingException {\r
+ if (initialMap instanceof THashMap)\r
+ return initialMap;\r
+ \r
// Replace with TreeMap. Create comparator from binding.\r
THashMap<Object, Object> result = new THashMap<Object, Object>();\r
putAll(result, initialMap);\r
}\r
\r
public Object create(Set<?> initialSet) throws BindingException {\r
+ if (initialSet instanceof THashSet)\r
+ return initialSet;\r
+ \r
return new THashSet<Object>(initialSet);\r
}\r
\r
- @Override\r
+ @SuppressWarnings({ "unchecked", "rawtypes" })\r
+ @Override\r
public Object create(Map initialMap) throws BindingException {\r
return new THashSet<Object>(initialMap.keySet());\r
}\r
--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2010 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.databoard.binding.impl;\r
+\r
+import java.util.IdentityHashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Map.Entry;\r
+import java.util.Set;\r
+import java.util.TreeMap;\r
+\r
+import org.simantics.databoard.binding.ArrayBinding;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.MapBinding;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.type.MapType;\r
+\r
+/**\r
+ * Binds java.util.Map to MapType\r
+ * \r
+ * This Binding type accepts all java.util.Map instances, but creates\r
+ * java.util.TreeMap instances by default.\r
+ * \r
+ * @author Reino Ruusu <reino.ruusu@vtt.fi>\r
+ */\r
+@SuppressWarnings("rawtypes")\r
+public class DefaultMapBinding extends MapBinding {\r
+\r
+ public DefaultMapBinding(Binding keyBinding, Binding valueBinding) {\r
+ super(keyBinding, valueBinding);\r
+ }\r
+\r
+ public DefaultMapBinding(MapType mapType, Binding keyBinding,\r
+ Binding valueBinding) {\r
+ super(mapType, keyBinding, valueBinding);\r
+ }\r
+\r
+ public void postConstruction() {\r
+ }\r
+\r
+ @Override\r
+ public Object create() {\r
+ return new TreeMap<Object,Object>( keyBinding );\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public Object create(Object[] keys, Object[] values) {\r
+ if (keys.length != values.length)\r
+ throw new IllegalArgumentException("Equal length arrays expected");\r
+\r
+ int len = keys.length;\r
+ Map result = new TreeMap<Object,Object>( keyBinding );\r
+\r
+ for (int i = 0; i < len; i++) {\r
+ Object key = keys[i];\r
+ Object value = values[i];\r
+ result.put(key, value);\r
+ }\r
+\r
+ return result;\r
+ }\r
+ \r
+ \r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public Object create(List<Object> keys, List<Object> values) {\r
+ if (keys.size()!=values.size())\r
+ throw new IllegalArgumentException("Equal length arrays expected");\r
+ \r
+ int len = keys.size();\r
+ Map result = new TreeMap<Object,Object>( keyBinding );\r
+ \r
+ for (int i=0; i<len; i++) {\r
+ Object key = keys.get(i);\r
+ Object value = values.get(i);\r
+ result.put(key, value);\r
+ }\r
+ \r
+ return result;\r
+ } \r
+\r
+ @Override\r
+ public Object create(Map<?,?> map) {\r
+ return map;\r
+ }\r
+ \r
+ @Override\r
+ public void clear(Object map) {\r
+ ((Map) map).clear();\r
+ }\r
+\r
+ @Override\r
+ public boolean containsKey(Object map, Object key) {\r
+ Map m = ((Map) map);\r
+ return m.containsKey(key);\r
+ }\r
+\r
+ @Override\r
+ public boolean containsValue(Object map, Object value) {\r
+ Map m = ((Map) map);\r
+ Binding vb = getValueBinding();\r
+ for (Object v : m.values())\r
+ {\r
+ if (vb.equals(v, value)) return true;\r
+ }\r
+ return false;\r
+ }\r
+\r
+ @Override\r
+ public Object get(Object map, Object key) {\r
+ Map m = ((Map) map);\r
+ return m.get(key);\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public Object[] getKeys(Object map) {\r
+ Map m = ((Map) map);\r
+ return m.keySet().toArray(new Object[m.size()]);\r
+ }\r
+ \r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public void getKeys(Object map, Set<Object> keys) throws BindingException {\r
+ Map m = ((Map)map);\r
+ keys.addAll(m.keySet());\r
+ } \r
+ \r
+ /**\r
+ * Count the number of entries between two keyes\r
+ * @param from\r
+ * @param fromInclusive\r
+ * @param end \r
+ * @param endInclusive\r
+ * @throws BindingException\r
+ */\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public int count(Object src, Object from, boolean fromInclusive, Object end, boolean endInclusive) throws BindingException {\r
+ // Assert end > from\r
+ if (keyBinding.compare(from, end)>0) return 0;\r
+ \r
+ if (src instanceof TreeMap) {\r
+ TreeMap m = (TreeMap) src;\r
+ Map sm = m.subMap(from, fromInclusive, end, endInclusive);\r
+ return sm.size();\r
+ }\r
+ else {\r
+ int result = 0;\r
+ Map<Object, Object> m = ((Map<Object, Object>)src);\r
+ for (Object k : m.keySet()) {\r
+ int fk = keyBinding.compare(from, k);\r
+ int ek = keyBinding.compare(k, end);\r
+ boolean fromMatches = fromInclusive ? fk<=0 : fk<0;\r
+ boolean endMatches = endInclusive ? ek<=0 : ek <0; \r
+ if ( fromMatches && endMatches ) result++;\r
+ } \r
+ return result;\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * Read a range of entries\r
+ * \r
+ * @param src\r
+ * @param from\r
+ * @param fromInclusive\r
+ * @param end \r
+ * @param endInclusive\r
+ * @param dstKeyArrayBinding\r
+ * @param dstKeyArray\r
+ * @param dstValueArrayBinding\r
+ * @param dstValueArray\r
+ * @throws BindingException\r
+ */\r
+ 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
+ if (src instanceof TreeMap) {\r
+ return new TreeMapBinding(keyBinding, valueBinding).getEntries(src, from, fromInclusive, end, endInclusive, dstKeyArrayBinding, dstKeyArray, dstValueArrayBinding, dstValueArray, limit);\r
+ }\r
+ else {\r
+ return new HashMapBinding(keyBinding, valueBinding).getEntries(src, from, fromInclusive, end, endInclusive, dstKeyArrayBinding, dstKeyArray, dstValueArrayBinding, dstValueArray, limit);\r
+ }\r
+ }\r
+ \r
+\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public Object[] getValues(Object map) {\r
+ Map m = ((Map) map);\r
+ return m.values().toArray(new Object[m.size()]);\r
+ }\r
+\r
+ @Override\r
+ public <K, V> void put(Object map, K key, V value) {\r
+ @SuppressWarnings("unchecked")\r
+ Map<K, V> m = ((Map<K, V>) map);\r
+ m.put(key, value);\r
+ }\r
+\r
+ @Override\r
+ public <K, V> void putAll(Object dstMap, Map<K, V> srcMap) {\r
+ @SuppressWarnings("unchecked")\r
+ Map<K, V> dst = ((Map<K, V>) dstMap);\r
+ dst.putAll(srcMap);\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public void getAll(Object mapFrom, Map to) {\r
+ Map<?, ?> m = ((Map<?, ?>) mapFrom);\r
+ to.putAll(m);\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public void getAll(Object mapFrom, Object[] keys, Object[] values) {\r
+ Map m = (Map) mapFrom;\r
+ int i = 0;\r
+ for (Entry<Object, Object> e : (Set<Entry<Object, Object>>) m.entrySet()) {\r
+ keys[i] = e.getKey();\r
+ values[i] = e.getValue();\r
+ i++;\r
+ }\r
+ }\r
+ \r
+ @Override\r
+ public Object remove(Object map, Object key) {\r
+ Map m = ((Map) map);\r
+ return m.remove(key);\r
+ }\r
+\r
+ @Override\r
+ public int size(Object map) {\r
+ Map m = ((Map) map);\r
+ return m.size();\r
+ }\r
+\r
+ @Override\r
+ public boolean isInstance(Object obj) {\r
+ return obj instanceof Map;\r
+ }\r
+\r
+ @Override\r
+ public int deepHashValue(Object map, IdentityHashMap<Object, Object> hashedObjects) throws BindingException {\r
+ int result = 0;\r
+ Map m = ((Map) map);\r
+ @SuppressWarnings("unchecked")\r
+ Set<Entry> s = m.entrySet();\r
+ for (Entry e : s) {\r
+ int keyTree = getKeyBinding().deepHashValue(e.getKey(), hashedObjects);\r
+ int valueTree = getValueBinding().deepHashValue(e.getValue(), hashedObjects);\r
+ result += (keyTree ^ valueTree);\r
+ }\r
+ return result;\r
+ }\r
+\r
+ @Override\r
+ public Object getCeilingKey(Object map, Object key) {\r
+ if (map instanceof TreeMap) {\r
+ return new TreeMapBinding(keyBinding, valueBinding).getCeilingKey(map, key);\r
+ }\r
+ else {\r
+ return new HashMapBinding(keyBinding, valueBinding).getCeilingKey(map, key);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public Object getFirstKey(Object map) {\r
+ if (map instanceof TreeMap) {\r
+ return new TreeMapBinding(keyBinding, valueBinding).getFirstKey(map);\r
+ }\r
+ else {\r
+ return new HashMapBinding(keyBinding, valueBinding).getFirstKey(map);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public Object getFloorKey(Object map, Object key) {\r
+ if (map instanceof TreeMap) {\r
+ return new TreeMapBinding(keyBinding, valueBinding).getFloorKey(map, key);\r
+ }\r
+ else {\r
+ return new HashMapBinding(keyBinding, valueBinding).getFloorKey(map, key);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public Object getHigherKey(Object map, Object key) {\r
+ if (map instanceof TreeMap) {\r
+ return new TreeMapBinding(keyBinding, valueBinding).getHigherKey(map, key);\r
+ }\r
+ else {\r
+ return new HashMapBinding(keyBinding, valueBinding).getHigherKey(map, key);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public Object getLastKey(Object map) {\r
+ if (map instanceof TreeMap) {\r
+ return new TreeMapBinding(keyBinding, valueBinding).getLastKey(map);\r
+ }\r
+ else {\r
+ return new HashMapBinding(keyBinding, valueBinding).getLastKey(map);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public Object getLowerKey(Object map, Object key) {\r
+ if (map instanceof TreeMap) {\r
+ return new TreeMapBinding(keyBinding, valueBinding).getLowerKey(map, key);\r
+ }\r
+ else {\r
+ return new HashMapBinding(keyBinding, valueBinding).getLowerKey(map, key);\r
+ }\r
+ }\r
+}\r
--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2010 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.databoard.binding.impl;\r
+\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Set;\r
+import java.util.TreeSet;\r
+\r
+import org.simantics.databoard.binding.ArrayBinding;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.MapBinding;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.binding.reflection.VoidBinding;\r
+import org.simantics.databoard.type.MapType;\r
+import org.simantics.databoard.type.RecordType;\r
+\r
+/**\r
+ * Binds java.util.Set to Map(T, {})\r
+ * \r
+ * This binding accepts all java.util.Set instances, but instantiates\r
+ * java.util.TreeSet objects.\r
+ *\r
+ * @author Reino Ruusu <reino.ruusu@vtt.fi>\r
+ */\r
+@SuppressWarnings("unchecked")\r
+public class DefaultSetBinding extends MapBinding {\r
+ \r
+ public DefaultSetBinding(MapType mapType, Binding elementBinding) {\r
+ super(mapType, elementBinding, VoidBinding.VOID_BINDING);\r
+ }\r
+ \r
+ public DefaultSetBinding(Binding elementBinding) {\r
+ super(new MapType(elementBinding.type(), RecordType.VOID_TYPE), elementBinding, VoidBinding.VOID_BINDING);\r
+ }\r
+\r
+ @Override\r
+ public void clear(Object set) throws BindingException {\r
+ Set<Object> _set = (Set<Object>) set;\r
+ _set.clear();\r
+ }\r
+\r
+ @Override\r
+ public boolean containsKey(Object set, Object key) throws BindingException {\r
+ Set<Object> _set = (Set<Object>) set;\r
+ return _set.contains(key);\r
+ }\r
+\r
+ @Override\r
+ public boolean containsValue(Object set, Object value)\r
+ throws BindingException {\r
+ return false;\r
+ }\r
+\r
+ @Override\r
+ public Object create() throws BindingException {\r
+ return new TreeSet<Object>( keyBinding );\r
+ }\r
+\r
+ public Object create(Set<?> initialSet) throws BindingException {\r
+ return initialSet;\r
+ }\r
+\r
+ @Override\r
+ public Object create(Map<?, ?> initialMap) throws BindingException {\r
+ Set<Object> result = new TreeSet<Object>( keyBinding );\r
+ result.addAll(initialMap.keySet());\r
+ return result;\r
+ }\r
+\r
+ @Override\r
+ public Object create(Object[] keys, Object[] values)\r
+ throws BindingException { \r
+ Set<Object> result = new TreeSet<Object>( keyBinding );\r
+ for (int i=0; i<keys.length; i++) {\r
+ result.add( keys[i] );\r
+ }\r
+ return result;\r
+ }\r
+ \r
+ @Override\r
+ public Object create(List<Object> keys, List<Object> values) {\r
+ Set<Object> result = new TreeSet<Object>( keyBinding );\r
+ for (int i=0; i<keys.size(); i++)\r
+ result.add(keys.get(i));\r
+ return result;\r
+ } \r
+\r
+ @Override\r
+ public Object get(Object set, Object key) throws BindingException {\r
+ return null;\r
+ }\r
+ \r
+ @Override\r
+ public <K, V> void getAll(Object setFrom, Map<K, V> to) throws BindingException {\r
+ Map<K, V> _to = (Map<K, V>) to;\r
+ Set<K> _setFrom = (Set<K>) setFrom;\r
+ for (K k : _setFrom)\r
+ _to.put(k, null);\r
+ }\r
+\r
+ @Override\r
+ public void getAll(Object setFrom, Object[] keys, Object[] values)\r
+ throws BindingException {\r
+ Set<Object> _setFrom = (Set<Object>) setFrom;\r
+ int i=0;\r
+ for (Object k : _setFrom) {\r
+ keys[i] = k;\r
+ values[i] = null;\r
+ } \r
+ }\r
+\r
+ @Override\r
+ public Object[] getKeys(Object set) throws BindingException {\r
+ Set<Object> _set = (Set<Object>) set;\r
+ return _set.toArray(new Object[_set.size()]);\r
+ }\r
+\r
+ @Override\r
+ public void getKeys(Object set, Set<Object> keys) throws BindingException {\r
+ Set<Object> s = (Set<Object>) set;\r
+ keys.addAll(s);\r
+ }\r
+ \r
+ @Override\r
+ public Object[] getValues(Object set) throws BindingException {\r
+ Set<Object> _set = (Set<Object>) set;\r
+ return new Object[_set.size()];\r
+ }\r
+ \r
+ @Override\r
+ public int count(Object src, Object from, boolean fromInclusive, Object end, boolean endInclusive) throws BindingException {\r
+ if (src instanceof TreeSet)\r
+ return new TreeSetBinding(keyBinding).count(src, from, fromInclusive, end, endInclusive);\r
+ else\r
+ return new HashSetBinding(keyBinding).count(src, from, fromInclusive, end, endInclusive);\r
+ }\r
+ \r
+ @Override\r
+ 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
+ return 0;\r
+ }\r
+\r
+ @Override\r
+ public void put(Object set, Object key, Object value)\r
+ throws BindingException {\r
+ Set<Object> _set = (Set<Object>) set;\r
+ if (value!=null) throw new BindingException("Cannot put non-null to a Set");\r
+ _set.add(key);\r
+ }\r
+\r
+ public void putAll(Object setTo, Set<?> from) {\r
+ Set<Object> _set = (Set<Object>) setTo;\r
+ _set.addAll(from);\r
+ }\r
+ \r
+ @Override\r
+ public <K,V> void putAll(Object setTo, Map<K,V> from) throws BindingException {\r
+ Set<Object> _set = (Set<Object>) setTo;\r
+ _set.addAll(from.keySet());\r
+ }\r
+\r
+ @Override\r
+ public Object remove(Object set, Object key) throws BindingException {\r
+ Set<Object> _set = (Set<Object>) set;\r
+ _set.remove(key);\r
+ return null;\r
+ }\r
+\r
+ @Override\r
+ public int size(Object set) throws BindingException {\r
+ Set<Object> _set = (Set<Object>) set;\r
+ return _set.size();\r
+ }\r
+\r
+ @Override\r
+ public boolean isInstance(Object obj) {\r
+ return obj instanceof Set;\r
+ }\r
+\r
+ @Override\r
+ public Object getCeilingKey(Object set, Object key) {\r
+ if (set instanceof TreeSet)\r
+ return new TreeSetBinding(keyBinding).getCeilingKey(set, key);\r
+ else\r
+ return new HashSetBinding(keyBinding).getCeilingKey(set, key);\r
+ }\r
+\r
+ @Override\r
+ public Object getFirstKey(Object set) {\r
+ if (set instanceof TreeSet)\r
+ return new TreeSetBinding(keyBinding).getFirstKey(set);\r
+ else\r
+ return new HashSetBinding(keyBinding).getFirstKey(set);\r
+ }\r
+\r
+ @Override\r
+ public Object getFloorKey(Object set, Object key) {\r
+ if (set instanceof TreeSet)\r
+ return new TreeSetBinding(keyBinding).getFloorKey(set, key);\r
+ else\r
+ return new HashSetBinding(keyBinding).getFloorKey(set, key);\r
+ }\r
+\r
+ @Override\r
+ public Object getHigherKey(Object set, Object key) {\r
+ if (set instanceof TreeSet)\r
+ return new TreeSetBinding(keyBinding).getHigherKey(set, key);\r
+ else\r
+ return new HashSetBinding(keyBinding).getHigherKey(set, key);\r
+ }\r
+\r
+ @Override\r
+ public Object getLastKey(Object set) {\r
+ if (set instanceof TreeSet)\r
+ return new TreeSetBinding(keyBinding).getLastKey(set);\r
+ else\r
+ return new HashSetBinding(keyBinding).getLastKey(set);\r
+ }\r
+\r
+ @Override\r
+ public Object getLowerKey(Object set, Object key) {\r
+ if (set instanceof TreeSet)\r
+ return new TreeSetBinding(keyBinding).getLowerKey(set, key);\r
+ else\r
+ return new HashSetBinding(keyBinding).getLowerKey(set, key);\r
+ }\r
+\r
+}\r
+\r
super(mapType, keyBinding, valueBinding);
}
- public void postConstruction() {}\r
- \r
- @Override
- public Object create() {
- return new HashMap<Object, Object>();
- }
-
+ @Override\r
+ public Object create() { \r
+ return new HashMap<Object, Object>();\r
+ }\r
+ \r
@Override
public Object create(Object[] keys, Object[] values) {
if (keys.length!=values.length)
}
@Override
- public Object create(Map<?, ?> initialMap) throws BindingException {
+ public Object create(Map<?, ?> initialMap) throws BindingException {\r
+ if (initialMap instanceof HashMap)\r
+ return initialMap;\r
+
// Replace with TreeMap. Create comparator from binding.
HashMap<Object, Object> result = new HashMap<Object, Object>();
putAll(result, initialMap);
*
* @author Toni Kalajainen <toni.kalajainen@vtt.fi>
*/\r
-@SuppressWarnings("all")
+@SuppressWarnings({"rawtypes", "unchecked"})\r
public class HashSetBinding extends MapBinding {
public HashSetBinding(MapType mapType, Binding elementBinding) {
super(new MapType(elementBinding.type(), RecordType.VOID_TYPE), elementBinding, VoidBinding.VOID_BINDING);
}
- @SuppressWarnings("unchecked")
@Override
public void clear(Object set) throws BindingException {
Set _set = (Set) set;
keys.addAll(s);
}
\r
- @SuppressWarnings("unchecked")\r
@Override\r
public int count(Object src, Object from, boolean fromInclusive,\r
Object end, boolean endInclusive) throws BindingException {\r
s.add(key);
}
- @SuppressWarnings("unchecked")
Object getComparableKey(Object set, Object key) {
// if (keyIsComparable) return key;
_set.addAll(from);
}
- @SuppressWarnings("unchecked")
@Override
public void putAll(Object setTo, Map from) throws BindingException {
Set<Object> s = (Set<Object>) setTo;
- Binding kb = getKeyBinding();
for (Entry<Object, Object> e : (Set<Entry<Object, Object>>) from.entrySet()) {
Object k = getComparableKey(s, e.getKey());
s.remove(k);
return false;
}
- @Override
- public Object create() throws BindingException {
- return new TreeSet<Object>( getKeyBinding() );
- }
-
- public Object create(Set<?> initialSet) throws BindingException {
+ @Override\r
+ public Object create() throws BindingException {\r
+ return new TreeSet<Object>( getKeyBinding() );\r
+ }\r
+\r
+ public Object create(Set<?> initialSet) throws BindingException {\r
+ if (initialSet instanceof TreeSet && ((TreeSet<?>) initialSet).comparator() == getKeyBinding())\r
+ return initialSet;\r
+
TreeSet<Object> result = new TreeSet<Object>(getKeyBinding());
result.addAll(initialSet);
return result;
public static final JavaMathOperation SOR = new JavaMathOperation(Opcodes.IOR, Types.SHORT, Types.SHORT, Types.SHORT);
public static final JavaMathOperation SXOR = new JavaMathOperation(Opcodes.IXOR, Types.SHORT, Types.SHORT, Types.SHORT);
+ public static final JavaMathOperation BADD = new JavaMathOperation(Opcodes.IADD, Types.BYTE, Types.BYTE, Types.BYTE);
+ public static final JavaMathOperation BSUB = new JavaMathOperation(Opcodes.ISUB, Types.BYTE, Types.BYTE, Types.BYTE);
+ public static final JavaMathOperation BMUL = new JavaMathOperation(Opcodes.IMUL, Types.BYTE, Types.BYTE, Types.BYTE);
+ public static final JavaMathOperation BDIV = new JavaMathOperation(Opcodes.IDIV, Types.BYTE, Types.BYTE, Types.BYTE);
+ public static final JavaMathOperation BREM = new JavaMathOperation(Opcodes.IREM, Types.BYTE, Types.BYTE, Types.BYTE);
+ public static final JavaMathOperation BNEG = new JavaMathOperation(Opcodes.INEG, Types.BYTE, Types.BYTE);
+ public static final JavaMathOperation BAND = new JavaMathOperation(Opcodes.IAND, Types.BYTE, Types.BYTE, Types.BYTE);
+ public static final JavaMathOperation BOR = new JavaMathOperation(Opcodes.IOR, Types.BYTE, Types.BYTE, Types.BYTE);
+ public static final JavaMathOperation BXOR = new JavaMathOperation(Opcodes.IXOR, Types.BYTE, Types.BYTE, Types.BYTE);
+
public static final JavaMathOperation CADD = new JavaMathOperation(Opcodes.IADD, Types.CHARACTER, Types.CHARACTER, Types.INTEGER);
public static final JavaMathOperation CSUB = new JavaMathOperation(Opcodes.ISUB, Types.INTEGER, Types.CHARACTER, Types.CHARACTER);
addValue("sor", JavaMathOperation.SOR);
addValue("sxor", JavaMathOperation.SXOR);
+ addValue("badd", JavaMathOperation.BADD);
+ addValue("bsub", JavaMathOperation.BSUB);
+ addValue("bmul", JavaMathOperation.BMUL);
+ addValue("bdiv", JavaMathOperation.BDIV);
+ addValue("brem", JavaMathOperation.BREM);
+ addValue("bneg", JavaMathOperation.BNEG);
+ addValue("band", JavaMathOperation.BAND);
+ addValue("bor", JavaMathOperation.BOR);
+ addValue("bxor", JavaMathOperation.BXOR);
+
// Conversions
for(JavaConversionOperation operation : JavaConversionOperation.OPCODES)
addValue(operation.getMnemonic(), operation);
addValue("lcmpgt", new JavaComparisonOperation(">", Types.LONG));
addValue("lcmpge", new JavaComparisonOperation(">=", Types.LONG));
+ addValue("bcmpeq", new JavaComparisonOperation("==", Types.BYTE));
+ addValue("bcmpne", new JavaComparisonOperation("!=", Types.BYTE));
+ addValue("bcmplt", new JavaComparisonOperation("<", Types.BYTE));
+ addValue("bcmple", new JavaComparisonOperation("<=", Types.BYTE));
+ addValue("bcmpgt", new JavaComparisonOperation(">", Types.BYTE));
+ addValue("bcmpge", new JavaComparisonOperation(">=", Types.BYTE));
+
addValue("scmpeq", new JavaComparisonOperation("==", Types.SHORT));
addValue("scmpne", new JavaComparisonOperation("!=", Types.SHORT));
addValue("scmplt", new JavaComparisonOperation("<", Types.SHORT));
addValue("ifge", new JavaComparisonToZeroOperation(">="));
TVar A = Types.var(Kinds.STAR);
- TVar B = Types.var(Kinds.STAR);
addValue("unsafeCoerce", UnsafeCoerce.INSTANCE);
addValue("equals", new JavaMethod(true,
\r
private static final THashMap<TCon,Constant> BINDING_CONSTANTS2 = new THashMap<TCon,Constant>();\r
static {\r
- BINDING_CONSTANTS2.put(MAP, new JavaConstructor("org/simantics/databoard/binding/impl/HashMapBinding", Types.NO_EFFECTS, Types.pred(Types.SERIALIZABLE, Types.apply(MAP, A, B)), Types.pred(Types.SERIALIZABLE, A), Types.pred(Types.SERIALIZABLE, B)));\r
- BINDING_CONSTANTS2.put(MMAP, new JavaConstructor("org/simantics/databoard/binding/impl/HashMapBinding", Types.NO_EFFECTS, Types.pred(Types.SERIALIZABLE, Types.apply(MMAP, A, B)), Types.pred(Types.SERIALIZABLE, A), Types.pred(Types.SERIALIZABLE, B)));\r
+ BINDING_CONSTANTS2.put(MAP, new JavaConstructor("org/simantics/databoard/binding/impl/DefaultMapBinding", Types.NO_EFFECTS, Types.pred(Types.SERIALIZABLE, Types.apply(MAP, A, B)), Types.pred(Types.SERIALIZABLE, A), Types.pred(Types.SERIALIZABLE, B)));\r
+ BINDING_CONSTANTS2.put(MMAP, new JavaConstructor("org/simantics/databoard/binding/impl/DefaultMapBinding", Types.NO_EFFECTS, Types.pred(Types.SERIALIZABLE, Types.apply(MMAP, A, B)), Types.pred(Types.SERIALIZABLE, A), Types.pred(Types.SERIALIZABLE, B)));\r
}\r
\r
public static Reduction reduceSerializable(Type parameter) {\r
"graph"\r
"org.simantics.db.WriteGraph"\r
\r
-importJava "org.simantics.databoard.type.Datatype" where\r
- data Datatype\r
- @JavaName toString\r
- showDatatype :: Datatype -> String\r
-\r
-instance Show Datatype where\r
- show = showDatatype\r
-\r
-importJava "org.simantics.databoard.Bindings" where\r
- @JavaName getBinding \r
- datatypeBinding :: Datatype -> Binding Dynamic\r
-\r
-importJava "org.simantics.databoard.binding.mutable.Variant" where\r
- @JavaName "<init>"\r
- createVariant_ :: Binding Dynamic -> Dynamic -> Variant\r
- @JavaName "type"\r
- variantDatatype :: Variant -> Datatype\r
-\r
-createVariant :: Datatype -> Dynamic -> Variant\r
-createVariant dt v = createVariant_ (datatypeBinding dt) v\r
-\r
-importJava "org.simantics.databoard.Datatypes" where\r
- @JavaName translate\r
- translateDatatype :: String -> Datatype\r
-\r
importJava "org.simantics.db.Resource" where\r
"A resource is a node in a semantic graph."\r
data Resource\r
--- /dev/null
+import "Prelude"\r
+import "Random"\r
+\r
+/// Datatype ///\r
+\r
+"A data type component with component name and data type"\r
+@JavaType "org.simantics.databoard.type.Component"\r
+@FieldNames [name, "type"]\r
+data DatatypeComponent = DatatypeComponent String Datatype\r
+\r
+"""A data type that represents the data types supported by the Simantics\r
+Databoard plugin."""\r
+@JavaType "org.simantics.databoard.type.Datatype"\r
+data Datatype =\r
+ @JavaType "org.simantics.databoard.type.BooleanType"\r
+ BooleanType\r
+ | @JavaType "org.simantics.databoard.type.ByteType"\r
+ ByteType\r
+ | @JavaType "org.simantics.databoard.type.IntegerType"\r
+ IntegerType\r
+ | @JavaType "org.simantics.databoard.type.LongType"\r
+ LongType\r
+ | @JavaType "org.simantics.databoard.type.FloatType"\r
+ FloatType\r
+ | @JavaType "org.simantics.databoard.type.DoubleType"\r
+ DoubleType\r
+ | @JavaType "org.simantics.databoard.type.StringType"\r
+ StringType\r
+ | @JavaType "org.simantics.databoard.type.ArrayType"\r
+ @FieldNames [componentType]\r
+ ArrayType Datatype\r
+ | @JavaType "org.simantics.databoard.type.OptionalType"\r
+ @FieldNames [componentType]\r
+ OptionalType Datatype\r
+ | @JavaType "org.simantics.databoard.type.MapType"\r
+ @FieldNames [keyType, valueType]\r
+ MapType Datatype Datatype\r
+ | @JavaType "org.simantics.databoard.type.RecordType"\r
+ @FieldNames [components]\r
+ RecordType (Vector DatatypeComponent)\r
+ | @JavaType "org.simantics.databoard.type.UntionType"\r
+ @FieldNames [components]\r
+ UnionType (Vector DatatypeComponent)\r
+ | @JavaType "org.simantics.databoard.type.VariantType"\r
+ VariantType\r
+\r
+importJava "org.simantics.databoard.type.Datatype" where\r
+ @private\r
+ @JavaName toString\r
+ showDatatype :: Datatype -> String\r
+ \r
+ "Get the number of type components in an data type"\r
+ @JavaName getComponentCount\r
+ datatypeCompnentCount :: Datatype -> Integer\r
+\r
+ "Get a component type of a composite data type"\r
+ @JavaName getComponentType\r
+ datatypeComponentType :: Datatype -> ChildReference -> Datatype\r
+ \r
+ @private\r
+ @JavaName equals\r
+ datatypeEquals :: Datatype -> Datatype -> Boolean \r
+\r
+instance Show Datatype where\r
+ show = showDatatype\r
+\r
+instance Eq Datatype where\r
+ (==) = datatypeEquals\r
+\r
+/// Binding ///\r
+\r
+importJava "org.simantics.databoard.binding.Binding" where\r
+ "Check whether a dynamic object is an instance of a given binding"\r
+ @JavaName isInstance\r
+ isBindingInstance :: Binding Dynamic -> Dynamic -> Boolean\r
+\r
+ "Create a serializable object from a textual representation"\r
+ parseValueDefinition :: Serializable a => String -> a\r
+ \r
+ "Compare two serializable objects\n\nResult is -1, 0 or 1 depending the partial ordering of the objects."\r
+ @JavaName compare\r
+ compareObjects :: Serializable a => a -> a -> Integer\r
+\r
+ "Return true, if two serializable values are equal" \r
+ @JavaName equals\r
+ serializableEq :: Serializable a => a -> a -> Boolean\r
+ \r
+ "The default value of a serializable type"\r
+ @JavaName createDefault\r
+ serializableDefaultValue :: Serializable a => a\r
+ \r
+ "Create a random value of a serializable type"\r
+ @JavaName createRandom\r
+ serializableRandomValue :: Serializable a => <Random> a\r
+ \r
+ "Get a textual representation of a serializable value"\r
+ @JavaName toString\r
+ showSerializable :: Serializable a => a -> String\r
+ \r
+ @private\r
+ @JavaName getComponent \r
+ getSerializableComponent_ :: Serializable a => a -> ChildReference -> Binding b -> b \r
+\r
+ "Get a component binding"\r
+ @JavaName getComponentBinding\r
+ getComponentBinding :: Binding a -> ChildReference -> Binding b\r
+ \r
+ @private\r
+ @JavaName equals\r
+ bindingEquals :: Binding a -> Binding a -> Boolean\r
+ \r
+instance Eq (Binding a) where\r
+ (==) = bindingEquals\r
+\r
+"Get a child data component of a composite serializable value"\r
+getSerializableComponent :: Serializable a => Serializable b => a -> ChildReference -> b\r
+getSerializableComponent object ref = getSerializableComponent_ object ref binding\r
+\r
+/// Serializer ///\r
+\r
+importJava "org.simantics.databoard.serialization.Serializer" where\r
+ "A data serializer for SCL type a" \r
+ data Serializer a\r
+\r
+ @private\r
+ @JavaName "serialize" \r
+ serialize_ :: Serializer a -> a -> ByteArray\r
+ \r
+ @private\r
+ @JavaName "deserialize"\r
+ deserialize_ :: Serializer a -> ByteArray -> a\r
+\r
+importJava "org.simantics.databoard.Bindings" where\r
+ @private\r
+ @JavaName "getSerializer"\r
+ serializerOf :: Binding a -> Serializer a\r
+ \r
+ @private\r
+ @JavaName toString\r
+ bindingToString :: Binding a -> String\r
+\r
+ "Adapt between types using explicitly provided binding objects: `adapt_ value from to`"\r
+ @JavaName adapt\r
+ adapt_ :: a -> Binding a -> Binding b -> b\r
+ \r
+"Adapt value from one serializable type to another"\r
+adapt :: Serializable a => Serializable b => a -> b\r
+adapt x = adapt_ x binding binding\r
+ \r
+instance Show (Binding a) where\r
+ show = bindingToString\r
+\r
+"Serializes a value to a byte array using default serializer."\r
+serialize :: Serializable a => a -> ByteArray\r
+serialize v = serialize_ (serializerOf binding) v\r
+\r
+"Deserializes a value from a byte array using default serializer."\r
+deserialize :: Serializable a => ByteArray -> a\r
+deserialize ba = deserialize_ (serializerOf binding) ba\r
+\r
+importJava "org.simantics.databoard.Bindings" where\r
+ "Get a default binding for a given data type" \r
+ @JavaName getBinding \r
+ datatypeBinding :: Datatype -> Binding Dynamic\r
+\r
+importJava "org.simantics.databoard.Datatypes" where\r
+ "Get a data type from a string representation"\r
+ @JavaName translate\r
+ translateDatatype :: String -> Datatype\r
+\r
+importJava "org.simantics.databoard.binding.mutable.Variant" where\r
+ // data Variant (in Builtins)\r
+ "Create a variant using an explicitly provided binding value (unchecked cast)"\r
+ @JavaName "<init>"\r
+ createVariant_ :: Binding Dynamic -> Dynamic -> Variant\r
+ \r
+ "Get the data type of a variant object"\r
+ @JavaName "type"\r
+ variantDatatype :: Variant -> Datatype\r
+ \r
+ "Get raw value contained by a variant (unchecked cast)"\r
+ @JavaName getValue\r
+ rawVariantValue :: Variant -> a\r
+ \r
+ "Create a variant from a raw object (based on Java class)"\r
+ @JavaName ofInstance\r
+ variantOf :: a -> Variant\r
+ \r
+ "Create a variant with explicitly provided binding and value"\r
+ @JavaName "<init>"\r
+ variant_ :: Binding a -> a -> Variant\r
+ \r
+ "Get value from a variant using a given binding"\r
+ @JavaName getValue\r
+ variantValue_ :: Variant -> Binding a -> a\r
+ \r
+ @private\r
+ @JavaName toString\r
+ showVariant :: Variant -> String\r
+ \r
+ "Get a component of compound data value in a variant"\r
+ @JavaName getComponent\r
+ variantComponent :: Variant -> ChildReference -> Variant\r
+\r
+"Create a variant of a given data type from an object in the default binding (unchecked, use with extreme caution)"\r
+createVariant :: Datatype -> Dynamic -> Variant\r
+createVariant dt v = createVariant_ (datatypeBinding dt) v\r
+\r
+"Create a variant from a serializable value"\r
+variant :: Serializable a => a -> Variant\r
+variant v = variant_ binding v\r
+\r
+"Get the value of a variant in a serializable type"\r
+variantValue :: Serializable a => Variant -> a\r
+variantValue v = variantValue_ v binding\r
+\r
+instance Show Variant where\r
+ show = showVariant\r
+\r
+"Get an element of a compound variant value using an index reference" \r
+variantElement :: Serializable a => Variant -> Integer -> a\r
+variantElement v i = variantValue (variantComponent v (indexReference i))\r
+\r
+importJava "org.simantics.databoard.accessor.reference.ChildReference" where\r
+ "A reference to a child element in a composite data type/binding or value"\r
+ data ChildReference\r
+\r
+ "Combine a list of child data object references into a single path reference"\r
+ @JavaName compile \r
+ compileReference :: [ChildReference] -> ChildReference\r
+\r
+importJava "org.simantics.databoard.accessor.reference.IndexReference" where\r
+ """Get a reference to a child data object using an index (zero-based)\r
+* Element index of an array object\r
+* Field index of a record or union type\r
+* 0:\r
+ * Key component of a map type/binding\r
+ * Component of any single-component type/binding (optional, array)\r
+ * Contained value/type of any single-element object (optional, union, variant)\r
+* 1:\r
+ * Value component of a map type/binding\r
+ """\r
+ @JavaName "<init>"\r
+ indexReference :: Integer -> ChildReference\r
+\r
+importJava "org.simantics.databoard.accessor.reference.KeyReference" where\r
+ """Get a reference to a MapType child data object using a given key value\r
+* Contained value of a map object for a given key value\r
+ """\r
+ @JavaName "<init>"\r
+ keyReference :: Variant -> ChildReference\r
+\r
+importJava "org.simantics.databoard.accessor.reference.NameReference" where\r
+ """Get a reference to a child data object using a field name\r
+* A component name of a record or union data type/binding\r
+* "key": The key component of a map data type/binding\r
+* "value": The value component of a map data type/binding\r
+ """\r
+ @JavaName "<init>"\r
+ nameReference :: String -> ChildReference\r
+ \r
+importJava "org.simantics.databoard.accessor.reference.LabelReference" where\r
+ """Get a reference to a child data object using a label\r
+* A component name of a record or union data type/binding\r
+* A string representation of the index of a record or union data type/binding component\r
+* "v": The component type of an array/optional data type/binding\r
+* "0"/"key": The key component of a map data type/binding\r
+* "1"/"value": The value component of a map data type/binding\r
+ """\r
+ @JavaName "<init>"\r
+ labelReference :: String -> ChildReference\r
+\r
+importJava "org.simantics.databoard.accessor.reference.ComponentReference" where\r
+ """Get a reference to a component child data object\r
+* Component of an array/optional data type/binding\r
+* Contained value of an optional/variant/union object\r
+ """\r
+ @JavaName "<init>"\r
+ componentReference :: ChildReference\r
/// Integer ///
+@private
+importJava "java.lang.Byte" where
+ @JavaName toString
+ showByte :: Byte -> String
+
+ @JavaName parseByte
+ readByte :: String -> Byte
+
+instance Eq Byte where
+ (==) = Java.bcmpeq
+ (!=) = Java.bcmpne
+
+instance Ord Byte where
+ (<) = Java.bcmplt
+ (<=) = Java.bcmple
+ (>) = Java.bcmpgt
+ (>=) = Java.bcmpge
+
+instance Additive Byte where
+ zero = Java.i2b Java.iconst_0
+ (+) = Java.badd
+
+instance Ring Byte where
+ neg = Java.bneg
+ (-) = Java.bsub
+ one = Java.i2b Java.iconst_1
+ (*) = Java.bmul
+ fromInteger = Java.i2b
+
+instance Show Byte where
+ show = showByte
+ precedence v = if v >= 0 then 0 else 100
+
+instance Read Byte where
+ read = readByte
+
+
@private
importJava "java.lang.Short" where
@JavaName toString
isSpecialType (TCon "Builtin" "(,,,)") = True
isSpecialType (TApply a _) = isSpecialType a
*/
-// Serializable
-
-importJava "org.simantics.databoard.serialization.Serializer" where
- data Serializer a
-
- @private
- @JavaName "serialize"
- serialize_ :: Serializer a -> a -> ByteArray
-
- @private
- @JavaName "deserialize"
- deserialize_ :: Serializer a -> ByteArray -> a
-
-importJava "org.simantics.databoard.Bindings" where
- @private
- @JavaName "getSerializer"
- serializerOf :: Binding a -> Serializer a
-
- @private
- @JavaName toString
- bindingToString :: Binding a -> String
-
- @JavaName adapt
- adapt_ :: a -> Binding a -> Binding b -> b
-
-adapt :: Serializable a => Serializable b => a -> b
-adapt x = adapt_ x binding binding
-
-instance Show (Binding a) where
- show = bindingToString
-
-"Serializes a value to a byte array."
-serialize :: Serializable a => a -> ByteArray
-serialize v = serialize_ (serializerOf binding) v
-"Deserializes a value from a byte array."
-deserialize :: Serializable a => ByteArray -> a
-deserialize ba = deserialize_ (serializerOf binding) ba
-
// ByteArray
importJava "java.util.Arrays" where
instance Show ByteArray where
show = byteArrayToString
-importJava "org.simantics.databoard.binding.mutable.Variant" where
- // data Variant (in Builtins)
- @JavaName getValue
- rawVariantValue :: Variant -> a
- @JavaName ofInstance
- variantOf :: a -> Variant\r
- @JavaName "<init>"
- variantOfWithBinding :: Binding a -> a -> Variant\r
- @JavaName getValue
- variantValue_ :: Variant -> Binding a -> a
- @JavaName toString
- showVariant :: Variant -> String
-
- @JavaName getComponent
- variantComponent :: Variant -> ChildReference -> Variant
-
-variant :: Serializable a => a -> Variant
-variant v = variantOfWithBinding binding v
-
-variantValue :: Serializable a => Variant -> a
-variantValue v = variantValue_ v binding
-
-instance Show Variant where
- show = showVariant
-
-variantElement :: Serializable a => Variant -> Integer -> a
-variantElement v i = variantValue (variantComponent v (indexReference i))
-
-importJava "org.simantics.databoard.accessor.reference.ChildReference" where
- data ChildReference
-
- @JavaName compile
- compileReference :: [ChildReference] -> ChildReference
-
-importJava "org.simantics.databoard.accessor.reference.IndexReference" where
- @JavaName "<init>"
- indexReference :: Integer -> ChildReference
-
-importJava "org.simantics.databoard.accessor.reference.KeyReference" where
- @JavaName "<init>"
- keyReference :: Variant -> ChildReference
-
-importJava "org.simantics.databoard.accessor.reference.NameReference" where
- @JavaName "<init>"
- nameReference :: String -> ChildReference
-
-importJava "org.simantics.databoard.accessor.reference.LabelReference" where
- @JavaName "<init>"
- labelReference :: String -> ChildReference
-
// Type
@private
include "ArrayList" as ArrayList
include "String" as String
include "Vector"
+include "Databoard"
include "Debug" as Debug
include "Lazy" as Lazy
include "File" as File