X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Faccessor%2Fjava%2FJavaArray.java;h=3559abfed9a3487f0523c1c02852cebeafda4b53;hb=a7693ba6aaec35931c5448ef569046984e9f12f3;hp=82970dafcbc87da7e5e1b5d7a47faafae053868f;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/java/JavaArray.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/java/JavaArray.java index 82970dafc..3559abfed 100644 --- a/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/java/JavaArray.java +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/java/JavaArray.java @@ -1,51 +1,51 @@ -/******************************************************************************* - * 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.io.File; -import java.lang.ref.SoftReference; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; -import java.util.SortedMap; -import java.util.TreeMap; -import java.util.concurrent.Executor; - -import org.simantics.databoard.accessor.Accessor; -import org.simantics.databoard.accessor.ArrayAccessor; -import org.simantics.databoard.accessor.StreamAccessor; -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.ArrayElementAdded; -import org.simantics.databoard.accessor.event.ArrayElementRemoved; -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.ArrayInterestSet; -import org.simantics.databoard.accessor.interestset.InterestSet; -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.adapter.AdaptException; -import org.simantics.databoard.adapter.Adapter; -import org.simantics.databoard.adapter.AdapterConstructionException; -import org.simantics.databoard.binding.ArrayBinding; -import org.simantics.databoard.binding.Binding; -import org.simantics.databoard.binding.error.BindingException; -import org.simantics.databoard.binding.mutable.MutableVariant; -import org.simantics.databoard.type.ArrayType; +import java.io.File; +import java.lang.ref.SoftReference; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.SortedMap; +import java.util.TreeMap; +import java.util.concurrent.Executor; + +import org.simantics.databoard.accessor.Accessor; +import org.simantics.databoard.accessor.ArrayAccessor; +import org.simantics.databoard.accessor.StreamAccessor; +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.ArrayElementAdded; +import org.simantics.databoard.accessor.event.ArrayElementRemoved; +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.ArrayInterestSet; +import org.simantics.databoard.accessor.interestset.InterestSet; +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.adapter.AdaptException; +import org.simantics.databoard.adapter.Adapter; +import org.simantics.databoard.adapter.AdapterConstructionException; +import org.simantics.databoard.binding.ArrayBinding; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.binding.error.BindingException; +import org.simantics.databoard.binding.mutable.MutableVariant; +import org.simantics.databoard.type.ArrayType; public class JavaArray extends JavaObject implements ArrayAccessor, StreamAccessor { @@ -78,7 +78,7 @@ public class JavaArray extends JavaObject implements ArrayAccessor, StreamAccess } @Override - public void add(int index, Binding binding, Object value) throws AccessorException { + public void add(int index, Binding binding, Object value) throws AccessorException { writeLock(); try { boolean lastEntry = index==size(); @@ -159,8 +159,8 @@ public class JavaArray extends JavaObject implements ArrayAccessor, StreamAccess throw new AccessorException(e); // } catch (AccessorConstructionException e) { // throw new AccessorException(e); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } } @@ -266,8 +266,8 @@ public class JavaArray extends JavaObject implements ArrayAccessor, StreamAccess throw new AccessorException(e); // } catch (AccessorConstructionException e) { // throw new AccessorException(e); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } } @@ -275,18 +275,18 @@ public class JavaArray extends JavaObject implements ArrayAccessor, StreamAccess @Override public T getComponent(ChildReference reference) throws AccessorConstructionException { - if (reference==null) return (T) this; - if (reference instanceof LabelReference) { - LabelReference lr = (LabelReference) reference; - try { - Integer index = new Integer( lr.label ); - Accessor result = getAccessor(index); - if (reference.getChildReference() != null) - result = result.getComponent(reference.getChildReference()); - return (T) result; - } catch ( NumberFormatException nfe ) { - throw new ReferenceException(nfe); - } + if (reference==null) return (T) this; + if (reference instanceof LabelReference) { + LabelReference lr = (LabelReference) reference; + try { + Integer index = new Integer( lr.label ); + Accessor result = getAccessor(index); + if (reference.getChildReference() != null) + result = result.getComponent(reference.getChildReference()); + return (T) result; + } catch ( NumberFormatException nfe ) { + throw new ReferenceException(nfe); + } } else if (reference instanceof IndexReference) { IndexReference ref = (IndexReference) reference; int index = ref.getIndex(); @@ -299,22 +299,22 @@ public class JavaArray extends JavaObject implements ArrayAccessor, StreamAccess @SuppressWarnings("unchecked") @Override - public T getAccessor(int index) throws AccessorConstructionException { + public T getAccessor(int index) throws AccessorConstructionException { try { int size = getBinding().size(object); if (index<0 || index>=size) throw new ReferenceException("Element index ("+index+") out of bounds ("+size+")"); // Get existing or create new JavaObject sa = getExistingAccessor(index); - if (sa==null) { - readLock(); + if (sa==null) { + readLock(); try { // Instantiate new accessor Binding cb = getBinding().getComponentBinding(); Object cv = getBinding().get(object, index); // Instantiate correct sub accessor. - sa = createSubAccessor(this, cb, cv, params); + sa = createSubAccessor(this, cb, cv, params); sa.keyInParent = index; children.put(index, new SoftReference(sa) ); @@ -347,9 +347,9 @@ public class JavaArray extends JavaObject implements ArrayAccessor, StreamAccess // Next listener le = le.next; - } - } finally { - readUnlock(); + } + } finally { + readUnlock(); } } @@ -375,7 +375,7 @@ public class JavaArray extends JavaObject implements ArrayAccessor, StreamAccess @Override public void getAll(Binding valueBinding, Collection values) - throws AccessorException { + throws AccessorException { readLock(); try { Adapter adapter = params.adapterScheme.getAdapter(getBinding().getComponentBinding(), valueBinding, true, true); @@ -391,13 +391,13 @@ public class JavaArray extends JavaObject implements ArrayAccessor, StreamAccess throw new AccessorException(e); } catch (BindingException e) { throw new AccessorException(e); - } finally { - readUnlock(); + } finally { + readUnlock(); } } @Override - public void getAll(Binding valueBinding, Object[] array) throws AccessorException { + public void getAll(Binding valueBinding, Object[] array) throws AccessorException { readLock(); try { Adapter adapter = params.adapterScheme.getAdapter(getBinding().getComponentBinding(), valueBinding, true, true); @@ -411,14 +411,14 @@ public class JavaArray extends JavaObject implements ArrayAccessor, StreamAccess throw new AccessorException(e); } catch (AdapterConstructionException e) { throw new AccessorException(e); - } finally { - readUnlock(); + } finally { + readUnlock(); } } @Override public Object get(int index, Binding valueBinding) - throws AccessorException { + throws AccessorException { readLock(); try { Adapter adapter = params.adapterScheme.getAdapter(getBinding().getComponentBinding(), valueBinding, true, true); @@ -431,30 +431,30 @@ public class JavaArray extends JavaObject implements ArrayAccessor, StreamAccess throw new AccessorException(e); } catch (AdapterConstructionException e) { throw new AccessorException(e); - } finally { - readUnlock(); + } finally { + readUnlock(); + } + } + + @Override + public void get(int index, Binding valueBinding, Object dst) + throws AccessorException { + readLock(); + try { + Binding scb = getBinding().getComponentBinding(); + Object sv = getBinding().get(object, index); + valueBinding.readFrom(scb, sv, dst); + } catch (BindingException e) { + throw new AccessorException(e); + } finally { + readUnlock(); } - } - - @Override - public void get(int index, Binding valueBinding, Object dst) - throws AccessorException { - readLock(); - try { - Binding scb = getBinding().getComponentBinding(); - Object sv = getBinding().get(object, index); - valueBinding.readFrom(scb, sv, dst); - } catch (BindingException e) { - throw new AccessorException(e); - } finally { - readUnlock(); - } } @Override public void remove(int index, int count) throws AccessorException { if (index<0 || index+count>size()) throw new AccessorException("Index out of bounds"); - + writeLock(); try { boolean lastEntry = index==size()-count; @@ -535,8 +535,8 @@ public class JavaArray extends JavaObject implements ArrayAccessor, StreamAccess throw new AccessorException(e); } catch (BindingException e) { throw new AccessorException(e); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } } @@ -544,7 +544,7 @@ public class JavaArray extends JavaObject implements ArrayAccessor, StreamAccess @Override public void set(int index, Binding binding, Object value) throws AccessorException { - if (index<0 || index>=size()) throw new AccessorException("Index out of bounds"); + if (index<0 || index>=size()) throw new AccessorException("Index out of bounds"); writeLock(); try { JavaObject sa = getExistingAccessor(index); @@ -596,14 +596,14 @@ public class JavaArray extends JavaObject implements ArrayAccessor, StreamAccess throw new AccessorException(e); // } catch (AccessorConstructionException e) { // throw new AccessorException(e); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } } @Override public void setValue(Binding arrayBinding, Object newArray) - throws AccessorException { + throws AccessorException { writeLock(); try { // Replace all elements @@ -634,50 +634,50 @@ public class JavaArray extends JavaObject implements ArrayAccessor, StreamAccess } catch (BindingException e) { throw new AccessorException(e); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } - } - - @Override - public void setSize(int newSize) throws AccessorException { - if (newSize<0) throw new AccessorException("Index out of bounds"); - - writeLock(); - try { - int oldSize = getBinding().size(object); - - // Add dummy instances - if (newSize>oldSize) { - Binding c = getBinding().getComponentBinding(); - int count = newSize-oldSize; - Object[] arr = new Object[count]; - for (int i=0; ioldSize) { + Binding c = getBinding().getComponentBinding(); + int count = newSize-oldSize; + Object[] arr = new Object[count]; + for (int i=0; i