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