-/*******************************************************************************\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
+/*******************************************************************************
+ * 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;
-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
+import java.util.IdentityHashMap;
+import java.util.Set;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.databoard.accessor.reference.ChildReference;
+import org.simantics.databoard.accessor.reference.IndexReference;
+import org.simantics.databoard.accessor.reference.LabelReference;
+import org.simantics.databoard.accessor.reference.NameReference;
+import org.simantics.databoard.adapter.AdaptException;
+import org.simantics.databoard.binding.error.BindingException;
+import org.simantics.databoard.binding.error.RuntimeBindingException;
+import org.simantics.databoard.binding.impl.BindingPrintContext;
+import org.simantics.databoard.type.UnionType;
+import org.simantics.databoard.util.IdentityPair;
/**
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 getComponentBinding(int tagIndex) {
+ return componentBindings[tagIndex];
+ }
+
+ public Binding getComponentBinding(String tagName) {
+ return componentBindings[ type().getComponentIndex2(tagName) ];
+ }
+
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
+
+ /**
+ * Get tag number of an instance.
+ *
+ * @param obj
+ * @return the tag number
+ * @throws BindingException is thrown if the instance is not a tag of this union
*/
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 abstract Object create(int tag, Object value) throws BindingException;
+
+ /**
+ * Create a new union object with tag of default value.
+ *
+ * @param tag
+ * @return new union object
+ * @throws BindingException
+ */
+ public Object createDefault(int tag) throws BindingException {
+ Binding cb = getComponentBinding(tag);
+ Object to = cb.createDefault();
+ return create(tag, to);
}
public Object create(String tag, Object value) throws BindingException {
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
+
+ @Override
+ public void readFrom(Binding srcBinding, Object src, Object dst)
+ throws BindingException {
+ UnionBinding sb = (UnionBinding) srcBinding;
+ int newTag = sb.getTag(src);
+ int oldTag = getTag(dst);
+
+ // New value binding
+ Binding nvb = sb.getComponentBinding(newTag);
+ // New value
+ Object nv = sb.getValue(src);
+
+ // Same tag
+ if (newTag==oldTag) {
+ // Same tag - old value is used if possible
+
+ // Old value binding
+ Binding ovb = getComponentBinding(oldTag);
+
+ Object ov = getValue(dst);
+ ov = ovb.readFromTry(nvb, nv, ov);
+ setValue(dst, oldTag, ov);
+
+ } else {
+ // Different tag - old value is not used
+ boolean clone = !nvb.isImmutable();
+ Binding dcb = getComponentBinding(newTag);
+ boolean adapt = nvb!=dcb;
+
+ if ( !adapt && !clone) {
+ setValue(dst, newTag, nv);
+ } else {
+ try {
+ // Clone or adapt value if necessary.
+ Object dv = Bindings.clone(nv, nvb, dcb);
+ setValue(dst, newTag, dv);
+ } catch(AdaptException e) {
+ throw new BindingException(e);
+ }
+ }
+ }
+
+ /*
+ if (dcb.isImmutable() || st!=dt) {
+ try {
+ Object dv = Bindings.clone(sv, scb, dcb);
+ setValue(dst, st, dv);
+ } catch(AdaptException e) {
+ throw new BindingException(e);
+ }
+ } else {
+ Object dv = getValue(dst);
+ dv = dcb.readFromTry(scb, sv, dv);
+ setValue(dst, st, dv);
+ }
+ */
+ }
+
/**
* Set value to an union.
* Throws BindingException if value cannot be written.
* @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
+ public abstract void setValue(Object union, int tag, Object value) throws BindingException;
+
+ /**
+ * Set to tag with default value.
+ *
+ * @param union
+ * @param tag
+ * @throws BindingException
+ */
+ public void setTag(Object union, int tag) throws BindingException {
+ Binding componentBinding = getComponentBinding(tag);
+ Object instance = componentBinding.createDefault();
+ setValue(union, tag, instance);
}
@Override
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
+
+ @Override
+ protected void toString(Object value, BindingPrintContext ctx) throws BindingException {
+ int tag = getTag(value);
+ ctx.b.append(type().getComponent(tag).name);
+ ctx.b.append(' ');
+ getComponentBinding(tag).toString(getValue(value), ctx);
+ }
+
+ @Override
+ public Binding getComponentBinding(ChildReference path) {
+ if (path==null) return this;
+ if (path instanceof IndexReference) {
+ IndexReference ir = (IndexReference) path;
+ return componentBindings[ir.index].getComponentBinding(path.childReference);
+ }
+ if (path instanceof NameReference) {
+ NameReference nr = (NameReference) path;
+ return getComponentBinding( nr.name ).getComponentBinding(path.childReference);
+ }
+ if (path instanceof LabelReference) {
+ LabelReference lr = (LabelReference) path;
+ try {
+ Integer i = new Integer(lr.label);
+ return getComponentBinding( i ).getComponentBinding(path.childReference);
+ } catch (NumberFormatException nfe) {
+ return getComponentBinding( lr.label ).getComponentBinding(path.childReference);
+ }
+ }
+ throw new IllegalArgumentException();
+ }
+
+ /**
+ * Returns true if the tag of this union type can be modified
+ *
+ * @return true if mutable
+ */
+ public boolean isTagMutable() {
+ return true;
+ }
+
+ @Override
+ protected boolean deepEquals(Object obj,
+ Set<IdentityPair<Binding, Binding>> compareHistory) {
+ if (!super.deepEquals( obj, compareHistory ))
+ return false;
+
+ UnionBinding o = (UnionBinding)obj;
+ if (componentBindings.length != o.componentBindings.length) return false;
+
+ for (int i = 0; i < componentBindings.length; i++)
+ if (!componentBindings[i].equals(o.componentBindings[i], compareHistory))
+ return false;
+
+ return true;
+ }
+
+ @Override
+ public int deepHashCode(IdentityHashMap<Object, Object> hashedObjects) {
+ int code = super.deepHashCode( hashedObjects );
+ for (int i = 0; i < componentBindings.length; i++)
+ code = 17 * code + componentBindings[i].hashCode(hashedObjects);
+ return code;
+ }
}