]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/NumberBinding.java
Use type reflection tools from databoard in objmap2.
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / NumberBinding.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.Set;
15
16 import org.simantics.databoard.accessor.reference.ChildReference;
17 import org.simantics.databoard.binding.error.BindingException;
18 import org.simantics.databoard.binding.error.RuntimeBindingException;
19 import org.simantics.databoard.binding.impl.BindingPrintContext;
20 import org.simantics.databoard.type.NumberType;
21 import org.simantics.databoard.util.Range;
22
23 /**
24  * Super class for number types. 
25  *
26  * @author Toni Kalajainen
27  */
28 public abstract class NumberBinding extends Binding {
29
30         /**
31          * Create value by converting it from any Number instance to a Number 
32          * instance of this Binding type.
33          * 
34          * NOTE WARNING! Using this method may lose precision or value in the conversion. 
35          *   E.g. Double to Integer, or Long to Byte
36          * 
37          * @param value
38          * @return the value in the format of the binding type
39          */
40         public abstract Object create(Number value)
41         throws BindingException;
42         
43         /**
44          * Creates a value from its string representation
45          * @param value
46          * @return number
47          */
48         public abstract Object create(String value)
49         throws BindingException;
50         
51         /**
52          * Get Data type
53          * 
54          * @return data type
55          */
56         @Override
57         public NumberType type() {
58                 return (NumberType) type;
59         }
60         
61         /**
62          * Get numeric value of an object
63          * 
64          * @param obj object
65          * @return Number
66          * @throws BindingException thrown if obj is incorrect class
67          */
68         public Number getValue(Object obj) throws BindingException {
69                 return ((Number)obj);
70         }
71         
72         public abstract void setValue(Object obj, Number value) throws BindingException;
73         
74         /**
75          * Assert the obj is a valid number. 
76          * 
77          * This asserts 
78          *  1. The value is within the range defined in the NumberType
79          * 
80          * @throws BindingException
81          */
82         @Override
83         public void assertInstaceIsValid(Object obj, Set<Object> validInstances) throws BindingException {
84                 if (!isInstance(obj)) throw new BindingException("Not a correct instance");             
85                 NumberType type = type();
86                 Range range = type.getRange();
87                 if (range!=null) {
88                         Number value = getValue(obj);
89                         if (!range.contains(value)) {
90                                 throw new BindingException("Value ("+value+") out of range ("+range+")");
91                         }
92                 }
93         }
94         
95         public Object createUnchecked(String value) throws RuntimeBindingException {
96                 try {
97                         return create(value);
98                 } catch (BindingException e) {
99                         return new RuntimeBindingException(e);
100                 }
101         }
102         public Object createUnchecked(Number value) throws RuntimeBindingException {
103                 try {
104                         return create(value);
105                 } catch (BindingException e) {
106                         return new RuntimeBindingException(e);
107                 }
108         }
109         
110         @Override
111         protected void toString(Object value, BindingPrintContext ctx) throws BindingException {
112                 ctx.b.append(getValue(value).toString());
113         }
114         
115         @Override
116         public void readFrom(Binding srcBinding, Object src, Object dst)
117                         throws BindingException {
118                 Number v = ((NumberBinding)srcBinding).getValue(src);
119                 setValue(dst, v);               
120         }
121         
122         @Override
123         public Binding getComponentBinding(ChildReference path) {
124                 if (path==null) return this;            
125                 throw new IllegalArgumentException();
126         }       
127         
128         @Override
129         public int getComponentCount() {
130                 return 0;
131         }
132         
133         @Override
134         public Binding getComponentBinding(int index) {
135                 throw new IllegalArgumentException();
136         }
137         
138 }
139