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.DoubleAccessor;
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.FileDoubleAccessor;
25 import org.simantics.databoard.accessor.impl.AccessorParams;
26 import org.simantics.databoard.accessor.impl.ListenerEntry;
27 import org.simantics.databoard.accessor.interestset.DoubleInterestSet;
28 import org.simantics.databoard.accessor.reference.ChildReference;
29 import org.simantics.databoard.binding.Binding;
30 import org.simantics.databoard.binding.DoubleBinding;
31 import org.simantics.databoard.binding.error.BindingException;
32 import org.simantics.databoard.type.DoubleType;
33 import org.simantics.databoard.util.binary.Blob;
35 public class BinaryDouble extends BinaryObject implements DoubleAccessor, FileDoubleAccessor {
37 public BinaryDouble(BinaryObject parent, Blob blob, DoubleType type, AccessorParams params)
38 throws AccessorConstructionException {
39 super(parent, blob, type, params);
42 } catch (IOException e) {
43 throw new AccessorConstructionException(e);
48 public DoubleType type() {
49 return (DoubleType) type;
53 public double getValue() throws AccessorException {
58 return b.readDouble();
59 } catch (IOException e) {
60 throw new AccessorException(e);
67 Event applyLocal(Event e, boolean makeRollback) throws AccessorException {
68 Event rollback = makeRollback ? new ValueAssigned( Bindings.DOUBLE, getValue() ) : null;
69 if (e instanceof ValueAssigned) {
70 ValueAssigned va = (ValueAssigned) e;
71 if (va.newValue == null) throw new AccessorException("Double value expected, got null");
72 setValueNoflush(va.newValue.getBinding(), va.newValue.getValue());
75 throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Double");
79 @SuppressWarnings("unchecked")
81 public <T extends Accessor> T getComponent(ChildReference reference)
82 throws AccessorConstructionException {
83 if (reference==null) return (T) this;
84 throw new ReferenceException(reference.getClass()+" is not a subreference of DoubleType");
88 public Object getValue(Binding binding) throws AccessorException {
90 DoubleBinding bb = (DoubleBinding) binding;
91 double v = getValue();
93 } catch(BindingException e) {
94 throw new AccessorException(e);
98 public void setValueNoflush(double newValue) throws AccessorException {
104 b.writeDouble( newValue );
107 ListenerEntry le = listeners;
109 DoubleInterestSet is = le.getInterestSet();
110 if (is.inNotifications()) {
111 Event e = new ValueAssigned( Bindings.DOUBLE, is.inValues() ? newValue : null );
117 } catch (IOException e) {
118 throw new AccessorException(e);
125 public void setValue(double newValue) throws AccessorException {
129 setValueNoflush(newValue);
131 } catch (IOException e) {
132 throw new AccessorException(e);
139 public void setValueNoflush(Binding binding, Object newValue)
140 throws AccessorException {
142 double nv = ((DoubleBinding)binding).getValue_(newValue);
144 } catch (BindingException e) {
145 throw new AccessorException(e);