--- /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;
+
+import java.util.IdentityHashMap;\r
+import java.util.Set;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.accessor.reference.ChildReference;\r
+import org.simantics.databoard.accessor.reference.IndexReference;\r
+import org.simantics.databoard.accessor.reference.LabelReference;\r
+import org.simantics.databoard.accessor.reference.NameReference;\r
+import org.simantics.databoard.adapter.AdaptException;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.binding.error.RuntimeBindingException;\r
+import org.simantics.databoard.binding.impl.BindingPrintContext;\r
+import org.simantics.databoard.type.UnionType;\r
+import org.simantics.databoard.util.IdentityPair;\r
+
+
+/**
+ * This is a binding of Union Type and a Java Object.
+ *
+ * @see UnionType
+ * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
+ */
+public abstract class UnionBinding extends Binding {
+
+ protected Binding[] componentBindings;
+
+ public UnionBinding() {}
+
+ public UnionBinding(Binding...componentBindings) {
+ this.componentBindings = componentBindings;
+ }
+
+ @Override
+ public UnionType type() {
+ return (UnionType) type;
+ }
+
+ public int getComponentCount() {
+ return type().getComponentCount();
+ }
+
+ public Binding getComponentBinding(int tagIndex) {\r
+ return componentBindings[tagIndex];\r
+ }\r
+ \r
+ public Binding getComponentBinding(String tagName) {\r
+ return componentBindings[ type().getComponentIndex2(tagName) ];\r
+ }\r
+ \r
+ public Binding[] getComponentBindings() {
+ return componentBindings;
+ }
+ \r
+ /**\r
+ * Get tag number of an instance.\r
+ * \r
+ * @param obj\r
+ * @return the tag number\r
+ * @throws BindingException is thrown if the instance is not a tag of this union\r
+ */
+ public abstract int getTag(Object obj) throws BindingException;
+
+ public abstract Object getValue(Object obj) throws BindingException;
+
+ public abstract Object create(int tag, Object value) throws BindingException;\r
+ \r
+ /**\r
+ * Create a new union object with tag of default value.\r
+ * \r
+ * @param tag\r
+ * @return new union object\r
+ * @throws BindingException\r
+ */\r
+ public Object createDefault(int tag) throws BindingException {\r
+ Binding cb = getComponentBinding(tag);\r
+ Object to = cb.createDefault();\r
+ return create(tag, to);\r
+ }
+
+ public Object create(String tag, Object value) throws BindingException {
+ Integer tagIndex = type().getComponentIndex(tag);
+ if(tagIndex == null)
+ throw new BindingException("Union type does not have a tag " + tag + ".");
+ return create(tagIndex, value);
+ }
+
+ public Object createUnchecked(int tag, Object value) throws RuntimeBindingException
+ {
+ try {
+ return create(tag, value);
+ } catch (BindingException e) {
+ throw new RuntimeBindingException(e);
+ }
+ }
+\r
+ @Override\r
+ public void readFrom(Binding srcBinding, Object src, Object dst)\r
+ throws BindingException {\r
+ UnionBinding sb = (UnionBinding) srcBinding;\r
+ int newTag = sb.getTag(src);\r
+ int oldTag = getTag(dst);\r
+ \r
+ // New value binding\r
+ Binding nvb = sb.getComponentBinding(newTag);\r
+ // New value\r
+ Object nv = sb.getValue(src);\r
+ \r
+ // Same tag\r
+ if (newTag==oldTag) {\r
+ // Same tag - old value is used if possible\r
+ \r
+ // Old value binding\r
+ Binding ovb = getComponentBinding(oldTag);\r
+ \r
+ Object ov = getValue(dst);\r
+ ov = ovb.readFromTry(nvb, nv, ov);\r
+ setValue(dst, oldTag, ov);\r
+ \r
+ } else {\r
+ // Different tag - old value is not used\r
+ boolean clone = !nvb.isImmutable();\r
+ Binding dcb = getComponentBinding(newTag);\r
+ boolean adapt = nvb!=dcb;\r
+ \r
+ if ( !adapt && !clone) {\r
+ setValue(dst, newTag, nv);\r
+ } else {\r
+ try {\r
+ // Clone or adapt value if necessary.\r
+ Object dv = Bindings.clone(nv, nvb, dcb);\r
+ setValue(dst, newTag, dv);\r
+ } catch(AdaptException e) {\r
+ throw new BindingException(e);\r
+ } \r
+ }\r
+ }\r
+ \r
+ /*\r
+ if (dcb.isImmutable() || st!=dt) {\r
+ try {\r
+ Object dv = Bindings.clone(sv, scb, dcb);\r
+ setValue(dst, st, dv);\r
+ } catch(AdaptException e) {\r
+ throw new BindingException(e);\r
+ }\r
+ } else {\r
+ Object dv = getValue(dst);\r
+ dv = dcb.readFromTry(scb, sv, dv);\r
+ setValue(dst, st, dv);\r
+ }\r
+ */\r
+ }\r
+ \r
+ /**
+ * Set value to an union.
+ * Throws BindingException if value cannot be written.
+ *
+ * @param union
+ * @param tag
+ * @param value
+ * @throws BindingException
+ */
+ public abstract void setValue(Object union, int tag, Object value) throws BindingException;\r
+ \r
+ /**\r
+ * Set to tag with default value.\r
+ * \r
+ * @param union\r
+ * @param tag\r
+ * @throws BindingException\r
+ */\r
+ public void setTag(Object union, int tag) throws BindingException {\r
+ Binding componentBinding = getComponentBinding(tag);\r
+ Object instance = componentBinding.createDefault();\r
+ setValue(union, tag, instance);\r
+ }
+
+ @Override
+ public void accept(Visitor1 v, Object obj) {
+ v.visit(this, obj);
+ }
+
+ @Override
+ public <T> T accept(Visitor<T> v) {
+ return v.visit(this);
+ }
+
+ /**
+ * Asserts the obj is valid to its UnionType.
+ *
+ * Asserts the obj using the component type
+ *
+ * @throws BindingException if obj is not valid according to the UnionType
+ */
+ @Override
+ public void assertInstaceIsValid(Object obj, Set<Object> validInstances) throws BindingException {
+ UnionType type = type();
+ int length = type.getComponentCount();
+ if (length==0) return;
+ int tag = getTag(obj);
+ if (tag<0 || tag>=length)
+ throw new BindingException("Instance tag ("+tag+") is out of range [0.."+(length-1)+"], faulty UnionBinding");
+
+ Object componentValue = getValue(obj);
+ Binding componentBinding = getComponentBindings()[tag];
+ componentBinding.assertInstaceIsValid(componentValue, validInstances);
+ }
+
+ @Override
+ public int deepHashValue(Object value, IdentityHashMap<Object, Object> hashedObjects) throws BindingException {
+ int tag = getTag(value);
+ Object element = getValue(value);
+ return tag + componentBindings[tag].deepHashValue(element, hashedObjects);
+ }
+
+ @Override
+ public int deepCompare(Object o1, Object o2,
+ Set<IdentityPair<Object, Object>> compareHistory)
+ throws BindingException {
+ Integer t1 = getTag(o1);
+ Integer t2 = getTag(o2);
+ int dif = t1.compareTo(t2);
+ if (dif!=0) return dif;
+ Object v1 = getValue(o1);
+ Object v2 = getValue(o2);
+ Binding c = getComponentBindings()[t1];
+ return c.deepCompare(v1, v2, compareHistory);
+ }
+
+ public void setComponentBindings(Binding[] componentBindings) {
+ this.componentBindings = componentBindings;
+ }
+\r
+ @Override\r
+ protected void toString(Object value, BindingPrintContext ctx) throws BindingException {\r
+ int tag = getTag(value);\r
+ ctx.b.append(type().getComponent(tag).name);\r
+ ctx.b.append(' ');\r
+ getComponentBinding(tag).toString(getValue(value), ctx);\r
+ }\r
+ \r
+ @Override\r
+ public Binding getComponentBinding(ChildReference path) {\r
+ if (path==null) return this;\r
+ if (path instanceof IndexReference) {\r
+ IndexReference ir = (IndexReference) path;\r
+ return componentBindings[ir.index].getComponentBinding(path.childReference);\r
+ }\r
+ if (path instanceof NameReference) {\r
+ NameReference nr = (NameReference) path;\r
+ return getComponentBinding( nr.name ).getComponentBinding(path.childReference);\r
+ }\r
+ if (path instanceof LabelReference) {\r
+ LabelReference lr = (LabelReference) path; \r
+ try {\r
+ Integer i = new Integer(lr.label);\r
+ return getComponentBinding( i ).getComponentBinding(path.childReference);\r
+ } catch (NumberFormatException nfe) {\r
+ return getComponentBinding( lr.label ).getComponentBinding(path.childReference);\r
+ }\r
+ }\r
+ throw new IllegalArgumentException();\r
+ } \r
+ \r
+ /**\r
+ * Returns true if the tag of this union type can be modified\r
+ * \r
+ * @return true if mutable\r
+ */\r
+ public boolean isTagMutable() {\r
+ return true;\r
+ }\r
+\r
+ @Override\r
+ protected boolean deepEquals(Object obj,\r
+ Set<IdentityPair<Binding, Binding>> compareHistory) {\r
+ if (!super.deepEquals( obj, compareHistory ))\r
+ return false;\r
+ \r
+ UnionBinding o = (UnionBinding)obj;\r
+ if (componentBindings.length != o.componentBindings.length) return false;\r
+ \r
+ for (int i = 0; i < componentBindings.length; i++)\r
+ if (!componentBindings[i].equals(o.componentBindings[i], compareHistory))\r
+ return false;\r
+ \r
+ return true;\r
+ }\r
+ \r
+ @Override\r
+ public int deepHashCode(IdentityHashMap<Object, Object> hashedObjects) {\r
+ int code = super.deepHashCode( hashedObjects );\r
+ for (int i = 0; i < componentBindings.length; i++)\r
+ code = 17 * code + componentBindings[i].hashCode(hashedObjects);\r
+ return code;\r
+ }\r
+}