]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/factory/DefaultBindingFactory.java
Merge commit 'bd5bc6e45f700e755b61bd112631796631330ecb'
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / factory / DefaultBindingFactory.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.factory;
13
14 import java.util.Map;\r
15 \r
16 import org.simantics.databoard.Bindings;\r
17 import org.simantics.databoard.binding.Binding;\r
18 import org.simantics.databoard.binding.error.BindingConstructionException;\r
19 import org.simantics.databoard.binding.impl.BooleanArrayBinding;\r
20 import org.simantics.databoard.binding.impl.BooleanBindingDefault;\r
21 import org.simantics.databoard.binding.impl.ByteArrayBinding;\r
22 import org.simantics.databoard.binding.impl.ByteBindingDefault;\r
23 import org.simantics.databoard.binding.impl.DefaultMapBinding;\r
24 import org.simantics.databoard.binding.impl.DoubleArrayBinding;\r
25 import org.simantics.databoard.binding.impl.DoubleBindingDefault;\r
26 import org.simantics.databoard.binding.impl.FloatArrayBinding;\r
27 import org.simantics.databoard.binding.impl.FloatBindingDefault;\r
28 import org.simantics.databoard.binding.impl.IntArrayBinding;\r
29 import org.simantics.databoard.binding.impl.IntegerBindingDefault;\r
30 import org.simantics.databoard.binding.impl.LongArrayBinding;\r
31 import org.simantics.databoard.binding.impl.LongBindingDefault;\r
32 import org.simantics.databoard.binding.impl.ObjectArrayBinding;\r
33 import org.simantics.databoard.binding.impl.StringBindingDefault;\r
34 import org.simantics.databoard.binding.mutable.ContainerOptionalBinding;\r
35 import org.simantics.databoard.binding.mutable.UnionTaggedObjectBinding;\r
36 import org.simantics.databoard.type.ArrayType;\r
37 import org.simantics.databoard.type.BooleanType;\r
38 import org.simantics.databoard.type.ByteType;\r
39 import org.simantics.databoard.type.Datatype;\r
40 import org.simantics.databoard.type.DoubleType;\r
41 import org.simantics.databoard.type.FloatType;\r
42 import org.simantics.databoard.type.IntegerType;\r
43 import org.simantics.databoard.type.LongType;\r
44 import org.simantics.databoard.type.MapType;\r
45 import org.simantics.databoard.type.OptionalType;\r
46 import org.simantics.databoard.type.RecordType;\r
47 import org.simantics.databoard.type.StringType;\r
48 import org.simantics.databoard.type.UnionType;\r
49
50 /**
51  * DefaultBindingScheme is a type to binding mapping that binds any DataType to an Object.\r
52  * All resulting bindings typicaly immutable java classes.
53  * 
54  * DataType           | Class of the bound instance
55  * ===================|==================
56  * BooleanType        | Boolean.class
57  * ByteType           | Byte.class
58  * FloatType          | Float.class
59  * DoubleType         | Double.class
60  * IntegerType        | Int.class
61  * LongType           | Long.class
62  * StringType         | String.class
63  * UnionType          | TaggedObject.class
64  * OptionType         | ValueContainer.class
65  * RecordType         | Object[].class
66  * MapType            | TreeMap.class
67  * VariantType        | Variant.class\r
68  * ArrayType(Boolean) | boolean[].class\r
69  * ArrayType(Byte)    | byte[].class\r
70  * ArrayType(Integer) | int[].class\r
71  * ArrayType(Long)    | long[].class\r
72  * ArrayType(Float)   | float[].class\r
73  * ArrayType(Double)  | double[].class\r
74  * ArrayType(Byte)    | byte[].class\r
75  * ArrayType( T )     | Object[].class\r
76  * 
77  *
78  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
79  */
80 public class DefaultBindingFactory extends TypeBindingFactory {\r
81         \r
82         /**\r
83          * Construct a binding factory.\r
84          */\r
85         public DefaultBindingFactory() {\r
86                 super();\r
87         }\r
88         \r
89         /**\r
90          * Construct a scheme factory that appends constructed bindings to the user given\r
91          * repository  \r
92          * \r
93          * @param repository repository where bindings are placed\r
94          */\r
95         public DefaultBindingFactory(Map<Datatype, Binding> repository) {\r
96                 super(repository);\r
97         }\r
98 \r
99         @Override\r
100         protected Binding doConstruct(Datatype type)\r
101                         throws BindingConstructionException {\r
102 \r
103                 // Exact, non-annotated types\r
104                 if (type.equals( Bindings.BOOLEAN.type() )) return Bindings.BOOLEAN;\r
105                 if (type.equals( Bindings.BYTE.type() )) return Bindings.BYTE;\r
106                 if (type.equals( Bindings.INTEGER.type() )) return Bindings.INTEGER;\r
107                 if (type.equals( Bindings.LONG.type() )) return Bindings.LONG;\r
108                 if (type.equals( Bindings.FLOAT.type() )) return Bindings.FLOAT;\r
109                 if (type.equals( Bindings.DOUBLE.type() )) return Bindings.DOUBLE;\r
110                 if (type.equals( Bindings.STRING.type() )) return Bindings.STRING;\r
111                 if (type.equals( Bindings.VARIANT.type() )) return Bindings.VARIANT;\r
112                 if (type.equals( Bindings.BOOLEAN_ARRAY.type() )) return Bindings.BOOLEAN_ARRAY;\r
113                 if (type.equals( Bindings.BYTE_ARRAY.type() )) return Bindings.BYTE_ARRAY;\r
114                 if (type.equals( Bindings.INT_ARRAY.type() )) return Bindings.INT_ARRAY;\r
115                 if (type.equals( Bindings.LONG_ARRAY.type() )) return Bindings.LONG_ARRAY;\r
116                 if (type.equals( Bindings.FLOAT_ARRAY.type() )) return Bindings.FLOAT_ARRAY;\r
117                 if (type.equals( Bindings.DOUBLE_ARRAY.type() )) return Bindings.DOUBLE_ARRAY;\r
118                 if (type.equals( Bindings.BOOLEAN_ARRAY.type() )) return Bindings.BOOLEAN_ARRAY;\r
119                 if (type.equals( Bindings.STRING_ARRAY.type() )) return Bindings.STRING_ARRAY;\r
120                 \r
121                 // Annotated types \r
122                 if (type instanceof BooleanType) return new BooleanBindingDefault((BooleanType)type);\r
123                 if (type instanceof DoubleType) return new DoubleBindingDefault((DoubleType)type);\r
124                 if (type instanceof FloatType) return new FloatBindingDefault((FloatType)type);\r
125                 if (type instanceof ByteType) return new ByteBindingDefault((ByteType)type);\r
126                 if (type instanceof IntegerType) return new IntegerBindingDefault((IntegerType)type);\r
127                 if (type instanceof LongType) return new LongBindingDefault((LongType)type);\r
128                 if (type instanceof StringType) return new StringBindingDefault((StringType)type);\r
129                 \r
130                 // Constructed types\r
131                 if (type instanceof ArrayType) {
132                         ArrayType arrayType = (ArrayType) type;
133                         Datatype componentType = arrayType.componentType();\r
134                         
135                         if (componentType instanceof BooleanType) return BooleanArrayBinding.createFrom(arrayType);\r
136                         if (componentType instanceof ByteType) return ByteArrayBinding.createFrom(arrayType);\r
137                         if (componentType instanceof IntegerType) return IntArrayBinding.createFrom(arrayType);\r
138                         if (componentType instanceof LongType) return LongArrayBinding.createFrom(arrayType);\r
139                         if (componentType instanceof FloatType) return FloatArrayBinding.createFrom(arrayType);\r
140                         if (componentType instanceof DoubleType) return DoubleArrayBinding.createFrom(arrayType);\r
141 \r
142                         ObjectArrayBinding binding = new ObjectArrayBinding(arrayType, null);\r
143                         inprogress.put(type, binding);\r
144                         binding.componentBinding = construct( componentType );                                          \r
145                         inprogress.remove(type);\r
146                         return binding;\r
147                 }
148                 
149                 if (type instanceof OptionalType) {
150                         OptionalType optionalType = (OptionalType) type;
151                         Datatype componentType = optionalType.componentType;
152                         ContainerOptionalBinding binding = new ContainerOptionalBinding( optionalType, null );
153                         inprogress.put(type, binding);\r
154                         binding.componentBinding = construct( componentType );\r
155                         inprogress.remove(type);\r
156                         return binding;
157                 }
158                 
159                 if (type instanceof RecordType) {                       
160                         RecordType recordType = (RecordType) type;
161                         Binding componentBindings[] = new Binding[ recordType.getComponentCount() ];
162                         RecordObjectArrayBinding binding = new RecordObjectArrayBinding(recordType, componentBindings);
163                         inprogress.put(type, binding);\r
164                         for (int i=0; i<componentBindings.length; i++) {\r
165                                 componentBindings[i] = construct( recordType.getComponentType(i) );\r
166                         }\r
167                         inprogress.remove(type);\r
168                         return binding;
169                 }
170                 
171                 if (type instanceof UnionType) {
172                         UnionType unionType = (UnionType) type;
173                         Binding componentBindings[] = new Binding[ unionType.components.length ];
174                         UnionTaggedObjectBinding binding = new UnionTaggedObjectBinding(unionType, componentBindings);
175                         inprogress.put(type, binding);\r
176                         for (int i=0; i<componentBindings.length; i++) {\r
177                                 componentBindings[i] = construct( unionType.getComponent(i).type );\r
178                         }\r
179                         inprogress.remove(type);\r
180                         return binding;\r
181                 }               
182                 
183                 if (type instanceof MapType) {
184                         MapType mapType = (MapType) type;
185                         DefaultMapBinding binding = new DefaultMapBinding(mapType, null, null);
186                         inprogress.put(type, binding);\r
187                         binding.setKeyBinding( construct(mapType.keyType) );\r
188                         binding.setValueBinding( construct(mapType.valueType) );\r
189                         inprogress.remove(type);\r
190                         return binding;\r
191                 }
192                 
193                 throw new BindingConstructionException("Unexpected, I don't know how to create binding for "+type);
194         }\r
195 \r
196         @Override\r
197         public boolean supportsType(Datatype type) {\r
198                 // unexpected\r
199                 if (failures.containsKey(type)) return false;\r
200                 return true;\r
201         }\r
202         
203 }
204
205
206