]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/classfactory/ImmutableClassesFactory.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / classfactory / ImmutableClassesFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.binding.classfactory;
13
14 import java.lang.annotation.Annotation;
15 import java.lang.reflect.Array;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.TreeMap;
19
20 import org.simantics.databoard.annotations.ArgumentImpl;
21 import org.simantics.databoard.annotations.LengthImpl;
22 import org.simantics.databoard.annotations.MIMETypeImpl;
23 import org.simantics.databoard.annotations.PatternImpl;
24 import org.simantics.databoard.annotations.RangeImpl;
25 import org.simantics.databoard.annotations.UnitImpl;
26 import org.simantics.databoard.binding.error.BindingConstructionException;
27 import org.simantics.databoard.binding.mutable.Variant;
28 import org.simantics.databoard.binding.reflection.BindingRequest;
29 import org.simantics.databoard.type.ArrayType;
30 import org.simantics.databoard.type.BooleanType;
31 import org.simantics.databoard.type.ByteType;
32 import org.simantics.databoard.type.Datatype;
33 import org.simantics.databoard.type.DoubleType;
34 import org.simantics.databoard.type.FloatType;
35 import org.simantics.databoard.type.IntegerType;
36 import org.simantics.databoard.type.LongType;
37 import org.simantics.databoard.type.MapType;
38 import org.simantics.databoard.type.NumberType;
39 import org.simantics.databoard.type.StringType;
40 import org.simantics.databoard.type.VariantType;
41
42 public class ImmutableClassesFactory implements TypeClassSubFactory {
43         
44         public ImmutableClassesFactory() {
45         }
46         
47         @Override
48         public BindingRequest construct(TypeClassFactory mainFactory, Datatype type) 
49         throws BindingConstructionException 
50         {
51                 
52                 if ( type instanceof ArrayType ) {
53                         List<Annotation> annotations = new ArrayList<Annotation>();
54                         ArrayType at = (ArrayType) type;
55                         BindingRequest cbr = construct(mainFactory, at.componentType);
56                         if ( cbr == null ) {
57                                 cbr = mainFactory.getClass(at.componentType);                           
58                         }
59
60                         String length = at.metadata.get(ArrayType.KEY_LENGTH);
61                         if (length != null) annotations.add( new LengthImpl( length ) );
62                         
63                         if ( cbr.getClazz() != null ) {
64                                 for (Annotation a : cbr.annotations)
65                                 {
66                                         annotations.add( a );
67                                 }
68                                 
69                                 Class<?> arrayClass = Array.newInstance(cbr.getClazz(), 0).getClass();
70                                 return new BindingRequest( arrayClass, annotations );
71                         }
72                 }
73                 
74                 if ( type instanceof MapType ) {
75                         MapType mt = (MapType) type;
76                         List<Annotation> annotations = new ArrayList<Annotation>();
77                         BindingRequest kbr = construct(mainFactory, mt.keyType);
78                         BindingRequest vbr = construct(mainFactory, mt.valueType);
79                         if ( kbr == null ) kbr = mainFactory.getClass(mt.keyType);                                                      
80                         if ( vbr == null ) vbr = mainFactory.getClass(mt.valueType);
81                         if ( kbr.getClazz()!=null && vbr.getClazz()!=null ) {
82                                 annotations.add( new ArgumentImpl(kbr.getClazz(), vbr.getClazz()) );                    
83                                 return new BindingRequest( TreeMap.class, annotations );
84                         }
85                 }
86                 
87                 if ( type instanceof StringType ) {
88                         List<Annotation> annotations = new ArrayList<Annotation>();
89                         StringType st = (StringType) type;
90                         
91                         String pattern = st.metadata.get(StringType.KEY_PATTERN);
92                         if (pattern != null) annotations.add( new PatternImpl( pattern ) );
93                         
94                         String mimetype = st.metadata.get(StringType.KEY_MIMETYPE);
95                         if (mimetype != null) annotations.add( new MIMETypeImpl( mimetype ) );
96                         
97                         String length = st.metadata.get(StringType.KEY_LENGTH);
98                         if (length != null) annotations.add( new LengthImpl( length ) );
99                         
100                         return new BindingRequest( String.class, annotations );                 
101                 }
102                 
103                 if ( type instanceof NumberType ) {
104                         List<Annotation> annotations = new ArrayList<Annotation>();
105                         NumberType nt = (NumberType) type;
106                         
107                         String unit = nt.metadata.get(NumberType.KEY_UNIT);
108                         if (unit != null) annotations.add( new UnitImpl(unit) );
109                         
110                         String range = nt.metadata.get(NumberType.KEY_RANGE);
111                         if (range != null) annotations.add( new RangeImpl(range) );
112                         
113                         Class<?> clazz = null;
114                         if ( type instanceof IntegerType ) clazz = int.class;
115                         if ( type instanceof ByteType ) clazz = byte.class;
116                         if ( type instanceof LongType ) clazz = long.class;
117                         if ( type instanceof DoubleType ) clazz = double.class;
118                         if ( type instanceof FloatType ) clazz = float.class;
119                         
120                         return new BindingRequest( clazz, annotations );
121                 }
122                 
123                 if ( type instanceof BooleanType ) {
124                         return new BindingRequest( boolean.class );
125                 }
126                 
127                 if ( type instanceof VariantType ) {
128                         return new BindingRequest( Variant.class );
129                 }
130                 
131                 return null;
132         }
133
134 }