]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/java/JavaByte.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / accessor / java / JavaByte.java
1 /*******************************************************************************\r
2  *  Copyright (c) 2010 Association for Decentralized Information Management in\r
3  *  Industry THTH ry.\r
4  *  All rights reserved. This program and the accompanying materials\r
5  *  are made available under the terms of the Eclipse Public License v1.0\r
6  *  which accompanies this distribution, and is available at\r
7  *  http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  *  Contributors:\r
10  *      VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.databoard.accessor.java;
13
14 import org.simantics.databoard.Bindings;\r
15 import org.simantics.databoard.accessor.Accessor;\r
16 import org.simantics.databoard.accessor.ByteAccessor;\r
17 import org.simantics.databoard.accessor.error.AccessorConstructionException;\r
18 import org.simantics.databoard.accessor.error.AccessorException;\r
19 import org.simantics.databoard.accessor.error.ReferenceException;\r
20 import org.simantics.databoard.accessor.event.Event;\r
21 import org.simantics.databoard.accessor.event.ValueAssigned;\r
22 import org.simantics.databoard.accessor.impl.AccessorParams;\r
23 import org.simantics.databoard.accessor.impl.ListenerEntry;\r
24 import org.simantics.databoard.accessor.interestset.ByteInterestSet;\r
25 import org.simantics.databoard.accessor.reference.ChildReference;\r
26 import org.simantics.databoard.binding.ArrayBinding;\r
27 import org.simantics.databoard.binding.Binding;\r
28 import org.simantics.databoard.binding.ByteBinding;\r
29 import org.simantics.databoard.binding.RecordBinding;\r
30 import org.simantics.databoard.binding.VariantBinding;\r
31 import org.simantics.databoard.binding.error.BindingException;\r
32 import org.simantics.databoard.type.ByteType;\r
33
34 /**
35  * Accessor to a Java Object that contains a Byte Type. 
36  *
37  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
38  */
39 public class JavaByte extends JavaObject implements ByteAccessor {
40         
41         public JavaByte(Accessor parent, ByteBinding binding, Object object, AccessorParams params) {
42                 super(parent, binding, object, params);
43         }
44         
45         public ByteBinding getBinding() {
46                 return (ByteBinding) binding;
47         }
48
49         @Override
50         public ByteType type() {
51                 return getBinding().type();
52         }
53
54         @Override
55         public byte getValue() throws AccessorException {\r
56                 readLock();
57                 try {
58                         return getBinding().getValue_(object);
59                 } catch (BindingException e) {
60                         throw new AccessorException(e);
61                 } finally {\r
62                         readUnlock();\r
63                 }
64         }\r
65         
66         @Override
67         public void setValue(byte newValue) throws AccessorException {
68                 if (newValue == getValue()) return;\r
69                 writeLock();\r
70                 try {
71                         if (binding.isImmutable() && parent!=null && parent instanceof JavaArray) {\r
72                                 JavaObject jo = (JavaObject) parent;\r
73                                 ArrayBinding ab = (ArrayBinding) jo.binding;\r
74                                 Object nv = getBinding().create(newValue);\r
75                                 ab.set(jo.object, (Integer)keyInParent, nv);\r
76                                 this.object = nv;\r
77                         } else if (binding.isImmutable() && parent!=null && parent instanceof JavaRecord) {\r
78                                 JavaObject jo = (JavaObject) parent;\r
79                                 RecordBinding rb = (RecordBinding) jo.binding;\r
80                                 Object nv = getBinding().create(newValue);\r
81                                 rb.setComponent(jo.object, (Integer)keyInParent, nv);\r
82                                 this.object = nv;\r
83                         } else if (binding.isImmutable() && parent!=null && parent instanceof JavaVariant) {\r
84                                 JavaObject jo = (JavaObject) parent;\r
85                                 VariantBinding vb = (VariantBinding) jo.binding;\r
86                                 Object nv = getBinding().create(newValue);\r
87                                 vb.setContent(jo.object, getBinding(), nv);\r
88                                 this.object = nv;\r
89                         } else {                        \r
90                                 // Set value\r
91                                 getBinding().setValue(object, newValue);\r
92                         }\r
93 \r
94                         
95                         // Notify
96                         ListenerEntry le = listeners;
97                         while (le!=null) {
98                                 ByteInterestSet is = le.getInterestSet();
99                                 if (is.inNotifications()) {
100                                         Event e = new ValueAssigned( Bindings.BYTE, is.inValues() ? newValue : null );
101                                         emitEvent(le, e);
102                                 }
103                                 le = le.next;
104                         }
105                         
106                 } catch (BindingException e) {
107                         throw new AccessorException(e);
108                 } finally {\r
109                         writeUnlock();\r
110                 }
111         }
112
113         @Override
114         public void setValue(Binding binding, Object newValue) throws AccessorException {\r
115                 try {
116                         byte nv = ((ByteBinding)binding).getValue_(newValue);
117                         setValue(nv);
118                 } catch (BindingException e) {
119                         throw new AccessorException(e);
120                 }
121         }
122
123         @SuppressWarnings("unchecked")
124         @Override
125         public <T extends Accessor> T getComponent(ChildReference reference)
126         throws AccessorConstructionException {
127                 if (reference==null) return (T) this;
128                 
129                 throw new ReferenceException(reference.getClass()+" is not a subreference of BooleanType");     
130         }
131
132         @Override
133         Event applyLocal(Event e, boolean makeRollback) throws AccessorException {\r
134                 Event rollback = makeRollback ? new ValueAssigned( Bindings.BYTE, getValue() ) : null;          \r
135                 if (e instanceof ValueAssigned) {\r
136                         ValueAssigned va = (ValueAssigned) e;\r
137                         if (va.newValue == null) throw new AccessorException("Byte value expected, got null");                  \r
138                         setValue(va.newValue.getBinding(), va.newValue.getValue());\r
139                         return rollback;\r
140                 } else {\r
141                         throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Byte");\r
142                 }
143         }
144         
145 }
146