--- /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.Datatypes;\r
+import org.simantics.databoard.accessor.reference.ChildReference;\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.binding.impl.ObjectVariantBinding;\r
+import org.simantics.databoard.binding.impl.StringVariantBinding;\r
+import org.simantics.databoard.binding.mutable.ImmutableVariantBinding;\r
+import org.simantics.databoard.binding.mutable.MutableVariant;\r
+import org.simantics.databoard.binding.mutable.MutableVariantBinding;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.databoard.type.VariantType;\r
+import org.simantics.databoard.util.DataValueUtil;\r
+import org.simantics.databoard.util.IdentityPair;\r
+
+/**
+ * This is the abstract base class for bindings of VariantType Java Objects.\r
+ * Variant is a container that has value of any Datatype.
+ *
+ * @see VariantType The Datatype\r
+ * @see ImmutableVariantBinding Binds variant to {@link Variant}\r
+ * @see MutableVariantBinding Binds variant to {@link MutableVariant}\r
+ * @see ObjectVariantBinding Binds variant to java.lang.Object\r
+ * @see StringVariantBinding Binds variant to java.lang.String (Filename and URL compatible)
+ * @author Toni Kalajainen
+ */
+public abstract class VariantBinding extends Binding {
+
+ public VariantBinding()
+ {
+ super();
+ this.type = Datatypes.VARIANT;
+ }
+
+ /**
+ * Get the value in the variant. The returned value may represent internal
+ * value of <code>variant</code>.
+ *
+ * @param variant the variant object to read the content from
+ * @param contentBinding the format of return value
+ * @return value variant's content
+ * @throws BindingException
+ */
+ public abstract Object getContent(Object variant, Binding contentBinding)
+ throws BindingException;
+
+ /**
+ * Get the value of the variant. The value is bound with the suggested\r
+ * binding, see {@link #getContentBinding(Object)}.
+ *
+ * @param variant the variant object
+ * @return value variant's content
+ * @throws BindingException
+ */
+ public abstract Object getContent(Object variant)
+ throws BindingException;
+ \r
+ /**\r
+ * Get the data type of the content.\r
+ * \r
+ * @param variant the variant object\r
+ * @return the data type\r
+ * @throws BindingException\r
+ */
+ public abstract Datatype getContentType(Object variant)
+ throws BindingException;
+ \r
+ /**\r
+ * Return a suggestion for the binding of the content of this variant.\r
+ * \r
+ * @param variant variant object\r
+ * @return binding a binding\r
+ */\r
+ public abstract Binding getContentBinding(Object variant)\r
+ throws BindingException;\r
+
+ /**
+ * Create a new variant object. \r
+ * The <code>value</code> argument may be included in the result.
+ *
+ * @param contentBinding the binding of the content\r
+ * @param content content
+ * @return new variant
+ * @throws BindingException
+ */
+ public abstract Object create(Binding contentBinding, Object content)
+ throws BindingException;
+ \r
+ /**\r
+ * Create a new variant object. \r
+ * \r
+ * @param contentBinding binding of the content\r
+ * @param content content \r
+ * @return new variant\r
+ * @throws RuntimeBindingException\r
+ */
+ public Object createUnchecked(Binding contentBinding, Object content)
+ throws RuntimeBindingException {
+ try {
+ return create(contentBinding, content);
+ } catch (BindingException e) {
+ throw new RuntimeBindingException(e);
+ }
+ }\r
+ \r
+ @Override\r
+ public void readFrom(Binding srcBinding, Object src, Object dst)\r
+ throws BindingException {\r
+ try {\r
+ VariantBinding sb = (VariantBinding) srcBinding;\r
+ Datatype newType = sb.getContentType(src);\r
+ Datatype oldType = getContentType(dst);\r
+ Binding scb = sb.getContentBinding(src);\r
+ Object sc = sb.getContent(src, scb);\r
+ if (newType.equals(oldType)) {\r
+ Binding dcb = getContentBinding(dst);\r
+ if (dcb.isImmutable()) {\r
+ Object dc = Bindings.clone(sc, scb, scb);\r
+ setContent(dst, scb, dc);\r
+ } else {\r
+ Object dc = getContent(dst, dcb);\r
+ dc = dcb.readFromTry(scb, sc, dc);\r
+ setContent(dst, dcb, dc);\r
+ }\r
+ } else {\r
+ Object dc = Bindings.clone(sc, scb, scb);\r
+ setContent(dst, scb, dc);\r
+ }\r
+ } catch (AdaptException e) {\r
+ throw new BindingException(e);\r
+ } \r
+ \r
+ }
+
+ /**
+ * Set the content of an variant.
+ *
+ * @param variant variant object
+ * @param contentBinding content's binding\r
+ * @param content new content
+ * @throws BindingException
+ */
+ public abstract void setContent(Object variant, Binding contentBinding, Object content)
+ throws BindingException;
+
+ @Override
+ public void accept(Visitor1 v, Object obj) {
+ v.visit(this, obj);
+ }
+
+ @Override
+ public <T> T accept(Visitor<T> v) {
+ return v.visit(this);
+ }
+
+ @Override
+ public int deepHashValue(Object value, IdentityHashMap<Object, Object> hashedObjects) throws BindingException {
+ Datatype type = getContentType(value);
+ Binding binding = getContentBinding(value);
+ Binding dataTypeBinding = Bindings.getBindingUnchecked(Datatype.class);
+ Object element = getContent(value, binding);
+ return dataTypeBinding.deepHashValue(type, hashedObjects) + binding.deepHashValue(element, hashedObjects);
+ }
+
+ @Override
+ public int deepCompare(Object o1, Object o2,
+ Set<IdentityPair<Object, Object>> compareHistory)
+ throws BindingException {
+ // Compare Type
+ Datatype t1 = getContentType(o1);
+ Datatype t2 = getContentType(o2);
+ Binding dataTypeBinding = Bindings.getBindingUnchecked(Datatype.class);
+ int dif = dataTypeBinding.compare(t1, t2);
+ if (dif!=0) return dif;
+ // Compare Value
+ Binding bi1 = getContentBinding(o1);
+ Binding bi2 = getContentBinding(o2);
+ Object va1 = getContent(o1, bi1);
+ Object va2 = getContent(o2, bi2);
+ return DataValueUtil.compare(bi1, va1, bi2, va2);
+ }
+\r
+ @Override\r
+ protected void toString(Object value, BindingPrintContext ctx) throws BindingException {\r
+ Binding b = getContentBinding(value);\r
+ b.toString(getContent(value), ctx);\r
+ \r
+ ctx.b.append(" : ");\r
+ ctx.b.append( ctx.singleLine ? b.type.toSingleLineString() : b.type.toString() );\r
+ }\r
+ \r
+ @Override\r
+ public Binding getComponentBinding(ChildReference path) {\r
+ if (path==null) return this; \r
+ throw new IllegalArgumentException();\r
+ } \r
+ \r
+ @Override\r
+ public int getComponentCount() {\r
+ return 0;\r
+ }\r
+ \r
+ @Override\r
+ public Binding getComponentBinding(int index) {\r
+ throw new IllegalArgumentException();\r
+ }\r
+
+}
+