1 /*******************************************************************************
2 * Copyright (c) 2007, 2013 Association for Decentralized Information Management
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.objmap.graph.annotations.factories;
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;
32 public class DataTypeUtils {
34 private static final Logger LOGGER = LoggerFactory.getLogger(DataTypeUtils.class);
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))
40 else if(clazz.equals(String.class))
42 else if(clazz.equals(Integer.class) || clazz.equals(int.class))
44 else if(clazz.equals(Float.class) || clazz.equals(float.class))
46 else if(clazz.equals(Boolean.class) || clazz.equals(boolean.class))
48 else if(clazz.equals(Long.class) || clazz.equals(long.class))
50 else if(clazz.equals(Byte.class) || clazz.equals(byte.class))
53 else if(clazz.equals(double[].class))
55 else if(clazz.equals(int[].class))
56 return b.IntegerArray;
57 else if(clazz.equals(byte[].class))
59 else if(clazz.equals(float[].class))
61 else if(clazz.equals(boolean[].class))
62 return b.BooleanArray;
63 else if(clazz.equals(String[].class))
65 else if(clazz.equals(long[].class))
69 Datatype type = Datatypes.getDatatype(clazz);
70 final Resource result = dataTypeOfDatatype(g, type);
73 } catch (DatatypeConstructionException e) {
76 LOGGER.error("No literal type found for class {}", clazz);
81 public static Resource dataTypeOfDatatype(ReadGraph g, Datatype type) {
82 if (type instanceof OptionalType)
83 return dataTypeOfDatatype(g, ((OptionalType) type).getComponentType());
85 Layer0 b = Layer0.getInstance(g);
86 if (type instanceof DoubleType)
88 else if(type instanceof StringType)
90 else if(type instanceof IntegerType)
92 else if(type instanceof FloatType)
94 else if(type instanceof BooleanType)
96 else if(type instanceof LongType)
98 else if(type instanceof ByteType)
101 else if (type instanceof ArrayType) {
102 type = ((ArrayType) type).componentType();
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)
110 else if(type instanceof FloatType)
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)
120 LOGGER.error("No literal type found for data type {}", type);