+/*******************************************************************************\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.KeyReference;\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.OptionalType;\r
+import org.simantics.databoard.util.IdentityPair;\r
+
+
+/**
+ * This is a binding of Optional Type and a Java Object.
+ *
+ * @see OptionalType
+ * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
+ */
+public abstract class OptionalBinding extends Binding {
+
+ public Binding componentBinding;
+
+ public OptionalBinding(OptionalType type, Binding componentBinding) {
+ this.componentBinding = componentBinding;
+ this.type = type;
+ }
+
+ public OptionalBinding(Binding componentBinding) {
+ this.componentBinding = componentBinding;
+ this.type = new OptionalType(componentBinding.type());
+ }
+
+ @Override
+ public OptionalType type() {
+ return (OptionalType) type;
+ }
+
+ /**
+ * Create result with no value
+ *
+ * @return no value
+ */
+ public abstract Object createNoValue() throws BindingException;
+
+ /**
+ * Create result with a value
+ *
+ * @param value
+ * @return argument that contains a value
+ */
+ public abstract Object createValue(Object value) throws BindingException;
+
+
+ /**
+ * Tests whether arg contains a value
+ *
+ * @param arg
+ * @return true if arg contained a value
+ */
+ public abstract boolean hasValue(Object arg) throws BindingException;
+
+ /**
+ * Get the non-null value, the arg did not contain a value,
+ * BindingException is thrown.
+ *
+ * @param arg argument that contains a value
+ * @return the composite value
+ * @throws BindingException
+ */
+ public abstract Object getValue(Object arg) throws BindingException;
+
+ public abstract void setValue(Object optional, Object componentValue) throws BindingException;
+
+ public abstract void setNoValue(Object optional) throws BindingException;
+
+ public abstract boolean isInstance(Object obj);
+
+ public Binding getComponentBinding() {
+ return componentBinding;
+ }
+ \r
+ @Override\r
+ public void readFrom(Binding srcBinding, Object src, Object dst)\r
+ throws BindingException {\r
+ OptionalBinding sb = (OptionalBinding) srcBinding;\r
+ if (sb.hasValue(src)) {\r
+ Binding dcb = getComponentBinding();\r
+ Binding scb = sb.getComponentBinding();\r
+ Object scv = sb.getValue(src);\r
+ if (dcb.isImmutable() || !hasValue(dst)) {\r
+ try {\r
+ Object v = Bindings.clone(scv, scb, dcb);\r
+ v = dcb.readFromTry(scb, scv, v);\r
+ setValue(dst, v);\r
+ } catch (AdaptException e) {\r
+ throw new BindingException(e);\r
+ }\r
+ } else {\r
+ Object v = getValue(src);\r
+ v = dcb.readFromTry(scb, scv, v);\r
+ setValue(dst, v);\r
+ }\r
+ } else {\r
+ setNoValue(dst);\r
+ }\r
+ }\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);
+ }
+
+ /**
+ * Assert tje obj is a correct instance.
+ * Assert the obj is valid also according to the component binding.
+ */
+ @Override
+ public void assertInstaceIsValid(Object obj, Set<Object> validInstances) throws BindingException {
+ if (!hasValue(obj)) return;
+ Object componentValue = getValue(obj);
+ componentBinding.assertInstaceIsValid(componentValue, validInstances);
+ }
+
+ @Override
+ public int deepHashValue(Object value, IdentityHashMap<Object, Object> hashedObjects) throws BindingException {
+ return hasValue(value) ? componentBinding.deepHashValue( getValue(value), hashedObjects ) : 0;
+ }
+
+ @Override
+ public int deepCompare(Object o1, Object o2, Set<IdentityPair<Object, Object>> compareHistory) throws BindingException \r
+ {
+ Boolean h1 = hasValue(o1);
+ Boolean h2 = hasValue(o2);
+ if (!h1 && !h2) return 0;
+ int dif = h1.compareTo(h2);
+ if (dif!=0) return dif;
+
+ Binding c = getComponentBinding();
+ Object v1 = getValue(o1);
+ Object v2 = getValue(o2);
+ return c.deepCompare(v1, v2, compareHistory);
+ }
+
+ public Object createNoValueUnchecked() throws RuntimeBindingException {
+ try {
+ return createNoValue();
+ } catch (BindingException e) {
+ throw new RuntimeBindingException(e);
+ }
+ }
+
+ public Object createValueUnchecked(Object value)
+ throws RuntimeBindingException {
+ try {
+ return createValue(value);
+ } catch (BindingException e) {
+ throw new RuntimeBindingException(e);
+ }
+ }
+
+ public boolean hasValueUnchecked(Object arg) throws RuntimeBindingException {
+ try {
+ return hasValue(arg);
+ } catch (BindingException e) {
+ throw new RuntimeBindingException(e);
+ }
+ }
+\r
+ @Override\r
+ protected void toString(Object value, BindingPrintContext ctx) throws BindingException {\r
+ if(hasValue(value)) {\r
+ getComponentBinding().toString(getValue(value), ctx);\r
+ } else {\r
+ ctx.b.append("null");\r
+ }\r
+ }\r
+ \r
+ @Override\r
+ public Binding getComponentBinding(ChildReference path) {\r
+ if (path==null) return this;\r
+ if (path instanceof KeyReference) throw new IllegalArgumentException("KeyReference is not supported in OptionalType"); \r
+ if (path instanceof NameReference) throw new IllegalArgumentException("NameReference is not supported in OptionalType"); \r
+ if (path instanceof IndexReference && ((IndexReference) path).index!=0) throw new IllegalArgumentException("Index out of bounds");\r
+ if (path instanceof LabelReference && !((LabelReference) path).label.equals("v")) throw new IllegalArgumentException("Unknown label");\r
+ return componentBinding.getComponentBinding(path.childReference);\r
+ } \r
+ \r
+ @Override\r
+ public int getComponentCount() {\r
+ return 1;\r
+ }\r
+ \r
+ @Override\r
+ public Binding getComponentBinding(int index) {\r
+ if (index==0) return componentBinding;\r
+ throw new IllegalArgumentException();\r
+ }\r
+\r
+ @Override\r
+ protected boolean deepEquals(Object obj,\r
+ Set<IdentityPair<Binding, Binding>> compareHistory) {\r
+ return super.deepEquals( obj, compareHistory ) && componentBinding.equals(((OptionalBinding) obj).componentBinding, compareHistory);\r
+ }\r
+ \r
+ @Override\r
+ public int deepHashCode(IdentityHashMap<Object, Object> hashedObjects) {\r
+ return super.deepHashCode( hashedObjects ) + componentBinding.hashCode(hashedObjects);\r
+ }
+}