X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Faccessor%2Fjava%2FJavaVariant.java;h=ab1f7523103c13cb66a83810c2e4e3d05abf14a7;hb=694c553a3c402b9397ff99e904b4f99b77b51df1;hp=74b7efb0d4bf0c1ded4c0c7190a6dddfac382cca;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/java/JavaVariant.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/java/JavaVariant.java index 74b7efb0d..ab1f75231 100644 --- a/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/java/JavaVariant.java +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/java/JavaVariant.java @@ -1,43 +1,43 @@ -/******************************************************************************* - * 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.java; -import java.lang.ref.SoftReference; -import java.util.concurrent.Executor; - -import org.simantics.databoard.Bindings; -import org.simantics.databoard.accessor.Accessor; -import org.simantics.databoard.accessor.VariantAccessor; -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.ValueAssigned; -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.VariantInterestSet; -import org.simantics.databoard.accessor.reference.ChildReference; -import org.simantics.databoard.accessor.reference.ComponentReference; -import org.simantics.databoard.accessor.reference.LabelReference; -import org.simantics.databoard.adapter.AdaptException; -import org.simantics.databoard.binding.ArrayBinding; -import org.simantics.databoard.binding.Binding; -import org.simantics.databoard.binding.RecordBinding; -import org.simantics.databoard.binding.VariantBinding; -import org.simantics.databoard.binding.error.BindingException; -import org.simantics.databoard.binding.mutable.MutableVariant; -import org.simantics.databoard.type.Datatype; -import org.simantics.databoard.type.VariantType; +import java.lang.ref.SoftReference; +import java.util.concurrent.Executor; + +import org.simantics.databoard.Bindings; +import org.simantics.databoard.accessor.Accessor; +import org.simantics.databoard.accessor.VariantAccessor; +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.ValueAssigned; +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.VariantInterestSet; +import org.simantics.databoard.accessor.reference.ChildReference; +import org.simantics.databoard.accessor.reference.ComponentReference; +import org.simantics.databoard.accessor.reference.LabelReference; +import org.simantics.databoard.adapter.AdaptException; +import org.simantics.databoard.binding.ArrayBinding; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.binding.RecordBinding; +import org.simantics.databoard.binding.VariantBinding; +import org.simantics.databoard.binding.error.BindingException; +import org.simantics.databoard.binding.mutable.MutableVariant; +import org.simantics.databoard.type.Datatype; +import org.simantics.databoard.type.VariantType; public class JavaVariant extends JavaObject implements VariantAccessor { @@ -62,9 +62,9 @@ public class JavaVariant extends JavaObject implements VariantAccessor { public T getContentAccessor() throws AccessorConstructionException { try { JavaObject sa = getExistingAccessor(); - if (sa==null) - { - readLock(); + if (sa==null) + { + readLock(); try { // Create new child Binding cb = getBinding().getContentBinding(object); @@ -96,8 +96,8 @@ public class JavaVariant extends JavaObject implements VariantAccessor { } le = le.next; } - } finally { - readUnlock(); + } finally { + readUnlock(); } } return (T) sa; @@ -112,22 +112,22 @@ public class JavaVariant extends JavaObject implements VariantAccessor { @Override public T getComponent(ChildReference reference) throws AccessorConstructionException { - if (reference==null) return (T) this; - - if (reference instanceof LabelReference) { - LabelReference lr = (LabelReference) reference; - if (lr.label.equals("v")) { - Accessor sa = getContentAccessor(); - if (reference.getChildReference()!=null) sa = sa.getComponent(reference.getChildReference()); - return (T) sa; - } - } + if (reference==null) return (T) this; + + if (reference instanceof LabelReference) { + LabelReference lr = (LabelReference) reference; + if (lr.label.equals("v")) { + Accessor sa = getContentAccessor(); + if (reference.getChildReference()!=null) sa = sa.getComponent(reference.getChildReference()); + return (T) sa; + } + } - if (reference instanceof ComponentReference) { - Accessor sa = getContentAccessor(); - if (reference.getChildReference()!=null) sa = sa.getComponent(reference.getChildReference()); - return (T) sa; - } + if (reference instanceof ComponentReference) { + Accessor sa = getContentAccessor(); + if (reference.getChildReference()!=null) sa = sa.getComponent(reference.getChildReference()); + return (T) sa; + } throw new ReferenceException("Variant value reference expected, got "+reference.getClass().getName()); } @@ -141,50 +141,50 @@ public class JavaVariant extends JavaObject implements VariantAccessor { @Override public void setValue(Binding variantBinding, Object newVariant) - throws AccessorException { + throws AccessorException { writeLock(); try { if (binding instanceof VariantBinding == false) - throw new AccessorException("VariantBinding is expected."); - + throw new AccessorException("VariantBinding is expected."); + Binding newValueBinding = ((VariantBinding)variantBinding).getContentBinding(newVariant); Object newValueObject = ((VariantBinding)variantBinding).getContent(newVariant); setContentValue(newValueBinding, newValueObject); } catch (BindingException e) { throw new AccessorException( e ); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } } @Override public Object getContentValue(Binding contentBinding) - throws AccessorException { + throws AccessorException { readLock(); try { return getBinding().getContent(object); } catch (BindingException e) { throw new AccessorException(e); - } finally { - readUnlock(); + } finally { + readUnlock(); } } @Override - public Datatype getContentType() throws AccessorException { + public Datatype getContentType() throws AccessorException { readLock(); try { return getBinding().getContentType(object); } catch (BindingException e) { throw new AccessorException(e); - } finally { - readUnlock(); + } finally { + readUnlock(); } } @Override public void setContentValue(Binding valueBinding, Object newValue) - throws AccessorException { + throws AccessorException { writeLock(); try { // reuse sub-accessor, if type matches, and sub-accessor exists @@ -201,31 +201,31 @@ public class JavaVariant extends JavaObject implements VariantAccessor { sa.invalidatedNotification(); child = null; sa = null; - } - - - if (binding.isImmutable() && parent!=null && parent instanceof JavaArray) { - JavaObject jo = (JavaObject) parent; - ArrayBinding ab = (ArrayBinding) jo.binding; - Object nv = getBinding().create(valueBinding, newValue); - ab.set(jo.object, (Integer)keyInParent, nv); - this.object = nv; - } else if (binding.isImmutable() && parent!=null && parent instanceof JavaRecord) { - JavaObject jo = (JavaObject) parent; - RecordBinding rb = (RecordBinding) jo.binding; - Object nv = getBinding().create(valueBinding, newValue); - rb.setComponent(jo.object, (Integer)keyInParent, nv); - this.object = nv; - } else if (binding.isImmutable() && parent!=null && parent instanceof JavaVariant) { - JavaObject jo = (JavaObject) parent; - VariantBinding vb = (VariantBinding) jo.binding; - Object nv = getBinding().create(valueBinding, newValue); - vb.setContent(jo.object, getBinding(), nv); - this.object = nv; - } else { - // Write new value - getBinding().setContent(object, valueBinding, newValue); - } + } + + + if (binding.isImmutable() && parent!=null && parent instanceof JavaArray) { + JavaObject jo = (JavaObject) parent; + ArrayBinding ab = (ArrayBinding) jo.binding; + Object nv = getBinding().create(valueBinding, newValue); + ab.set(jo.object, (Integer)keyInParent, nv); + this.object = nv; + } else if (binding.isImmutable() && parent!=null && parent instanceof JavaRecord) { + JavaObject jo = (JavaObject) parent; + RecordBinding rb = (RecordBinding) jo.binding; + Object nv = getBinding().create(valueBinding, newValue); + rb.setComponent(jo.object, (Integer)keyInParent, nv); + this.object = nv; + } else if (binding.isImmutable() && parent!=null && parent instanceof JavaVariant) { + JavaObject jo = (JavaObject) parent; + VariantBinding vb = (VariantBinding) jo.binding; + Object nv = getBinding().create(valueBinding, newValue); + vb.setContent(jo.object, getBinding(), nv); + this.object = nv; + } else { + // Write new value + getBinding().setContent(object, valueBinding, newValue); + } // Notify Listeners @@ -233,13 +233,13 @@ public class JavaVariant extends JavaObject implements VariantAccessor { while (le!=null) { // Notification VariantInterestSet is = le.getInterestSet(); - if (is.inNotifications()) { - if (is.inValues()) { - MutableVariant value = new MutableVariant( valueBinding, valueBinding.isImmutable() ? newValue : valueBinding.clone(newValue) ); - ValueAssigned e = new ValueAssigned(Bindings.MUTABLE_VARIANT, value ); - emitEvent(le, e); - } else { - emitEvent( le, new ValueAssigned() ); + if (is.inNotifications()) { + if (is.inValues()) { + MutableVariant value = new MutableVariant( valueBinding, valueBinding.isImmutable() ? newValue : valueBinding.clone(newValue) ); + ValueAssigned e = new ValueAssigned(Bindings.MUTABLE_VARIANT, value ); + emitEvent(le, e); + } else { + emitEvent( le, new ValueAssigned() ); } } @@ -259,8 +259,8 @@ public class JavaVariant extends JavaObject implements VariantAccessor { // throw new AccessorException(e); } catch (AdaptException e) { throw new AccessorException(e); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } } @@ -317,13 +317,13 @@ public class JavaVariant extends JavaObject implements VariantAccessor { Object cv = getBinding().getContent(object); rollback = new ValueAssigned( Bindings.MUTABLE_VARIANT, new MutableVariant( cb, cv ) ); } - + setValue(va.newValue.getBinding(), va.newValue.getValue()); // setContentValue(va.newValue.getBinding(), va.newValue.getValue()); - return rollback; - } else { - throw new AccessorException("Invalid event "+e.getClass().getName()); + return rollback; + } else { + throw new AccessorException("Invalid event "+e.getClass().getName()); } } catch (BindingException be) { throw new AccessorException(be);