1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.databoard;
\r
14 import java.io.IOException;
\r
15 import java.io.InputStream;
\r
16 import java.nio.charset.Charset;
\r
18 import org.simantics.databoard.binding.error.BindingConstructionException;
\r
19 import org.simantics.databoard.binding.error.DatatypeConstructionException;
\r
20 import org.simantics.databoard.binding.error.RuntimeDatatypeConstructionException;
\r
21 import org.simantics.databoard.binding.reflection.ClassBindingFactory;
\r
22 import org.simantics.databoard.binding.reflection.VoidBinding;
\r
23 import org.simantics.databoard.parser.repository.DataTypeRepository;
\r
24 import org.simantics.databoard.parser.repository.DataTypeSyntaxError;
\r
25 import org.simantics.databoard.type.ArrayType;
\r
26 import org.simantics.databoard.type.BooleanType;
\r
27 import org.simantics.databoard.type.ByteType;
\r
28 import org.simantics.databoard.type.Datatype;
\r
29 import org.simantics.databoard.type.DoubleType;
\r
30 import org.simantics.databoard.type.FloatType;
\r
31 import org.simantics.databoard.type.IntegerType;
\r
32 import org.simantics.databoard.type.LongType;
\r
33 import org.simantics.databoard.type.OptionalType;
\r
34 import org.simantics.databoard.type.StringType;
\r
35 import org.simantics.databoard.type.VariantType;
\r
36 import org.simantics.databoard.util.StreamUtil;
\r
39 * This class is a facade to the data type services.
\r
41 * @author Hannu Niemisto
\r
42 * @author Toni Kalajainen
\r
44 public class Datatypes {
\r
46 public static final BooleanType BOOLEAN = new BooleanType();
\r
47 public static final ByteType BYTE = new ByteType();
\r
48 public static final IntegerType INTEGER = new IntegerType();
\r
49 public static final LongType LONG = new LongType();
\r
50 public static final FloatType FLOAT = new FloatType();
\r
51 public static final DoubleType DOUBLE = new DoubleType();
\r
52 public static final StringType STRING = new StringType();
\r
53 public static final VariantType VARIANT = new VariantType();
\r
55 public static final Datatype VOID = VoidBinding.VOID_BINDING.type();
\r
57 public static final ArrayType BOOLEAN_ARRAY = new ArrayType( BOOLEAN );
\r
58 public static final ArrayType BYTE_ARRAY = new ArrayType( BYTE );
\r
59 public static final ArrayType INTEGER_ARRAY = new ArrayType( INTEGER );
\r
60 public static final ArrayType LONG_ARRAY = new ArrayType( LONG );
\r
61 public static final ArrayType FLOAT_ARRAY = new ArrayType( FLOAT );
\r
62 public static final ArrayType DOUBLE_ARRAY = new ArrayType( DOUBLE );
\r
63 public static final ArrayType STRING_ARRAY = new ArrayType( STRING );
\r
64 public static final ArrayType VARIANT_ARRAY = new ArrayType( VARIANT );
\r
66 public static final DataTypeRepository datatypeRepository = new DataTypeRepository();
\r
68 /** Make type optional */
\r
69 public static Datatype optional( Datatype type ) { return new OptionalType( type ); }
\r
72 * Read representation from a class. DataType details and parameters
\r
73 * are read as annotations placed in the class.
\r
74 * (See org.simantics.databoard.annotations)
\r
76 * As an exception, in the subclasses of {@link Throwable}, the fields of
\r
77 * Throwable are omited.
\r
79 * @see ClassBindingFactory
\r
82 * @throws DatatypeConstructionException
\r
84 @SuppressWarnings("unchecked")
\r
85 public static <T extends Datatype> T getDatatype(Class<?> clazz)
\r
86 throws DatatypeConstructionException {
\r
88 return (T) Bindings.getBinding(clazz).type();
\r
89 } catch (BindingConstructionException e) {
\r
90 throw new DatatypeConstructionException(e);
\r
95 * Read representation from a class. DataType details and parameters
\r
96 * are read as annotations placed in the class.
\r
97 * (See org.simantics.databoard.annotations)
\r
99 * This method is used when the caller is 100% sure that binding will be
\r
100 * constructed without exceptions. Such classes
\r
101 * are all primitive types (Double, Integer, etc, arrays, DataType, ...)
\r
103 * This method is unchecked if binding construction to the clazz cannot be trusted.
\r
104 * If construction fails, a RuntimeException is thrown.
\r
106 * @see ClassBindingFactory
\r
108 * @return data type
\r
109 * @throws RuntimeDatatypeConstructionException
\r
111 @SuppressWarnings("unchecked")
\r
112 public static <T extends Datatype> T getDatatypeUnchecked(Class<?> clazz)
\r
113 throws RuntimeDatatypeConstructionException {
\r
115 return (T) Bindings.getBinding(clazz).type();
\r
116 } catch (BindingConstructionException e) {
\r
117 throw new RuntimeDatatypeConstructionException(new DatatypeConstructionException(e));
\r
122 * Adds a type to the repository.
\r
124 * @param name Name of the type
\r
125 * @param type Type to be added
\r
127 public static void addDatatype(String name, Datatype type) {
\r
128 datatypeRepository.add(name, type);
\r
132 * Get data type by name.
\r
134 * e.g. get("Vec2");
\r
137 * @return data type
\r
139 public static Datatype getDatatype(String name) {
\r
140 return datatypeRepository.get(name);
\r
144 * Parses and adds type definitions to the repository.
\r
146 * The data type can be acquired with #getType
\r
148 * e.g. "type Vec2 = { x : Double, y : Double }"
\r
150 * @param definitions Definitions in textual format.
\r
151 * @throws DataTypeSyntaxError
\r
153 public static void addDefinitions(String definitions) throws DataTypeSyntaxError {
\r
154 datatypeRepository.addDefinitions(definitions);
\r
158 * Parses an unnamed data type.
\r
160 * <a href="http://dev.simantics.org/index.php/Data_type_notation">Datatype Notation</a>
\r
162 * e.g. "{ direction : Vec, vector : Vec2 }"
\r
164 * @param typeString The textual representation of the type to be translated
\r
165 * @return Translated data type
\r
166 * @throws DataTypeSyntaxError
\r
168 public static Datatype translate(String typeString) throws DataTypeSyntaxError {
\r
169 return datatypeRepository.translate(typeString);
\r
173 Charset UTF8 = Charset.forName("UTF-8");
\r
176 InputStream is = Datatypes.class.getResourceAsStream("standardTypes.dbt");
\r
178 String defs = StreamUtil.readString(is, UTF8);
\r
179 Datatypes.datatypeRepository.addDefinitions(defs);
\r
180 } catch (IOException e) {
\r
181 throw new RuntimeException( e );
\r
182 } catch (DataTypeSyntaxError e) {
\r
183 throw new RuntimeException( e );
\r
185 try { is.close(); } catch (IOException e) {}
\r