X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Fbinding%2Ffactory%2FMutableBindingFactory.java;fp=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Fbinding%2Ffactory%2FMutableBindingFactory.java;h=2cacfd69f994d31d0b51ddccfc17f14aa408b787;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/factory/MutableBindingFactory.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/factory/MutableBindingFactory.java new file mode 100644 index 000000000..2cacfd69f --- /dev/null +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/factory/MutableBindingFactory.java @@ -0,0 +1,181 @@ +/******************************************************************************* + * Copyright (c) 2010 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.databoard.binding.factory; + +import java.util.Map; + +import org.simantics.databoard.Bindings; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.binding.error.BindingConstructionException; +import org.simantics.databoard.binding.impl.ArrayListBinding; +import org.simantics.databoard.binding.impl.TreeMapBinding; +import org.simantics.databoard.binding.mutable.ContainerOptionalBinding; +import org.simantics.databoard.binding.mutable.MutableBooleanBinding; +import org.simantics.databoard.binding.mutable.MutableByteBinding; +import org.simantics.databoard.binding.mutable.MutableDoubleBinding; +import org.simantics.databoard.binding.mutable.MutableFloatBinding; +import org.simantics.databoard.binding.mutable.MutableIntegerBinding; +import org.simantics.databoard.binding.mutable.MutableLongBinding; +import org.simantics.databoard.binding.mutable.MutableStringBinding; +import org.simantics.databoard.binding.mutable.UnionTaggedObjectBinding; +import org.simantics.databoard.type.ArrayType; +import org.simantics.databoard.type.BooleanType; +import org.simantics.databoard.type.ByteType; +import org.simantics.databoard.type.Datatype; +import org.simantics.databoard.type.DoubleType; +import org.simantics.databoard.type.FloatType; +import org.simantics.databoard.type.IntegerType; +import org.simantics.databoard.type.LongType; +import org.simantics.databoard.type.MapType; +import org.simantics.databoard.type.OptionalType; +import org.simantics.databoard.type.RecordType; +import org.simantics.databoard.type.StringType; +import org.simantics.databoard.type.UnionType; + +/** + * MutableBindingScheme is a type to binding mapping that binds any DataType to an Object. + * All resulting bindings are completely mutable java classes. + * + * DataType | Class of the bound instance + * ===================|================== + * BooleanType | MutableBoolean.class + * ByteType | MutableByte.class + * FloatType | MutableFloat.class + * DoubleType | MutableDouble.class + * IntegerType | MutableInt.class + * LongType | MutableLong.class + * StringType | MutableString.class + * UnionType | TaggedObject.class + * OptionType | ValueContainer.class + * RecordType | Object[].class + * ArrayType | ArrayList.class + * MapType | TreeMap.class + * VariantType | MutableVariant.class + * + * @author Toni Kalajainen + */ + +public class MutableBindingFactory extends TypeBindingFactory { + + /** + * Construct a binding factory. + */ + public MutableBindingFactory() { + super(); + } + + /** + * Construct a scheme factory that appends constructed bindings to the user given + * repository + * + * @param repository repository where bindings are placed + */ + public MutableBindingFactory(Map repository) { + super(repository); + } + + @Override + protected Binding doConstruct(Datatype type) + throws BindingConstructionException { + + // Exact, non-annotated types + if (type.equals( Bindings.BOOLEAN.type() )) return Bindings.MUTABLE_BOOLEAN; + if (type.equals( Bindings.BYTE.type() )) return Bindings.MUTABLE_BYTE; + if (type.equals( Bindings.INTEGER.type() )) return Bindings.MUTABLE_INTEGER; + if (type.equals( Bindings.LONG.type() )) return Bindings.MUTABLE_LONG; + if (type.equals( Bindings.FLOAT.type() )) return Bindings.MUTABLE_FLOAT; + if (type.equals( Bindings.DOUBLE.type() )) return Bindings.MUTABLE_DOUBLE; + if (type.equals( Bindings.STRING.type() )) return Bindings.MUTABLE_STRING; + if (type.equals( Bindings.VARIANT.type() )) return Bindings.MUTABLE_VARIANT; + + // Annotated types + if (type instanceof BooleanType) return new MutableBooleanBinding((BooleanType)type); + if (type instanceof DoubleType) return new MutableDoubleBinding((DoubleType)type); + if (type instanceof FloatType) return new MutableFloatBinding((FloatType)type); + if (type instanceof ByteType) return new MutableByteBinding((ByteType)type); + if (type instanceof IntegerType) return new MutableIntegerBinding((IntegerType)type); + if (type instanceof LongType) return new MutableLongBinding((LongType)type); + if (type instanceof StringType) return new MutableStringBinding((StringType)type); + + // Constructed types + if (type instanceof ArrayType) { + ArrayType arrayType = (ArrayType) type; + Datatype componentType = arrayType.componentType(); + + ArrayListBinding binding = new ArrayListBinding(arrayType, null); + inprogress.put(type, binding); + binding.componentBinding = construct( componentType ); + inprogress.remove(type); + return binding; + } + + if (type instanceof OptionalType) { + OptionalType optionalType = (OptionalType) type; + Datatype componentType = optionalType.componentType; + ContainerOptionalBinding binding = new ContainerOptionalBinding( optionalType, null ); + inprogress.put(type, binding); + binding.componentBinding = construct( componentType ); + inprogress.remove(type); + return binding; + } + + if (type instanceof RecordType) { + RecordType recordType = (RecordType) type; + Binding componentBindings[] = new Binding[ recordType.getComponentCount() ]; + RecordObjectArrayBinding binding = new RecordObjectArrayBinding(recordType, componentBindings); + inprogress.put(type, binding); + for (int i=0; i