]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/factories/DataTypeUtils.java
6d3f33a1b23e98c1932f1a167b004b3ead6d73c2
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / graph / annotations / factories / DataTypeUtils.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2013 Association for Decentralized Information Management
3  * in 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.objmap.graph.annotations.factories;
13
14 import org.simantics.databoard.Datatypes;
15 import org.simantics.databoard.binding.error.DatatypeConstructionException;
16 import org.simantics.databoard.type.ArrayType;
17 import org.simantics.databoard.type.BooleanType;
18 import org.simantics.databoard.type.ByteType;
19 import org.simantics.databoard.type.Datatype;
20 import org.simantics.databoard.type.DoubleType;
21 import org.simantics.databoard.type.FloatType;
22 import org.simantics.databoard.type.IntegerType;
23 import org.simantics.databoard.type.LongType;
24 import org.simantics.databoard.type.OptionalType;
25 import org.simantics.databoard.type.StringType;
26 import org.simantics.db.ReadGraph;
27 import org.simantics.db.Resource;
28 import org.simantics.layer0.Layer0;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class DataTypeUtils {
33     
34     private static final Logger LOGGER = LoggerFactory.getLogger(DataTypeUtils.class);
35
36     public static Resource dataTypeOfClass(ReadGraph g, Class<?> clazz) {
37         Layer0 b = Layer0.getInstance(g);
38         if(clazz.equals(Double.class) || clazz.equals(double.class))
39             return b.Double;
40         else if(clazz.equals(String.class))
41             return b.String;
42         else if(clazz.equals(Integer.class) || clazz.equals(int.class))
43             return b.Integer;
44         else if(clazz.equals(Float.class) || clazz.equals(float.class))
45             return b.Float;
46         else if(clazz.equals(Boolean.class) || clazz.equals(boolean.class))
47             return b.Boolean;
48         else if(clazz.equals(Long.class) || clazz.equals(long.class))
49             return b.Long;
50         else if(clazz.equals(Byte.class) || clazz.equals(byte.class))
51             return b.Byte;
52         
53         else if(clazz.equals(double[].class))
54             return b.DoubleArray;
55         else if(clazz.equals(int[].class))
56             return b.IntegerArray;
57         else if(clazz.equals(byte[].class))
58             return b.ByteArray;
59         else if(clazz.equals(float[].class))
60             return b.FloatArray;
61         else if(clazz.equals(boolean[].class))
62             return b.BooleanArray;
63         else if(clazz.equals(String[].class))
64             return b.StringArray;
65         else if(clazz.equals(long[].class))
66             return b.LongArray;
67         else {
68             try {
69                 Datatype type = Datatypes.getDatatype(clazz);
70                 final Resource result = dataTypeOfDatatype(g, type);
71                 if (result != null)
72                     return result;
73             } catch (DatatypeConstructionException e) {
74             }
75             
76             LOGGER.error("No literal type found for class {}", clazz);
77             return null;
78         }
79     }
80
81     public static Resource dataTypeOfDatatype(ReadGraph g, Datatype type) {
82         if (type instanceof OptionalType)
83             return dataTypeOfDatatype(g, ((OptionalType) type).getComponentType());
84         
85         Layer0 b = Layer0.getInstance(g);
86         if (type instanceof DoubleType)
87             return b.Double;
88         else if(type instanceof StringType)
89             return b.String;
90         else if(type instanceof IntegerType)
91             return b.Integer;
92         else if(type instanceof FloatType)
93             return b.Float;
94         else if(type instanceof BooleanType)
95             return b.Boolean;
96         else if(type instanceof LongType)
97             return b.Long;
98         else if(type instanceof ByteType)
99             return b.Byte;
100         
101         else if (type instanceof ArrayType) {
102             type = ((ArrayType) type).componentType();
103             
104             if (type instanceof DoubleType)
105                 return b.DoubleArray;
106             else if(type instanceof IntegerType)
107                 return b.IntegerArray;
108             else if(type instanceof ByteType)
109                 return b.ByteArray;
110             else if(type instanceof FloatType)
111                 return b.FloatArray;
112             else if(type instanceof BooleanType)
113                 return b.BooleanArray;
114             else if(type instanceof StringType)
115                 return b.StringArray;
116             else if(type instanceof LongType)
117                 return b.LongArray;
118         }
119         
120         LOGGER.error("No literal type found for data type {}", type);
121         return null;
122     }
123 }