]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/DoubleBinding.java
Use type reflection tools from databoard in objmap2.
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / DoubleBinding.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;
13
14 import java.util.IdentityHashMap;
15 import java.util.Set;
16
17 import org.simantics.databoard.binding.error.BindingException;
18 import org.simantics.databoard.binding.error.RuntimeBindingException;
19 import org.simantics.databoard.type.DoubleType;
20 import org.simantics.databoard.util.IdentityPair;
21
22
23 /**
24  * This is a binding of Double Type and a Java Object.
25  *
26  * @see DoubleType
27  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
28  */
29 public abstract class DoubleBinding extends NumberBinding {
30
31         public DoubleBinding(DoubleType type) {
32                 this.type = type;
33         }
34         
35         public DoubleType type() {
36                 return (DoubleType) type;
37         }
38         
39         public abstract Object create(Double value) throws BindingException;
40         public abstract Object create(double value) throws BindingException;
41         public abstract Object create(Number value) throws BindingException;
42         public abstract Object create(String value) throws BindingException;
43         public abstract Double getValue(Object o) throws BindingException;
44         public abstract double getValue_(Object o) throws BindingException;
45         public abstract void setValue(Object obj, Number value) throws BindingException;
46         public abstract void setValue(Object obj, double value) throws BindingException;
47         public abstract boolean isInstance(Object obj);
48     
49     @Override
50     public void accept(Visitor1 v, Object obj) {
51         v.visit(this, obj);        
52     }
53     
54     @Override
55     public <T> T accept(Visitor<T> v) {
56         return v.visit(this);
57     }    
58         
59     @Override
60     public int deepCompare(Object o1, Object o2,
61                 Set<IdentityPair<Object, Object>> compareHistory)
62                 throws BindingException {
63         double v1 = getValue_(o1);
64         double v2 = getValue_(o2);
65         return Double.compare(v1, v2);          
66     }
67     
68     @Override
69     public int deepHashValue(Object value, IdentityHashMap<Object, Object> hashedObjects) throws BindingException {
70         double v = getValue_(value);
71         long bits = Double.doubleToLongBits(v);
72         return (int)(bits ^ (bits >>> 32));
73     }
74     
75         public Object createUnchecked(double value) throws RuntimeBindingException {
76                 try {
77                         return create(value);
78                 } catch (BindingException e) {
79                         return new RuntimeBindingException(e);
80                 }
81         }
82         public Object createUnchecked(Double value) throws RuntimeBindingException {
83                 try {
84                         return create(value);
85                 } catch (BindingException e) {
86                         return new RuntimeBindingException(e);
87                 }
88         }
89         
90 }