]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  *  Copyright (c) 2010 Association for Decentralized Information Management in\r
3  *  Industry THTH ry.\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
8  *\r
9  *  Contributors:\r
10  *      VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.databoard.binding.impl;
13
14 import org.simantics.databoard.binding.DoubleBinding;\r
15 import org.simantics.databoard.binding.error.BindingException;\r
16 import org.simantics.databoard.binding.error.UnsupportedOperationException;\r
17 import org.simantics.databoard.type.DoubleType;\r
18
19 /**
20  * Binds DoubleType to java.lang.Double
21  *
22  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
23  */
24 public class DoubleBindingDefault extends DoubleBinding {
25
26         public DoubleBindingDefault(DoubleType type) {
27                 super(type);
28         }
29         
30         @Override
31         public DoubleType type() {
32                 return (DoubleType) type;
33         }
34         
35         public Object create(Double value) {
36                 return value;
37         }
38
39         public Object create(double value) {
40                 return Double.valueOf(value);
41         }
42         
43         @Override
44         public Object create(Number value) 
45         {
46                 if (value.getClass()==Double.class) return value;
47                 return Double.valueOf( value.doubleValue() );
48         }
49
50         @Override
51         public Object create(String value) throws BindingException {\r
52                 try {
53                         return Double.parseDouble(value);\r
54                 } catch (java.lang.NumberFormatException e) {\r
55                         throw new BindingException( e );\r
56                 }
57         }
58         
59         public Double getValue(Object o) 
60         throws BindingException
61         {
62                 if (o.getClass()!=Double.class)
63                         throw new BindingException("Unexpected class "+o.getClass().getSimpleName()+", Double expected");
64                 return ((Double)o);
65         }
66         
67         public double getValue_(Object o)
68         throws BindingException
69         {
70                 if (o.getClass()!=Double.class)
71                         throw new BindingException("Unexpected class "+o.getClass().getSimpleName()+", Double expected");
72                 return ((Double)o);
73         }
74         
75         @Override
76         public void setValue(Object obj, Number value) throws BindingException {
77                 throw new UnsupportedOperationException("Cannot change the value of immutable java.lang.Double");
78         }
79         
80         public void setValue(Object obj, double value) throws BindingException {
81                 throw new UnsupportedOperationException("Cannot change the value of immutable java.lang.Double");
82         }
83         
84     @Override
85         public boolean isInstance(Object obj) {
86                 return obj instanceof Double;
87         }
88                 
89     @Override
90     public boolean isImmutable() {
91         return true;
92     }
93     \r
94     @Override\r
95     public int compare(Object o1, Object o2)\r
96     {\r
97         Double d1 = (Double) o1;\r
98         Double d2 = (Double) o2;\r
99         return d1.compareTo(d2);\r
100     }\r
101     
102 }
103