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%2Fmutable%2FMutableVariant.java;h=dca32b2c65a09a139c8a5843bd09c10756ca78b2;hp=0d6b6f1e7800bac95c45e2ea2e680e2acfe6905f;hb=refs%2Fchanges%2F38%2F238%2F2;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/mutable/MutableVariant.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/mutable/MutableVariant.java index 0d6b6f1e7..dca32b2c6 100644 --- a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/mutable/MutableVariant.java +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/mutable/MutableVariant.java @@ -1,33 +1,33 @@ -/******************************************************************************* - * 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.mutable; -import java.io.IOException; -import java.io.InputStream; -import java.io.InvalidObjectException; -import java.io.ObjectStreamException; -import java.io.Serializable; - -import org.simantics.databoard.Accessors; -import org.simantics.databoard.Bindings; -import org.simantics.databoard.accessor.error.AccessorConstructionException; -import org.simantics.databoard.accessor.error.AccessorException; -import org.simantics.databoard.accessor.java.JavaObject; -import org.simantics.databoard.accessor.reference.ChildReference; -import org.simantics.databoard.adapter.AdaptException; -import org.simantics.databoard.binding.Binding; -import org.simantics.databoard.binding.error.BindingException; -import org.simantics.databoard.binding.reflection.VoidBinding; -import org.simantics.databoard.type.Datatype; +import java.io.IOException; +import java.io.InputStream; +import java.io.InvalidObjectException; +import java.io.ObjectStreamException; +import java.io.Serializable; + +import org.simantics.databoard.Accessors; +import org.simantics.databoard.Bindings; +import org.simantics.databoard.accessor.error.AccessorConstructionException; +import org.simantics.databoard.accessor.error.AccessorException; +import org.simantics.databoard.accessor.java.JavaObject; +import org.simantics.databoard.accessor.reference.ChildReference; +import org.simantics.databoard.adapter.AdaptException; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.binding.error.BindingException; +import org.simantics.databoard.binding.reflection.VoidBinding; +import org.simantics.databoard.type.Datatype; /** * MutableVariant is a container to a data value of any type. @@ -35,80 +35,80 @@ import org.simantics.databoard.type.Datatype; * * MutableVariant is hash-equals-comparable, even variants of bindings (and types). * The hash function and comparison rules are defined in the manual. - * + * * @see MutableVariantBinding is binding for Variant-class * @author Toni Kalajainen */ public class MutableVariant extends Variant implements Serializable, Cloneable { private static final long serialVersionUID = 1L; - - public static MutableVariant ofInstance(Object instance) { - Binding binding = Bindings.getBindingUnchecked( instance.getClass() ); - return new MutableVariant(binding, instance); - } - - /** - * Constract a variant with a default value of empty record {} + + public static MutableVariant ofInstance(Object instance) { + Binding binding = Bindings.getBindingUnchecked( instance.getClass() ); + return new MutableVariant(binding, instance); + } + + /** + * Constract a variant with a default value of empty record {} */ - public MutableVariant() { - binding = VoidBinding.VOID_BINDING; - value = null; - } + public MutableVariant() { + binding = VoidBinding.VOID_BINDING; + value = null; + } - public MutableVariant(Variant v) { -// assert(isValid(binding, value)); - this.binding = v.getBinding(); - this.value = v.getValue(); - } + public MutableVariant(Variant v) { +// assert(isValid(binding, value)); + this.binding = v.getBinding(); + this.value = v.getValue(); + } public MutableVariant(Binding binding, Object value) { // assert(isValid(binding, value)); this.binding = binding; this.value = value; } - - /** - * Set value and binding from a variant. This method takes the references, - * and does not clone the value. - * - * @param v source variant + + /** + * Set value and binding from a variant. This method takes the references, + * and does not clone the value. + * + * @param v source variant */ - public void setValue(Variant v) { - this.binding = v.getBinding(); - this.value = v.getValue(); - } + public void setValue(Variant v) { + this.binding = v.getBinding(); + this.value = v.getValue(); + } public void setValue(Binding binding, Object newValue) { // assert(isValid(binding, newValue)); this.binding = binding; this.value = newValue; - } - - public void readFrom(Binding binding, Object newValue) throws BindingException { - if (binding.isImmutable()) { - this.binding = binding; - this.value = newValue; - return; - } - - if (this.binding == binding) { - binding.readFrom(binding, newValue, this.value); - } else { - try { - this.value = binding.clone( newValue ); - this.binding = binding; - } catch (AdaptException e) { - throw new BindingException( e ); - } - } } - - public MutableVariant clone() { - if (binding.isImmutable()) return new MutableVariant(binding, value); - Object newValue = Bindings.cloneUnchecked(value, binding, binding); - return new MutableVariant(binding, newValue); - } + + public void readFrom(Binding binding, Object newValue) throws BindingException { + if (binding.isImmutable()) { + this.binding = binding; + this.value = newValue; + return; + } + + if (this.binding == binding) { + binding.readFrom(binding, newValue, this.value); + } else { + try { + this.value = binding.clone( newValue ); + this.binding = binding; + } catch (AdaptException e) { + throw new BindingException( e ); + } + } + } + + public MutableVariant clone() { + if (binding.isImmutable()) return new MutableVariant(binding, value); + Object newValue = Bindings.cloneUnchecked(value, binding, binding); + return new MutableVariant(binding, newValue); + } private void writeObject(java.io.ObjectOutputStream out) throws IOException { Binding dataTypeBinding = Bindings.getBindingUnchecked( Datatype.class ); @@ -130,19 +130,19 @@ public class MutableVariant extends Variant implements Serializable, Cloneable { throw new InvalidObjectException("Don't know how to instantiate "+type()+" with no data"); } - public MutableVariant getComponent(ChildReference ref) throws AccessorConstructionException { - if ( ref == null ) return this; - JavaObject jo = (JavaObject) Accessors.getAccessor(this, ref); - return new MutableVariant( jo.getBinding(), jo.getObject() ); - } - - public void setComponent(ChildReference ref, Binding binding, Object value) throws AccessorException, AccessorConstructionException { - if ( ref == null ) { - setValue( binding, value ); - } - else { - JavaObject jo = (JavaObject) Accessors.getAccessor(this, ref); - jo.setValue( ref, binding, value ); - } - } + public MutableVariant getComponent(ChildReference ref) throws AccessorConstructionException { + if ( ref == null ) return this; + JavaObject jo = (JavaObject) Accessors.getAccessor(this, ref); + return new MutableVariant( jo.getBinding(), jo.getObject() ); + } + + public void setComponent(ChildReference ref, Binding binding, Object value) throws AccessorException, AccessorConstructionException { + if ( ref == null ) { + setValue( binding, value ); + } + else { + JavaObject jo = (JavaObject) Accessors.getAccessor(this, ref); + jo.setValue( ref, binding, value ); + } + } }