1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.databoard.accessor.binary;
14 import java.io.IOException;
\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
35 public class BinaryBoolean extends BinaryObject implements BooleanAccessor, FileBooleanAccessor {
37 public BinaryBoolean(BinaryObject parent, Blob blob, BooleanType type, AccessorParams params)
38 throws AccessorConstructionException {
39 super(parent, blob, type, params);
42 } catch (IOException e) {
43 throw new AccessorConstructionException(e);
48 public BooleanType type() {
49 return (BooleanType) type;
53 public boolean getValue() throws AccessorException {
\r
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);
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
78 throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Boolean");
\r
82 @SuppressWarnings("unchecked")
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");
92 public Object getValue(Binding binding) throws AccessorException {
\r
96 BooleanBinding bb = (BooleanBinding) binding;
97 boolean v = getValue();
99 } catch(BindingException e) {
100 throw new AccessorException(e);
106 public void setValueNoflush(boolean newValue) throws AccessorException {
\r
112 b.write( newValue ? (byte) 1 : (byte) 0 );
115 ListenerEntry le = listeners;
117 BooleanInterestSet is = le.getInterestSet();
118 if (is.inNotifications()) {
119 Event e = new ValueAssigned( Bindings.BOOLEAN, is.inValues() ? newValue : null );
125 } catch (IOException e) {
126 throw new AccessorException(e);
133 public void setValue(boolean newValue) throws AccessorException {
\r
137 setValueNoflush(newValue);
139 } catch (IOException e) {
140 throw new AccessorException(e);
147 public void setValueNoflush(Binding binding, Object newValue)
148 throws AccessorException {
\r
152 boolean nv = ((BooleanBinding)binding).getValue_(newValue);
154 } catch (BindingException e) {
155 throw new AccessorException(e);