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%2FVariantBinding.java;h=60e01409020d399d2f15c1ac2083960a8eed9e06;hp=788dc8ed6dde1f3f32150cb67e43593c8b523de7;hb=a1696e5257fae039410c924155fdeffc1ce1b3e9;hpb=969bd23cab98a79ca9101af33334000879fb60c5 diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/VariantBinding.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/VariantBinding.java index 788dc8ed6..60e014090 100644 --- a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/VariantBinding.java +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/VariantBinding.java @@ -1,45 +1,45 @@ -/******************************************************************************* - * 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.Datatypes; -import org.simantics.databoard.accessor.reference.ChildReference; -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.binding.impl.ObjectVariantBinding; -import org.simantics.databoard.binding.impl.StringVariantBinding; -import org.simantics.databoard.binding.mutable.ImmutableVariantBinding; -import org.simantics.databoard.binding.mutable.MutableVariant; -import org.simantics.databoard.binding.mutable.MutableVariantBinding; -import org.simantics.databoard.binding.mutable.Variant; -import org.simantics.databoard.type.Datatype; -import org.simantics.databoard.type.VariantType; -import org.simantics.databoard.util.DataValueUtil; -import org.simantics.databoard.util.IdentityPair; +import java.util.IdentityHashMap; +import java.util.Set; + +import org.simantics.databoard.Bindings; +import org.simantics.databoard.Datatypes; +import org.simantics.databoard.accessor.reference.ChildReference; +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.binding.impl.ObjectVariantBinding; +import org.simantics.databoard.binding.impl.StringVariantBinding; +import org.simantics.databoard.binding.mutable.ImmutableVariantBinding; +import org.simantics.databoard.binding.mutable.MutableVariant; +import org.simantics.databoard.binding.mutable.MutableVariantBinding; +import org.simantics.databoard.binding.mutable.Variant; +import org.simantics.databoard.type.Datatype; +import org.simantics.databoard.type.VariantType; +import org.simantics.databoard.util.DataValueUtil; +import org.simantics.databoard.util.IdentityPair; /** - * This is the abstract base class for bindings of VariantType Java Objects. + * This is the abstract base class for bindings of VariantType Java Objects. * Variant is a container that has value of any Datatype. * - * @see VariantType The Datatype - * @see ImmutableVariantBinding Binds variant to {@link Variant} - * @see MutableVariantBinding Binds variant to {@link MutableVariant} - * @see ObjectVariantBinding Binds variant to java.lang.Object + * @see VariantType The Datatype + * @see ImmutableVariantBinding Binds variant to {@link Variant} + * @see MutableVariantBinding Binds variant to {@link MutableVariant} + * @see ObjectVariantBinding Binds variant to java.lang.Object * @see StringVariantBinding Binds variant to java.lang.String (Filename and URL compatible) * @author Toni Kalajainen */ @@ -64,7 +64,7 @@ public abstract class VariantBinding extends Binding { throws BindingException; /** - * Get the value of the variant. The value is bound with the suggested + * Get the value of the variant. The value is bound with the suggested * binding, see {@link #getContentBinding(Object)}. * * @param variant the variant object @@ -73,45 +73,45 @@ public abstract class VariantBinding extends Binding { */ public abstract Object getContent(Object variant) throws BindingException; - - /** - * Get the data type of the content. - * - * @param variant the variant object - * @return the data type - * @throws BindingException + + /** + * Get the data type of the content. + * + * @param variant the variant object + * @return the data type + * @throws BindingException */ public abstract Datatype getContentType(Object variant) throws BindingException; - - /** - * Return a suggestion for the binding of the content of this variant. - * - * @param variant variant object - * @return binding a binding - */ - public abstract Binding getContentBinding(Object variant) - throws BindingException; /** - * Create a new variant object. + * Return a suggestion for the binding of the content of this variant. + * + * @param variant variant object + * @return binding a binding + */ + public abstract Binding getContentBinding(Object variant) + throws BindingException; + + /** + * Create a new variant object. * The value argument may be included in the result. * - * @param contentBinding the binding of the content + * @param contentBinding the binding of the content * @param content content * @return new variant * @throws BindingException */ public abstract Object create(Binding contentBinding, Object content) throws BindingException; - - /** - * Create a new variant object. - * - * @param contentBinding binding of the content - * @param content content - * @return new variant - * @throws RuntimeBindingException + + /** + * Create a new variant object. + * + * @param contentBinding binding of the content + * @param content content + * @return new variant + * @throws RuntimeBindingException */ public Object createUnchecked(Binding contentBinding, Object content) throws RuntimeBindingException { @@ -120,42 +120,42 @@ public abstract class VariantBinding extends Binding { } catch (BindingException e) { throw new RuntimeBindingException(e); } - } - - @Override - public void readFrom(Binding srcBinding, Object src, Object dst) - throws BindingException { - try { - VariantBinding sb = (VariantBinding) srcBinding; - Datatype newType = sb.getContentType(src); - Datatype oldType = getContentType(dst); - Binding scb = sb.getContentBinding(src); - Object sc = sb.getContent(src, scb); - if (newType.equals(oldType)) { - Binding dcb = getContentBinding(dst); - if (dcb.isImmutable()) { - Object dc = Bindings.clone(sc, scb, scb); - setContent(dst, scb, dc); - } else { - Object dc = getContent(dst, dcb); - dc = dcb.readFromTry(scb, sc, dc); - setContent(dst, dcb, dc); - } - } else { - Object dc = Bindings.clone(sc, scb, scb); - setContent(dst, scb, dc); - } - } catch (AdaptException e) { - throw new BindingException(e); - } - + } + + @Override + public void readFrom(Binding srcBinding, Object src, Object dst) + throws BindingException { + try { + VariantBinding sb = (VariantBinding) srcBinding; + Datatype newType = sb.getContentType(src); + Datatype oldType = getContentType(dst); + Binding scb = sb.getContentBinding(src); + Object sc = sb.getContent(src, scb); + if (newType.equals(oldType)) { + Binding dcb = getContentBinding(dst); + if (dcb.isImmutable()) { + Object dc = Bindings.clone(sc, scb, scb); + setContent(dst, scb, dc); + } else { + Object dc = getContent(dst, dcb); + dc = dcb.readFromTry(scb, sc, dc); + setContent(dst, dcb, dc); + } + } else { + Object dc = Bindings.clone(sc, scb, scb); + setContent(dst, scb, dc); + } + } catch (AdaptException e) { + throw new BindingException(e); + } + } /** * Set the content of an variant. * * @param variant variant object - * @param contentBinding content's binding + * @param contentBinding content's binding * @param content new content * @throws BindingException */ @@ -174,11 +174,10 @@ public abstract class VariantBinding extends Binding { @Override public int deepHashValue(Object value, IdentityHashMap hashedObjects) throws BindingException { - Datatype type = getContentType(value); + Datatype type = getContentType(value); Binding binding = getContentBinding(value); - Binding dataTypeBinding = Bindings.getBindingUnchecked(Datatype.class); Object element = getContent(value, binding); - return dataTypeBinding.deepHashValue(type, hashedObjects) + binding.deepHashValue(element, hashedObjects); + return Bindings.DATATYPE.deepHashValue(type, hashedObjects) + binding.deepHashValue(element, hashedObjects); } @Override @@ -188,8 +187,7 @@ public abstract class VariantBinding extends Binding { // Compare Type Datatype t1 = getContentType(o1); Datatype t2 = getContentType(o2); - Binding dataTypeBinding = Bindings.getBindingUnchecked(Datatype.class); - int dif = dataTypeBinding.compare(t1, t2); + int dif = Bindings.DATATYPE.compare(t1, t2); if (dif!=0) return dif; // Compare Value Binding bi1 = getContentBinding(o1); @@ -198,31 +196,31 @@ public abstract class VariantBinding extends Binding { Object va2 = getContent(o2, bi2); return DataValueUtil.compare(bi1, va1, bi2, va2); } - - @Override - protected void toString(Object value, BindingPrintContext ctx) throws BindingException { - Binding b = getContentBinding(value); - b.toString(getContent(value), ctx); - - ctx.b.append(" : "); - ctx.b.append( ctx.singleLine ? b.type.toSingleLineString() : b.type.toString() ); - } - - @Override - public Binding getComponentBinding(ChildReference path) { - if (path==null) return this; - throw new IllegalArgumentException(); - } - - @Override - public int getComponentCount() { - return 0; - } - - @Override - public Binding getComponentBinding(int index) { - throw new IllegalArgumentException(); - } + + @Override + protected void toString(Object value, BindingPrintContext ctx) throws BindingException { + Binding b = getContentBinding(value); + b.toString(getContent(value), ctx); + + ctx.b.append(" : "); + ctx.b.append( ctx.singleLine ? b.type.toSingleLineString() : b.type.toString() ); + } + + @Override + public Binding getComponentBinding(ChildReference path) { + if (path==null) return this; + throw new IllegalArgumentException(); + } + + @Override + public int getComponentCount() { + return 0; + } + + @Override + public Binding getComponentBinding(int index) { + throw new IllegalArgumentException(); + } }