--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.databoard.binding.classfactory;\r
+\r
+import java.lang.annotation.Annotation;\r
+import java.lang.reflect.Array;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+import java.util.TreeMap;\r
+\r
+import org.simantics.databoard.annotations.ArgumentImpl;\r
+import org.simantics.databoard.annotations.LengthImpl;\r
+import org.simantics.databoard.annotations.MIMETypeImpl;\r
+import org.simantics.databoard.annotations.PatternImpl;\r
+import org.simantics.databoard.annotations.RangeImpl;\r
+import org.simantics.databoard.annotations.UnitImpl;\r
+import org.simantics.databoard.binding.error.BindingConstructionException;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.databoard.binding.reflection.BindingRequest;\r
+import org.simantics.databoard.type.ArrayType;\r
+import org.simantics.databoard.type.BooleanType;\r
+import org.simantics.databoard.type.ByteType;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.databoard.type.DoubleType;\r
+import org.simantics.databoard.type.FloatType;\r
+import org.simantics.databoard.type.IntegerType;\r
+import org.simantics.databoard.type.LongType;\r
+import org.simantics.databoard.type.MapType;\r
+import org.simantics.databoard.type.NumberType;\r
+import org.simantics.databoard.type.StringType;\r
+import org.simantics.databoard.type.VariantType;\r
+\r
+public class ImmutableClassesFactory implements TypeClassSubFactory {\r
+ \r
+ public ImmutableClassesFactory() {\r
+ }\r
+ \r
+ @Override\r
+ public BindingRequest construct(TypeClassFactory mainFactory, Datatype type) \r
+ throws BindingConstructionException \r
+ {\r
+ \r
+ if ( type instanceof ArrayType ) {\r
+ List<Annotation> annotations = new ArrayList<Annotation>();\r
+ ArrayType at = (ArrayType) type;\r
+ BindingRequest cbr = construct(mainFactory, at.componentType);\r
+ if ( cbr == null ) {\r
+ cbr = mainFactory.getClass(at.componentType); \r
+ }\r
+\r
+ String length = at.metadata.get(ArrayType.KEY_LENGTH);\r
+ if (length != null) annotations.add( new LengthImpl( length ) );\r
+ \r
+ if ( cbr.getClazz() != null ) {\r
+ for (Annotation a : cbr.annotations)\r
+ {\r
+ annotations.add( a );\r
+ }\r
+ \r
+ Class<?> arrayClass = Array.newInstance(cbr.getClazz(), 0).getClass();\r
+ return new BindingRequest( arrayClass, annotations );\r
+ }\r
+ }\r
+ \r
+ if ( type instanceof MapType ) {\r
+ MapType mt = (MapType) type;\r
+ List<Annotation> annotations = new ArrayList<Annotation>();\r
+ BindingRequest kbr = construct(mainFactory, mt.keyType);\r
+ BindingRequest vbr = construct(mainFactory, mt.valueType);\r
+ if ( kbr == null ) kbr = mainFactory.getClass(mt.keyType); \r
+ if ( vbr == null ) vbr = mainFactory.getClass(mt.valueType);\r
+ if ( kbr.getClazz()!=null && vbr.getClazz()!=null ) {\r
+ annotations.add( new ArgumentImpl(kbr.getClazz(), vbr.getClazz()) ); \r
+ return new BindingRequest( TreeMap.class, annotations );\r
+ }\r
+ }\r
+ \r
+ if ( type instanceof StringType ) {\r
+ List<Annotation> annotations = new ArrayList<Annotation>();\r
+ StringType st = (StringType) type;\r
+ \r
+ String pattern = st.metadata.get(StringType.KEY_PATTERN);\r
+ if (pattern != null) annotations.add( new PatternImpl( pattern ) );\r
+ \r
+ String mimetype = st.metadata.get(StringType.KEY_MIMETYPE);\r
+ if (mimetype != null) annotations.add( new MIMETypeImpl( mimetype ) );\r
+ \r
+ String length = st.metadata.get(StringType.KEY_LENGTH);\r
+ if (length != null) annotations.add( new LengthImpl( length ) );\r
+ \r
+ return new BindingRequest( String.class, annotations ); \r
+ }\r
+ \r
+ if ( type instanceof NumberType ) {\r
+ List<Annotation> annotations = new ArrayList<Annotation>();\r
+ NumberType nt = (NumberType) type;\r
+ \r
+ String unit = nt.metadata.get(NumberType.KEY_UNIT);\r
+ if (unit != null) annotations.add( new UnitImpl(unit) );\r
+ \r
+ String range = nt.metadata.get(NumberType.KEY_RANGE);\r
+ if (range != null) annotations.add( new RangeImpl(range) );\r
+ \r
+ Class<?> clazz = null;\r
+ if ( type instanceof IntegerType ) clazz = int.class;\r
+ if ( type instanceof ByteType ) clazz = byte.class;\r
+ if ( type instanceof LongType ) clazz = long.class;\r
+ if ( type instanceof DoubleType ) clazz = double.class;\r
+ if ( type instanceof FloatType ) clazz = float.class;\r
+ \r
+ return new BindingRequest( clazz, annotations );\r
+ }\r
+ \r
+ if ( type instanceof BooleanType ) {\r
+ return new BindingRequest( boolean.class );\r
+ }\r
+ \r
+ if ( type instanceof VariantType ) {\r
+ return new BindingRequest( Variant.class );\r
+ }\r
+ \r
+ return null;\r
+ }\r
+\r
+}\r