]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/classfactory/ImmutableClassesFactory.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / classfactory / ImmutableClassesFactory.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 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.classfactory;\r
13 \r
14 import java.lang.annotation.Annotation;\r
15 import java.lang.reflect.Array;\r
16 import java.util.ArrayList;\r
17 import java.util.List;\r
18 import java.util.TreeMap;\r
19 \r
20 import org.simantics.databoard.annotations.ArgumentImpl;\r
21 import org.simantics.databoard.annotations.LengthImpl;\r
22 import org.simantics.databoard.annotations.MIMETypeImpl;\r
23 import org.simantics.databoard.annotations.PatternImpl;\r
24 import org.simantics.databoard.annotations.RangeImpl;\r
25 import org.simantics.databoard.annotations.UnitImpl;\r
26 import org.simantics.databoard.binding.error.BindingConstructionException;\r
27 import org.simantics.databoard.binding.mutable.Variant;\r
28 import org.simantics.databoard.binding.reflection.BindingRequest;\r
29 import org.simantics.databoard.type.ArrayType;\r
30 import org.simantics.databoard.type.BooleanType;\r
31 import org.simantics.databoard.type.ByteType;\r
32 import org.simantics.databoard.type.Datatype;\r
33 import org.simantics.databoard.type.DoubleType;\r
34 import org.simantics.databoard.type.FloatType;\r
35 import org.simantics.databoard.type.IntegerType;\r
36 import org.simantics.databoard.type.LongType;\r
37 import org.simantics.databoard.type.MapType;\r
38 import org.simantics.databoard.type.NumberType;\r
39 import org.simantics.databoard.type.StringType;\r
40 import org.simantics.databoard.type.VariantType;\r
41 \r
42 public class ImmutableClassesFactory implements TypeClassSubFactory {\r
43         \r
44         public ImmutableClassesFactory() {\r
45         }\r
46         \r
47         @Override\r
48         public BindingRequest construct(TypeClassFactory mainFactory, Datatype type) \r
49         throws BindingConstructionException \r
50         {\r
51                 \r
52                 if ( type instanceof ArrayType ) {\r
53                         List<Annotation> annotations = new ArrayList<Annotation>();\r
54                         ArrayType at = (ArrayType) type;\r
55                         BindingRequest cbr = construct(mainFactory, at.componentType);\r
56                         if ( cbr == null ) {\r
57                                 cbr = mainFactory.getClass(at.componentType);                           \r
58                         }\r
59 \r
60                         String length = at.metadata.get(ArrayType.KEY_LENGTH);\r
61                         if (length != null) annotations.add( new LengthImpl( length ) );\r
62                         \r
63                         if ( cbr.getClazz() != null ) {\r
64                                 for (Annotation a : cbr.annotations)\r
65                                 {\r
66                                         annotations.add( a );\r
67                                 }\r
68                                 \r
69                                 Class<?> arrayClass = Array.newInstance(cbr.getClazz(), 0).getClass();\r
70                                 return new BindingRequest( arrayClass, annotations );\r
71                         }\r
72                 }\r
73                 \r
74                 if ( type instanceof MapType ) {\r
75                         MapType mt = (MapType) type;\r
76                         List<Annotation> annotations = new ArrayList<Annotation>();\r
77                         BindingRequest kbr = construct(mainFactory, mt.keyType);\r
78                         BindingRequest vbr = construct(mainFactory, mt.valueType);\r
79                         if ( kbr == null ) kbr = mainFactory.getClass(mt.keyType);                                                      \r
80                         if ( vbr == null ) vbr = mainFactory.getClass(mt.valueType);\r
81                         if ( kbr.getClazz()!=null && vbr.getClazz()!=null ) {\r
82                                 annotations.add( new ArgumentImpl(kbr.getClazz(), vbr.getClazz()) );                    \r
83                                 return new BindingRequest( TreeMap.class, annotations );\r
84                         }\r
85                 }\r
86                 \r
87                 if ( type instanceof StringType ) {\r
88                         List<Annotation> annotations = new ArrayList<Annotation>();\r
89                         StringType st = (StringType) type;\r
90                         \r
91                         String pattern = st.metadata.get(StringType.KEY_PATTERN);\r
92                         if (pattern != null) annotations.add( new PatternImpl( pattern ) );\r
93                         \r
94                         String mimetype = st.metadata.get(StringType.KEY_MIMETYPE);\r
95                         if (mimetype != null) annotations.add( new MIMETypeImpl( mimetype ) );\r
96                         \r
97                         String length = st.metadata.get(StringType.KEY_LENGTH);\r
98                         if (length != null) annotations.add( new LengthImpl( length ) );\r
99                         \r
100                         return new BindingRequest( String.class, annotations );                 \r
101                 }\r
102                 \r
103                 if ( type instanceof NumberType ) {\r
104                         List<Annotation> annotations = new ArrayList<Annotation>();\r
105                         NumberType nt = (NumberType) type;\r
106                         \r
107                         String unit = nt.metadata.get(NumberType.KEY_UNIT);\r
108                         if (unit != null) annotations.add( new UnitImpl(unit) );\r
109                         \r
110                         String range = nt.metadata.get(NumberType.KEY_RANGE);\r
111                         if (range != null) annotations.add( new RangeImpl(range) );\r
112                         \r
113                         Class<?> clazz = null;\r
114                         if ( type instanceof IntegerType ) clazz = int.class;\r
115                         if ( type instanceof ByteType ) clazz = byte.class;\r
116                         if ( type instanceof LongType ) clazz = long.class;\r
117                         if ( type instanceof DoubleType ) clazz = double.class;\r
118                         if ( type instanceof FloatType ) clazz = float.class;\r
119                         \r
120                         return new BindingRequest( clazz, annotations );\r
121                 }\r
122                 \r
123                 if ( type instanceof BooleanType ) {\r
124                         return new BindingRequest( boolean.class );\r
125                 }\r
126                 \r
127                 if ( type instanceof VariantType ) {\r
128                         return new BindingRequest( Variant.class );\r
129                 }\r
130                 \r
131                 return null;\r
132         }\r
133 \r
134 }\r