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.type;
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
21 * NumberType is comparable primitive type:
22 * ByteType, IntegerType, DoubleType, LongType or FloatType
25 public abstract class NumberType extends Datatype {
27 /** Unit string describes the quantity, magnitude and unit of the value. */
\r
28 public static final String KEY_UNIT = "unit";
\r
30 /** Value ranges that are valid. */
\r
31 public static final String KEY_RANGE = "range";
\r
34 private transient Range range;
\r
35 private transient String rangeIsForStr;
\r
40 * Unit string describes the quentity, magnity and unit of the value.
45 public String getUnit() {
46 return metadata.get( KEY_UNIT );
51 * Unit string describes the quentity, magnity and unit of the value.
53 * @param unit the unit type
56 public void setUnit(String unit) {
57 if (unit==null) metadata.remove( KEY_UNIT );
\r
58 else metadata.put( KEY_UNIT, unit );
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
66 rangeIsForStr = rangeStr;
\r
67 range = Range.valueOf( rangeStr );
\r
68 } catch (RangeException e) {
\r
74 public String getRangeStr() {
75 return metadata.get( KEY_RANGE );
78 public void setRange(String range) {
\r
79 if (range==null) metadata.remove( KEY_RANGE ); else
80 metadata.put( KEY_RANGE, range );
83 public void setRange(Range range) {
\r
86 rangeIsForStr = null;
\r
87 metadata.remove( KEY_RANGE );
\r
89 rangeIsForStr = range.toString();
90 metadata.put( KEY_RANGE, rangeIsForStr );
\r
95 public int getComponentCount() {
\r
100 public Datatype getComponentType(int index) {
\r
101 throw new IllegalArgumentException();
\r
105 public Datatype getComponentType(ChildReference path) {
\r
106 if (path==null) return this;
\r
107 throw new IllegalArgumentException();
\r
110 @SuppressWarnings("unchecked")
\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