]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryBoolean.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / accessor / binary / BinaryBoolean.java
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryBoolean.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryBoolean.java
new file mode 100644 (file)
index 0000000..b3ad167
--- /dev/null
@@ -0,0 +1,162 @@
+/*******************************************************************************\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.BooleanAccessor;\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.FileBooleanAccessor;\r
+import org.simantics.databoard.accessor.impl.AccessorParams;\r
+import org.simantics.databoard.accessor.impl.ListenerEntry;\r
+import org.simantics.databoard.accessor.interestset.BooleanInterestSet;\r
+import org.simantics.databoard.accessor.reference.ChildReference;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.BooleanBinding;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.type.BooleanType;\r
+import org.simantics.databoard.util.binary.Blob;\r
+
+public class BinaryBoolean extends BinaryObject implements BooleanAccessor, FileBooleanAccessor {
+
+       public BinaryBoolean(BinaryObject parent, Blob blob, BooleanType type, AccessorParams params) 
+       throws AccessorConstructionException {
+               super(parent, blob, type, params);
+               try {
+                       blob.setLength(1L);
+               } catch (IOException e) {
+                       throw new AccessorConstructionException(e);
+               }
+       }
+
+       @Override
+       public BooleanType type() {
+               return (BooleanType) type;
+       }
+
+       @Override
+       public boolean getValue() throws AccessorException {\r
+               assert b.isOpen();\r
+               readLock();
+               try {
+                       b.position(0L);
+                       byte v = b.readByte();
+                       if (v==0) return false;
+                       if (v==1) return true;
+                       throw new AccessorException("Unexpected value "+v+", expected 0 or 1 for Boolean");
+               } 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.BOOLEAN, getValue() ) : null;               \r
+               if (e instanceof ValueAssigned) {\r
+                       ValueAssigned va = (ValueAssigned) e;\r
+                       if (va.newValue == null) throw new AccessorException("Boolean 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 Boolean");                      \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 BooleanType");     
+       }
+
+
+       @Override
+       public Object getValue(Binding binding) throws AccessorException {\r
+               assert b.isOpen();\r
+               readLock();
+               try {
+                       BooleanBinding bb = (BooleanBinding) binding; 
+                       boolean v = getValue();
+                       return bb.create(v);
+               } catch(BindingException e) {           
+                       throw new AccessorException(e);
+               } finally {\r
+                       readUnlock();\r
+               }
+       }
+
+       public void setValueNoflush(boolean newValue) throws AccessorException {\r
+               assert b.isOpen();\r
+               writeLock();
+               try {
+                       // Set value
+                       b.position(0);                  
+                       b.write( newValue ? (byte) 1 : (byte) 0 );
+                       
+                       // Notify
+                       ListenerEntry le = listeners;
+                       while (le!=null) {
+                               BooleanInterestSet is = le.getInterestSet();
+                               if (is.inNotifications()) {                                     
+                                       Event e = new ValueAssigned( Bindings.BOOLEAN, 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(boolean 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
+               assert b.isOpen();\r
+               writeLock();
+               try {
+                       boolean nv = ((BooleanBinding)binding).getValue_(newValue);
+                       setValueNoflush(nv);
+               } catch (BindingException e) {
+                       throw new AccessorException(e);
+               } finally {\r
+                       writeUnlock();\r
+               }
+       }
+       
+}
+