]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryBoolean.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / accessor / binary / BinaryBoolean.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.binary;
13
14 import java.io.IOException;\r
15 \r
16 import org.simantics.databoard.Bindings;\r
17 import org.simantics.databoard.accessor.Accessor;\r
18 import org.simantics.databoard.accessor.BooleanAccessor;\r
19 import org.simantics.databoard.accessor.error.AccessorConstructionException;\r
20 import org.simantics.databoard.accessor.error.AccessorException;\r
21 import org.simantics.databoard.accessor.error.ReferenceException;\r
22 import org.simantics.databoard.accessor.event.Event;\r
23 import org.simantics.databoard.accessor.event.ValueAssigned;\r
24 import org.simantics.databoard.accessor.file.FileBooleanAccessor;\r
25 import org.simantics.databoard.accessor.impl.AccessorParams;\r
26 import org.simantics.databoard.accessor.impl.ListenerEntry;\r
27 import org.simantics.databoard.accessor.interestset.BooleanInterestSet;\r
28 import org.simantics.databoard.accessor.reference.ChildReference;\r
29 import org.simantics.databoard.binding.Binding;\r
30 import org.simantics.databoard.binding.BooleanBinding;\r
31 import org.simantics.databoard.binding.error.BindingException;\r
32 import org.simantics.databoard.type.BooleanType;\r
33 import org.simantics.databoard.util.binary.Blob;\r
34
35 public class BinaryBoolean extends BinaryObject implements BooleanAccessor, FileBooleanAccessor {
36
37         public BinaryBoolean(BinaryObject parent, Blob blob, BooleanType type, AccessorParams params) 
38         throws AccessorConstructionException {
39                 super(parent, blob, type, params);
40                 try {
41                         blob.setLength(1L);
42                 } catch (IOException e) {
43                         throw new AccessorConstructionException(e);
44                 }
45         }
46
47         @Override
48         public BooleanType type() {
49                 return (BooleanType) type;
50         }
51
52         @Override
53         public boolean getValue() throws AccessorException {\r
54                 assert b.isOpen();\r
55                 readLock();
56                 try {
57                         b.position(0L);
58                         byte v = b.readByte();
59                         if (v==0) return false;
60                         if (v==1) return true;
61                         throw new AccessorException("Unexpected value "+v+", expected 0 or 1 for Boolean");
62                 } catch (IOException e) {
63                         throw new AccessorException(e);
64                 } finally {\r
65                         readUnlock();\r
66                 }
67         }
68
69         @Override
70         Event applyLocal(Event e, boolean makeRollback) throws AccessorException {
71                 Event rollback = makeRollback ? new ValueAssigned( Bindings.BOOLEAN, getValue() ) : null;               \r
72                 if (e instanceof ValueAssigned) {\r
73                         ValueAssigned va = (ValueAssigned) e;\r
74                         if (va.newValue == null) throw new AccessorException("Boolean value expected, got null");\r
75                         setValueNoflush(va.newValue.getBinding(), va.newValue.getValue());\r
76                         return rollback;\r
77                 } else {\r
78                         throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Boolean");                      \r
79                 }
80         }
81
82         @SuppressWarnings("unchecked")
83         @Override
84         public <T extends Accessor> T getComponent(ChildReference reference)
85                         throws AccessorConstructionException {
86                 if (reference==null) return (T) this;           
87                 throw new ReferenceException(reference.getClass()+" is not a subreference of BooleanType");     
88         }
89
90
91         @Override
92         public Object getValue(Binding binding) throws AccessorException {\r
93                 assert b.isOpen();\r
94                 readLock();
95                 try {
96                         BooleanBinding bb = (BooleanBinding) binding; 
97                         boolean v = getValue();
98                         return bb.create(v);
99                 } catch(BindingException e) {           
100                         throw new AccessorException(e);
101                 } finally {\r
102                         readUnlock();\r
103                 }
104         }
105
106         public void setValueNoflush(boolean newValue) throws AccessorException {\r
107                 assert b.isOpen();\r
108                 writeLock();
109                 try {
110                         // Set value
111                         b.position(0);                  
112                         b.write( newValue ? (byte) 1 : (byte) 0 );
113                         
114                         // Notify
115                         ListenerEntry le = listeners;
116                         while (le!=null) {
117                                 BooleanInterestSet is = le.getInterestSet();
118                                 if (is.inNotifications()) {                                     
119                                         Event e = new ValueAssigned( Bindings.BOOLEAN, is.inValues() ? newValue : null );
120                                         emitEvent(le, e);
121                                 }
122                                 le = le.next;
123                         }
124                         
125                 } catch (IOException e) {
126                         throw new AccessorException(e);
127                 } finally {\r
128                         writeUnlock();\r
129                 }
130         }
131         
132         @Override
133         public void setValue(boolean newValue) throws AccessorException {\r
134                 assert b.isOpen();\r
135                 writeLock();
136                 try {
137                         setValueNoflush(newValue);
138                         b.flush();
139                 } catch (IOException e) {
140                         throw new AccessorException(e);
141                 } finally {\r
142                         writeUnlock();\r
143                 }
144         }
145         
146         @Override
147         public void setValueNoflush(Binding binding, Object newValue)
148                         throws AccessorException {\r
149                 assert b.isOpen();\r
150                 writeLock();
151                 try {
152                         boolean nv = ((BooleanBinding)binding).getValue_(newValue);
153                         setValueNoflush(nv);
154                 } catch (BindingException e) {
155                         throw new AccessorException(e);
156                 } finally {\r
157                         writeUnlock();\r
158                 }
159         }
160         
161 }
162