]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryOptional.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / accessor / binary / BinaryOptional.java
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryOptional.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryOptional.java
new file mode 100644 (file)
index 0000000..a7870e7
--- /dev/null
@@ -0,0 +1,384 @@
+/*******************************************************************************\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
+import java.lang.ref.SoftReference;\r
+import java.util.concurrent.Executor;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.accessor.Accessor;\r
+import org.simantics.databoard.accessor.OptionalAccessor;\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.OptionalValueAssigned;\r
+import org.simantics.databoard.accessor.event.OptionalValueRemoved;\r
+import org.simantics.databoard.accessor.event.ValueAssigned;\r
+import org.simantics.databoard.accessor.file.FileOptionalAccessor;\r
+import org.simantics.databoard.accessor.impl.AccessorParams;\r
+import org.simantics.databoard.accessor.impl.ListenerEntry;\r
+import org.simantics.databoard.accessor.interestset.InterestSet;\r
+import org.simantics.databoard.accessor.interestset.OptionalInterestSet;\r
+import org.simantics.databoard.accessor.reference.ChildReference;\r
+import org.simantics.databoard.accessor.reference.ComponentReference;\r
+import org.simantics.databoard.accessor.reference.LabelReference;\r
+import org.simantics.databoard.adapter.AdaptException;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.OptionalBinding;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.binding.mutable.MutableVariant;\r
+import org.simantics.databoard.serialization.Serializer;\r
+import org.simantics.databoard.serialization.SerializerConstructionException;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.databoard.type.OptionalType;\r
+import org.simantics.databoard.util.binary.Blob;\r
+
+public class BinaryOptional extends BinaryObject implements OptionalAccessor, FileOptionalAccessor {
+
+       /** Accessor to component */
+       SoftReference<BinaryObject> component;
+       
+       public BinaryOptional(BinaryObject parent, Blob blob, OptionalType type, AccessorParams params) 
+       throws AccessorConstructionException {
+               super(parent, blob, type, params);
+       }
+
+       @Override
+       public OptionalType type() {
+               return (OptionalType) type;
+       }
+
+       @Override
+       public void setValueNoflush(Binding binding, Object newValue) throws AccessorException {\r
+               assert b.isOpen();\r
+               writeLock();
+               try {
+                       OptionalBinding ob = (OptionalBinding) binding;
+                       Object ov = newValue;
+                       boolean hasValue = ob.hasValue(newValue);
+                       
+                       if (hasValue) { 
+                               Binding cb = ob.getComponentBinding();
+                               Object cv = ob.getValue(ov);
+                               setComponentValueNoflush(cb, cv);
+                       } else {
+                               setNoValueNoflush();                            
+                       }
+                       
+               } catch (BindingException e) {
+                       throw new AccessorException(e);
+               } finally {\r
+                       writeUnlock();\r
+               }
+       }
+       
+       public void setComponentValueNoflush(Binding cb, Object cv)
+                       throws AccessorException {\r
+               assert b.isOpen();\r
+               writeLock();
+               try {
+                       BinaryObject sa = getExistingAccessor();
+                       if (sa!=null) {
+                               sa.setValue(cb, cv);
+                               return;
+                       }
+       
+                       // Write
+                       Serializer cs = params.serializerScheme.getSerializer( cb );
+                       int size = cs.getSize(cv, null)+1;
+                       b.setLength(size);
+                       b.position(0L);
+                       b.write((byte)1);
+                       cs.serialize(b, null, cv);
+                       
+                       // Notify Listeners
+                       ListenerEntry le = listeners;
+                       while (le!=null) {                              
+                               OptionalInterestSet is = le.getInterestSet();
+                               if (is.inNotifications()) {
+                                       MutableVariant newComponentValue = null;
+                                       if (is.inValues()) newComponentValue = new MutableVariant(cb, cb.isImmutable() ? cv : cb.clone(cv));
+                                       OptionalValueAssigned e = new OptionalValueAssigned(newComponentValue);
+                                       emitEvent(le, e);
+                               }
+                               
+                               // Attach component interest
+//                             if (is.getComponentInterest()!=null && getComponentAccessor()!=null) {
+//                                     sa = getComponentAccessor();
+//                             }
+                               
+                               le = le.next;
+                       }
+               } catch (IOException e) {
+                       throw new AccessorException(e);
+               } catch (AdaptException e) {
+                       throw new AccessorException(e);
+               } catch (SerializerConstructionException e) {\r
+                       throw new AccessorException(e);\r
+               } finally {\r
+                       writeUnlock();\r
+               }
+               
+       }
+
+       public void setNoValueNoflush() throws AccessorException {\r
+               assert b.isOpen();\r
+               writeLock();
+               try {
+                       // Write
+                       b.position(0L);
+                       b.setLength(1);
+                       b.write((byte)0);
+
+                       // Disconnect sub-accessor
+                       BinaryObject sa = getExistingAccessor();
+                       component = null;
+                       // Notify about disconnection of sub-accessor
+                       if (sa!=null) sa.invalidatedNotification();
+                                               
+                       // Notify Listeners
+                       ListenerEntry le = listeners;
+                       while (le!=null) {                              
+                               OptionalInterestSet is = le.getInterestSet();
+                               if (is.inNotifications()) {
+                                       OptionalValueRemoved e = new OptionalValueRemoved();
+                                       emitEvent(le, e);
+                               }
+                               le = le.next;
+                       }
+                       
+               } catch (IOException e) {
+                       throw new AccessorException(e);
+               } finally {\r
+                       writeUnlock();\r
+               }
+               
+       }
+
+       @Override
+       public void setNoValue() throws AccessorException {\r
+               assert b.isOpen();\r
+               writeLock();
+               try {
+                       setNoValueNoflush();
+                       b.flush();
+               } catch (IOException e) {
+                       throw new AccessorException( e );
+               } finally {\r
+                       writeUnlock();\r
+               }
+       }
+
+       @Override
+       public void setComponentValue(Binding cb, Object cv)
+                       throws AccessorException {\r
+               assert b.isOpen();\r
+               writeLock();
+               try {
+                       setComponentValueNoflush(cb, cv);
+                       b.flush();
+               } catch (IOException e) {
+                       throw new AccessorException( e );
+               } finally {\r
+                       writeUnlock();\r
+               }
+       }
+       
+       @SuppressWarnings("unchecked")
+       @Override
+       public <T extends Accessor> T getComponentAccessor()
+                       throws AccessorConstructionException {\r
+               assert b.isOpen();\r
+               readLock();
+               try {
+                       // Get existing or create new
+                       BinaryObject sa = getExistingAccessor();                        
+                       if (sa!=null) return (T) sa;
+                       
+                       // Instantiate new accessor
+                       Datatype ct = type().getComponentType(); 
+                       sa = createSubAccessor(ct, 1, b.length()-1, params);
+                       component = new SoftReference<BinaryObject>(sa);
+
+                       // Add listener to component, if it is in our interest set
+                       ListenerEntry le = listeners;
+                       while (le!=null) {                              
+                               OptionalInterestSet is = le.getInterestSet();
+                               InterestSet cis = is.getComponentInterest(); 
+                               if (cis != null) {
+                                       try {
+                                               ChildReference childPath = ChildReference.concatenate( le.path, new ComponentReference() );
+                                               sa.addListener(le.listener, cis, childPath, le.executor);
+                                       } catch (AccessorException e) {
+                                               throw new AccessorConstructionException(e);
+                                       }
+                               }
+                               
+                               // Attach component interest
+                               if (is.getComponentInterest()!=null) {
+                                       sa = getComponentAccessor();
+                                       sa.addListener(le.listener, is.getComponentInterest(), null, le.executor);
+                               }
+                               
+                               le = le.next;
+                       }                               
+                       
+                       return (T) sa;
+                       
+               } catch (IOException e) {
+                       throw new AccessorConstructionException( e );
+               } catch (AccessorException e) {
+                       throw new AccessorConstructionException( e );
+               } finally {\r
+                       readUnlock();                   \r
+               }
+       }
+       
+       /**
+        * Get existing sub accessor
+        * 
+        * @return sub-accessor or <code>null</code>
+        */
+       BinaryObject getExistingAccessor()
+       {
+               // Get existing or create new
+               SoftReference<BinaryObject> ref = component;
+               BinaryObject accessor = (ref!=null)?(BinaryObject)ref.get():null;
+               return accessor;
+       }               
+
+       @Override
+       public Object getComponentValue(Binding cb)
+                       throws AccessorException {\r
+               assert b.isOpen();\r
+               readLock();
+               try {
+                       b.position(1L);
+                       Serializer cs = params.serializerScheme.getSerializer( cb );
+                       return cs.deserialize(b, null);         
+               } catch (IOException e) {
+                       throw new AccessorException( e );
+               } catch (SerializerConstructionException e) {\r
+                       throw new AccessorException( e );\r
+               } finally {\r
+                       readUnlock();\r
+               }
+       }
+
+       @Override
+       public boolean hasValue() 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/1");
+               } catch (IOException e) {
+                       throw new AccessorException( e );
+               } finally {\r
+                       readUnlock();\r
+               }
+       }
+
+       @SuppressWarnings("unchecked")
+       @Override
+       public <T extends Accessor> T getComponent(ChildReference reference)
+                       throws AccessorConstructionException {
+               if (reference==null) return (T) this;\r
+\r
+               if (reference instanceof LabelReference) {\r
+                       LabelReference lr = (LabelReference) reference;\r
+                       if (lr.label.equals("o")) {\r
+                               Accessor result = getComponentAccessor();\r
+                               if (reference.getChildReference() != null)\r
+                                       result = result.getComponent(reference.getChildReference());\r
+                               return (T) result;                      \r
+                       }                       \r
+               }\r
+               
+               if (reference instanceof ComponentReference) {
+                       Accessor result = getComponentAccessor();
+                       if (reference.getChildReference() != null)
+                               result = result.getComponent(reference.getChildReference());
+                       return (T) result;
+               }\r
+               
+               throw new ReferenceException(reference.getClass()+" is not a reference of OptionalType");
+               
+       }
+       
+       @Override
+       public void addListener(Listener listener, InterestSet interestSet,
+                       ChildReference path, Executor executor) throws AccessorException {
+               super.addListener(listener, interestSet, path, executor);
+               OptionalInterestSet is = (OptionalInterestSet) interestSet;
+               InterestSet cis = is.getComponentInterest();
+               if (cis==null) return;
+               BinaryObject sa = getExistingAccessor();
+               if (sa==null) return;
+               ChildReference childPath = ChildReference.concatenate( path, new ComponentReference() );
+               sa.addListener(listener, cis, childPath, executor);
+       }
+       
+       @Override
+       public void removeListener(Listener listener) throws AccessorException {
+               ListenerEntry e = detachListener(listener);
+               if (e==null) return;
+               OptionalInterestSet is = (OptionalInterestSet) e.interestSet;
+               
+               InterestSet cis = is.getComponentInterest();
+               if (cis==null) return;
+               BinaryObject sa = getExistingAccessor();
+               if (sa==null) return;
+               sa.removeListener(listener);
+       }
+       
+       @Override
+       Event applyLocal(Event e, boolean makeRollback) throws AccessorException {
+               Event rollback = null;
+               if (makeRollback) {
+                       if (hasValue()) {
+                               OptionalBinding cb = (OptionalBinding) Bindings
+                                               .getMutableBinding(type().getComponentType());
+                               Object cv = getComponentValue(cb);
+                               rollback = new OptionalValueAssigned(cb, cv);
+                       } else {
+                               rollback = new OptionalValueRemoved();
+                       }
+               }
+
+               if (e instanceof ValueAssigned) {\r
+                       ValueAssigned va = (ValueAssigned) e;\r
+                       setValueNoflush(va.newValue.getBinding(), va.newValue.getValue());\r
+                       return rollback;\r
+               } else          \r
+               if (e instanceof OptionalValueAssigned) {
+                       OptionalValueAssigned oa = (OptionalValueAssigned) e;
+                       if (oa.newValue == null)
+                               throw new AccessorException("Cannot apply field assignment event, the value is missing");
+                       setComponentValueNoflush(oa.newValue.getBinding(), oa.newValue.getValue());
+               } else if (e instanceof OptionalValueRemoved) {
+                       setNoValueNoflush();
+               } else {
+                       throw new AccessorException("Unexpected event type "+ e.getClass().getName() + " for an Optional Type");
+               }
+
+               return rollback;
+       }
+
+
+}
+