]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/type/NumberType.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/type/NumberType.java
new file mode 100644 (file)
index 0000000..53ce107
--- /dev/null
@@ -0,0 +1,118 @@
+/*******************************************************************************\r
+ *  Copyright (c) 2010 Association for Decentralized Information Management in\r
+ *  Industry THTH ry.\r
+ *  All rights reserved. This program and the accompanying materials\r
+ *  are made available under the terms of the Eclipse Public License v1.0\r
+ *  which accompanies this distribution, and is available at\r
+ *  http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ *  Contributors:\r
+ *      VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.databoard.type;
+
+import org.simantics.databoard.Units;\r
+import org.simantics.databoard.accessor.error.ReferenceException;\r
+import org.simantics.databoard.accessor.reference.ChildReference;\r
+import org.simantics.databoard.util.Range;\r
+import org.simantics.databoard.util.RangeException;\r
+
+/**
+ * NumberType is comparable primitive type:
+ *  ByteType, IntegerType, DoubleType, LongType or FloatType 
+ * 
+ */
+public abstract class NumberType extends Datatype {
+       \r
+       /** Unit string describes the quantity, magnitude and unit of the value. */\r
+       public static final String KEY_UNIT = "unit";\r
+       \r
+       /** Value ranges that are valid. */\r
+       public static final String KEY_RANGE = "range";\r
+       \r
+       // Cached Range\r
+       private transient Range range;\r
+       private transient String rangeIsForStr;\r
+               
+       /**
+        * Get the unit type.
+        * 
+        * Unit string describes the quentity, magnity and unit of the value.
+        *
+        * @return the unit
+        * @see Units 
+        */
+       public String getUnit() {
+               return metadata.get( KEY_UNIT );
+       }
+
+       /**
+        * Set the unit type.
+        * Unit string describes the quentity, magnity and unit of the value.
+        *      
+        * @param unit the unit type
+        * @see Units 
+        */
+       public void setUnit(String unit) {
+               if (unit==null) metadata.remove( KEY_UNIT ); \r
+               else metadata.put( KEY_UNIT, unit );
+       }
+  
+       public Range getRange() {\r
+               String rangeStr = metadata.get( KEY_RANGE );\r
+               if (rangeStr == null) return null;\r
+               if (range != null && rangeStr!=null && rangeStr==rangeIsForStr) return range;\r
+               try {\r
+                       rangeIsForStr = rangeStr;\r
+                       range = Range.valueOf( rangeStr );\r
+               } catch (RangeException e) {\r
+                       range = null;\r
+               }\r
+               return range;\r
+       }
+       
+       public String getRangeStr() {
+               return metadata.get( KEY_RANGE );
+       }
+
+       public void setRange(String range) {\r
+               if (range==null) metadata.remove( KEY_RANGE ); else
+       metadata.put( KEY_RANGE, range );
+       }
+       
+       public void setRange(Range range) {\r
+               this.range = range;\r
+               if (range==null) {\r
+                       rangeIsForStr = null;\r
+                       metadata.remove( KEY_RANGE );\r
+               } else {\r
+                       rangeIsForStr = range.toString();
+                       metadata.put( KEY_RANGE, rangeIsForStr );\r
+               }\r
+       }\r
+       \r
+    @Override\r
+    public int getComponentCount() {\r
+       return 0;\r
+    }\r
+    \r
+    @Override\r
+    public Datatype getComponentType(int index) {\r
+       throw new IllegalArgumentException();\r
+    }\r
+    \r
+    @Override\r
+    public Datatype getComponentType(ChildReference path) {\r
+       if (path==null) return this;\r
+       throw new IllegalArgumentException();\r
+    }  \r
+\r
+       @SuppressWarnings("unchecked")\r
+       @Override\r
+       public <T extends Datatype> T getChildType(ChildReference reference) throws ReferenceException {\r
+               if (reference==null) return (T) this;\r
+               throw new ReferenceException(reference.getClass()+" is not a subreference of NumberType");      \r
+       }\r
+    
+}
+