]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryFloat.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / accessor / binary / BinaryFloat.java
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryFloat.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryFloat.java
new file mode 100644 (file)
index 0000000..c603399
--- /dev/null
@@ -0,0 +1,154 @@
+/*******************************************************************************\r
+ *  Copyright (c) 2010 Association for Decentralized Information Management in\r
+ *  Industry THTH ry.\r
+ *  All rights reserved. This program and the accompanying materials\r
+ *  are made available under the terms of the Eclipse Public License v1.0\r
+ *  which accompanies this distribution, and is available at\r
+ *  http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ *  Contributors:\r
+ *      VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.databoard.accessor.binary;
+
+import java.io.IOException;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.accessor.Accessor;\r
+import org.simantics.databoard.accessor.FloatAccessor;\r
+import org.simantics.databoard.accessor.error.AccessorConstructionException;\r
+import org.simantics.databoard.accessor.error.AccessorException;\r
+import org.simantics.databoard.accessor.error.ReferenceException;\r
+import org.simantics.databoard.accessor.event.Event;\r
+import org.simantics.databoard.accessor.event.ValueAssigned;\r
+import org.simantics.databoard.accessor.file.FileFloatAccessor;\r
+import org.simantics.databoard.accessor.impl.AccessorParams;\r
+import org.simantics.databoard.accessor.impl.ListenerEntry;\r
+import org.simantics.databoard.accessor.interestset.FloatInterestSet;\r
+import org.simantics.databoard.accessor.reference.ChildReference;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.FloatBinding;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.type.FloatType;\r
+import org.simantics.databoard.util.binary.Blob;\r
+
+public class BinaryFloat extends BinaryObject implements FloatAccessor, FileFloatAccessor {
+
+       public BinaryFloat(BinaryObject parent, Blob blob, FloatType type, AccessorParams params) 
+       throws AccessorConstructionException {
+               super(parent, blob, type, params);
+               try {
+                       blob.setLength(4L);
+               } catch (IOException e) {
+                       throw new AccessorConstructionException(e);
+               }
+       }
+
+       @Override
+       public FloatType type() {
+               return (FloatType) type;
+       }
+
+       @Override
+       public float getValue() throws AccessorException {\r
+               assert b.isOpen();\r
+               readLock();
+               try {
+                       b.position(0L);
+                       return b.readFloat();
+               } catch (IOException e) {
+                       throw new AccessorException(e);
+               } finally {\r
+                       readUnlock();\r
+               }
+       }
+
+       @Override
+       Event applyLocal(Event e, boolean makeRollback) throws AccessorException {
+               Event rollback = makeRollback ? new ValueAssigned( Bindings.FLOAT, getValue() ) : null;                 \r
+               if (e instanceof ValueAssigned) {\r
+                       ValueAssigned va = (ValueAssigned) e;\r
+                       if (va.newValue == null) throw new AccessorException("Float value expected, got null");\r
+                       setValueNoflush(va.newValue.getBinding(), va.newValue.getValue());\r
+                       return rollback;\r
+               } else {\r
+                       throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Float");\r
+               }
+       }
+
+       @SuppressWarnings("unchecked")
+       @Override
+       public <T extends Accessor> T getComponent(ChildReference reference)
+                       throws AccessorConstructionException {
+               if (reference==null) return (T) this;           
+               throw new ReferenceException(reference.getClass()+" is not a subreference of FloatType");       
+       }
+
+       @Override
+       public Object getValue(Binding binding) throws AccessorException {\r
+               assert b.isOpen();\r
+               readLock();
+               try {
+                       FloatBinding bb = (FloatBinding) binding; 
+                       float v = getValue();
+                       return bb.create(v);
+               } catch(BindingException e) {           
+                       throw new AccessorException(e);
+               } finally {\r
+                       readUnlock();\r
+               }
+       }
+
+       public void setValueNoflush(float newValue) throws AccessorException {\r
+               assert b.isOpen();\r
+               writeLock();
+               try {
+                       // Set value
+                       b.position(0);                  
+                       b.writeFloat( newValue );
+                       
+                       // Notify
+                       ListenerEntry le = listeners;
+                       while (le!=null) {
+                               FloatInterestSet is = le.getInterestSet();
+                               if (is.inNotifications()) {                                     
+                                       Event e = new ValueAssigned( Bindings.FLOAT, is.inValues() ? newValue : null );
+                                       emitEvent(le, e);
+                               }
+                               le = le.next;
+                       }
+                       
+               } catch (IOException e) {
+                       throw new AccessorException(e);
+               } finally {\r
+                       writeUnlock();\r
+               }
+       } 
+       
+       @Override
+       public void setValue(float newValue) throws AccessorException {\r
+               assert b.isOpen();\r
+               writeLock();
+               try {
+                       setValueNoflush(newValue);
+                       b.flush();
+               } catch (IOException e) {
+                       throw new AccessorException(e);
+               } finally {\r
+                       writeUnlock();\r
+               }
+       }
+       
+       @Override
+       public void setValueNoflush(Binding binding, Object newValue)
+                       throws AccessorException {\r
+               try {
+                       float nv = ((FloatBinding)binding).getValue_(newValue);
+                       setValueNoflush(nv);
+               } catch (BindingException e) {
+                       throw new AccessorException(e);
+               }
+       }
+       
+}
+