]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/DoubleBindingDefault.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / impl / DoubleBindingDefault.java
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/DoubleBindingDefault.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/DoubleBindingDefault.java
new file mode 100644 (file)
index 0000000..cc39c30
--- /dev/null
@@ -0,0 +1,103 @@
+/*******************************************************************************\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.impl;
+
+import org.simantics.databoard.binding.DoubleBinding;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.binding.error.UnsupportedOperationException;\r
+import org.simantics.databoard.type.DoubleType;\r
+
+/**
+ * Binds DoubleType to java.lang.Double
+ *
+ * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
+ */
+public class DoubleBindingDefault extends DoubleBinding {
+
+       public DoubleBindingDefault(DoubleType type) {
+               super(type);
+       }
+       
+       @Override
+       public DoubleType type() {
+               return (DoubleType) type;
+       }
+       
+       public Object create(Double value) {
+               return value;
+       }
+
+       public Object create(double value) {
+               return Double.valueOf(value);
+       }
+       
+       @Override
+       public Object create(Number value) 
+       {
+               if (value.getClass()==Double.class) return value;
+               return Double.valueOf( value.doubleValue() );
+       }
+
+       @Override
+       public Object create(String value) throws BindingException {\r
+               try {
+                       return Double.parseDouble(value);\r
+               } catch (java.lang.NumberFormatException e) {\r
+                       throw new BindingException( e );\r
+               }
+       }
+       
+       public Double getValue(Object o) 
+       throws BindingException
+       {
+               if (o.getClass()!=Double.class)
+                       throw new BindingException("Unexpected class "+o.getClass().getSimpleName()+", Double expected");
+               return ((Double)o);
+       }
+       
+       public double getValue_(Object o)
+       throws BindingException
+       {
+               if (o.getClass()!=Double.class)
+                       throw new BindingException("Unexpected class "+o.getClass().getSimpleName()+", Double expected");
+               return ((Double)o);
+       }
+       
+       @Override
+       public void setValue(Object obj, Number value) throws BindingException {
+               throw new UnsupportedOperationException("Cannot change the value of immutable java.lang.Double");
+       }
+       
+       public void setValue(Object obj, double value) throws BindingException {
+               throw new UnsupportedOperationException("Cannot change the value of immutable java.lang.Double");
+       }
+       
+    @Override
+       public boolean isInstance(Object obj) {
+               return obj instanceof Double;
+       }
+               
+    @Override
+    public boolean isImmutable() {
+       return true;
+    }
+    \r
+    @Override\r
+    public int compare(Object o1, Object o2)\r
+    {\r
+       Double d1 = (Double) o1;\r
+       Double d2 = (Double) o2;\r
+       return d1.compareTo(d2);\r
+    }\r
+    
+}
+