]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/ByteBinding.java
Use type reflection tools from databoard in objmap2.
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / ByteBinding.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.ByteType;
20 import org.simantics.databoard.util.IdentityPair;
21
22 /**
23  * This is a binding of Byte Type and a Java Object.
24  * 
25  * @see ByteType
26  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
27  */
28 public abstract class ByteBinding extends NumberBinding {
29         
30         public ByteBinding(ByteType type) {
31                 this.type = type;
32         }
33         
34         @Override
35         public ByteType type() {
36                 return (ByteType) type;
37         }
38         
39         public abstract Object create(byte value) throws BindingException;
40         public abstract Object create(Byte value) throws BindingException;
41         public abstract Object create(String value) throws BindingException;
42         public abstract Object create(Number value) throws BindingException;
43         public abstract Byte getValue(Object obj) throws BindingException;
44         public abstract byte getValue_(Object obj) throws BindingException;
45         public abstract void setValue(Object obj, Number value) throws BindingException;
46         public abstract void setValue(Object obj, byte 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         
60     @Override
61     public int deepCompare(Object o1, Object o2,
62                 Set<IdentityPair<Object, Object>> compareHistory)
63                 throws BindingException {
64         byte v1 = getValue_(o1);
65         byte v2 = getValue_(o2);
66         return (int)v1 - (int)v2;       
67     }
68     
69     @Override
70     public int deepHashValue(Object value, IdentityHashMap<Object, Object> hashedObjects) throws BindingException {
71         byte v = getValue_(value);
72         return v;
73     }
74         
75         public Object createUnchecked(byte value) throws RuntimeBindingException {
76                 try {
77                         return create(value);
78                 } catch (BindingException e) {
79                         return new RuntimeBindingException(e);
80                 }
81         }
82         public Object createUnchecked(Byte value) throws RuntimeBindingException {
83                 try {
84                         return create(value);
85                 } catch (BindingException e) {
86                         return new RuntimeBindingException(e);
87                 }
88         }
89         
90     /*    
91     @Override
92     public void assertInstaceIsValid(Object obj)
93                 throws BindingException {
94         if (!isInstance(obj)) throw new BindingException("Not a boolean instance");
95         ByteType type = getDataType();
96         Byte min = type.getMin();
97         Byte max = type.getMax();
98         if (min!=null || max!=null) {
99                 Byte value = getValue(obj);
100                 if (min!=null && value<min) throw new BindingException("The value is("+value+") below the minimum ("+min+")");
101                 if (max!=null && value>max) throw new BindingException("The value is("+value+") over the maximum ("+max+")");
102         }
103     }
104 */    
105 }
106