]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/VariantBinding.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / VariantBinding.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
4  *  All rights reserved. This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License v1.0
6  *  which accompanies this distribution, and is available at
7  *  http://www.eclipse.org/legal/epl-v10.html
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.binding;
13
14 import java.util.IdentityHashMap;
15 import java.util.Set;
16
17 import org.simantics.databoard.Bindings;
18 import org.simantics.databoard.Datatypes;
19 import org.simantics.databoard.accessor.reference.ChildReference;
20 import org.simantics.databoard.adapter.AdaptException;
21 import org.simantics.databoard.binding.error.BindingException;
22 import org.simantics.databoard.binding.error.RuntimeBindingException;
23 import org.simantics.databoard.binding.impl.BindingPrintContext;
24 import org.simantics.databoard.binding.impl.ObjectVariantBinding;
25 import org.simantics.databoard.binding.impl.StringVariantBinding;
26 import org.simantics.databoard.binding.mutable.ImmutableVariantBinding;
27 import org.simantics.databoard.binding.mutable.MutableVariant;
28 import org.simantics.databoard.binding.mutable.MutableVariantBinding;
29 import org.simantics.databoard.binding.mutable.Variant;
30 import org.simantics.databoard.type.Datatype;
31 import org.simantics.databoard.type.VariantType;
32 import org.simantics.databoard.util.DataValueUtil;
33 import org.simantics.databoard.util.IdentityPair;
34
35 /**
36  * This is the abstract base class for bindings of VariantType Java Objects.
37  * Variant is a container that has value of any Datatype. 
38  * 
39  * @see VariantType The Datatype
40  * @see ImmutableVariantBinding Binds variant to {@link Variant}
41  * @see MutableVariantBinding Binds variant to {@link MutableVariant}
42  * @see ObjectVariantBinding Binds variant to java.lang.Object
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
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         
77         /**
78          * Get the data type of the content.
79          * 
80          * @param variant the variant object
81          * @return the data type
82          * @throws BindingException
83          */
84         public abstract Datatype getContentType(Object variant)
85         throws BindingException;
86         
87         /**
88          * Return a suggestion for the binding of the content of this variant.
89          * 
90          * @param variant variant object
91          * @return binding a binding
92          */
93         public abstract Binding getContentBinding(Object variant)
94         throws BindingException;
95         
96         /**
97          * Create a new variant object. 
98          * The <code>value</code> argument may be included in the result. 
99          * 
100          * @param contentBinding the binding of the content
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         
108         /**
109          * Create a new variant object. 
110          * 
111          * @param contentBinding binding of the content
112          * @param content content 
113          * @return new variant
114          * @throws RuntimeBindingException
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         }
124         
125         @Override
126         public void readFrom(Binding srcBinding, Object src, Object dst)
127                         throws BindingException {
128                 try {
129                         VariantBinding sb = (VariantBinding) srcBinding;
130                         Datatype newType = sb.getContentType(src);
131                         Datatype oldType = getContentType(dst);
132                         Binding scb = sb.getContentBinding(src);
133                         Object sc = sb.getContent(src, scb);
134                         if (newType.equals(oldType)) {
135                                 Binding dcb = getContentBinding(dst);
136                                 if (dcb.isImmutable()) {
137                                         Object dc = Bindings.clone(sc, scb, scb);
138                                         setContent(dst, scb, dc);
139                                 } else {
140                                         Object dc = getContent(dst, dcb);
141                                         dc = dcb.readFromTry(scb, sc, dc);
142                                         setContent(dst, dcb, dc);
143                                 }
144                         } else {
145                                 Object dc = Bindings.clone(sc, scb, scb);
146                                 setContent(dst, scb, dc);
147                         }
148                 } catch (AdaptException e) {
149                         throw new BindingException(e);
150                 }                               
151                 
152         }
153         
154         /**
155          * Set the content of an variant.
156          * 
157          * @param variant variant object
158          * @param contentBinding content's binding
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         Object element = getContent(value, binding);
180         return Bindings.DATATYPE.deepHashValue(type, hashedObjects) + binding.deepHashValue(element, hashedObjects);
181     }
182
183         @Override
184         public int deepCompare(Object o1, Object o2,
185                         Set<IdentityPair<Object, Object>> compareHistory)
186                         throws BindingException {
187                 // Compare Type
188                 Datatype t1 = getContentType(o1);
189                 Datatype t2 = getContentType(o2);
190                 int dif = Bindings.DATATYPE.compare(t1, t2);
191                 if (dif!=0) return dif;
192                 // Compare Value
193                 Binding bi1 = getContentBinding(o1);
194                 Binding bi2 = getContentBinding(o2);
195                 Object va1 = getContent(o1, bi1);
196                 Object va2 = getContent(o2, bi2);
197                 return DataValueUtil.compare(bi1, va1, bi2, va2);               
198         }
199
200         @Override
201         protected void toString(Object value, BindingPrintContext ctx) throws BindingException {
202                 Binding b = getContentBinding(value);
203                 b.toString(getContent(value), ctx);
204                 
205                 ctx.b.append(" : ");
206                 ctx.b.append( ctx.singleLine ? b.type.toSingleLineString() : b.type.toString() );
207         }
208         
209         @Override
210         public Binding getComponentBinding(ChildReference path) {
211                 if (path==null) return this;            
212                 throw new IllegalArgumentException();
213         }       
214         
215     @Override
216     public int getComponentCount() {
217         return 0;
218     }
219     
220     @Override
221     public Binding getComponentBinding(int index) {
222         throw new IllegalArgumentException();
223     }
224         
225 }
226