1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.type;
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;
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. */
28 public static final String KEY_UNIT = "unit";
30 /** Value ranges that are valid. */
31 public static final String KEY_RANGE = "range";
34 private transient Range range;
35 private transient String rangeIsForStr;
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 );
58 else metadata.put( KEY_UNIT, unit );
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;
66 rangeIsForStr = rangeStr;
67 range = Range.valueOf( rangeStr );
68 } catch (RangeException e) {
74 public String getRangeStr() {
75 return metadata.get( KEY_RANGE );
78 public void setRange(String range) {
79 if (range==null) metadata.remove( KEY_RANGE ); else
80 metadata.put( KEY_RANGE, range );
83 public void setRange(Range range) {
87 metadata.remove( KEY_RANGE );
89 rangeIsForStr = range.toString();
90 metadata.put( KEY_RANGE, rangeIsForStr );
95 public int getComponentCount() {
100 public Datatype getComponentType(int index) {
101 throw new IllegalArgumentException();
105 public Datatype getComponentType(ChildReference path) {
106 if (path==null) return this;
107 throw new IllegalArgumentException();
110 @SuppressWarnings("unchecked")
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");