]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/DoubleBindingDefault.java
Merge "Remove unnecessary getComparableKey from HashMapBinding"
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / impl / DoubleBindingDefault.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
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
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.binding.impl;
13
14 import org.simantics.databoard.binding.DoubleBinding;
15 import org.simantics.databoard.binding.error.BindingException;
16 import org.simantics.databoard.binding.error.UnsupportedOperationException;
17 import org.simantics.databoard.type.DoubleType;
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 {
52                 try {
53                         return Double.parseDouble(value);
54                 } catch (java.lang.NumberFormatException e) {
55                         throw new BindingException( e );
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     
94     @Override
95     public int compare(Object o1, Object o2)
96     {
97         Double d1 = (Double) o1;
98         Double d2 = (Double) o2;
99         return d1.compareTo(d2);
100     }
101     
102 }
103