1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.databoard.binding.factory;
14 import java.util.Map;
\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
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.
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
64 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
67 public class MutableBindingFactory extends TypeBindingFactory {
\r
70 * Construct a binding factory.
\r
72 public MutableBindingFactory() {
\r
77 * Construct a scheme factory that appends constructed bindings to the user given
\r
80 * @param repository repository where bindings are placed
\r
82 public MutableBindingFactory(Map<Datatype, Binding> repository) {
\r
87 protected Binding doConstruct(Datatype type)
\r
88 throws BindingConstructionException {
\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
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
109 // Constructed types
\r
110 if (type instanceof ArrayType) {
\r
111 ArrayType arrayType = (ArrayType) type;
\r
112 Datatype componentType = arrayType.componentType();
\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
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
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
139 inprogress.remove(type);
\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
151 inprogress.remove(type);
\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
165 throw new BindingConstructionException("Unexpected, I don't know how to create binding for "+type);
\r
169 public boolean supportsType(Datatype type) {
\r
171 if (failures.containsKey(type)) return false;
\r