]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/Datatypes.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / Datatypes.java
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/Datatypes.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/Datatypes.java
new file mode 100644 (file)
index 0000000..24bfa6c
--- /dev/null
@@ -0,0 +1,192 @@
+/*******************************************************************************\r
+ *  Copyright (c) 2010 Association for Decentralized Information Management in\r
+ *  Industry THTH ry.\r
+ *  All rights reserved. This program and the accompanying materials\r
+ *  are made available under the terms of the Eclipse Public License v1.0\r
+ *  which accompanies this distribution, and is available at\r
+ *  http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ *  Contributors:\r
+ *      VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.databoard;\r
+\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.nio.charset.Charset;\r
+\r
+import org.simantics.databoard.binding.error.BindingConstructionException;\r
+import org.simantics.databoard.binding.error.DatatypeConstructionException;\r
+import org.simantics.databoard.binding.error.RuntimeDatatypeConstructionException;\r
+import org.simantics.databoard.binding.reflection.ClassBindingFactory;\r
+import org.simantics.databoard.binding.reflection.VoidBinding;\r
+import org.simantics.databoard.parser.repository.DataTypeRepository;\r
+import org.simantics.databoard.parser.repository.DataTypeSyntaxError;\r
+import org.simantics.databoard.type.ArrayType;\r
+import org.simantics.databoard.type.BooleanType;\r
+import org.simantics.databoard.type.ByteType;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.databoard.type.DoubleType;\r
+import org.simantics.databoard.type.FloatType;\r
+import org.simantics.databoard.type.IntegerType;\r
+import org.simantics.databoard.type.LongType;\r
+import org.simantics.databoard.type.OptionalType;\r
+import org.simantics.databoard.type.StringType;\r
+import org.simantics.databoard.type.VariantType;\r
+import org.simantics.databoard.util.StreamUtil;\r
+\r
+/**\r
+ * This class is a facade to the data type services. \r
+ * \r
+ * @author Hannu Niemisto\r
+ * @author Toni Kalajainen\r
+ */\r
+public class Datatypes {\r
+       \r
+       public static final BooleanType   BOOLEAN = new BooleanType();\r
+       public static final ByteType      BYTE    = new ByteType();\r
+       public static final IntegerType   INTEGER = new IntegerType();\r
+       public static final LongType      LONG    = new LongType();\r
+       public static final FloatType     FLOAT   = new FloatType();\r
+       public static final DoubleType    DOUBLE  = new DoubleType();\r
+       public static final StringType    STRING  = new StringType();\r
+       public static final VariantType   VARIANT = new VariantType();\r
+       \r
+       public static final Datatype      VOID    = VoidBinding.VOID_BINDING.type();\r
+\r
+       public static final ArrayType     BOOLEAN_ARRAY = new ArrayType( BOOLEAN );\r
+       public static final ArrayType     BYTE_ARRAY    = new ArrayType( BYTE );\r
+       public static final ArrayType     INTEGER_ARRAY = new ArrayType( INTEGER );\r
+       public static final ArrayType     LONG_ARRAY    = new ArrayType( LONG );\r
+       public static final ArrayType     FLOAT_ARRAY   = new ArrayType( FLOAT );\r
+       public static final ArrayType     DOUBLE_ARRAY  = new ArrayType( DOUBLE );\r
+       public static final ArrayType     STRING_ARRAY  = new ArrayType( STRING );\r
+       public static final ArrayType     VARIANT_ARRAY  = new ArrayType( VARIANT );\r
+       \r
+       public static final DataTypeRepository datatypeRepository = new DataTypeRepository();\r
+\r
+       /** Make type optional */\r
+       public static Datatype optional( Datatype type ) { return new OptionalType( type ); }\r
+       \r
+       /**\r
+        * Read representation from a class. DataType details and parameters\r
+        * are read as annotations placed in the class.  \r
+        * (See org.simantics.databoard.annotations)  \r
+        * <p>\r
+        * As an exception, in the subclasses of {@link Throwable}, the fields of\r
+        * Throwable are omited.\r
+        * \r
+        * @see ClassBindingFactory  \r
+        * @param clazz\r
+        * @return data type\r
+        * @throws DatatypeConstructionException \r
+        */\r
+       @SuppressWarnings("unchecked")\r
+       public static <T extends Datatype> T getDatatype(Class<?> clazz) \r
+       throws DatatypeConstructionException {\r
+               try {\r
+                       return (T) Bindings.getBinding(clazz).type();\r
+               } catch (BindingConstructionException e) {\r
+                       throw new DatatypeConstructionException(e);\r
+               }\r
+       }\r
+       \r
+       /**\r
+        * Read representation from a class. DataType details and parameters\r
+        * are read as annotations placed in the class.  \r
+        * (See org.simantics.databoard.annotations)  \r
+        * <p>\r
+        * This method is used when the caller is 100% sure that binding will be \r
+        * constructed without exceptions. Such classes\r
+        * are all primitive types (Double, Integer, etc, arrays, DataType, ...)\r
+        * \r
+        * This method is unchecked if binding construction to the clazz cannot be trusted.\r
+        * If construction fails, a RuntimeException is thrown.\r
+        * \r
+        * @see ClassBindingFactory  \r
+        * @param clazz\r
+        * @return data type\r
+        * @throws RuntimeDatatypeConstructionException \r
+        */\r
+       @SuppressWarnings("unchecked")\r
+       public static <T extends Datatype> T getDatatypeUnchecked(Class<?> clazz) \r
+       throws RuntimeDatatypeConstructionException {\r
+               try {\r
+                       return (T) Bindings.getBinding(clazz).type();\r
+               } catch (BindingConstructionException e) {\r
+                       throw new RuntimeDatatypeConstructionException(new DatatypeConstructionException(e));\r
+               }\r
+       }       \r
+       \r
+       /**\r
+        * Adds a type to the repository.\r
+        * \r
+        * @param name Name of the type\r
+        * @param type Type to be added\r
+        */\r
+       public static void addDatatype(String name, Datatype type) {\r
+               datatypeRepository.add(name, type);\r
+       }\r
+       \r
+       /**\r
+        * Get data type by name.\r
+        * \r
+        * e.g. get("Vec2");\r
+        * \r
+        * @param name\r
+        * @return data type\r
+        */\r
+       public static Datatype getDatatype(String name) {\r
+               return datatypeRepository.get(name);\r
+       }       \r
+       \r
+       /**\r
+        * Parses and adds type definitions to the repository.\r
+        * \r
+        * The data type can be acquired with #getType\r
+        * \r
+        * e.g. "type Vec2 = { x : Double, y : Double }"\r
+        * \r
+        * @param definitions Definitions in textual format.\r
+        * @throws DataTypeSyntaxError \r
+        */\r
+       public static void addDefinitions(String definitions) throws DataTypeSyntaxError {\r
+               datatypeRepository.addDefinitions(definitions);\r
+       }\r
+       \r
+       /**\r
+        * Parses an unnamed data type.\r
+        * \r
+        * <a href="http://dev.simantics.org/index.php/Data_type_notation">Datatype Notation</a>\r
+        * \r
+        * e.g. "{ direction : Vec, vector : Vec2 }" \r
+        * \r
+        * @param typeString The textual representation of the type to be translated\r
+        * @return Translated data type\r
+        * @throws DataTypeSyntaxError \r
+        */\r
+       public static Datatype translate(String typeString) throws DataTypeSyntaxError {\r
+               return datatypeRepository.translate(typeString);\r
+       }       \r
+\r
+       static {\r
+               Charset UTF8        = Charset.forName("UTF-8");\r
+               \r
+               // Read File\r
+               InputStream is = Datatypes.class.getResourceAsStream("standardTypes.dbt");\r
+               try {\r
+                       String defs = StreamUtil.readString(is, UTF8);\r
+                       Datatypes.datatypeRepository.addDefinitions(defs);                      \r
+               } catch (IOException e) {\r
+                       throw new RuntimeException( e );\r
+               } catch (DataTypeSyntaxError e) {\r
+                       throw new RuntimeException( e );\r
+               } finally {\r
+                       try { is.close(); } catch (IOException e) {}\r
+               }\r
+               \r
+       }\r
+       \r
+}\r
+\r
+\r