X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Faccessor%2Fjava%2FJavaMap.java;fp=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Faccessor%2Fjava%2FJavaMap.java;h=ebfe4052cf78e7817221dd8d0f94457f0010349c;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=bc1fedabbe3148c5d0f49f0265ed561b7c834df1;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/java/JavaMap.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/java/JavaMap.java index bc1fedabb..ebfe4052c 100644 --- a/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/java/JavaMap.java +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/java/JavaMap.java @@ -1,50 +1,50 @@ -/******************************************************************************* - * 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.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.TreeMap; -import java.util.TreeSet; -import java.util.concurrent.Executor; - -import org.simantics.databoard.Bindings; -import org.simantics.databoard.accessor.Accessor; -import org.simantics.databoard.accessor.MapAccessor; -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.MapEntryAdded; -import org.simantics.databoard.accessor.event.MapEntryRemoved; -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.MapInterestSet; -import org.simantics.databoard.accessor.reference.ChildReference; -import org.simantics.databoard.accessor.reference.KeyReference; -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.MapBinding; -import org.simantics.databoard.binding.error.BindingException; -import org.simantics.databoard.binding.mutable.MutableVariant; -import org.simantics.databoard.type.MapType; +import java.lang.ref.SoftReference; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.concurrent.Executor; + +import org.simantics.databoard.Bindings; +import org.simantics.databoard.accessor.Accessor; +import org.simantics.databoard.accessor.MapAccessor; +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.MapEntryAdded; +import org.simantics.databoard.accessor.event.MapEntryRemoved; +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.MapInterestSet; +import org.simantics.databoard.accessor.reference.ChildReference; +import org.simantics.databoard.accessor.reference.KeyReference; +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.MapBinding; +import org.simantics.databoard.binding.error.BindingException; +import org.simantics.databoard.binding.mutable.MutableVariant; +import org.simantics.databoard.type.MapType; public class JavaMap extends JavaObject implements MapAccessor { @@ -75,7 +75,7 @@ public class JavaMap extends JavaObject implements MapAccessor { } @Override - public void clear() throws AccessorException { + public void clear() throws AccessorException { writeLock(); try { if (getBinding().size(object)==0) return; @@ -112,8 +112,8 @@ public class JavaMap extends JavaObject implements MapAccessor { } catch (BindingException e) { throw new AccessorException( e ); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } } @@ -123,7 +123,7 @@ public class JavaMap extends JavaObject implements MapAccessor { * @param localKey * @throws AccessorException */ - void removeLocal(Object lk) throws AccessorException { + void removeLocal(Object lk) throws AccessorException { writeLock(); try { boolean hadValue = getBinding().containsKey(object, lk); @@ -157,12 +157,12 @@ public class JavaMap extends JavaObject implements MapAccessor { } catch (BindingException e) { throw new AccessorException( e ); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } } - void putLocal(Object lk, Object lv) throws AccessorException { + void putLocal(Object lk, Object lv) throws AccessorException { writeLock(); try { JavaObject sa = getExistingAccessor(lk); @@ -221,14 +221,14 @@ public class JavaMap extends JavaObject implements MapAccessor { throw new AccessorException( e ); } catch (AdaptException e) { throw new AccessorException( e ); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } } @Override public void put(Binding keyBinding, Object key, Binding valueBinding, - Object value) throws AccessorException { + Object value) throws AccessorException { writeLock(); try { Object rk = key; @@ -243,15 +243,15 @@ public class JavaMap extends JavaObject implements MapAccessor { throw new AccessorException(e); } catch (AdaptException e) { throw new AccessorException(e); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } } @Override public void putAll(Binding keyBinding, Binding valueBinding, Map from) - throws AccessorException { + throws AccessorException { writeLock(); try { // Convert keys and values @@ -269,13 +269,13 @@ public class JavaMap extends JavaObject implements MapAccessor { throw new AccessorException( e ); } catch (AdapterConstructionException e) { throw new AccessorException( e ); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } } @Override - public void putAll(Binding keyBinding, Binding valueBinding, Object[] keys, Object[] values) throws AccessorException { + public void putAll(Binding keyBinding, Binding valueBinding, Object[] keys, Object[] values) throws AccessorException { writeLock(); try { // Convert keys and values @@ -295,8 +295,8 @@ public class JavaMap extends JavaObject implements MapAccessor { throw new AccessorException( e ); } catch (AdapterConstructionException e) { throw new AccessorException( e ); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } } @@ -308,8 +308,8 @@ public class JavaMap extends JavaObject implements MapAccessor { removeLocal(lk); } catch (AdaptException e) { throw new AccessorException( e ); - } catch (AdapterConstructionException e) { - throw new AccessorException( e ); + } catch (AdapterConstructionException e) { + throw new AccessorException( e ); } } @@ -349,7 +349,7 @@ public class JavaMap extends JavaObject implements MapAccessor { * @param values * @throws AccessorException */ - void setValueLocal(Object keys[], Object values[]) throws AccessorException { + void setValueLocal(Object keys[], Object values[]) throws AccessorException { writeLock(); try { Set oldKeys = new TreeSet(getKeyBinding()); @@ -370,11 +370,11 @@ public class JavaMap extends JavaObject implements MapAccessor { } catch (BindingException e) { throw new AccessorException( e ); - } finally { - writeUnlock(); + } finally { + writeUnlock(); } - } + } @SuppressWarnings("unchecked") @Override @@ -390,9 +390,9 @@ public class JavaMap extends JavaObject implements MapAccessor { } JavaObject sa = getExistingAccessor(lk); - if (sa!=null) return (T) sa; - - readLock(); + if (sa!=null) return (T) sa; + + readLock(); try { Binding vb = getBinding().getValueBinding(); Binding kb = getBinding().getKeyBinding(); @@ -400,7 +400,7 @@ public class JavaMap extends JavaObject implements MapAccessor { MutableVariant kv = new MutableVariant(kb, lk); // Instantiate correct sub accessor. - sa = createSubAccessor(this, vb, lv, params); + sa = createSubAccessor(this, vb, lv, params); sa.keyInParent = lk; children.put(lk, new SoftReference(sa)); @@ -434,9 +434,9 @@ public class JavaMap extends JavaObject implements MapAccessor { // Next listener le = le.next; } - } finally { - readUnlock(); - } + } finally { + readUnlock(); + } return (T) sa; } catch (BindingException e) { @@ -469,48 +469,48 @@ public class JavaMap extends JavaObject implements MapAccessor { @Override public T getComponent(ChildReference reference) throws AccessorConstructionException { - if (reference==null) return (T) this; - if (reference instanceof LabelReference) { - LabelReference lr = (LabelReference) reference; - try { - Binding kb = getKeyBinding(); - MutableVariant variant = (MutableVariant) adapt(lr.label, Bindings.STRING, Bindings.MUTABLE_VARIANT); - Object value = variant.getValue(kb); - - Accessor result = (T) getValueAccessor(kb, value); - if (reference.getChildReference() != null) - result = result.getComponent(reference.getChildReference()); - return (T) result; - } catch (AdaptException e2) { - throw new ReferenceException(e2); - } catch (AdapterConstructionException e) { - throw new ReferenceException(e); - } + if (reference==null) return (T) this; + if (reference instanceof LabelReference) { + LabelReference lr = (LabelReference) reference; + try { + Binding kb = getKeyBinding(); + MutableVariant variant = (MutableVariant) adapt(lr.label, Bindings.STRING, Bindings.MUTABLE_VARIANT); + Object value = variant.getValue(kb); + + Accessor result = (T) getValueAccessor(kb, value); + if (reference.getChildReference() != null) + result = result.getComponent(reference.getChildReference()); + return (T) result; + } catch (AdaptException e2) { + throw new ReferenceException(e2); + } catch (AdapterConstructionException e) { + throw new ReferenceException(e); + } } else if (reference instanceof KeyReference) { KeyReference ref = (KeyReference) reference; Accessor result = getValueAccessor(ref.key.getBinding(), ref.key.getValue()); if (reference.getChildReference() != null) result = result.getComponent(reference.getChildReference()); return (T) result; - } + } throw new ReferenceException(reference.getClass().getName()+" is not a reference of a map"); } @Override - public int size() throws AccessorException { + public int size() throws AccessorException { readLock(); try { return getBinding().size(object); } catch (BindingException e) { throw new AccessorException(e); - } finally { - readUnlock(); + } finally { + readUnlock(); } } @Override public boolean containsKey(Binding keyBinding, Object key) - throws AccessorException { + throws AccessorException { readLock(); try { MapBinding mb = getBinding(); @@ -523,16 +523,16 @@ public class JavaMap extends JavaObject implements MapAccessor { throw new AccessorException(e); } catch (BindingException e) { throw new AccessorException(e); - } catch (AdapterConstructionException e) { - throw new AccessorException(e); - } finally { - readUnlock(); + } catch (AdapterConstructionException e) { + throw new AccessorException(e); + } finally { + readUnlock(); } } @Override public boolean containsValue(Binding valueBinding, Object value) - throws AccessorException { + throws AccessorException { readLock(); try { MapBinding mb = getBinding(); @@ -545,16 +545,16 @@ public class JavaMap extends JavaObject implements MapAccessor { throw new AccessorException(e); } catch (BindingException e) { throw new AccessorException(e); - } catch (AdapterConstructionException e) { - throw new AccessorException(e); - } finally { - readUnlock(); + } catch (AdapterConstructionException e) { + throw new AccessorException(e); + } finally { + readUnlock(); } } @Override public Object get(Binding keyBinding, Object key, Binding valueBinding) - throws AccessorException { + throws AccessorException { readLock(); try { MapBinding mb = getBinding(); @@ -564,7 +564,7 @@ public class JavaMap extends JavaObject implements MapAccessor { Binding rvb = valueBinding; Object rk = key; Object lk = adapt(rk, rkb, lkb); - Object lv = mb.get(object, lk); + Object lv = mb.get(object, lk); if (lv == null) return null; Object rv = adapt(lv, lvb, rvb); return rv; @@ -572,55 +572,55 @@ public class JavaMap extends JavaObject implements MapAccessor { throw new AccessorException(e); } catch (BindingException e) { throw new AccessorException(e); - } catch (AdapterConstructionException e) { - throw new AccessorException(e); - } finally { - readUnlock(); + } catch (AdapterConstructionException e) { + throw new AccessorException(e); + } finally { + readUnlock(); } - } - - @Override - public int count(Binding keyBinding, Object from, - boolean fromInclusive, Object end, boolean endInclusive) - throws AccessorException { - readLock(); - try { - MapBinding mb = getBinding(); - Object lf = params.adapterScheme.adapt(from, keyBinding, getKeyBinding()); - Object le = params.adapterScheme.adapt(end, keyBinding, getKeyBinding()); - return mb.count(object, lf, fromInclusive, le, endInclusive); - } catch (BindingException e) { - throw new AccessorException(e); - } catch (AdaptException e) { - throw new AccessorException(e); - } finally { - readUnlock(); - } - } - - - @Override - public int getEntries(Binding keyBinding, Object from, boolean fromInclusive, Object end, boolean endInclusive, ArrayBinding keyArrayBinding, Object keysArray, ArrayBinding valueArrayBinding, Object valueArray, int limit) throws AccessorException { - readLock(); - try { - MapBinding mb = getBinding(); - Object lfrom = params.adapterScheme.adapt(from, keyBinding, getKeyBinding()); - Object lend = params.adapterScheme.adapt(end, keyBinding, getKeyBinding()); - - return mb.getEntries(object, lfrom, fromInclusive, lend, endInclusive, keyArrayBinding, keysArray, valueArrayBinding, valueArray, limit); - } catch (BindingException e) { - throw new AccessorException(e); - } catch (AdaptException e) { - throw new AccessorException(e); - } finally { - readUnlock(); - } - } + } + + @Override + public int count(Binding keyBinding, Object from, + boolean fromInclusive, Object end, boolean endInclusive) + throws AccessorException { + readLock(); + try { + MapBinding mb = getBinding(); + Object lf = params.adapterScheme.adapt(from, keyBinding, getKeyBinding()); + Object le = params.adapterScheme.adapt(end, keyBinding, getKeyBinding()); + return mb.count(object, lf, fromInclusive, le, endInclusive); + } catch (BindingException e) { + throw new AccessorException(e); + } catch (AdaptException e) { + throw new AccessorException(e); + } finally { + readUnlock(); + } + } + + + @Override + public int getEntries(Binding keyBinding, Object from, boolean fromInclusive, Object end, boolean endInclusive, ArrayBinding keyArrayBinding, Object keysArray, ArrayBinding valueArrayBinding, Object valueArray, int limit) throws AccessorException { + readLock(); + try { + MapBinding mb = getBinding(); + Object lfrom = params.adapterScheme.adapt(from, keyBinding, getKeyBinding()); + Object lend = params.adapterScheme.adapt(end, keyBinding, getKeyBinding()); + + return mb.getEntries(object, lfrom, fromInclusive, lend, endInclusive, keyArrayBinding, keysArray, valueArrayBinding, valueArray, limit); + } catch (BindingException e) { + throw new AccessorException(e); + } catch (AdaptException e) { + throw new AccessorException(e); + } finally { + readUnlock(); + } + } @Override public void getAll(Binding keyBinding, Binding valueBinding, Map to) - throws AccessorException { + throws AccessorException { readLock(); try { MapBinding mb = getBinding(); @@ -644,13 +644,13 @@ public class JavaMap extends JavaObject implements MapAccessor { throw new AccessorException(e); } catch (BindingException e) { throw new AccessorException(e); - } finally { - readUnlock(); + } finally { + readUnlock(); } } @Override - public void getAll(Binding keyBinding, Binding valueBinding, Object[] keys, Object[] values) throws AccessorException { + public void getAll(Binding keyBinding, Binding valueBinding, Object[] keys, Object[] values) throws AccessorException { readLock(); try { int length = getBinding().size(object); @@ -668,13 +668,13 @@ public class JavaMap extends JavaObject implements MapAccessor { throw new AccessorException(e); } catch (BindingException e) { throw new AccessorException(e); - } finally { - readUnlock(); + } finally { + readUnlock(); } } @Override - public Object[] getKeys(Binding keyBinding) throws AccessorException { + public Object[] getKeys(Binding keyBinding) throws AccessorException { readLock(); try { Object[] result = getBinding().getKeys(object); @@ -689,13 +689,13 @@ public class JavaMap extends JavaObject implements MapAccessor { throw new AccessorException(e); } catch (BindingException e) { throw new AccessorException(e); - } finally { - readUnlock(); + } finally { + readUnlock(); } } @Override - public Object[] getValues(Binding valueBinding) throws AccessorException { + public Object[] getValues(Binding valueBinding) throws AccessorException { readLock(); try { Object[] result = getBinding().getValues(object); @@ -710,13 +710,13 @@ public class JavaMap extends JavaObject implements MapAccessor { throw new AccessorException(e); } catch (BindingException e) { throw new AccessorException(e); - } finally { - readUnlock(); + } finally { + readUnlock(); } } @Override - public Object getCeilingKey(Binding keyBinding, Object key) throws AccessorException { + public Object getCeilingKey(Binding keyBinding, Object key) throws AccessorException { readLock(); try { // Local & Requested Key Bindings @@ -730,15 +730,15 @@ public class JavaMap extends JavaObject implements MapAccessor { return rck; } 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(); } } @Override - public Object getFirstKey(Binding keyBinding) throws AccessorException { + public Object getFirstKey(Binding keyBinding) throws AccessorException { readLock(); try { Binding lkb = getKeyBinding(); @@ -748,15 +748,15 @@ public class JavaMap extends JavaObject implements MapAccessor { return rfk; } 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(); } } @Override - public Object getLastKey(Binding keyBinding) throws AccessorException { + public Object getLastKey(Binding keyBinding) throws AccessorException { readLock(); try { Binding lkb = getKeyBinding(); @@ -766,15 +766,15 @@ public class JavaMap extends JavaObject implements MapAccessor { return rfk; } 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(); } } @Override - public Object getFloorKey(Binding keyBinding, Object key) throws AccessorException { + public Object getFloorKey(Binding keyBinding, Object key) throws AccessorException { readLock(); try { // Local & Requested Key Bindings @@ -788,15 +788,15 @@ public class JavaMap extends JavaObject implements MapAccessor { return rfk; } 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(); } } @Override - public Object getHigherKey(Binding keyBinding, Object key) throws AccessorException { + public Object getHigherKey(Binding keyBinding, Object key) throws AccessorException { readLock(); try { // Local & Requested Key Bindings @@ -810,15 +810,15 @@ public class JavaMap extends JavaObject implements MapAccessor { return rhk; } 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(); } } @Override - public Object getLowerKey(Binding keyBinding, Object key) throws AccessorException { + public Object getLowerKey(Binding keyBinding, Object key) throws AccessorException { readLock(); try { // Local & Requested Key Bindings @@ -832,10 +832,10 @@ public class JavaMap extends JavaObject implements MapAccessor { return rlk; } 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(); } } @@ -894,11 +894,11 @@ public class JavaMap extends JavaObject implements MapAccessor { Event applyLocal(Event e, boolean makeRollback) throws AccessorException { Event rollback = null; - if (e instanceof ValueAssigned) { - ValueAssigned va = (ValueAssigned) e; - if (makeRollback) rollback = new ValueAssigned(getBinding(), getValue(getBinding())); - setValue(va.newValue.getBinding(), va.newValue.getValue()); - return rollback; + if (e instanceof ValueAssigned) { + ValueAssigned va = (ValueAssigned) e; + if (makeRollback) rollback = new ValueAssigned(getBinding(), getValue(getBinding())); + setValue(va.newValue.getBinding(), va.newValue.getValue()); + return rollback; } else if (e instanceof MapEntryAdded) { MapEntryAdded ea = (MapEntryAdded) e; if (ea.key==null) throw new AccessorException("Cannot apply entry added event because key is missing");