]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/NumberBinding.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / NumberBinding.java
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/NumberBinding.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/NumberBinding.java
new file mode 100644 (file)
index 0000000..e5cf14e
--- /dev/null
@@ -0,0 +1,139 @@
+/*******************************************************************************\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.binding;
+
+import java.util.Set;\r
+\r
+import org.simantics.databoard.accessor.reference.ChildReference;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.binding.error.RuntimeBindingException;\r
+import org.simantics.databoard.binding.impl.BindingPrintContext;\r
+import org.simantics.databoard.type.NumberType;\r
+import org.simantics.databoard.util.Range;\r
+
+/**
+ * Super class for number types. 
+ *
+ * @author Toni Kalajainen
+ */
+public abstract class NumberBinding extends Binding {
+
+       /**
+        * Create value by converting it from any Number instance to a Number 
+        * instance of this Binding type.
+        * 
+        * NOTE WARNING! Using this method may lose precision or value in the conversion. 
+        *   E.g. Double to Integer, or Long to Byte
+        * 
+        * @param value
+        * @return the value in the format of the binding type
+        */
+       public abstract Object create(Number value)
+       throws BindingException;
+       
+       /**
+        * Creates a value from its string representation
+        * @param value
+        * @return number
+        */
+       public abstract Object create(String value)
+       throws BindingException;
+       
+       /**
+        * Get Data type
+        * 
+        * @return data type
+        */
+       @Override
+       public NumberType type() {
+               return (NumberType) type;
+       }
+       
+       /**
+        * Get numeric value of an object
+        * 
+        * @param obj object
+        * @return Number
+        * @throws BindingException thrown if obj is incorrect class
+        */
+       public Number getValue(Object obj) throws BindingException {
+               return ((Number)obj);
+       }
+       
+       public abstract void setValue(Object obj, Number value) throws BindingException;
+       
+       /**
+        * Assert the obj is a valid number. 
+        * 
+        * This asserts 
+        *  1. The value is within the range defined in the NumberType
+        * 
+        * @throws BindingException
+        */
+       @Override
+       public void assertInstaceIsValid(Object obj, Set<Object> validInstances) throws BindingException {
+               if (!isInstance(obj)) throw new BindingException("Not a correct instance");             
+               NumberType type = type();
+               Range range = type.getRange();
+               if (range!=null) {
+                       Number value = getValue(obj);
+                       if (!range.contains(value)) {
+                               throw new BindingException("Value ("+value+") out of range ("+range+")");
+                       }
+               }
+       }
+       
+       public Object createUnchecked(String value) throws RuntimeBindingException {
+               try {
+                       return create(value);
+               } catch (BindingException e) {
+                       return new RuntimeBindingException(e);
+               }
+       }
+       public Object createUnchecked(Number value) throws RuntimeBindingException {
+               try {
+                       return create(value);
+               } catch (BindingException e) {
+                       return new RuntimeBindingException(e);
+               }
+       }\r
+       \r
+       @Override\r
+       protected void toString(Object value, BindingPrintContext ctx) throws BindingException {\r
+               ctx.b.append(getValue(value).toString());\r
+       }\r
+       \r
+       @Override\r
+       public void readFrom(Binding srcBinding, Object src, Object dst)\r
+                       throws BindingException {\r
+               Number v = ((NumberBinding)srcBinding).getValue(src);\r
+               setValue(dst, v);               \r
+       }\r
+       \r
+       @Override\r
+       public Binding getComponentBinding(ChildReference path) {\r
+               if (path==null) return this;            \r
+               throw new IllegalArgumentException();\r
+       }       \r
+       
+       @Override\r
+       public int getComponentCount() {\r
+               return 0;\r
+       }\r
+       \r
+       @Override\r
+       public Binding getComponentBinding(int index) {\r
+               throw new IllegalArgumentException();\r
+       }\r
+       
+}
+