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