-/*******************************************************************************\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.mutable;\r
-\r
-import java.io.IOException;\r
-\r
-import org.simantics.databoard.Accessors;\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.accessor.VariantAccessor;\r
-import org.simantics.databoard.accessor.error.AccessorConstructionException;\r
-import org.simantics.databoard.accessor.java.JavaObject;\r
-import org.simantics.databoard.accessor.reference.ChildReference;\r
-import org.simantics.databoard.adapter.AdaptException;\r
-import org.simantics.databoard.binding.Binding;\r
-import org.simantics.databoard.binding.error.BindingException;\r
-import org.simantics.databoard.binding.error.RuntimeBindingException;\r
-import org.simantics.databoard.binding.reflection.VoidBinding;\r
-import org.simantics.databoard.parser.DataValuePrinter;\r
-import org.simantics.databoard.parser.PrintFormat;\r
-import org.simantics.databoard.parser.repository.DataValueRepository;\r
-import org.simantics.databoard.parser.unparsing.DataTypePrinter;\r
-import org.simantics.databoard.type.Datatype;\r
-\r
-/**\r
- * Variant is a container to a data value of any type. \r
- * This very class is immutable, the value and type cannot be changed, but the \r
- * sub-class {@link MutableVariant} is not. <p>\r
- *\r
- * Variant is hash-equals-comparable, even variants of bindings (and types). \r
- * The hash function and comparison rules are defined in the manual. <p>\r
- *\r
- * @see ImmutableVariantBinding is binding for Variant-class\r
- * @author Toni Kalajainen <toni.kalajainen@vtt.fi>\r
- */\r
-public class Variant implements Comparable<Variant>, Cloneable {\r
- \r
- public static Variant ofInstance(Object instance) {\r
- Binding binding = Bindings.getBindingUnchecked( instance.getClass() );\r
- return new Variant(binding, instance);\r
- }\r
- \r
- Binding binding;\r
- Object value;\r
- \r
- /**\r
- * Constract a variant with a default value of empty record {}\r
- */\r
- public Variant() {\r
- binding = VoidBinding.VOID_BINDING;\r
- value = null;\r
- }\r
-\r
- public Variant(Variant v) {\r
- //assert(isValid(binding, value));\r
- this.binding = v.getBinding();\r
- this.value = v.getValue();\r
- } \r
- \r
- public Variant(Binding binding, Object value) {\r
- //assert(isValid(binding, value));\r
- this.binding = binding;\r
- this.value = value;\r
- }\r
- \r
- public Object getValue() {\r
- return value;\r
- }\r
-\r
- /**\r
- * Get and if necessary and possible, type-adapt the value.\r
- * \r
- * @param binding\r
- * @return the value in given binding\r
- * @throws AdaptException\r
- */\r
- public Object getValue(Binding binding) throws AdaptException {\r
- return Bindings.adapt(value, this.binding, binding);\r
- }\r
-\r
- public VariantAccessor getAccessor() {\r
- try {\r
- return (VariantAccessor) Accessors.getAccessor( Bindings.VARIANT, this);\r
- } catch (AccessorConstructionException e) {\r
- // Unexpected\r
- throw new RuntimeException(e);\r
- }\r
- }\r
- \r
- public Datatype type() {\r
- return binding.type();\r
- }\r
- \r
- public Binding getBinding() {\r
- return binding;\r
- }\r
- \r
- @Override\r
- public int hashCode() {\r
- try {\r
- return binding.hashValue(value);\r
- } catch (BindingException e) {\r
- throw new RuntimeBindingException(e);\r
- }\r
- }\r
- \r
- @Override\r
- public boolean equals(Object obj) {\r
- if(this == obj)\r
- return true;\r
- if(obj == null)\r
- return false;\r
- if(!(obj instanceof Variant))\r
- return false;\r
- try {\r
- Variant o = (Variant) obj;\r
- if (o.binding==binding && o.value == value) return true;\r
- return Bindings.equals(binding, value, o.binding, o.value);\r
- } catch (BindingException e) {\r
- throw new RuntimeBindingException(e);\r
- }\r
- }\r
- \r
- public boolean valueEquals(Binding binding, Object obj)\r
- {\r
- if (this.binding == binding) return binding.equals(obj, this.value);\r
- Object adapted;\r
- try {\r
- adapted = Bindings.adapt(obj, binding, this.binding);\r
- return this.binding.equals(adapted, this.value);\r
- } catch (AdaptException e) {\r
- return false;\r
- }\r
- }\r
- \r
- @Override\r
- public int compareTo(Variant o) {\r
- try {\r
- if (o.binding==binding && o.value == value) return 0;\r
- return Bindings.compare(binding, value, o.binding, o.value);\r
- } catch (BindingException e) {\r
- throw new RuntimeBindingException(e);\r
- }\r
- }\r
- \r
- @Override\r
- public String toString() {\r
- try {\r
- StringBuilder sb = new StringBuilder(); \r
- DataValueRepository repo = new DataValueRepository();\r
- DataValuePrinter printer = new DataValuePrinter(sb, repo);\r
- printer.setFormat(PrintFormat.SINGLE_LINE);\r
- printer.print(binding, value);\r
- sb.append(" : ");\r
- DataTypePrinter printer2 = new DataTypePrinter(sb);\r
- printer2.print(binding.type());\r
- return sb.toString();\r
- } catch (IOException e) {\r
- throw new RuntimeException(e);\r
- } catch (BindingException e) {\r
- throw new RuntimeBindingException(e);\r
- }\r
- }\r
- \r
- public Variant clone() {\r
- if (binding.isImmutable()) return new Variant(binding, value);\r
- Object newValue = Bindings.cloneUnchecked(value, binding, binding);\r
- return new Variant(binding, newValue);\r
- }\r
-\r
- boolean isValid(Binding binding, Object obj) throws RuntimeBindingException {\r
- try {\r
- binding.assertInstaceIsValid(obj);\r
- return true;\r
- } catch (BindingException e) {\r
- throw new RuntimeBindingException(e);\r
- }\r
- }\r
-\r
- \r
- /**\r
- * Open a variant to a component object.\r
- * \r
- * @param ref child reference, if null then this object is returned\r
- * @return variant to this or new variant that refers to actual child object\r
- * @throws AccessorConstructionException \r
- */\r
- public Variant getComponent(ChildReference ref) throws AccessorConstructionException {\r
- if ( ref == null ) return this;\r
- JavaObject jo = (JavaObject) Accessors.getAccessor(this, ref);\r
- return new Variant( jo.getBinding(), jo.getObject() );\r
- }\r
- \r
-}\r
-\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.mutable;
+
+import java.io.IOException;
+
+import org.simantics.databoard.Accessors;
+import org.simantics.databoard.Bindings;
+import org.simantics.databoard.accessor.VariantAccessor;
+import org.simantics.databoard.accessor.error.AccessorConstructionException;
+import org.simantics.databoard.accessor.java.JavaObject;
+import org.simantics.databoard.accessor.reference.ChildReference;
+import org.simantics.databoard.adapter.AdaptException;
+import org.simantics.databoard.binding.Binding;
+import org.simantics.databoard.binding.error.BindingException;
+import org.simantics.databoard.binding.error.RuntimeBindingException;
+import org.simantics.databoard.binding.reflection.VoidBinding;
+import org.simantics.databoard.parser.DataValuePrinter;
+import org.simantics.databoard.parser.PrintFormat;
+import org.simantics.databoard.parser.repository.DataValueRepository;
+import org.simantics.databoard.parser.unparsing.DataTypePrinter;
+import org.simantics.databoard.type.Datatype;
+
+/**
+ * Variant is a container to a data value of any type.
+ * This very class is immutable, the value and type cannot be changed, but the
+ * sub-class {@link MutableVariant} is not. <p>
+ *
+ * Variant is hash-equals-comparable, even variants of bindings (and types).
+ * The hash function and comparison rules are defined in the manual. <p>
+ *
+ * @see ImmutableVariantBinding is binding for Variant-class
+ * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
+ */
+public class Variant implements Comparable<Variant>, Cloneable {
+
+ public static Variant ofInstance(Object instance) {
+ Binding binding = Bindings.getBindingUnchecked( instance.getClass() );
+ return new Variant(binding, instance);
+ }
+
+ Binding binding;
+ Object value;
+
+ /**
+ * Constract a variant with a default value of empty record {}
+ */
+ public Variant() {
+ binding = VoidBinding.VOID_BINDING;
+ value = null;
+ }
+
+ public Variant(Variant v) {
+ //assert(isValid(binding, value));
+ this.binding = v.getBinding();
+ this.value = v.getValue();
+ }
+
+ public Variant(Binding binding, Object value) {
+ //assert(isValid(binding, value));
+ this.binding = binding;
+ this.value = value;
+ }
+
+ public Object getValue() {
+ return value;
+ }
+
+ /**
+ * Get and if necessary and possible, type-adapt the value.
+ *
+ * @param binding
+ * @return the value in given binding
+ * @throws AdaptException
+ */
+ public Object getValue(Binding binding) throws AdaptException {
+ return Bindings.adapt(value, this.binding, binding);
+ }
+
+ public VariantAccessor getAccessor() {
+ try {
+ return (VariantAccessor) Accessors.getAccessor( Bindings.VARIANT, this);
+ } catch (AccessorConstructionException e) {
+ // Unexpected
+ throw new RuntimeException(e);
+ }
+ }
+
+ public Datatype type() {
+ return binding.type();
+ }
+
+ public Binding getBinding() {
+ return binding;
+ }
+
+ @Override
+ public int hashCode() {
+ try {
+ return binding.hashValue(value);
+ } catch (BindingException e) {
+ throw new RuntimeBindingException(e);
+ }
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if(this == obj)
+ return true;
+ if(obj == null)
+ return false;
+ if(!(obj instanceof Variant))
+ return false;
+ try {
+ Variant o = (Variant) obj;
+ if (o.binding==binding && o.value == value) return true;
+ return Bindings.equals(binding, value, o.binding, o.value);
+ } catch (BindingException e) {
+ throw new RuntimeBindingException(e);
+ }
+ }
+
+ public boolean valueEquals(Binding binding, Object obj)
+ {
+ if (this.binding == binding) return binding.equals(obj, this.value);
+ Object adapted;
+ try {
+ adapted = Bindings.adapt(obj, binding, this.binding);
+ return this.binding.equals(adapted, this.value);
+ } catch (AdaptException e) {
+ return false;
+ }
+ }
+
+ @Override
+ public int compareTo(Variant o) {
+ try {
+ if (o.binding==binding && o.value == value) return 0;
+ return Bindings.compare(binding, value, o.binding, o.value);
+ } catch (BindingException e) {
+ throw new RuntimeBindingException(e);
+ }
+ }
+
+ @Override
+ public String toString() {
+ try {
+ StringBuilder sb = new StringBuilder();
+ DataValueRepository repo = new DataValueRepository();
+ DataValuePrinter printer = new DataValuePrinter(sb, repo);
+ printer.setFormat(PrintFormat.SINGLE_LINE);
+ printer.print(binding, value);
+ sb.append(" : ");
+ DataTypePrinter printer2 = new DataTypePrinter(sb);
+ printer2.print(binding.type());
+ return sb.toString();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ } catch (BindingException e) {
+ throw new RuntimeBindingException(e);
+ }
+ }
+
+ public Variant clone() {
+ if (binding.isImmutable()) return new Variant(binding, value);
+ Object newValue = Bindings.cloneUnchecked(value, binding, binding);
+ return new Variant(binding, newValue);
+ }
+
+ boolean isValid(Binding binding, Object obj) throws RuntimeBindingException {
+ try {
+ binding.assertInstaceIsValid(obj);
+ return true;
+ } catch (BindingException e) {
+ throw new RuntimeBindingException(e);
+ }
+ }
+
+
+ /**
+ * Open a variant to a component object.
+ *
+ * @param ref child reference, if null then this object is returned
+ * @return variant to this or new variant that refers to actual child object
+ * @throws AccessorConstructionException
+ */
+ public Variant getComponent(ChildReference ref) throws AccessorConstructionException {
+ if ( ref == null ) return this;
+ JavaObject jo = (JavaObject) Accessors.getAccessor(this, ref);
+ return new Variant( jo.getBinding(), jo.getObject() );
+ }
+
+}
+
+