]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/mutable/Variant.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / mutable / Variant.java
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/mutable/Variant.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/mutable/Variant.java
new file mode 100644 (file)
index 0000000..277a08f
--- /dev/null
@@ -0,0 +1,204 @@
+/*******************************************************************************\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