1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.accessor.binary;
14 import java.io.IOException;
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;
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 {
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;
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());
78 throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Boolean");
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 {
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 {
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 {
137 setValueNoflush(newValue);
139 } catch (IOException e) {
140 throw new AccessorException(e);
147 public void setValueNoflush(Binding binding, Object newValue)
148 throws AccessorException {
152 boolean nv = ((BooleanBinding)binding).getValue_(newValue);
154 } catch (BindingException e) {
155 throw new AccessorException(e);