X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.objmap2%2Fsrc%2Forg%2Fsimantics%2Fobjmap%2Fgraph%2Fannotations%2Ffactories%2FDataTypeUtils.java;h=6d3f33a1b23e98c1932f1a167b004b3ead6d73c2;hb=87d3c60a5c520cca584c54e6bc0fd8c9576141a7;hp=6fe7f9302ea51b2599df2736ce3ef8db3e4a2081;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/factories/DataTypeUtils.java b/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/factories/DataTypeUtils.java index 6fe7f9302..6d3f33a1b 100644 --- a/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/factories/DataTypeUtils.java +++ b/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/factories/DataTypeUtils.java @@ -1,57 +1,123 @@ -/******************************************************************************* - * Copyright (c) 2007, 2013 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.objmap.graph.annotations.factories; - -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.layer0.Layer0; - -public class DataTypeUtils { - - public static Resource dataTypeOfClass(ReadGraph g, Class clazz) { - Layer0 b = Layer0.getInstance(g); - if(clazz.equals(Double.class) || clazz.equals(double.class)) - return b.Double; - else if(clazz.equals(String.class)) - return b.String; - else if(clazz.equals(Integer.class) || clazz.equals(int.class)) - return b.Integer; - else if(clazz.equals(Float.class) || clazz.equals(float.class)) - return b.Float; - else if(clazz.equals(Boolean.class) || clazz.equals(boolean.class)) - return b.Boolean; - else if(clazz.equals(Long.class) || clazz.equals(long.class)) - return b.Long; - else if(clazz.equals(Byte.class) || clazz.equals(byte.class)) - return b.Byte; - - else if(clazz.equals(double[].class)) - return b.DoubleArray; - else if(clazz.equals(int[].class)) - return b.IntegerArray; - else if(clazz.equals(byte[].class)) - return b.ByteArray; - else if(clazz.equals(float[].class)) - return b.FloatArray; - else if(clazz.equals(boolean[].class)) - return b.BooleanArray; - else if(clazz.equals(String[].class)) - return b.StringArray; - else if(clazz.equals(long[].class)) - return b.LongArray; - else { - System.out.println("Couldn't find a data type for " + clazz); - return null; - } - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2013 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.objmap.graph.annotations.factories; + +import org.simantics.databoard.Datatypes; +import org.simantics.databoard.binding.error.DatatypeConstructionException; +import org.simantics.databoard.type.ArrayType; +import org.simantics.databoard.type.BooleanType; +import org.simantics.databoard.type.ByteType; +import org.simantics.databoard.type.Datatype; +import org.simantics.databoard.type.DoubleType; +import org.simantics.databoard.type.FloatType; +import org.simantics.databoard.type.IntegerType; +import org.simantics.databoard.type.LongType; +import org.simantics.databoard.type.OptionalType; +import org.simantics.databoard.type.StringType; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.layer0.Layer0; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DataTypeUtils { + + private static final Logger LOGGER = LoggerFactory.getLogger(DataTypeUtils.class); + + public static Resource dataTypeOfClass(ReadGraph g, Class clazz) { + Layer0 b = Layer0.getInstance(g); + if(clazz.equals(Double.class) || clazz.equals(double.class)) + return b.Double; + else if(clazz.equals(String.class)) + return b.String; + else if(clazz.equals(Integer.class) || clazz.equals(int.class)) + return b.Integer; + else if(clazz.equals(Float.class) || clazz.equals(float.class)) + return b.Float; + else if(clazz.equals(Boolean.class) || clazz.equals(boolean.class)) + return b.Boolean; + else if(clazz.equals(Long.class) || clazz.equals(long.class)) + return b.Long; + else if(clazz.equals(Byte.class) || clazz.equals(byte.class)) + return b.Byte; + + else if(clazz.equals(double[].class)) + return b.DoubleArray; + else if(clazz.equals(int[].class)) + return b.IntegerArray; + else if(clazz.equals(byte[].class)) + return b.ByteArray; + else if(clazz.equals(float[].class)) + return b.FloatArray; + else if(clazz.equals(boolean[].class)) + return b.BooleanArray; + else if(clazz.equals(String[].class)) + return b.StringArray; + else if(clazz.equals(long[].class)) + return b.LongArray; + else { + try { + Datatype type = Datatypes.getDatatype(clazz); + final Resource result = dataTypeOfDatatype(g, type); + if (result != null) + return result; + } catch (DatatypeConstructionException e) { + } + + LOGGER.error("No literal type found for class {}", clazz); + return null; + } + } + + public static Resource dataTypeOfDatatype(ReadGraph g, Datatype type) { + if (type instanceof OptionalType) + return dataTypeOfDatatype(g, ((OptionalType) type).getComponentType()); + + Layer0 b = Layer0.getInstance(g); + if (type instanceof DoubleType) + return b.Double; + else if(type instanceof StringType) + return b.String; + else if(type instanceof IntegerType) + return b.Integer; + else if(type instanceof FloatType) + return b.Float; + else if(type instanceof BooleanType) + return b.Boolean; + else if(type instanceof LongType) + return b.Long; + else if(type instanceof ByteType) + return b.Byte; + + else if (type instanceof ArrayType) { + type = ((ArrayType) type).componentType(); + + if (type instanceof DoubleType) + return b.DoubleArray; + else if(type instanceof IntegerType) + return b.IntegerArray; + else if(type instanceof ByteType) + return b.ByteArray; + else if(type instanceof FloatType) + return b.FloatArray; + else if(type instanceof BooleanType) + return b.BooleanArray; + else if(type instanceof StringType) + return b.StringArray; + else if(type instanceof LongType) + return b.LongArray; + } + + LOGGER.error("No literal type found for data type {}", type); + return null; + } +}