]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/FloatBindingDefault.java
Merge "Remove unnecessary getComparableKey from HashMapBinding"
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / impl / FloatBindingDefault.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.FloatBinding;
15 import org.simantics.databoard.binding.error.BindingException;
16 import org.simantics.databoard.binding.error.UnsupportedOperationException;
17 import org.simantics.databoard.type.FloatType;
18
19 /**
20  * Binds FloatType to java.lang.Float-class.
21  *
22  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
23  */
24 public class FloatBindingDefault extends FloatBinding {
25
26         public FloatBindingDefault(FloatType type) {
27                 super(type);
28         }
29
30         public Object create(Float value) {
31                 return value;
32         }
33         
34         public Object create(float value) {
35                 return Float.valueOf(value);
36         }
37         
38         @Override
39         public Object create(Number value) 
40         {
41                 if (value.getClass()==Float.class) return value;
42                 return Float.valueOf( value.floatValue() );
43         }
44     
45         @Override
46         public Object create(String value) throws BindingException {
47                 try  {
48                         return Float.parseFloat(value);
49                 } catch (java.lang.NumberFormatException e) {
50                         throw new BindingException( e );
51                 }
52         }
53         
54     @Override
55         public boolean isInstance(Object obj) {
56                 return obj instanceof Float;
57         }
58         
59         public Float getValue(Object o) 
60         throws BindingException {
61                 if (o.getClass()!=Float.class)
62                         throw new BindingException("Unexpected class "+o.getClass().getSimpleName()+", Float expected");                
63                 return ((Float)o);
64         }
65
66         public float getValue_(Object o) throws BindingException {
67                 if (o.getClass()!=Float.class)
68                         throw new BindingException("Unexpected class "+o.getClass().getSimpleName()+", Float expected");                
69                 return ((Float)o);              
70         }
71         
72         @Override
73         public void setValue(Object obj, Number value) throws BindingException {
74                 throw new UnsupportedOperationException("Cannot change the value of immutable java.lang.Float");
75         }
76
77         public void setValue(Object obj, float value) throws BindingException {
78                 throw new UnsupportedOperationException("Cannot change the value of immutable java.lang.Float");
79         }
80         
81         @Override
82         public boolean isImmutable() {
83                 return true;
84         }
85         
86     @Override
87     public int compare(Object o1, Object o2)
88     {
89         Float d1 = (Float) o1;
90         Float d2 = (Float) o2;
91         return d1.compareTo(d2);
92     }
93         
94         
95 }
96