X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Fbinding%2FUnionBinding.java;h=a7152f703b9db7a74d3333b9c7cf6362af47c0f5;hb=1dfc44ff89362fb4500ee9427432541b92d1953f;hp=6934c8ac5234b381b4687e57494143c178ba2d9f;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/UnionBinding.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/UnionBinding.java index 6934c8ac5..a7152f703 100644 --- a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/UnionBinding.java +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/UnionBinding.java @@ -1,30 +1,30 @@ -/******************************************************************************* - * 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.Set; - -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.binding.error.BindingException; -import org.simantics.databoard.binding.error.RuntimeBindingException; -import org.simantics.databoard.binding.impl.BindingPrintContext; -import org.simantics.databoard.type.UnionType; -import org.simantics.databoard.util.IdentityPair; +import java.util.IdentityHashMap; +import java.util.Set; + +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.binding.error.BindingException; +import org.simantics.databoard.binding.error.RuntimeBindingException; +import org.simantics.databoard.binding.impl.BindingPrintContext; +import org.simantics.databoard.type.UnionType; +import org.simantics.databoard.util.IdentityPair; /** @@ -52,42 +52,42 @@ public abstract class UnionBinding extends Binding { return type().getComponentCount(); } - public Binding getComponentBinding(int tagIndex) { - return componentBindings[tagIndex]; - } - - public Binding getComponentBinding(String tagName) { - return componentBindings[ type().getComponentIndex2(tagName) ]; - } - + public Binding getComponentBinding(int tagIndex) { + return componentBindings[tagIndex]; + } + + public Binding getComponentBinding(String tagName) { + return componentBindings[ type().getComponentIndex2(tagName) ]; + } + public Binding[] getComponentBindings() { return componentBindings; } - - /** - * Get tag number of an instance. - * - * @param obj - * @return the tag number - * @throws BindingException is thrown if the instance is not a tag of this union + + /** + * Get tag number of an instance. + * + * @param obj + * @return the tag number + * @throws BindingException is thrown if the instance is not a tag of this union */ public abstract int getTag(Object obj) throws BindingException; public abstract Object getValue(Object obj) throws BindingException; - public abstract Object create(int tag, Object value) throws BindingException; - - /** - * Create a new union object with tag of default value. - * - * @param tag - * @return new union object - * @throws BindingException - */ - public Object createDefault(int tag) throws BindingException { - Binding cb = getComponentBinding(tag); - Object to = cb.createDefault(); - return create(tag, to); + public abstract Object create(int tag, Object value) throws BindingException; + + /** + * Create a new union object with tag of default value. + * + * @param tag + * @return new union object + * @throws BindingException + */ + public Object createDefault(int tag) throws BindingException { + Binding cb = getComponentBinding(tag); + Object to = cb.createDefault(); + return create(tag, to); } public Object create(String tag, Object value) throws BindingException { @@ -105,65 +105,65 @@ public abstract class UnionBinding extends Binding { throw new RuntimeBindingException(e); } } - - @Override - public void readFrom(Binding srcBinding, Object src, Object dst) - throws BindingException { - UnionBinding sb = (UnionBinding) srcBinding; - int newTag = sb.getTag(src); - int oldTag = getTag(dst); - - // New value binding - Binding nvb = sb.getComponentBinding(newTag); - // New value - Object nv = sb.getValue(src); - - // Same tag - if (newTag==oldTag) { - // Same tag - old value is used if possible - - // Old value binding - Binding ovb = getComponentBinding(oldTag); - - Object ov = getValue(dst); - ov = ovb.readFromTry(nvb, nv, ov); - setValue(dst, oldTag, ov); - - } else { - // Different tag - old value is not used - boolean clone = !nvb.isImmutable(); - Binding dcb = getComponentBinding(newTag); - boolean adapt = nvb!=dcb; - - if ( !adapt && !clone) { - setValue(dst, newTag, nv); - } else { - try { - // Clone or adapt value if necessary. - Object dv = Bindings.clone(nv, nvb, dcb); - setValue(dst, newTag, dv); - } catch(AdaptException e) { - throw new BindingException(e); - } - } - } - - /* - if (dcb.isImmutable() || st!=dt) { - try { - Object dv = Bindings.clone(sv, scb, dcb); - setValue(dst, st, dv); - } catch(AdaptException e) { - throw new BindingException(e); - } - } else { - Object dv = getValue(dst); - dv = dcb.readFromTry(scb, sv, dv); - setValue(dst, st, dv); - } - */ - } - + + @Override + public void readFrom(Binding srcBinding, Object src, Object dst) + throws BindingException { + UnionBinding sb = (UnionBinding) srcBinding; + int newTag = sb.getTag(src); + int oldTag = getTag(dst); + + // New value binding + Binding nvb = sb.getComponentBinding(newTag); + // New value + Object nv = sb.getValue(src); + + // Same tag + if (newTag==oldTag) { + // Same tag - old value is used if possible + + // Old value binding + Binding ovb = getComponentBinding(oldTag); + + Object ov = getValue(dst); + ov = ovb.readFromTry(nvb, nv, ov); + setValue(dst, oldTag, ov); + + } else { + // Different tag - old value is not used + boolean clone = !nvb.isImmutable(); + Binding dcb = getComponentBinding(newTag); + boolean adapt = nvb!=dcb; + + if ( !adapt && !clone) { + setValue(dst, newTag, nv); + } else { + try { + // Clone or adapt value if necessary. + Object dv = Bindings.clone(nv, nvb, dcb); + setValue(dst, newTag, dv); + } catch(AdaptException e) { + throw new BindingException(e); + } + } + } + + /* + if (dcb.isImmutable() || st!=dt) { + try { + Object dv = Bindings.clone(sv, scb, dcb); + setValue(dst, st, dv); + } catch(AdaptException e) { + throw new BindingException(e); + } + } else { + Object dv = getValue(dst); + dv = dcb.readFromTry(scb, sv, dv); + setValue(dst, st, dv); + } + */ + } + /** * Set value to an union. * Throws BindingException if value cannot be written. @@ -173,19 +173,19 @@ public abstract class UnionBinding extends Binding { * @param value * @throws BindingException */ - public abstract void setValue(Object union, int tag, Object value) throws BindingException; - - /** - * Set to tag with default value. - * - * @param union - * @param tag - * @throws BindingException - */ - public void setTag(Object union, int tag) throws BindingException { - Binding componentBinding = getComponentBinding(tag); - Object instance = componentBinding.createDefault(); - setValue(union, tag, instance); + public abstract void setValue(Object union, int tag, Object value) throws BindingException; + + /** + * Set to tag with default value. + * + * @param union + * @param tag + * @throws BindingException + */ + public void setTag(Object union, int tag) throws BindingException { + Binding componentBinding = getComponentBinding(tag); + Object instance = componentBinding.createDefault(); + setValue(union, tag, instance); } @Override @@ -243,68 +243,68 @@ public abstract class UnionBinding extends Binding { public void setComponentBindings(Binding[] componentBindings) { this.componentBindings = componentBindings; } - - @Override - protected void toString(Object value, BindingPrintContext ctx) throws BindingException { - int tag = getTag(value); - ctx.b.append(type().getComponent(tag).name); - ctx.b.append(' '); - getComponentBinding(tag).toString(getValue(value), ctx); - } - - @Override - public Binding getComponentBinding(ChildReference path) { - if (path==null) return this; - if (path instanceof IndexReference) { - IndexReference ir = (IndexReference) path; - return componentBindings[ir.index].getComponentBinding(path.childReference); - } - if (path instanceof NameReference) { - NameReference nr = (NameReference) path; - return getComponentBinding( nr.name ).getComponentBinding(path.childReference); - } - if (path instanceof LabelReference) { - LabelReference lr = (LabelReference) path; - try { - Integer i = new Integer(lr.label); - return getComponentBinding( i ).getComponentBinding(path.childReference); - } catch (NumberFormatException nfe) { - return getComponentBinding( lr.label ).getComponentBinding(path.childReference); - } - } - throw new IllegalArgumentException(); - } - - /** - * Returns true if the tag of this union type can be modified - * - * @return true if mutable - */ - public boolean isTagMutable() { - return true; - } - - @Override - protected boolean deepEquals(Object obj, - Set> compareHistory) { - if (!super.deepEquals( obj, compareHistory )) - return false; - - UnionBinding o = (UnionBinding)obj; - if (componentBindings.length != o.componentBindings.length) return false; - - for (int i = 0; i < componentBindings.length; i++) - if (!componentBindings[i].equals(o.componentBindings[i], compareHistory)) - return false; - - return true; - } - - @Override - public int deepHashCode(IdentityHashMap hashedObjects) { - int code = super.deepHashCode( hashedObjects ); - for (int i = 0; i < componentBindings.length; i++) - code = 17 * code + componentBindings[i].hashCode(hashedObjects); - return code; - } + + @Override + protected void toString(Object value, BindingPrintContext ctx) throws BindingException { + int tag = getTag(value); + ctx.b.append(type().getComponent(tag).name); + ctx.b.append(' '); + getComponentBinding(tag).toString(getValue(value), ctx); + } + + @Override + public Binding getComponentBinding(ChildReference path) { + if (path==null) return this; + if (path instanceof IndexReference) { + IndexReference ir = (IndexReference) path; + return componentBindings[ir.index].getComponentBinding(path.childReference); + } + if (path instanceof NameReference) { + NameReference nr = (NameReference) path; + return getComponentBinding( nr.name ).getComponentBinding(path.childReference); + } + if (path instanceof LabelReference) { + LabelReference lr = (LabelReference) path; + try { + Integer i = new Integer(lr.label); + return getComponentBinding( i ).getComponentBinding(path.childReference); + } catch (NumberFormatException nfe) { + return getComponentBinding( lr.label ).getComponentBinding(path.childReference); + } + } + throw new IllegalArgumentException(); + } + + /** + * Returns true if the tag of this union type can be modified + * + * @return true if mutable + */ + public boolean isTagMutable() { + return true; + } + + @Override + protected boolean deepEquals(Object obj, + Set> compareHistory) { + if (!super.deepEquals( obj, compareHistory )) + return false; + + UnionBinding o = (UnionBinding)obj; + if (componentBindings.length != o.componentBindings.length) return false; + + for (int i = 0; i < componentBindings.length; i++) + if (!componentBindings[i].equals(o.componentBindings[i], compareHistory)) + return false; + + return true; + } + + @Override + public int deepHashCode(IdentityHashMap hashedObjects) { + int code = super.deepHashCode( hashedObjects ); + for (int i = 0; i < componentBindings.length; i++) + code = 17 * code + componentBindings[i].hashCode(hashedObjects); + return code; + } }