1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 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.classfactory;
\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
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
42 public class ImmutableClassesFactory implements TypeClassSubFactory {
\r
44 public ImmutableClassesFactory() {
\r
48 public BindingRequest construct(TypeClassFactory mainFactory, Datatype type)
\r
49 throws BindingConstructionException
\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
60 String length = at.metadata.get(ArrayType.KEY_LENGTH);
\r
61 if (length != null) annotations.add( new LengthImpl( length ) );
\r
63 if ( cbr.getClazz() != null ) {
\r
64 for (Annotation a : cbr.annotations)
\r
66 annotations.add( a );
\r
69 Class<?> arrayClass = Array.newInstance(cbr.getClazz(), 0).getClass();
\r
70 return new BindingRequest( arrayClass, annotations );
\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
87 if ( type instanceof StringType ) {
\r
88 List<Annotation> annotations = new ArrayList<Annotation>();
\r
89 StringType st = (StringType) type;
\r
91 String pattern = st.metadata.get(StringType.KEY_PATTERN);
\r
92 if (pattern != null) annotations.add( new PatternImpl( pattern ) );
\r
94 String mimetype = st.metadata.get(StringType.KEY_MIMETYPE);
\r
95 if (mimetype != null) annotations.add( new MIMETypeImpl( mimetype ) );
\r
97 String length = st.metadata.get(StringType.KEY_LENGTH);
\r
98 if (length != null) annotations.add( new LengthImpl( length ) );
\r
100 return new BindingRequest( String.class, annotations );
\r
103 if ( type instanceof NumberType ) {
\r
104 List<Annotation> annotations = new ArrayList<Annotation>();
\r
105 NumberType nt = (NumberType) type;
\r
107 String unit = nt.metadata.get(NumberType.KEY_UNIT);
\r
108 if (unit != null) annotations.add( new UnitImpl(unit) );
\r
110 String range = nt.metadata.get(NumberType.KEY_RANGE);
\r
111 if (range != null) annotations.add( new RangeImpl(range) );
\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
120 return new BindingRequest( clazz, annotations );
\r
123 if ( type instanceof BooleanType ) {
\r
124 return new BindingRequest( boolean.class );
\r
127 if ( type instanceof VariantType ) {
\r
128 return new BindingRequest( Variant.class );
\r