X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Fbinding%2FMapBinding.java;h=e36ce1a4b089caf4e4176f9a989addbe7ec6e612;hp=94f238ad91d9bbd788aa424896c4182776d380ab;hb=refs%2Fchanges%2F38%2F238%2F2;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/MapBinding.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/MapBinding.java index 94f238ad9..e36ce1a4b 100644 --- a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/MapBinding.java +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/MapBinding.java @@ -1,38 +1,38 @@ -/******************************************************************************* - * 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.binding; -import java.util.IdentityHashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.TreeMap; -import java.util.TreeSet; - -import org.simantics.databoard.Bindings; -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.Adapter; -import org.simantics.databoard.adapter.AdapterConstructionException; -import org.simantics.databoard.binding.error.BindingException; -import org.simantics.databoard.binding.error.RuntimeBindingException; -import org.simantics.databoard.binding.impl.BindingPrintContext; -import org.simantics.databoard.type.MapType; -import org.simantics.databoard.util.IdentityPair; +import java.util.IdentityHashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; + +import org.simantics.databoard.Bindings; +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.Adapter; +import org.simantics.databoard.adapter.AdapterConstructionException; +import org.simantics.databoard.binding.error.BindingException; +import org.simantics.databoard.binding.error.RuntimeBindingException; +import org.simantics.databoard.binding.impl.BindingPrintContext; +import org.simantics.databoard.type.MapType; +import org.simantics.databoard.util.IdentityPair; /** * This is a binding of Map Type and a Java Object @@ -175,41 +175,41 @@ public abstract class MapBinding extends Binding { public abstract Object[] getKeys(Object map) throws BindingException; public abstract void getKeys(Object map, Set keys) throws BindingException; - - /** - * Count the number of entries between two keyes - * - * @param src - * @param from - * @param fromInclusive - * @param end - * @param endInclusive - * @throws BindingException - */ - public abstract int count(Object src, Object from, boolean fromInclusive, Object end, boolean endInclusive) throws BindingException; - - /** - * Read a range of entries - * - * @param src - * @param from - * @param fromInclusive - * @param end - * @param endInclusive - * @param dstKeyArrayBinding - * @param dstKeyArray - * @param dstValueArrayBinding - * @param dstValueArray - * @param resultLimit maximum number of entries to read, -1 for no limit - * @return the number of entries read - * @throws BindingException - */ - public abstract int getEntries( - Object src, - Object from, boolean fromInclusive, Object end, boolean endInclusive, - ArrayBinding dstKeyArrayBinding, Object dstKeyArray, - ArrayBinding dstValueArrayBinding, Object dstValueArray, - int resultLimit) throws BindingException; + + /** + * Count the number of entries between two keyes + * + * @param src + * @param from + * @param fromInclusive + * @param end + * @param endInclusive + * @throws BindingException + */ + public abstract int count(Object src, Object from, boolean fromInclusive, Object end, boolean endInclusive) throws BindingException; + + /** + * Read a range of entries + * + * @param src + * @param from + * @param fromInclusive + * @param end + * @param endInclusive + * @param dstKeyArrayBinding + * @param dstKeyArray + * @param dstValueArrayBinding + * @param dstValueArray + * @param resultLimit maximum number of entries to read, -1 for no limit + * @return the number of entries read + * @throws BindingException + */ + public abstract int getEntries( + Object src, + Object from, boolean fromInclusive, Object end, boolean endInclusive, + ArrayBinding dstKeyArrayBinding, Object dstKeyArray, + ArrayBinding dstValueArrayBinding, Object dstValueArray, + int resultLimit) throws BindingException; /** * Get values in order @@ -252,52 +252,52 @@ public abstract class MapBinding extends Binding { public T accept(Visitor v) { return v.visit(this); } - - @Override - public void readFrom(Binding srcBinding, Object src, Object dst) - throws BindingException { - try { - MapBinding sb = (MapBinding) srcBinding; - Binding dkb = getKeyBinding(); - Binding dvb = getValueBinding(); - Set oldKeys = new TreeSet(dkb); - getKeys(dst, oldKeys); - Binding skb = sb.getKeyBinding(); - Binding svb = sb.getValueBinding(); - boolean cbImmutable = dvb.isImmutable(); - Adapter ka = Bindings.adapterFactory.getAdapter(skb, dkb, false, false); - Adapter va = cbImmutable ? Bindings.adapterFactory.getAdapter(svb, dvb, false, true) : null; - - // Copy keys from other map - for (Object sk : sb.getKeys(src)) { - Object dk = ka.adapt(sk); - Object sv = sb.get(src, sk); - if (cbImmutable) { - Object dv = va.adapt(sv); - put(dst, dk, dv); - } else - if (containsKey(dst, dk)) { - Object dv = get(dst, dk); - dv = dvb.readFromTry(svb, sv, dv); - put(dst, dk, dv); - } else { - Object dv = dvb.createDefault(); - dv = dvb.readFromTry(svb, sv, dv); - put(dst, dk, dv); - } - oldKeys.remove(dk); - } - - // Remove unused keys - for (Object k : oldKeys) remove(dst, k); - - } catch (AdapterConstructionException e) { - throw new BindingException(e); - } catch (AdaptException e) { - throw new BindingException(e); - } - } - + + @Override + public void readFrom(Binding srcBinding, Object src, Object dst) + throws BindingException { + try { + MapBinding sb = (MapBinding) srcBinding; + Binding dkb = getKeyBinding(); + Binding dvb = getValueBinding(); + Set oldKeys = new TreeSet(dkb); + getKeys(dst, oldKeys); + Binding skb = sb.getKeyBinding(); + Binding svb = sb.getValueBinding(); + boolean cbImmutable = dvb.isImmutable(); + Adapter ka = Bindings.adapterFactory.getAdapter(skb, dkb, false, false); + Adapter va = cbImmutable ? Bindings.adapterFactory.getAdapter(svb, dvb, false, true) : null; + + // Copy keys from other map + for (Object sk : sb.getKeys(src)) { + Object dk = ka.adapt(sk); + Object sv = sb.get(src, sk); + if (cbImmutable) { + Object dv = va.adapt(sv); + put(dst, dk, dv); + } else + if (containsKey(dst, dk)) { + Object dv = get(dst, dk); + dv = dvb.readFromTry(svb, sv, dv); + put(dst, dk, dv); + } else { + Object dv = dvb.createDefault(); + dv = dvb.readFromTry(svb, sv, dv); + put(dst, dk, dv); + } + oldKeys.remove(dk); + } + + // Remove unused keys + for (Object k : oldKeys) remove(dst, k); + + } catch (AdapterConstructionException e) { + throw new BindingException(e); + } catch (AdaptException e) { + throw new BindingException(e); + } + } + @Override public int deepHashValue(Object map, IdentityHashMap hashedObjects) throws BindingException { @@ -395,70 +395,70 @@ public abstract class MapBinding extends Binding { public abstract Object getCeilingKey(Object map, Object key); public abstract Object getHigherKey(Object map, Object key); - - @Override - protected void toString(Object value, BindingPrintContext ctx) throws BindingException { - Binding keyBinding = getKeyBinding(); - Binding valueBinding = getValueBinding(); - ctx.b.append("{ "); - boolean first = true; - for(Object key : getKeys(value)) { - if(first) - first = false; - else { - ctx.b.append(", "); - if ( !ctx.singleLine ) ctx.b.append('\n'); - } - keyBinding.toString(key, ctx); - ctx.b.append(" = "); - valueBinding.toString(get(value, key), ctx); - } - ctx.b.append(" }"); - } - - @Override - public Binding getComponentBinding(ChildReference path) { - if (path==null) return this; - if (path instanceof IndexReference) { - IndexReference ir = (IndexReference) path; - if (ir.index==0) return keyBinding.getComponentBinding(path.childReference); - if (ir.index==1) return valueBinding.getComponentBinding(path.childReference); - } - if (path instanceof LabelReference) { - LabelReference lr = (LabelReference) path; - if (lr.label.equals("0") || lr.label.equals("key")) return keyBinding.getComponentBinding(path.childReference); - if (lr.label.equals("1") || lr.label.equals("value")) return valueBinding.getComponentBinding(path.childReference); - } - if (path instanceof NameReference) { - NameReference nr = (NameReference) path; - if (nr.name.equals("key")) return keyBinding.getComponentBinding(path.childReference); - if (nr.name.equals("value")) return valueBinding.getComponentBinding(path.childReference); - } - throw new IllegalArgumentException(); - } - - - @Override - public int getComponentCount() { - return 2; - } - - @Override - public Binding getComponentBinding(int index) { - if (index==0) return keyBinding; - if (index==1) return valueBinding; - throw new IllegalArgumentException(); - } - - @Override - protected boolean deepEquals(Object obj, - Set> compareHistory) { - MapBinding o = (MapBinding)obj; - return super.deepEquals( obj, compareHistory ) && keyBinding.equals(o.keyBinding, compareHistory) && valueBinding.equals(o.valueBinding, compareHistory); - } - - @Override - public int deepHashCode(IdentityHashMap hashedObjects) { - return super.deepHashCode(hashedObjects) + 13 * keyBinding.hashCode(hashedObjects) + 17 * valueBinding.hashCode(hashedObjects); + + @Override + protected void toString(Object value, BindingPrintContext ctx) throws BindingException { + Binding keyBinding = getKeyBinding(); + Binding valueBinding = getValueBinding(); + ctx.b.append("{ "); + boolean first = true; + for(Object key : getKeys(value)) { + if(first) + first = false; + else { + ctx.b.append(", "); + if ( !ctx.singleLine ) ctx.b.append('\n'); + } + keyBinding.toString(key, ctx); + ctx.b.append(" = "); + valueBinding.toString(get(value, key), ctx); + } + ctx.b.append(" }"); + } + + @Override + public Binding getComponentBinding(ChildReference path) { + if (path==null) return this; + if (path instanceof IndexReference) { + IndexReference ir = (IndexReference) path; + if (ir.index==0) return keyBinding.getComponentBinding(path.childReference); + if (ir.index==1) return valueBinding.getComponentBinding(path.childReference); + } + if (path instanceof LabelReference) { + LabelReference lr = (LabelReference) path; + if (lr.label.equals("0") || lr.label.equals("key")) return keyBinding.getComponentBinding(path.childReference); + if (lr.label.equals("1") || lr.label.equals("value")) return valueBinding.getComponentBinding(path.childReference); + } + if (path instanceof NameReference) { + NameReference nr = (NameReference) path; + if (nr.name.equals("key")) return keyBinding.getComponentBinding(path.childReference); + if (nr.name.equals("value")) return valueBinding.getComponentBinding(path.childReference); + } + throw new IllegalArgumentException(); + } + + + @Override + public int getComponentCount() { + return 2; + } + + @Override + public Binding getComponentBinding(int index) { + if (index==0) return keyBinding; + if (index==1) return valueBinding; + throw new IllegalArgumentException(); + } + + @Override + protected boolean deepEquals(Object obj, + Set> compareHistory) { + MapBinding o = (MapBinding)obj; + return super.deepEquals( obj, compareHistory ) && keyBinding.equals(o.keyBinding, compareHistory) && valueBinding.equals(o.valueBinding, compareHistory); + } + + @Override + public int deepHashCode(IdentityHashMap hashedObjects) { + return super.deepHashCode(hashedObjects) + 13 * keyBinding.hashCode(hashedObjects) + 17 * valueBinding.hashCode(hashedObjects); } }