]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/type/NumberType.java
Added addFirst/After/Before + remove SCL functions for Ordered Sets
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / type / NumberType.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
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
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.type;
13
14 import org.simantics.databoard.Units;
15 import org.simantics.databoard.accessor.error.ReferenceException;
16 import org.simantics.databoard.accessor.reference.ChildReference;
17 import org.simantics.databoard.util.Range;
18 import org.simantics.databoard.util.RangeException;
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         
27         /** Unit string describes the quantity, magnitude and unit of the value. */
28         public static final String KEY_UNIT = "unit";
29         
30         /** Value ranges that are valid. */
31         public static final String KEY_RANGE = "range";
32         
33         // Cached Range
34         private transient Range range;
35         private transient String rangeIsForStr;
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 ); 
58                 else metadata.put( KEY_UNIT, unit );
59         }
60   
61         public Range getRange() {
62                 String rangeStr = metadata.get( KEY_RANGE );
63                 if (rangeStr == null) return null;
64                 if (range != null && rangeStr!=null && rangeStr==rangeIsForStr) return range;
65                 try {
66                         rangeIsForStr = rangeStr;
67                         range = Range.valueOf( rangeStr );
68                 } catch (RangeException e) {
69                         range = null;
70                 }
71                 return range;
72         }
73         
74         public String getRangeStr() {
75                 return metadata.get( KEY_RANGE );
76         }
77
78         public void setRange(String range) {
79                 if (range==null) metadata.remove( KEY_RANGE ); else
80         metadata.put( KEY_RANGE, range );
81         }
82         
83         public void setRange(Range range) {
84                 this.range = range;
85                 if (range==null) {
86                         rangeIsForStr = null;
87                         metadata.remove( KEY_RANGE );
88                 } else {
89                         rangeIsForStr = range.toString();
90                         metadata.put( KEY_RANGE, rangeIsForStr );
91                 }
92         }
93         
94     @Override
95     public int getComponentCount() {
96         return 0;
97     }
98     
99     @Override
100     public Datatype getComponentType(int index) {
101         throw new IllegalArgumentException();
102     }
103     
104     @Override
105     public Datatype getComponentType(ChildReference path) {
106         if (path==null) return this;
107         throw new IllegalArgumentException();
108     }   
109
110         @SuppressWarnings("unchecked")
111         @Override
112         public <T extends Datatype> T getChildType(ChildReference reference) throws ReferenceException {
113                 if (reference==null) return (T) this;
114                 throw new ReferenceException(reference.getClass()+" is not a subreference of NumberType");      
115         }
116     
117 }
118