]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/type/NumberType.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / type / NumberType.java
1 /*******************************************************************************\r
2  *  Copyright (c) 2010 Association for Decentralized Information Management in\r
3  *  Industry THTH ry.\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
8  *\r
9  *  Contributors:\r
10  *      VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.databoard.type;
13
14 import org.simantics.databoard.Units;\r
15 import org.simantics.databoard.accessor.error.ReferenceException;\r
16 import org.simantics.databoard.accessor.reference.ChildReference;\r
17 import org.simantics.databoard.util.Range;\r
18 import org.simantics.databoard.util.RangeException;\r
19
20 /**
21  * NumberType is comparable primitive type:
22  *  ByteType, IntegerType, DoubleType, LongType or FloatType 
23  * 
24  */
25 public abstract class NumberType extends Datatype {
26         \r
27         /** Unit string describes the quantity, magnitude and unit of the value. */\r
28         public static final String KEY_UNIT = "unit";\r
29         \r
30         /** Value ranges that are valid. */\r
31         public static final String KEY_RANGE = "range";\r
32         \r
33         // Cached Range\r
34         private transient Range range;\r
35         private transient String rangeIsForStr;\r
36                 
37         /**
38          * Get the unit type.
39          * 
40          * Unit string describes the quentity, magnity and unit of the value.
41          *
42          * @return the unit
43          * @see Units 
44          */
45         public String getUnit() {
46                 return metadata.get( KEY_UNIT );
47         }
48
49         /**
50          * Set the unit type.
51          * Unit string describes the quentity, magnity and unit of the value.
52          *      
53          * @param unit the unit type
54          * @see Units 
55          */
56         public void setUnit(String unit) {
57                 if (unit==null) metadata.remove( KEY_UNIT ); \r
58                 else metadata.put( KEY_UNIT, unit );
59         }
60   
61         public Range getRange() {\r
62                 String rangeStr = metadata.get( KEY_RANGE );\r
63                 if (rangeStr == null) return null;\r
64                 if (range != null && rangeStr!=null && rangeStr==rangeIsForStr) return range;\r
65                 try {\r
66                         rangeIsForStr = rangeStr;\r
67                         range = Range.valueOf( rangeStr );\r
68                 } catch (RangeException e) {\r
69                         range = null;\r
70                 }\r
71                 return range;\r
72         }
73         
74         public String getRangeStr() {
75                 return metadata.get( KEY_RANGE );
76         }
77
78         public void setRange(String range) {\r
79                 if (range==null) metadata.remove( KEY_RANGE ); else
80         metadata.put( KEY_RANGE, range );
81         }
82         
83         public void setRange(Range range) {\r
84                 this.range = range;\r
85                 if (range==null) {\r
86                         rangeIsForStr = null;\r
87                         metadata.remove( KEY_RANGE );\r
88                 } else {\r
89                         rangeIsForStr = range.toString();
90                         metadata.put( KEY_RANGE, rangeIsForStr );\r
91                 }\r
92         }\r
93         \r
94     @Override\r
95     public int getComponentCount() {\r
96         return 0;\r
97     }\r
98     \r
99     @Override\r
100     public Datatype getComponentType(int index) {\r
101         throw new IllegalArgumentException();\r
102     }\r
103     \r
104     @Override\r
105     public Datatype getComponentType(ChildReference path) {\r
106         if (path==null) return this;\r
107         throw new IllegalArgumentException();\r
108     }   \r
109 \r
110         @SuppressWarnings("unchecked")\r
111         @Override\r
112         public <T extends Datatype> T getChildType(ChildReference reference) throws ReferenceException {\r
113                 if (reference==null) return (T) this;\r
114                 throw new ReferenceException(reference.getClass()+" is not a subreference of NumberType");      \r
115         }\r
116     
117 }
118