]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/VariantBinding.java
Improve Databoard's dynamically typed data capabilities.
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / VariantBinding.java
1 /*******************************************************************************\r
2  *  Copyright (c) 2010 Association for Decentralized Information Management in\r
3  *  Industry THTH ry.\r
4  *  All rights reserved. This program and the accompanying materials\r
5  *  are made available under the terms of the Eclipse Public License v1.0\r
6  *  which accompanies this distribution, and is available at\r
7  *  http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  *  Contributors:\r
10  *      VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.databoard.binding;
13
14 import java.util.IdentityHashMap;\r
15 import java.util.Set;\r
16 \r
17 import org.simantics.databoard.Bindings;\r
18 import org.simantics.databoard.Datatypes;\r
19 import org.simantics.databoard.accessor.reference.ChildReference;\r
20 import org.simantics.databoard.adapter.AdaptException;\r
21 import org.simantics.databoard.binding.error.BindingException;\r
22 import org.simantics.databoard.binding.error.RuntimeBindingException;\r
23 import org.simantics.databoard.binding.impl.BindingPrintContext;\r
24 import org.simantics.databoard.binding.impl.ObjectVariantBinding;\r
25 import org.simantics.databoard.binding.impl.StringVariantBinding;\r
26 import org.simantics.databoard.binding.mutable.ImmutableVariantBinding;\r
27 import org.simantics.databoard.binding.mutable.MutableVariant;\r
28 import org.simantics.databoard.binding.mutable.MutableVariantBinding;\r
29 import org.simantics.databoard.binding.mutable.Variant;\r
30 import org.simantics.databoard.type.Datatype;\r
31 import org.simantics.databoard.type.VariantType;\r
32 import org.simantics.databoard.util.DataValueUtil;\r
33 import org.simantics.databoard.util.IdentityPair;\r
34
35 /**
36  * This is the abstract base class for bindings of VariantType Java Objects.\r
37  * Variant is a container that has value of any Datatype. 
38  * 
39  * @see VariantType The Datatype\r
40  * @see ImmutableVariantBinding Binds variant to {@link Variant}\r
41  * @see MutableVariantBinding Binds variant to {@link MutableVariant}\r
42  * @see ObjectVariantBinding Binds variant to java.lang.Object\r
43  * @see StringVariantBinding Binds variant to java.lang.String (Filename and URL compatible) 
44  * @author Toni Kalajainen
45  */
46 public abstract class VariantBinding extends Binding {
47         
48         public VariantBinding()
49         {
50                 super();
51                 this.type = Datatypes.VARIANT;
52         }
53
54         /**
55          * Get the value in the variant. The returned value may represent internal
56          * value of <code>variant</code>.  
57          * 
58          * @param variant the variant object to read the content from
59          * @param contentBinding the format of return value
60          * @return value variant's content
61          * @throws BindingException
62          */
63         public abstract Object getContent(Object variant, Binding contentBinding)
64         throws BindingException;
65
66         /**
67          * Get the value of the variant. The value is bound with the suggested\r
68          * binding, see {@link #getContentBinding(Object)}. 
69          * 
70          * @param variant the variant object
71          * @return value variant's content
72          * @throws BindingException
73          */
74         public abstract Object getContent(Object variant)
75         throws BindingException;
76         \r
77         /**\r
78          * Get the data type of the content.\r
79          * \r
80          * @param variant the variant object\r
81          * @return the data type\r
82          * @throws BindingException\r
83          */
84         public abstract Datatype getContentType(Object variant)
85         throws BindingException;
86         \r
87         /**\r
88          * Return a suggestion for the binding of the content of this variant.\r
89          * \r
90          * @param variant variant object\r
91          * @return binding a binding\r
92          */\r
93         public abstract Binding getContentBinding(Object variant)\r
94         throws BindingException;\r
95         
96         /**
97          * Create a new variant object. \r
98          * The <code>value</code> argument may be included in the result. 
99          * 
100          * @param contentBinding the binding of the content\r
101          * @param content content
102          * @return new variant
103          * @throws BindingException
104          */
105         public abstract Object create(Binding contentBinding, Object content)
106         throws BindingException;
107         \r
108         /**\r
109          * Create a new variant object. \r
110          * \r
111          * @param contentBinding binding of the content\r
112          * @param content content \r
113          * @return new variant\r
114          * @throws RuntimeBindingException\r
115          */
116         public Object createUnchecked(Binding contentBinding, Object content)
117         throws RuntimeBindingException {
118                 try {
119                         return create(contentBinding, content);
120                 } catch (BindingException e) {
121                         throw new RuntimeBindingException(e);
122                 }
123         }\r
124         \r
125         @Override\r
126         public void readFrom(Binding srcBinding, Object src, Object dst)\r
127                         throws BindingException {\r
128                 try {\r
129                         VariantBinding sb = (VariantBinding) srcBinding;\r
130                         Datatype newType = sb.getContentType(src);\r
131                         Datatype oldType = getContentType(dst);\r
132                         Binding scb = sb.getContentBinding(src);\r
133                         Object sc = sb.getContent(src, scb);\r
134                         if (newType.equals(oldType)) {\r
135                                 Binding dcb = getContentBinding(dst);\r
136                                 if (dcb.isImmutable()) {\r
137                                         Object dc = Bindings.clone(sc, scb, scb);\r
138                                         setContent(dst, scb, dc);\r
139                                 } else {\r
140                                         Object dc = getContent(dst, dcb);\r
141                                         dc = dcb.readFromTry(scb, sc, dc);\r
142                                         setContent(dst, dcb, dc);\r
143                                 }\r
144                         } else {\r
145                                 Object dc = Bindings.clone(sc, scb, scb);\r
146                                 setContent(dst, scb, dc);\r
147                         }\r
148                 } catch (AdaptException e) {\r
149                         throw new BindingException(e);\r
150                 }                               \r
151                 \r
152         }
153         
154         /**
155          * Set the content of an variant.
156          * 
157          * @param variant variant object
158          * @param contentBinding content's binding\r
159          * @param content new content
160          * @throws BindingException
161          */
162         public abstract void setContent(Object variant, Binding contentBinding, Object content)
163         throws BindingException;        
164                 
165     @Override
166     public void accept(Visitor1 v, Object obj) {
167         v.visit(this, obj);        
168     }
169     
170     @Override
171     public <T> T accept(Visitor<T> v) {
172         return v.visit(this);
173     }
174
175     @Override
176     public int deepHashValue(Object value, IdentityHashMap<Object, Object> hashedObjects) throws BindingException {
177                 Datatype type = getContentType(value);          
178                 Binding binding = getContentBinding(value);
179                 Binding dataTypeBinding = Bindings.getBindingUnchecked(Datatype.class);
180         Object element = getContent(value, binding);
181         return dataTypeBinding.deepHashValue(type, hashedObjects) + binding.deepHashValue(element, hashedObjects);
182     }
183
184         @Override
185         public int deepCompare(Object o1, Object o2,
186                         Set<IdentityPair<Object, Object>> compareHistory)
187                         throws BindingException {
188                 // Compare Type
189                 Datatype t1 = getContentType(o1);
190                 Datatype t2 = getContentType(o2);
191                 Binding dataTypeBinding = Bindings.getBindingUnchecked(Datatype.class);
192                 int dif = dataTypeBinding.compare(t1, t2);
193                 if (dif!=0) return dif;
194                 // Compare Value
195                 Binding bi1 = getContentBinding(o1);
196                 Binding bi2 = getContentBinding(o2);
197                 Object va1 = getContent(o1, bi1);
198                 Object va2 = getContent(o2, bi2);
199                 return DataValueUtil.compare(bi1, va1, bi2, va2);               
200         }
201 \r
202         @Override\r
203         protected void toString(Object value, BindingPrintContext ctx) throws BindingException {\r
204                 Binding b = getContentBinding(value);\r
205                 b.toString(getContent(value), ctx);\r
206                 \r
207                 ctx.b.append(" : ");\r
208                 ctx.b.append( ctx.singleLine ? b.type.toSingleLineString() : b.type.toString() );\r
209         }\r
210         \r
211         @Override\r
212         public Binding getComponentBinding(ChildReference path) {\r
213                 if (path==null) return this;            \r
214                 throw new IllegalArgumentException();\r
215         }       \r
216         \r
217     @Override\r
218     public int getComponentCount() {\r
219         return 0;\r
220     }\r
221     \r
222     @Override\r
223     public Binding getComponentBinding(int index) {\r
224         throw new IllegalArgumentException();\r
225     }\r
226         
227 }
228