]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryBoolean.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / accessor / binary / BinaryBoolean.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.binary;
13
14 import java.io.IOException;
15
16 import org.simantics.databoard.Bindings;
17 import org.simantics.databoard.accessor.Accessor;
18 import org.simantics.databoard.accessor.BooleanAccessor;
19 import org.simantics.databoard.accessor.error.AccessorConstructionException;
20 import org.simantics.databoard.accessor.error.AccessorException;
21 import org.simantics.databoard.accessor.error.ReferenceException;
22 import org.simantics.databoard.accessor.event.Event;
23 import org.simantics.databoard.accessor.event.ValueAssigned;
24 import org.simantics.databoard.accessor.file.FileBooleanAccessor;
25 import org.simantics.databoard.accessor.impl.AccessorParams;
26 import org.simantics.databoard.accessor.impl.ListenerEntry;
27 import org.simantics.databoard.accessor.interestset.BooleanInterestSet;
28 import org.simantics.databoard.accessor.reference.ChildReference;
29 import org.simantics.databoard.binding.Binding;
30 import org.simantics.databoard.binding.BooleanBinding;
31 import org.simantics.databoard.binding.error.BindingException;
32 import org.simantics.databoard.type.BooleanType;
33 import org.simantics.databoard.util.binary.Blob;
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 {
54                 assert b.isOpen();
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 {
65                         readUnlock();
66                 }
67         }
68
69         @Override
70         Event applyLocal(Event e, boolean makeRollback) throws AccessorException {
71                 Event rollback = makeRollback ? new ValueAssigned( Bindings.BOOLEAN, getValue() ) : null;               
72                 if (e instanceof ValueAssigned) {
73                         ValueAssigned va = (ValueAssigned) e;
74                         if (va.newValue == null) throw new AccessorException("Boolean value expected, got null");
75                         setValueNoflush(va.newValue.getBinding(), va.newValue.getValue());
76                         return rollback;
77                 } else {
78                         throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Boolean");                      
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 {
93                 assert b.isOpen();
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 {
102                         readUnlock();
103                 }
104         }
105
106         public void setValueNoflush(boolean newValue) throws AccessorException {
107                 assert b.isOpen();
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 {
128                         writeUnlock();
129                 }
130         }
131         
132         @Override
133         public void setValue(boolean newValue) throws AccessorException {
134                 assert b.isOpen();
135                 writeLock();
136                 try {
137                         setValueNoflush(newValue);
138                         b.flush();
139                 } catch (IOException e) {
140                         throw new AccessorException(e);
141                 } finally {
142                         writeUnlock();
143                 }
144         }
145         
146         @Override
147         public void setValueNoflush(Binding binding, Object newValue)
148                         throws AccessorException {
149                 assert b.isOpen();
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 {
157                         writeUnlock();
158                 }
159         }
160         
161 }
162