]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/UnsignedIntegerBinding.java
Allow loading databoard serialized files with type adapting
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / impl / UnsignedIntegerBinding.java
1 package org.simantics.databoard.binding.impl;
2
3 import org.simantics.databoard.binding.IntegerBinding;
4 import org.simantics.databoard.binding.error.BindingException;
5 import org.simantics.databoard.primitives.UnsignedInteger;
6 import org.simantics.databoard.type.IntegerType;
7
8 /**
9  * Binding of {@link UnsignedInteger} to integer type.
10  * This is bitwise binding, i.e. negative values bound to 0x100000000 + value.
11  * 
12  * @author Toni Kalajainen <toni.kalajainen@iki.fi>
13  */
14 public abstract class UnsignedIntegerBinding extends IntegerBinding {
15         
16         UnsignedIntegerBinding(IntegerType type) {
17                 super(type);
18         }
19
20         public static class Immutable extends UnsignedIntegerBinding {
21
22                 public Immutable(IntegerType type) {
23                         super(type);
24                 }
25                 
26                 @Override
27                 public Object create(int value) throws BindingException {
28                         return UnsignedInteger.fromBits(value);
29                 }
30
31                 @Override
32                 public Object create(Integer value) throws BindingException {
33                         try {
34                                 return UnsignedInteger.valueOf(value.intValue());
35                         } catch (java.lang.IllegalArgumentException e) {
36                                 throw new BindingException( e );
37                         }
38                 }
39
40                 @Override
41                 public Object create(Number value) throws BindingException {
42                         try { 
43                                 return UnsignedInteger.valueOf(value.longValue());
44                         } catch (java.lang.IllegalArgumentException e) {
45                                 throw new BindingException( e );
46                         }
47                 }
48
49                 @Override
50                 public Object create(String value) throws BindingException {
51                         try {
52                                 return UnsignedInteger.valueOf( Long.valueOf(value) );
53                         } catch (java.lang.IllegalArgumentException e) {
54                                 throw new BindingException( e );
55                         }
56                 }               
57                 
58                 @Override
59                 public void setValue(Object obj, Number value) throws BindingException {
60                         throw new BindingException("UnsignedInteger is immutable class");
61                 }
62
63                 @Override
64                 public void setValue(Object obj, int value) throws BindingException {
65                         throw new BindingException("UnsignedInteger is immutable class");
66                 }
67                 
68                 @Override
69                 public boolean isImmutable() {
70                         return true;
71                 }
72                                 
73                 @Override
74                 public boolean isInstance(Object obj) {
75                         return obj instanceof UnsignedInteger.Immutable;
76                 }               
77         }
78         
79         public static class Mutable extends UnsignedIntegerBinding {
80
81                 public Mutable(IntegerType type) {
82                         super(type);
83                 }
84                 
85                 @Override
86                 public Object create(int value) throws BindingException {
87                         return new UnsignedInteger.Mutable(value);
88                 }
89
90                 @Override
91                 public Object create(Integer value) throws BindingException {
92                         return new UnsignedInteger.Mutable(value);
93                 }
94
95                 @Override
96                 public Object create(Number value) throws BindingException {
97                         return UnsignedInteger.Mutable.fromBits(value.intValue());
98                 }
99
100                 @Override
101                 public Object create(String value) throws BindingException {
102                         return new UnsignedInteger.Mutable(value);
103                 }
104
105                 @Override
106                 public void setValue(Object obj, Number value) throws BindingException {
107                         UnsignedInteger.Mutable mi = (UnsignedInteger.Mutable) obj;
108                         mi.setBits(value.intValue());
109                 }
110
111                 @Override
112                 public void setValue(Object obj, int value) throws BindingException {
113                         UnsignedInteger.Mutable mi = (UnsignedInteger.Mutable) obj;
114                         mi.setBits(value);
115                 }
116
117                 @Override
118                 public boolean isImmutable() {
119                         return false;
120                 }
121
122                 @Override
123                 public boolean isInstance(Object obj) {
124                         return obj instanceof UnsignedInteger.Mutable;
125                 }
126                 
127         }
128
129         @Override
130         public Integer getValue(Object obj) throws BindingException {
131                 return ((UnsignedInteger)obj).toBits();
132         }
133
134         @Override
135         public int getValue_(Object obj) throws BindingException {
136                 return ((UnsignedInteger)obj).toBits();
137         }
138
139         @Override
140         public String toString(Object value) throws BindingException {
141                 return value.toString();
142         }
143
144
145 }