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