]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/java/JavaByte.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / accessor / java / JavaByte.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.accessor.java;
13
14 import org.simantics.databoard.Bindings;
15 import org.simantics.databoard.accessor.Accessor;
16 import org.simantics.databoard.accessor.ByteAccessor;
17 import org.simantics.databoard.accessor.error.AccessorConstructionException;
18 import org.simantics.databoard.accessor.error.AccessorException;
19 import org.simantics.databoard.accessor.error.ReferenceException;
20 import org.simantics.databoard.accessor.event.Event;
21 import org.simantics.databoard.accessor.event.ValueAssigned;
22 import org.simantics.databoard.accessor.impl.AccessorParams;
23 import org.simantics.databoard.accessor.impl.ListenerEntry;
24 import org.simantics.databoard.accessor.interestset.ByteInterestSet;
25 import org.simantics.databoard.accessor.reference.ChildReference;
26 import org.simantics.databoard.binding.ArrayBinding;
27 import org.simantics.databoard.binding.Binding;
28 import org.simantics.databoard.binding.ByteBinding;
29 import org.simantics.databoard.binding.RecordBinding;
30 import org.simantics.databoard.binding.VariantBinding;
31 import org.simantics.databoard.binding.error.BindingException;
32 import org.simantics.databoard.type.ByteType;
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 {
56                 readLock();
57                 try {
58                         return getBinding().getValue_(object);
59                 } catch (BindingException e) {
60                         throw new AccessorException(e);
61                 } finally {
62                         readUnlock();
63                 }
64         }
65         
66         @Override
67         public void setValue(byte newValue) throws AccessorException {
68                 if (newValue == getValue()) return;
69                 writeLock();
70                 try {
71                         if (binding.isImmutable() && parent!=null && parent instanceof JavaArray) {
72                                 JavaObject jo = (JavaObject) parent;
73                                 ArrayBinding ab = (ArrayBinding) jo.binding;
74                                 Object nv = getBinding().create(newValue);
75                                 ab.set(jo.object, (Integer)keyInParent, nv);
76                                 this.object = nv;
77                         } else if (binding.isImmutable() && parent!=null && parent instanceof JavaRecord) {
78                                 JavaObject jo = (JavaObject) parent;
79                                 RecordBinding rb = (RecordBinding) jo.binding;
80                                 Object nv = getBinding().create(newValue);
81                                 rb.setComponent(jo.object, (Integer)keyInParent, nv);
82                                 this.object = nv;
83                         } else if (binding.isImmutable() && parent!=null && parent instanceof JavaVariant) {
84                                 JavaObject jo = (JavaObject) parent;
85                                 VariantBinding vb = (VariantBinding) jo.binding;
86                                 Object nv = getBinding().create(newValue);
87                                 vb.setContent(jo.object, getBinding(), nv);
88                                 this.object = nv;
89                         } else {                        
90                                 // Set value
91                                 getBinding().setValue(object, newValue);
92                         }
93
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 {
109                         writeUnlock();
110                 }
111         }
112
113         @Override
114         public void setValue(Binding binding, Object newValue) throws AccessorException {
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 {
134                 Event rollback = makeRollback ? new ValueAssigned( Bindings.BYTE, getValue() ) : null;          
135                 if (e instanceof ValueAssigned) {
136                         ValueAssigned va = (ValueAssigned) e;
137                         if (va.newValue == null) throw new AccessorException("Byte value expected, got null");                  
138                         setValue(va.newValue.getBinding(), va.newValue.getValue());
139                         return rollback;
140                 } else {
141                         throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Byte");
142                 }
143         }
144         
145 }
146