]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryDouble.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / accessor / binary / BinaryDouble.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.DoubleAccessor;\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.FileDoubleAccessor;\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.DoubleInterestSet;\r
28 import org.simantics.databoard.accessor.reference.ChildReference;\r
29 import org.simantics.databoard.binding.Binding;\r
30 import org.simantics.databoard.binding.DoubleBinding;\r
31 import org.simantics.databoard.binding.error.BindingException;\r
32 import org.simantics.databoard.type.DoubleType;\r
33 import org.simantics.databoard.util.binary.Blob;\r
34
35 public class BinaryDouble extends BinaryObject implements DoubleAccessor, FileDoubleAccessor {
36
37         public BinaryDouble(BinaryObject parent, Blob blob, DoubleType type, AccessorParams params) 
38         throws AccessorConstructionException {
39                 super(parent, blob, type, params);
40                 try {
41                         blob.setLength(8L);
42                 } catch (IOException e) {
43                         throw new AccessorConstructionException(e);
44                 }
45         }
46
47         @Override
48         public DoubleType type() {
49                 return (DoubleType) type;
50         }
51
52         @Override
53         public double getValue() throws AccessorException {\r
54                 assert b.isOpen();\r
55                 readLock();
56                 try {
57                         b.position(0L);
58                         return b.readDouble();
59                 } catch (IOException e) {
60                         throw new AccessorException(e);
61                 } finally {\r
62                         readUnlock();\r
63                 }
64         }
65
66         @Override
67         Event applyLocal(Event e, boolean makeRollback) throws AccessorException {
68                 Event rollback = makeRollback ? new ValueAssigned( Bindings.DOUBLE, getValue() ) : null;                \r
69                 if (e instanceof ValueAssigned) {\r
70                         ValueAssigned va = (ValueAssigned) e;\r
71                         if (va.newValue == null) throw new AccessorException("Double value expected, got null");                        \r
72                         setValueNoflush(va.newValue.getBinding(), va.newValue.getValue());\r
73                         return rollback;\r
74                 } else {\r
75                         throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Double");                       \r
76                 }
77         }
78
79         @SuppressWarnings("unchecked")
80         @Override
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");      
85         }
86
87         @Override
88         public Object getValue(Binding binding) throws AccessorException {
89                 try {
90                         DoubleBinding bb = (DoubleBinding) binding; 
91                         double v = getValue();
92                         return bb.create(v);
93                 } catch(BindingException e) {           
94                         throw new AccessorException(e);
95                 }
96         }
97
98         public void setValueNoflush(double newValue) throws AccessorException {\r
99                 assert b.isOpen();\r
100                 writeLock();
101                 try {
102                         // Set value
103                         b.position(0);                  
104                         b.writeDouble( newValue );
105                         
106                         // Notify
107                         ListenerEntry le = listeners;
108                         while (le!=null) {
109                                 DoubleInterestSet is = le.getInterestSet();
110                                 if (is.inNotifications()) {                                     
111                                         Event e = new ValueAssigned( Bindings.DOUBLE, is.inValues() ? newValue : null );
112                                         emitEvent(le, e);
113                                 }
114                                 le = le.next;
115                         }
116                         
117                 } catch (IOException e) {
118                         throw new AccessorException(e);
119                 } finally {\r
120                         writeUnlock();\r
121                 }
122         }       
123         
124         @Override
125         public void setValue(double newValue) throws AccessorException {\r
126                 assert b.isOpen();\r
127                 writeLock();
128                 try {
129                         setValueNoflush(newValue);
130                         b.flush();
131                 } catch (IOException e) {
132                         throw new AccessorException(e);
133                 } finally {\r
134                         writeUnlock();\r
135                 }
136         }
137         
138         @Override
139         public void setValueNoflush(Binding binding, Object newValue)
140                         throws AccessorException {
141                 try {
142                         double nv = ((DoubleBinding)binding).getValue_(newValue);
143                         setValueNoflush(nv);
144                 } catch (BindingException e) {
145                         throw new AccessorException(e);
146                 }
147         }
148         
149 }
150