]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/IntegerBinding.java
Use type reflection tools from databoard in objmap2.
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / IntegerBinding.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.binding.impl.IntegerBindingDefault;
20 import org.simantics.databoard.binding.mutable.MutableIntegerBinding;
21 import org.simantics.databoard.type.IntegerType;
22 import org.simantics.databoard.util.IdentityPair;
23
24
25
26 /**
27  * This is a binding of an Integer datatype. It is abstract and therfore
28  * doesn't bind to any specific java class. Sub-classes bind to specific
29  * classes.
30  * 
31  * Subclass IntegerJavaBinding binds to java.lang.Integer, and
32  * MutableIntegerBinding to MutableInteger. 
33  * 
34  * @see IntegerType
35  * @see IntegerBindingDefault java.lang.Integer binding
36  * @see MutableIntegerBinding MutableInteger binding
37  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
38  */
39 public abstract class IntegerBinding extends NumberBinding {
40         
41         public IntegerBinding(IntegerType type) {
42                 this.type = type;
43         }
44         
45         @Override
46         public IntegerType type() {
47                 return (IntegerType) type;
48         }
49         
50         public abstract Object create(int value) throws BindingException;
51         public abstract Object create(Integer value) throws BindingException;
52         public abstract Object create(Number value) throws BindingException;
53         public abstract Object create(String value) throws BindingException;
54         public abstract Integer getValue(Object obj) throws BindingException;
55         public abstract int getValue_(Object obj) throws BindingException;
56         public abstract void setValue(Object obj, Number value) throws BindingException;
57         public abstract void setValue(Object obj, int value) throws BindingException;
58         public abstract boolean isInstance(Object obj);
59         
60     @Override
61     public void accept(Visitor1 v, Object obj) {
62         v.visit(this, obj);        
63     }
64     
65     @Override
66     public <T> T accept(Visitor<T> v) {
67         return v.visit(this);
68     }
69     
70     @Override
71     public int deepCompare(Object o1, Object o2,
72                 Set<IdentityPair<Object, Object>> compareHistory)
73                 throws BindingException {
74         
75         int v1 = getValue_(o1);
76         int v2 = getValue_(o2);
77         return (v1<v2 ? -1 : (v1==v2 ? 0 : 1));
78     }
79     
80     @Override
81     public int deepHashValue(Object value, IdentityHashMap<Object, Object> hashedObjects) throws BindingException {
82         int v = getValue_(value);
83         return v;
84     }
85
86         public Object createUnchecked(int value) throws RuntimeBindingException {
87                 try {
88                         return create(value);
89                 } catch (BindingException e) {
90                         return new RuntimeBindingException(e);
91                 }
92         }
93         public Object createUnchecked(Integer value) throws RuntimeBindingException {
94                 try {
95                         return create(value);
96                 } catch (BindingException e) {
97                         return new RuntimeBindingException(e);
98                 }
99         }
100     
101 }
102