X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Faccessor%2Fbinary%2FBinaryUnion.java;h=c70409205f5136cbb63c66bd63aadd3f0f022f47;hb=refs%2Fchanges%2F65%2F2565%2F2;hp=915722ced14d20f2cd7786bd684969bf4edfec93;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryUnion.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryUnion.java index 915722ced..c70409205 100644 --- a/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryUnion.java +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/binary/BinaryUnion.java @@ -1,54 +1,54 @@ -/******************************************************************************* - * Copyright (c) 2010 Association for Decentralized Information Management in - * Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ +/******************************************************************************* + * Copyright (c) 2010 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ package org.simantics.databoard.accessor.binary; -import gnu.trove.map.hash.TObjectIntHashMap; - -import java.io.IOException; -import java.lang.ref.SoftReference; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.Executor; - -import org.simantics.databoard.accessor.Accessor; -import org.simantics.databoard.accessor.UnionAccessor; -import org.simantics.databoard.accessor.error.AccessorConstructionException; -import org.simantics.databoard.accessor.error.AccessorException; -import org.simantics.databoard.accessor.error.ReferenceException; -import org.simantics.databoard.accessor.event.Event; -import org.simantics.databoard.accessor.event.UnionValueAssigned; -import org.simantics.databoard.accessor.event.ValueAssigned; -import org.simantics.databoard.accessor.file.FileUnionAccessor; -import org.simantics.databoard.accessor.impl.AccessorParams; -import org.simantics.databoard.accessor.impl.ListenerEntry; -import org.simantics.databoard.accessor.interestset.InterestSet; -import org.simantics.databoard.accessor.interestset.UnionInterestSet; -import org.simantics.databoard.accessor.reference.ChildReference; -import org.simantics.databoard.accessor.reference.ComponentReference; -import org.simantics.databoard.accessor.reference.IndexReference; -import org.simantics.databoard.accessor.reference.LabelReference; -import org.simantics.databoard.accessor.reference.NameReference; -import org.simantics.databoard.adapter.AdaptException; -import org.simantics.databoard.binding.Binding; -import org.simantics.databoard.binding.UnionBinding; -import org.simantics.databoard.binding.error.BindingConstructionException; -import org.simantics.databoard.binding.error.BindingException; -import org.simantics.databoard.binding.mutable.MutableVariant; -import org.simantics.databoard.serialization.Serializer; -import org.simantics.databoard.serialization.SerializerConstructionException; -import org.simantics.databoard.type.Datatype; -import org.simantics.databoard.type.UnionType; -import org.simantics.databoard.util.binary.Blob; -import org.simantics.databoard.util.binary.Endian; +import gnu.trove.map.hash.TObjectIntHashMap; + +import java.io.IOException; +import java.lang.ref.SoftReference; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Executor; + +import org.simantics.databoard.accessor.Accessor; +import org.simantics.databoard.accessor.UnionAccessor; +import org.simantics.databoard.accessor.error.AccessorConstructionException; +import org.simantics.databoard.accessor.error.AccessorException; +import org.simantics.databoard.accessor.error.ReferenceException; +import org.simantics.databoard.accessor.event.Event; +import org.simantics.databoard.accessor.event.UnionValueAssigned; +import org.simantics.databoard.accessor.event.ValueAssigned; +import org.simantics.databoard.accessor.file.FileUnionAccessor; +import org.simantics.databoard.accessor.impl.AccessorParams; +import org.simantics.databoard.accessor.impl.ListenerEntry; +import org.simantics.databoard.accessor.interestset.InterestSet; +import org.simantics.databoard.accessor.interestset.UnionInterestSet; +import org.simantics.databoard.accessor.reference.ChildReference; +import org.simantics.databoard.accessor.reference.ComponentReference; +import org.simantics.databoard.accessor.reference.IndexReference; +import org.simantics.databoard.accessor.reference.LabelReference; +import org.simantics.databoard.accessor.reference.NameReference; +import org.simantics.databoard.adapter.AdaptException; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.binding.UnionBinding; +import org.simantics.databoard.binding.error.BindingConstructionException; +import org.simantics.databoard.binding.error.BindingException; +import org.simantics.databoard.binding.mutable.MutableVariant; +import org.simantics.databoard.serialization.Serializer; +import org.simantics.databoard.serialization.SerializerConstructionException; +import org.simantics.databoard.type.Datatype; +import org.simantics.databoard.type.UnionType; +import org.simantics.databoard.util.binary.Blob; +import org.simantics.databoard.util.binary.Endian; public class BinaryUnion extends BinaryObject implements UnionAccessor, FileUnionAccessor { @@ -70,8 +70,8 @@ public class BinaryUnion extends BinaryObject implements UnionAccessor, FileUnio @Override public void setValueNoflush(Binding binding, Object newValue) - throws AccessorException { - assert b.isOpen(); + throws AccessorException { + assert b.isOpen(); writeLock(); try { UnionBinding ub = (UnionBinding) binding; @@ -81,16 +81,16 @@ public class BinaryUnion extends BinaryObject implements UnionAccessor, FileUnio setComponentValueNoflush(tag, cb, cv); } catch (BindingException e) { throw new AccessorException(e); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } } @SuppressWarnings("unchecked") @Override public T getComponentAccessor() - throws AccessorConstructionException { - assert b.isOpen(); + throws AccessorConstructionException { + assert b.isOpen(); readLock(); try { // Get existing or create new @@ -128,8 +128,8 @@ public class BinaryUnion extends BinaryObject implements UnionAccessor, FileUnio return (T) sa; } catch (IOException e) { throw new AccessorConstructionException( e ); - } finally { - readUnlock(); + } finally { + readUnlock(); } } @@ -146,8 +146,8 @@ public class BinaryUnion extends BinaryObject implements UnionAccessor, FileUnio @Override public Object getComponentValue(Binding componentBinding) - throws AccessorException { - assert b.isOpen(); + throws AccessorException { + assert b.isOpen(); readLock(); try { b.position( 0L ); @@ -162,76 +162,76 @@ public class BinaryUnion extends BinaryObject implements UnionAccessor, FileUnio return s.deserialize(b, ids); } catch (IOException e) { throw new AccessorException(e); - } catch (SerializerConstructionException e) { - throw new AccessorException(e); - } finally { - readUnlock(); + } catch (SerializerConstructionException e) { + throw new AccessorException(e); + } finally { + readUnlock(); } } @Override - public int getTag() throws AccessorException { - assert b.isOpen(); + public int getTag() throws AccessorException { + assert b.isOpen(); readLock(); try { b.position(0L); return Endian.getUInt(b, count()-1); } catch (IOException e) { throw new AccessorException(e); - } finally { - readUnlock(); + } finally { + readUnlock(); } } @Override public void setComponentValue(int tag, Binding componentBinding, - Object componentValue) throws AccessorException { - assert b.isOpen(); - writeLock(); + Object componentValue) throws AccessorException { + assert b.isOpen(); + writeLock(); try { setComponentValueNoflush(tag, componentBinding, componentValue); - flush(); - } finally { - writeUnlock(); + flush(); + } finally { + writeUnlock(); } } @SuppressWarnings("unchecked") @Override public T getComponent(ChildReference reference) - throws AccessorConstructionException { - assert b.isOpen(); + throws AccessorConstructionException { + assert b.isOpen(); readLock(); try { - if (reference==null) return (T) this; - - if (reference instanceof LabelReference) { - LabelReference lr = (LabelReference) reference; - Integer tag = type().getComponentIndex( lr.label ); - - if (tag==null && lr.label.equals("uv")) { - Accessor result = getComponentAccessor(); - if (reference.getChildReference() != null) - result = result.getComponent(reference.getChildReference()); - return (T) result; - } else if (tag==null) { - throw new ReferenceException("Tag \""+lr.label+"\" not found"); - } - - if (tag != getTag()) throw new ReferenceException("The union isn't currently assigned with the expected type ("+type().getComponent(tag).name+")"); - Accessor result = getComponentAccessor(); - if (reference.getChildReference() != null) - result = result.getComponent(reference.getChildReference()); - return (T) result; - } + if (reference==null) return (T) this; + + if (reference instanceof LabelReference) { + LabelReference lr = (LabelReference) reference; + Integer tag = type().getComponentIndex( lr.label ); + + if (tag==null && lr.label.equals("uv")) { + Accessor result = getComponentAccessor(); + if (reference.getChildReference() != null) + result = result.getComponent(reference.getChildReference()); + return (T) result; + } else if (tag==null) { + throw new ReferenceException("Tag \""+lr.label+"\" not found"); + } + + if (tag != getTag()) throw new ReferenceException("The union isn't currently assigned with the expected type ("+type().getComponent(tag).name+")"); + Accessor result = getComponentAccessor(); + if (reference.getChildReference() != null) + result = result.getComponent(reference.getChildReference()); + return (T) result; + } if (reference instanceof ComponentReference) { Accessor result = getComponentAccessor(); if (reference.getChildReference() != null) result = result.getComponent(reference.getChildReference()); return (T) result; - } - + } + if (reference instanceof IndexReference) { IndexReference ir = (IndexReference) reference; if (ir.index<0 || ir.index>=type().getComponentCount()) throw new ReferenceException("Tag index out of bounds"); @@ -240,8 +240,8 @@ public class BinaryUnion extends BinaryObject implements UnionAccessor, FileUnio if (reference.getChildReference() != null) result = result.getComponent(reference.getChildReference()); return (T) result; - } - + } + if (reference instanceof NameReference) { NameReference nr = (NameReference) reference; Integer tag = type().getComponentIndex( nr.name ); @@ -251,20 +251,20 @@ public class BinaryUnion extends BinaryObject implements UnionAccessor, FileUnio if (reference.getChildReference() != null) result = result.getComponent(reference.getChildReference()); return (T) result; - } + } throw new ReferenceException(reference.getClass()+" is not a reference of OptionalType"); } catch (AccessorException ae) { throw new AccessorConstructionException(ae); - } finally { - readUnlock(); + } finally { + readUnlock(); } } @Override public void setComponentValueNoflush(int tag, Binding cb, Object cv) throws AccessorException { - assert b.isOpen(); + assert b.isOpen(); writeLock(); try { int oldTag = getTag(); @@ -328,10 +328,10 @@ public class BinaryUnion extends BinaryObject implements UnionAccessor, FileUnio throw new AccessorException(e); } catch (AdaptException e) { throw new AccessorException(e); - } catch (SerializerConstructionException e) { - throw new AccessorException(e); - } finally { - writeUnlock(); + } catch (SerializerConstructionException e) { + throw new AccessorException(e); + } finally { + writeUnlock(); } } @@ -371,26 +371,26 @@ public class BinaryUnion extends BinaryObject implements UnionAccessor, FileUnio @Override Event applyLocal(Event e, boolean makeRollback) throws AccessorException { - Event rollback = null; - if (makeRollback) { - try { - UnionType ut = type(); - int tag = getTag(); - Datatype ct = ut.getComponent(tag).type; - Binding cb = params.bindingScheme.getBinding(ct); - Object cv = getComponentValue(cb); - MutableVariant v = new MutableVariant(cb, cv); - rollback = new UnionValueAssigned(tag, v); - } catch (BindingConstructionException e2) { - throw new AccessorException( e2 ); - } - } - - if (e instanceof ValueAssigned) { - ValueAssigned va = (ValueAssigned) e; - setValueNoflush(va.newValue.getBinding(), va.newValue.getValue()); - return rollback; - } else + Event rollback = null; + if (makeRollback) { + try { + UnionType ut = type(); + int tag = getTag(); + Datatype ct = ut.getComponent(tag).type; + Binding cb = params.bindingScheme.getBinding(ct); + Object cv = getComponentValue(cb); + MutableVariant v = new MutableVariant(cb, cv); + rollback = new UnionValueAssigned(tag, v); + } catch (BindingConstructionException e2) { + throw new AccessorException( e2 ); + } + } + + if (e instanceof ValueAssigned) { + ValueAssigned va = (ValueAssigned) e; + setValueNoflush(va.newValue.getBinding(), va.newValue.getValue()); + return rollback; + } else if (e instanceof UnionValueAssigned) { UnionValueAssigned ua = (UnionValueAssigned) e; if (ua.tag<0 || ua.tag>=type().getComponentCount()) throw new AccessorException("Tag index ("+ua.tag+") out of bounds."); @@ -398,9 +398,9 @@ public class BinaryUnion extends BinaryObject implements UnionAccessor, FileUnio throw new AccessorException("Cannot assign "+ua.newValue.type()+" to "+type().getComponent(ua.tag).type); setComponentValueNoflush(ua.tag, ua.newValue.getBinding(), ua.newValue.getValue()); - return rollback; - } else { - throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Union Type"); + return rollback; + } else { + throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Union Type"); } }