1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.binding.classfactory;
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;
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;
42 public class ImmutableClassesFactory implements TypeClassSubFactory {
44 public ImmutableClassesFactory() {
48 public BindingRequest construct(TypeClassFactory mainFactory, Datatype type)
49 throws BindingConstructionException
52 if ( type instanceof ArrayType ) {
53 List<Annotation> annotations = new ArrayList<Annotation>();
54 ArrayType at = (ArrayType) type;
55 BindingRequest cbr = construct(mainFactory, at.componentType);
57 cbr = mainFactory.getClass(at.componentType);
60 String length = at.metadata.get(ArrayType.KEY_LENGTH);
61 if (length != null) annotations.add( new LengthImpl( length ) );
63 if ( cbr.getClazz() != null ) {
64 for (Annotation a : cbr.annotations)
69 Class<?> arrayClass = Array.newInstance(cbr.getClazz(), 0).getClass();
70 return new BindingRequest( arrayClass, annotations );
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 );
87 if ( type instanceof StringType ) {
88 List<Annotation> annotations = new ArrayList<Annotation>();
89 StringType st = (StringType) type;
91 String pattern = st.metadata.get(StringType.KEY_PATTERN);
92 if (pattern != null) annotations.add( new PatternImpl( pattern ) );
94 String mimetype = st.metadata.get(StringType.KEY_MIMETYPE);
95 if (mimetype != null) annotations.add( new MIMETypeImpl( mimetype ) );
97 String length = st.metadata.get(StringType.KEY_LENGTH);
98 if (length != null) annotations.add( new LengthImpl( length ) );
100 return new BindingRequest( String.class, annotations );
103 if ( type instanceof NumberType ) {
104 List<Annotation> annotations = new ArrayList<Annotation>();
105 NumberType nt = (NumberType) type;
107 String unit = nt.metadata.get(NumberType.KEY_UNIT);
108 if (unit != null) annotations.add( new UnitImpl(unit) );
110 String range = nt.metadata.get(NumberType.KEY_RANGE);
111 if (range != null) annotations.add( new RangeImpl(range) );
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;
120 return new BindingRequest( clazz, annotations );
123 if ( type instanceof BooleanType ) {
124 return new BindingRequest( boolean.class );
127 if ( type instanceof VariantType ) {
128 return new BindingRequest( Variant.class );