X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Faccessor%2Fjava%2FJavaRecord.java;h=1bc6b5c2862cab903c5b1657008ab80eb481cd39;hb=refs%2Fchanges%2F15%2F3015%2F1;hp=bae7664026a090a6aad50fb83b4b624ac2e6f7c8;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/java/JavaRecord.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/java/JavaRecord.java index bae766402..1bc6b5c28 100644 --- a/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/java/JavaRecord.java +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/java/JavaRecord.java @@ -1,41 +1,41 @@ -/******************************************************************************* - * 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.accessor.Accessor; -import org.simantics.databoard.accessor.RecordAccessor; -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.RecordInterestSet; -import org.simantics.databoard.accessor.reference.ChildReference; -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.adapter.AdapterConstructionException; -import org.simantics.databoard.binding.Binding; -import org.simantics.databoard.binding.RecordBinding; -import org.simantics.databoard.binding.error.BindingException; -import org.simantics.databoard.binding.mutable.MutableVariant; -import org.simantics.databoard.type.RecordType; +import java.lang.ref.SoftReference; +import java.util.concurrent.Executor; + +import org.simantics.databoard.accessor.Accessor; +import org.simantics.databoard.accessor.RecordAccessor; +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.RecordInterestSet; +import org.simantics.databoard.accessor.reference.ChildReference; +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.adapter.AdapterConstructionException; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.binding.RecordBinding; +import org.simantics.databoard.binding.error.BindingException; +import org.simantics.databoard.binding.mutable.MutableVariant; +import org.simantics.databoard.type.RecordType; /** * Accessor to a Java Object of Record Type. @@ -86,7 +86,7 @@ public class JavaRecord extends JavaObject implements RecordAccessor { public T getFieldAccessor(int index) throws AccessorConstructionException { if (index<0 || index>=count()) throw new ReferenceException("Field index ("+index+") out of bounds ("+count()+")"); - + readLock(); try { // Get existing or create new @@ -97,7 +97,7 @@ public class JavaRecord extends JavaObject implements RecordAccessor { // Instantiate new accessor Binding cb = getBinding().getComponentBindings()[index]; Object cv = getBinding().getComponent(object, index); - sa = createSubAccessor(this, cb, cv, params); + sa = createSubAccessor(this, cb, cv, params); sa.keyInParent = index; children[index] = new SoftReference(sa); @@ -121,8 +121,8 @@ public class JavaRecord extends JavaObject implements RecordAccessor { return (T) sa; } catch (BindingException e) { throw new AccessorConstructionException(e); - } finally { - readUnlock(); + } finally { + readUnlock(); } } @@ -140,18 +140,18 @@ public class JavaRecord extends JavaObject implements RecordAccessor { JavaObject accessor = (ref!=null)?(JavaObject)ref.get():null; return accessor; } - - @Override - public Object getFieldValue(String fieldName, Binding fieldBinding) - throws AccessorException { - int fieldIndex = type().getComponentIndex(fieldName); - if (fieldIndex<0) throw new AccessorException("Field "+fieldName+" does not exist"); - return getFieldValue(fieldIndex, fieldBinding); - } + + @Override + public Object getFieldValue(String fieldName, Binding fieldBinding) + throws AccessorException { + int fieldIndex = type().getComponentIndex(fieldName); + if (fieldIndex<0) throw new AccessorException("Field "+fieldName+" does not exist"); + return getFieldValue(fieldIndex, fieldBinding); + } @Override public Object getFieldValue(int index, Binding fieldBinding) - throws AccessorException { + throws AccessorException { readLock(); try { Object cv = getBinding().getComponent(object, index); @@ -161,18 +161,18 @@ public class JavaRecord extends JavaObject implements RecordAccessor { throw new AccessorException(e); } catch (AdaptException e) { throw new AccessorException(e); - } catch (AdapterConstructionException e) { - throw new AccessorException(e); - } finally { - readUnlock(); + } catch (AdapterConstructionException e) { + throw new AccessorException(e); + } finally { + readUnlock(); } } - - public void setFieldValue(String fieldName, Binding fieldBinding, Object value) throws AccessorException { - int fieldIndex = type().getComponentIndex(fieldName); - if (fieldIndex<0) throw new AccessorException("Field "+fieldName+" does not exist"); - setFieldValue(fieldIndex, fieldBinding, value); - }; + + public void setFieldValue(String fieldName, Binding fieldBinding, Object value) throws AccessorException { + int fieldIndex = type().getComponentIndex(fieldName); + if (fieldIndex<0) throw new AccessorException("Field "+fieldName+" does not exist"); + setFieldValue(fieldIndex, fieldBinding, value); + }; @Override public void setFieldValue(int index, Binding binding, Object value) @@ -215,10 +215,10 @@ public class JavaRecord extends JavaObject implements RecordAccessor { throw new AccessorException(e); } catch (AdaptException e) { throw new AccessorException(e); - } catch (AdapterConstructionException e) { - throw new AccessorException(e); - } finally { - writeUnlock(); + } catch (AdapterConstructionException e) { + throw new AccessorException(e); + } finally { + writeUnlock(); } } @@ -264,40 +264,40 @@ public class JavaRecord extends JavaObject implements RecordAccessor { public T getComponent(ChildReference reference) throws AccessorConstructionException { if (reference==null) return (T) this; - if (reference instanceof LabelReference) { - LabelReference lr = (LabelReference) reference; - String fieldName = lr.label; - Integer index = type().getComponentIndex(fieldName); - if (index==null) throw new ReferenceException("RecordType doesn't have field by name \""+fieldName+"\""); - JavaObject sa = getFieldAccessor(index); - if (reference.getChildReference() != null) sa = sa.getComponent(reference.getChildReference()); - return (T) sa; - } + if (reference instanceof LabelReference) { + LabelReference lr = (LabelReference) reference; + String fieldName = lr.label; + Integer index = type().getComponentIndex(fieldName); + if (index==null) throw new ReferenceException("RecordType doesn't have field by name \""+fieldName+"\""); + JavaObject sa = getFieldAccessor(index); + if (reference.getChildReference() != null) sa = sa.getComponent(reference.getChildReference()); + return (T) sa; + } if (reference instanceof IndexReference) { IndexReference ref = (IndexReference) reference; int index = ref.getIndex(); - JavaObject sa = getFieldAccessor(index); - if (reference.getChildReference() != null) sa = sa.getComponent(reference.getChildReference()); + JavaObject sa = getFieldAccessor(index); + if (reference.getChildReference() != null) sa = sa.getComponent(reference.getChildReference()); return (T) sa; - } - + } + if (reference instanceof NameReference) { NameReference ref = (NameReference) reference; String fieldName = ref.getName(); Integer index = type().getComponentIndex(fieldName); if (index==null) throw new ReferenceException("RecordType doesn't have field by name \""+fieldName+"\""); - JavaObject sa = getFieldAccessor(index); - if (reference.getChildReference() != null) sa = sa.getComponent(reference.getChildReference()); + JavaObject sa = getFieldAccessor(index); + if (reference.getChildReference() != null) sa = sa.getComponent(reference.getChildReference()); return (T) sa; - } - + } + throw new ReferenceException(reference.getClass()+" is not a subreference of RecordType"); } @Override - public void setValue(Binding binding, Object newValue) throws AccessorException { + public void setValue(Binding binding, Object newValue) throws AccessorException { writeLock(); try { RecordBinding rb = (RecordBinding) binding; @@ -308,22 +308,22 @@ public class JavaRecord extends JavaObject implements RecordAccessor { } } catch (BindingException e) { throw new AccessorException(e); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } } - + @Override Event applyLocal(Event e, boolean makeRollback) throws AccessorException { - Event rollback = null; - if (makeRollback) rollback = new ValueAssigned(getBinding(), getValue(getBinding())); - if (e instanceof ValueAssigned) { - ValueAssigned va = (ValueAssigned) e; - setValue(va.newValue.getBinding(), va.newValue.getValue()); - return rollback; - } else { - throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Record Type"); + Event rollback = null; + if (makeRollback) rollback = new ValueAssigned(getBinding(), getValue(getBinding())); + if (e instanceof ValueAssigned) { + ValueAssigned va = (ValueAssigned) e; + setValue(va.newValue.getBinding(), va.newValue.getValue()); + return rollback; + } else { + throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Record Type"); } }