]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/factory/MutableBindingFactory.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / factory / MutableBindingFactory.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.ArrayListBinding;\r
20 import org.simantics.databoard.binding.impl.TreeMapBinding;\r
21 import org.simantics.databoard.binding.mutable.ContainerOptionalBinding;\r
22 import org.simantics.databoard.binding.mutable.MutableBooleanBinding;\r
23 import org.simantics.databoard.binding.mutable.MutableByteBinding;\r
24 import org.simantics.databoard.binding.mutable.MutableDoubleBinding;\r
25 import org.simantics.databoard.binding.mutable.MutableFloatBinding;\r
26 import org.simantics.databoard.binding.mutable.MutableIntegerBinding;\r
27 import org.simantics.databoard.binding.mutable.MutableLongBinding;\r
28 import org.simantics.databoard.binding.mutable.MutableStringBinding;\r
29 import org.simantics.databoard.binding.mutable.UnionTaggedObjectBinding;\r
30 import org.simantics.databoard.type.ArrayType;\r
31 import org.simantics.databoard.type.BooleanType;\r
32 import org.simantics.databoard.type.ByteType;\r
33 import org.simantics.databoard.type.Datatype;\r
34 import org.simantics.databoard.type.DoubleType;\r
35 import org.simantics.databoard.type.FloatType;\r
36 import org.simantics.databoard.type.IntegerType;\r
37 import org.simantics.databoard.type.LongType;\r
38 import org.simantics.databoard.type.MapType;\r
39 import org.simantics.databoard.type.OptionalType;\r
40 import org.simantics.databoard.type.RecordType;\r
41 import org.simantics.databoard.type.StringType;\r
42 import org.simantics.databoard.type.UnionType;\r
43
44 /**
45  * MutableBindingScheme is a type to binding mapping that binds any DataType to an Object.\r
46  * All resulting bindings are completely mutable java classes.
47  * 
48  * DataType           | Class of the bound instance
49  * ===================|==================
50  * BooleanType        | MutableBoolean.class
51  * ByteType           | MutableByte.class
52  * FloatType          | MutableFloat.class
53  * DoubleType         | MutableDouble.class
54  * IntegerType        | MutableInt.class
55  * LongType           | MutableLong.class
56  * StringType         | MutableString.class
57  * UnionType          | TaggedObject.class
58  * OptionType         | ValueContainer.class
59  * RecordType         | Object[].class
60  * ArrayType          | ArrayList.class
61  * MapType            | TreeMap.class
62  * VariantType        | MutableVariant.class
63  *
64  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
65  */
66 \r
67 public class MutableBindingFactory extends TypeBindingFactory {\r
68 \r
69         /**\r
70          * Construct a binding factory.\r
71          */\r
72         public MutableBindingFactory() {\r
73                 super();\r
74         }\r
75         \r
76         /**\r
77          * Construct a scheme factory that appends constructed bindings to the user given\r
78          * repository  \r
79          * \r
80          * @param repository repository where bindings are placed\r
81          */\r
82         public MutableBindingFactory(Map<Datatype, Binding> repository) {\r
83                 super(repository);\r
84         }\r
85 \r
86         @Override\r
87         protected Binding doConstruct(Datatype type)\r
88                         throws BindingConstructionException {\r
89 \r
90                 // Exact, non-annotated types\r
91                 if (type.equals( Bindings.BOOLEAN.type() )) return Bindings.MUTABLE_BOOLEAN;\r
92                 if (type.equals( Bindings.BYTE.type() )) return Bindings.MUTABLE_BYTE;\r
93                 if (type.equals( Bindings.INTEGER.type() )) return Bindings.MUTABLE_INTEGER;\r
94                 if (type.equals( Bindings.LONG.type() )) return Bindings.MUTABLE_LONG;\r
95                 if (type.equals( Bindings.FLOAT.type() )) return Bindings.MUTABLE_FLOAT;\r
96                 if (type.equals( Bindings.DOUBLE.type() )) return Bindings.MUTABLE_DOUBLE;\r
97                 if (type.equals( Bindings.STRING.type() )) return Bindings.MUTABLE_STRING;\r
98                 if (type.equals( Bindings.VARIANT.type() )) return Bindings.MUTABLE_VARIANT;\r
99                 \r
100                 // Annotated types \r
101                 if (type instanceof BooleanType) return new MutableBooleanBinding((BooleanType)type);\r
102                 if (type instanceof DoubleType) return new MutableDoubleBinding((DoubleType)type);\r
103                 if (type instanceof FloatType) return new MutableFloatBinding((FloatType)type);\r
104                 if (type instanceof ByteType) return new MutableByteBinding((ByteType)type);\r
105                 if (type instanceof IntegerType) return new MutableIntegerBinding((IntegerType)type);\r
106                 if (type instanceof LongType) return new MutableLongBinding((LongType)type);\r
107                 if (type instanceof StringType) return new MutableStringBinding((StringType)type);\r
108                 \r
109                 // Constructed types\r
110                 if (type instanceof ArrayType) {\r
111                         ArrayType arrayType = (ArrayType) type;\r
112                         Datatype componentType = arrayType.componentType();\r
113 \r
114                         ArrayListBinding binding = new ArrayListBinding(arrayType, null);\r
115                         inprogress.put(type, binding);\r
116                         binding.componentBinding = construct( componentType );                                          \r
117                         inprogress.remove(type);\r
118                         return binding;\r
119                 }\r
120                 \r
121                 if (type instanceof OptionalType) {\r
122                         OptionalType optionalType = (OptionalType) type;\r
123                         Datatype componentType = optionalType.componentType;\r
124                         ContainerOptionalBinding binding = new ContainerOptionalBinding( optionalType, null );\r
125                         inprogress.put(type, binding);\r
126                         binding.componentBinding = construct( componentType );\r
127                         inprogress.remove(type);\r
128                         return binding;\r
129                 }\r
130                 \r
131                 if (type instanceof RecordType) {                       \r
132                         RecordType recordType = (RecordType) type;\r
133                         Binding componentBindings[] = new Binding[ recordType.getComponentCount() ];\r
134                         RecordObjectArrayBinding binding = new RecordObjectArrayBinding(recordType, componentBindings);\r
135                         inprogress.put(type, binding);\r
136                         for (int i=0; i<componentBindings.length; i++) {\r
137                                 componentBindings[i] = construct( recordType.getComponentType(i) );\r
138                         }\r
139                         inprogress.remove(type);\r
140                         return binding;\r
141                 }\r
142                 \r
143                 if (type instanceof UnionType) {\r
144                         UnionType unionType = (UnionType) type;\r
145                         Binding componentBindings[] = new Binding[ unionType.components.length ];\r
146                         UnionTaggedObjectBinding binding = new UnionTaggedObjectBinding(unionType, componentBindings);\r
147                         inprogress.put(type, binding);\r
148                         for (int i=0; i<componentBindings.length; i++) {\r
149                                 componentBindings[i] = construct( unionType.getComponent(i).type );\r
150                         }\r
151                         inprogress.remove(type);\r
152                         return binding;\r
153                 }               \r
154                 \r
155                 if (type instanceof MapType) {                  \r
156                         MapType mapType = (MapType) type;\r
157                         TreeMapBinding binding = new TreeMapBinding(mapType, null, null);\r
158                         inprogress.put(type, binding);\r
159                         binding.setKeyBinding( construct(mapType.keyType) );\r
160                         binding.setValueBinding( construct(mapType.valueType) );\r
161                         inprogress.remove(type);\r
162                         return binding;\r
163                 }\r
164                 \r
165                 throw new BindingConstructionException("Unexpected, I don't know how to create binding for "+type);\r
166         }\r
167 \r
168         @Override\r
169         public boolean supportsType(Datatype type) {\r
170                 // unexpected\r
171                 if (failures.containsKey(type)) return false;\r
172                 return true;\r
173         }\r
174         \r
175 }\r
176 \r
177 \r
178 \r
179
180
181